Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VaadinResourceTrackerComponent.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. }
  64. VaadinResourceService resourceService = OsgiVaadinResources
  65. .getService();
  66. try {
  67. String pathPrefix = resourceService.getResourcePathPrefix();
  68. Long serviceId = (Long) themeRef.getProperty(Constants.SERVICE_ID);
  69. String alias = PathFormatHelper.getThemeAlias(theme.getName(),
  70. pathPrefix);
  71. String path = PathFormatHelper.getThemePath(theme.getName());
  72. httpService.registerResources(alias, path,
  73. new Delegate(httpService, bundle));
  74. themeToAlias.put(serviceId, alias);
  75. } finally {
  76. context.ungetService(themeRef);
  77. }
  78. }
  79. void unbindTheme(ServiceReference<OsgiVaadinTheme> themeRef) {
  80. Long serviceId = (Long) themeRef.getProperty(Constants.SERVICE_ID);
  81. String themeAlias = themeToAlias.remove(serviceId);
  82. if (themeAlias != null && httpService != null) {
  83. httpService.unregister(themeAlias);
  84. }
  85. }
  86. @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinWidgetset.class, policy = ReferencePolicy.DYNAMIC)
  87. void bindWidgetset(ServiceReference<OsgiVaadinWidgetset> widgetsetRef)
  88. throws ResourceBundleInactiveException, NamespaceException {
  89. Bundle bundle = widgetsetRef.getBundle();
  90. BundleContext context = bundle.getBundleContext();
  91. OsgiVaadinWidgetset widgetset = context.getService(widgetsetRef);
  92. if (widgetset == null) {
  93. return;
  94. }
  95. VaadinResourceService service = OsgiVaadinResources.getService();
  96. try {
  97. String pathPrefix = service.getResourcePathPrefix();
  98. Long serviceId = (Long) widgetsetRef
  99. .getProperty(Constants.SERVICE_ID);
  100. String alias = PathFormatHelper
  101. .getWidgetsetAlias(widgetset.getName(), pathPrefix);
  102. String path = PathFormatHelper
  103. .getWidgetsetPath(widgetset.getName());
  104. httpService.registerResources(alias, path,
  105. new Delegate(httpService, bundle));
  106. widgetsetToAlias.put(serviceId, alias);
  107. } finally {
  108. context.ungetService(widgetsetRef);
  109. }
  110. }
  111. void unbindWidgetset(ServiceReference<OsgiVaadinWidgetset> widgetsetRef) {
  112. Long serviceId = (Long) widgetsetRef.getProperty(Constants.SERVICE_ID);
  113. String widgetsetAlias = widgetsetToAlias.remove(serviceId);
  114. if (widgetsetAlias != null && httpService != null) {
  115. httpService.unregister(widgetsetAlias);
  116. }
  117. }
  118. @Reference
  119. void setHttpService(HttpService service) {
  120. this.httpService = service;
  121. }
  122. void unsetHttpService(HttpService service) {
  123. this.httpService = null;
  124. }
  125. static final class Delegate implements HttpContext {
  126. private HttpContext context;
  127. private Bundle bundle;
  128. public Delegate(HttpService service, Bundle bundle) {
  129. this.context = service.createDefaultHttpContext();
  130. this.bundle = bundle;
  131. }
  132. @Override
  133. public boolean handleSecurity(HttpServletRequest request,
  134. HttpServletResponse response) throws IOException {
  135. return context.handleSecurity(request, response);
  136. }
  137. @Override
  138. public URL getResource(String name) {
  139. return bundle.getResource(name);
  140. }
  141. @Override
  142. public String getMimeType(String name) {
  143. return context.getMimeType(name);
  144. }
  145. }
  146. }