]> source.dussan.org Git - vaadin-framework.git/commitdiff
OSGi: Fixing dynamic resource registration after activation (#11334)
authorS.W <wimmesberger@gmail.com>
Tue, 4 Dec 2018 09:47:07 +0000 (10:47 +0100)
committerSun Zhe <31067185+ZheSun88@users.noreply.github.com>
Tue, 4 Dec 2018 09:47:07 +0000 (11:47 +0200)
* Fixing dynamic resource registration after activation

* Fixed formatting

* Fixed formatting

* Merge branch 'master' into osgi-dynamic-fix

shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceTrackerComponent.java

index 932de4e787ad081a17e26403c295335b4c03e87e..031b1d1432be97f06600f548a750a07d7d3fb04f 100644 (file)
@@ -67,7 +67,7 @@ public class VaadinResourceTrackerComponent {
 
     @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinTheme.class, policy = ReferencePolicy.DYNAMIC)
     void bindTheme(ServiceReference<OsgiVaadinTheme> themeRef)
-            throws ResourceBundleInactiveException {
+            throws ResourceBundleInactiveException, NamespaceException {
 
         Bundle bundle = themeRef.getBundle();
         BundleContext context = bundle.getBundleContext();
@@ -94,7 +94,7 @@ public class VaadinResourceTrackerComponent {
 
     @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinWidgetset.class, policy = ReferencePolicy.DYNAMIC)
     void bindWidgetset(ServiceReference<OsgiVaadinWidgetset> widgetsetRef)
-            throws ResourceBundleInactiveException {
+            throws ResourceBundleInactiveException, NamespaceException {
         Bundle bundle = widgetsetRef.getBundle();
         BundleContext context = bundle.getBundleContext();
 
@@ -120,7 +120,7 @@ public class VaadinResourceTrackerComponent {
 
     @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinResource.class, policy = ReferencePolicy.DYNAMIC)
     void bindResource(ServiceReference<OsgiVaadinResource> resourceRef)
-            throws ResourceBundleInactiveException {
+            throws ResourceBundleInactiveException, NamespaceException {
         Bundle bundle = resourceRef.getBundle();
         BundleContext context = bundle.getBundleContext();
 
@@ -180,7 +180,7 @@ public class VaadinResourceTrackerComponent {
         Long serviceId = (Long) contributorRef
                 .getProperty(Constants.SERVICE_ID);
         List<ServiceRegistration<? extends OsgiVaadinResource>> registrations = contributorToRegistrations
-                .get(serviceId);
+                .remove(serviceId);
         if (registrations != null) {
             for (ServiceRegistration<? extends OsgiVaadinResource> reg : registrations) {
                 reg.unregister();
@@ -205,9 +205,7 @@ public class VaadinResourceTrackerComponent {
     @Activate
     protected void activate() throws NamespaceException {
         for (Delegate registration : resourceToRegistration.values()) {
-            registration.init(httpService);
-            httpService.registerResources(registration.alias, registration.path,
-                    registration);
+            registerResource(registration);
         }
     }
 
@@ -231,7 +229,8 @@ public class VaadinResourceTrackerComponent {
     }
 
     private void registerTheme(VaadinResourceService resourceService,
-            Bundle bundle, Long serviceId, OsgiVaadinTheme theme) {
+            Bundle bundle, Long serviceId, OsgiVaadinTheme theme)
+            throws NamespaceException {
         String pathPrefix = resourceService.getResourcePathPrefix();
 
         String alias = PathFormatHelper.getThemeAlias(theme.getName(),
@@ -242,7 +241,8 @@ public class VaadinResourceTrackerComponent {
     }
 
     private void registerWidget(VaadinResourceService resourceService,
-            Bundle bundle, Long serviceId, OsgiVaadinWidgetset widgetset) {
+            Bundle bundle, Long serviceId, OsgiVaadinWidgetset widgetset)
+            throws NamespaceException {
         String pathPrefix = resourceService.getResourcePathPrefix();
 
         String alias = PathFormatHelper.getWidgetsetAlias(widgetset.getName(),
@@ -253,7 +253,8 @@ public class VaadinResourceTrackerComponent {
     }
 
     private void registerResource(VaadinResourceService resourceService,
-            Bundle bundle, Long serviceId, OsgiVaadinResource resource) {
+            Bundle bundle, Long serviceId, OsgiVaadinResource resource)
+            throws NamespaceException {
         String pathPrefix = resourceService.getResourcePathPrefix();
 
         String alias = PathFormatHelper.getRootResourceAlias(resource.getName(),
@@ -264,9 +265,19 @@ public class VaadinResourceTrackerComponent {
     }
 
     private void registerResource(String alias, String path, Bundle bundle,
-            Long serviceId) {
-        resourceToRegistration.put(serviceId,
-                new Delegate(alias, path, bundle));
+            Long serviceId) throws NamespaceException {
+        Delegate registration = new Delegate(alias, path, bundle);
+        resourceToRegistration.put(serviceId, registration);
+        registerResource(registration);
+    }
+
+    private void registerResource(Delegate registration)
+            throws NamespaceException {
+        if (this.httpService != null && !registration.isInitialized()) {
+            registration.init(httpService);
+            httpService.registerResources(registration.alias, registration.path,
+                    registration);
+        }
     }
 
     private void unregisterResource(Long serviceId) {
@@ -297,6 +308,10 @@ public class VaadinResourceTrackerComponent {
             context = service.createDefaultHttpContext();
         }
 
+        public boolean isInitialized() {
+            return context != null;
+        }
+
         @Override
         public boolean handleSecurity(HttpServletRequest request,
                 HttpServletResponse response) throws IOException {