From: S.W Date: Tue, 4 Dec 2018 09:47:07 +0000 (+0100) Subject: OSGi: Fixing dynamic resource registration after activation (#11334) X-Git-Tag: 8.7.0.alpha1~12 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bd9d26f18145485e4b77cc37e18681256319cfdc;p=vaadin-framework.git OSGi: Fixing dynamic resource registration after activation (#11334) * Fixing dynamic resource registration after activation * Fixed formatting * Fixed formatting * Merge branch 'master' into osgi-dynamic-fix --- diff --git a/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceTrackerComponent.java b/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceTrackerComponent.java index 932de4e787..031b1d1432 100644 --- a/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceTrackerComponent.java +++ b/shared/src/main/java/com/vaadin/osgi/resources/impl/VaadinResourceTrackerComponent.java @@ -67,7 +67,7 @@ public class VaadinResourceTrackerComponent { @Reference(cardinality = ReferenceCardinality.MULTIPLE, service = OsgiVaadinTheme.class, policy = ReferencePolicy.DYNAMIC) void bindTheme(ServiceReference 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 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 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> registrations = contributorToRegistrations - .get(serviceId); + .remove(serviceId); if (registrations != null) { for (ServiceRegistration 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 {