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.

VaadinResourceTrackerComponent.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2000-2016 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.io.IOException;
  18. import java.net.URL;
  19. import java.util.Collections;
  20. import java.util.LinkedHashMap;
  21. import java.util.Map;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import org.osgi.framework.Bundle;
  25. import org.osgi.framework.BundleContext;
  26. import org.osgi.framework.Constants;
  27. import org.osgi.framework.ServiceReference;
  28. import org.osgi.service.component.annotations.Component;
  29. import org.osgi.service.component.annotations.Reference;
  30. import org.osgi.service.component.annotations.ReferenceCardinality;
  31. import org.osgi.service.component.annotations.ReferencePolicy;
  32. import org.osgi.service.http.HttpContext;
  33. import org.osgi.service.http.HttpService;
  34. import org.osgi.service.http.NamespaceException;
  35. import com.vaadin.osgi.resources.OsgiVaadinResources;
  36. import com.vaadin.osgi.resources.OsgiVaadinResources.ResourceBundleInactiveException;
  37. import com.vaadin.osgi.resources.OsgiVaadinTheme;
  38. import com.vaadin.osgi.resources.OsgiVaadinWidgetset;
  39. import com.vaadin.osgi.resources.VaadinResourceService;
  40. /**
  41. * Tracks {@link OsgiVaadinWidgetset} and {@link OsgiVaadinTheme} registration
  42. * and uses {@link HttpService} to register them.
  43. *
  44. * @author Vaadin Ltd.
  45. *
  46. * @since 8.1
  47. */
  48. @Component(immediate = true)
  49. public class VaadinResourceTrackerComponent {
  50. private HttpService httpService;
  51. private Map<Long, String> themeToAlias = Collections
  52. .synchronizedMap(new LinkedHashMap<>());
  53. private Map<Long, String> widgetsetToAlias = Collections
  54. .synchronizedMap(new LinkedHashMap<>());
  55. @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinTheme.class, policy = ReferencePolicy.DYNAMIC)
  56. void bindTheme(ServiceReference<OsgiVaadinTheme> themeRef)
  57. throws ResourceBundleInactiveException, NamespaceException {
  58. Bundle bundle = themeRef.getBundle();
  59. BundleContext context = bundle.getBundleContext();
  60. OsgiVaadinTheme theme = context.getService(themeRef);
  61. if (theme == null)
  62. return;
  63. VaadinResourceService resourceService = OsgiVaadinResources
  64. .getService();
  65. try {
  66. String pathPrefix = resourceService.getResourcePathPrefix();
  67. Long serviceId = (Long) themeRef.getProperty(Constants.SERVICE_ID);
  68. String alias = PathFormatHelper.getThemeAlias(theme.getName(),
  69. pathPrefix);
  70. String path = PathFormatHelper.getThemePath(theme.getName());
  71. httpService.registerResources(alias, path,
  72. new Delegate(httpService, bundle));
  73. themeToAlias.put(serviceId, alias);
  74. } finally {
  75. context.ungetService(themeRef);
  76. }
  77. }
  78. void unbindTheme(ServiceReference<OsgiVaadinTheme> themeRef) {
  79. Long serviceId = (Long) themeRef.getProperty(Constants.SERVICE_ID);
  80. String themeAlias = themeToAlias.remove(serviceId);
  81. if (themeAlias != null && httpService != null) {
  82. httpService.unregister(themeAlias);
  83. }
  84. }
  85. @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinWidgetset.class, policy = ReferencePolicy.DYNAMIC)
  86. void bindWidgetset(ServiceReference<OsgiVaadinWidgetset> widgetsetRef)
  87. throws ResourceBundleInactiveException, NamespaceException {
  88. Bundle bundle = widgetsetRef.getBundle();
  89. BundleContext context = bundle.getBundleContext();
  90. OsgiVaadinWidgetset widgetset = context.getService(widgetsetRef);
  91. if (widgetset == null)
  92. return;
  93. VaadinResourceService service = OsgiVaadinResources.getService();
  94. try {
  95. String pathPrefix = service.getResourcePathPrefix();
  96. Long serviceId = (Long) widgetsetRef
  97. .getProperty(Constants.SERVICE_ID);
  98. String alias = PathFormatHelper
  99. .getWidgetsetAlias(widgetset.getName(), pathPrefix);
  100. String path = PathFormatHelper
  101. .getWidgetsetPath(widgetset.getName());
  102. httpService.registerResources(alias, path,
  103. new Delegate(httpService, bundle));
  104. widgetsetToAlias.put(serviceId, alias);
  105. } finally {
  106. context.ungetService(widgetsetRef);
  107. }
  108. }
  109. void unbindWidgetset(ServiceReference<OsgiVaadinWidgetset> widgetsetRef) {
  110. Long serviceId = (Long) widgetsetRef.getProperty(Constants.SERVICE_ID);
  111. String widgetsetAlias = widgetsetToAlias.remove(serviceId);
  112. if (widgetsetAlias != null && httpService != null) {
  113. httpService.unregister(widgetsetAlias);
  114. }
  115. }
  116. @Reference
  117. void setHttpService(HttpService service) {
  118. this.httpService = service;
  119. }
  120. void unsetHttpService(HttpService service) {
  121. this.httpService = null;
  122. }
  123. static final class Delegate implements HttpContext {
  124. private HttpContext context;
  125. private Bundle bundle;
  126. public Delegate(HttpService service, Bundle bundle) {
  127. this.context = service.createDefaultHttpContext();
  128. this.bundle = bundle;
  129. }
  130. @Override
  131. public boolean handleSecurity(HttpServletRequest request,
  132. HttpServletResponse response) throws IOException {
  133. return context.handleSecurity(request, response);
  134. }
  135. @Override
  136. public URL getResource(String name) {
  137. return bundle.getResource(name);
  138. }
  139. @Override
  140. public String getMimeType(String name) {
  141. return context.getMimeType(name);
  142. }
  143. }
  144. }