You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VaadinResourceServiceImpl.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.osgi.resources.impl;
  17. import java.util.Dictionary;
  18. import java.util.Hashtable;
  19. import org.osgi.framework.BundleContext;
  20. import org.osgi.framework.ServiceRegistration;
  21. import org.osgi.framework.Version;
  22. import org.osgi.service.component.annotations.Activate;
  23. import org.osgi.service.component.annotations.Component;
  24. import org.osgi.service.component.annotations.Deactivate;
  25. import org.osgi.service.http.HttpService;
  26. import org.osgi.service.http.NamespaceException;
  27. import org.osgi.service.http.context.ServletContextHelper;
  28. import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
  29. import com.vaadin.osgi.resources.VaadinResourceService;
  30. /**
  31. * Implementation of {@link VaadinResourceService}. Uses bundle version as a
  32. * prefix for the /VAADIN/ folder.
  33. *
  34. * @author Vaadin Ltd.
  35. *
  36. * @since 8.1
  37. */
  38. @Component
  39. public class VaadinResourceServiceImpl implements VaadinResourceService {
  40. private static final String NAMESPACE_PREFIX = "vaadin-%s";
  41. // it's best practice to select a own context "namespace"
  42. private static final String CONTEXT_NAME = "com.vaadin";
  43. private String pathPrefix;
  44. private ServiceRegistration<ServletContextHelper> servletContextReg;
  45. @Activate
  46. public void start(BundleContext context) throws Exception {
  47. Version version = context.getBundle().getVersion();
  48. this.setBundleVersion(version.toString());
  49. // register the vaadin servlet context helper
  50. Dictionary<String, String> contextProps = new Hashtable<>();
  51. contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME,
  52. this.getContextName());
  53. contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH,
  54. "/" + this.getResourcePathPrefix());
  55. servletContextReg = context.registerService(ServletContextHelper.class,
  56. new VaadinServletContextFactory(), contextProps);
  57. }
  58. @Deactivate
  59. public void stop() {
  60. if (servletContextReg != null) {
  61. servletContextReg.unregister();
  62. servletContextReg = null;
  63. }
  64. }
  65. @Override
  66. public void publishTheme(String themeName, HttpService httpService)
  67. throws NamespaceException {
  68. String themeAlias = PathFormatHelper.getThemeAlias(themeName,
  69. pathPrefix);
  70. String themePath = PathFormatHelper.getThemePath(themeName);
  71. httpService.registerResources(themeAlias, themePath, null);
  72. }
  73. @Override
  74. public void publishResource(String resource, HttpService httpService)
  75. throws NamespaceException {
  76. String alias = PathFormatHelper.getRootResourceAlias(resource,
  77. pathPrefix);
  78. String path = PathFormatHelper.getRootResourcePath(resource);
  79. httpService.registerResources(alias, path, null);
  80. }
  81. @Override
  82. public void publishWidgetset(String widgetset, HttpService httpService)
  83. throws NamespaceException {
  84. String widgetsetAlias = PathFormatHelper.getWidgetsetAlias(widgetset,
  85. pathPrefix);
  86. String widgetsetPath = PathFormatHelper.getWidgetsetPath(widgetset);
  87. httpService.registerResources(widgetsetAlias, widgetsetPath, null);
  88. }
  89. @Override
  90. public String getResourcePathPrefix() {
  91. return this.pathPrefix;
  92. }
  93. @Override
  94. public String getContextName() {
  95. return CONTEXT_NAME;
  96. }
  97. /**
  98. * Sets the version of the bundle managing this service.
  99. *
  100. * <p>
  101. * This needs to be called before any other method after the service is
  102. * created.
  103. *
  104. * @param bundleVersion
  105. * the version of the bundle managing this service
  106. */
  107. private void setBundleVersion(String bundleVersion) {
  108. this.pathPrefix = String.format(NAMESPACE_PREFIX, bundleVersion);
  109. }
  110. }