sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1806138 - in /sling/trunk/bundles/extensions/bundleresource: pom.xml src/main/java/org/apache/sling/bundleresource/impl/Activator.java src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
Date Fri, 25 Aug 2017 09:33:14 GMT
Author: cziegeler
Date: Fri Aug 25 09:33:13 2017
New Revision: 1806138

URL: http://svn.apache.org/viewvc?rev=1806138&view=rev
Log:
SLING-7086 : Register bundles with context of extended bundle

Modified:
    sling/trunk/bundles/extensions/bundleresource/pom.xml
    sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java
    sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java

Modified: sling/trunk/bundles/extensions/bundleresource/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/pom.xml?rev=1806138&r1=1806137&r2=1806138&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/pom.xml (original)
+++ sling/trunk/bundles/extensions/bundleresource/pom.xml Fri Aug 25 09:33:13 2017
@@ -22,7 +22,7 @@
     <parent>
         <groupId>org.apache.sling</groupId>
         <artifactId>sling</artifactId>
-        <version>30</version>
+        <version>31</version>
         <relativePath/>
     </parent>
 

Modified: sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java?rev=1806138&r1=1806137&r2=1806138&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java
(original)
+++ sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/Activator.java
Fri Aug 25 09:33:13 2017
@@ -42,30 +42,19 @@ public class Activator implements Bundle
 
     private final Map<Long, BundleResourceProvider[]> bundleResourceProviderMap = new
HashMap<>();
 
-    private volatile BundleContext bundleContext;
-
     /**
      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
     @Override
     public void start(final BundleContext context) throws Exception {
-
-        this.bundleContext = context;
-
         context.addBundleListener(this);
 
-        try {
-            final Bundle[] bundles = context.getBundles();
-            for (final Bundle bundle : bundles) {
-                if (bundle.getState() == Bundle.ACTIVE) {
-                    // add bundle resource provider for active bundles
-                    addBundleResourceProvider(bundle);
-                }
+        final Bundle[] bundles = context.getBundles();
+        for (final Bundle bundle : bundles) {
+            if (bundle.getState() == Bundle.ACTIVE) {
+                // add bundle resource provider for active bundles
+                addBundleResourceProvider(bundle);
             }
-        } catch (Throwable t) {
-            log.error(
-                "activate: Problem while registering bundle resources for existing bundles",
-                t);
         }
 
         BundleResourceWebConsolePlugin.initPlugin(context);
@@ -79,7 +68,16 @@ public class Activator implements Bundle
         BundleResourceWebConsolePlugin.destroyPlugin();
 
         context.removeBundleListener(this);
-        this.bundleContext = null;
+        for(final BundleResourceProvider[] providers : this.bundleResourceProviderMap.values())
{
+            for(final BundleResourceProvider p : providers) {
+                try {
+                    p.unregisterService();
+                } catch ( final IllegalStateException ise) {
+                    // might happen on shutdown
+                }
+            }
+        }
+        this.bundleResourceProviderMap.clear();
     }
 
     /**
@@ -108,38 +106,62 @@ public class Activator implements Bundle
     // ---------- Bundle provided resources -----------------------------------
 
     private void addBundleResourceProvider(final Bundle bundle) {
-        final String prefixes = bundle.getHeaders().get(
-            BUNDLE_RESOURCE_ROOTS);
-        if (prefixes != null) {
-            log.debug(
-                "addBundleResourceProvider: Registering resources '{}' for bundle {}/{} as
service ",
-                new Object[] { prefixes, bundle.getSymbolicName(),
-                    bundle.getBundleId() });
-
-            final MappedPath[] roots = BundleResourceProvider.getRoots(bundle, prefixes);
-            final BundleResourceProvider[] providers = new BundleResourceProvider[roots.length];
-
-            int index = 0;
-            for(final MappedPath path : roots) {
-                final BundleResourceProvider brp = new BundleResourceProvider(bundle, path);
-                final long id = brp.registerService(bundleContext);
-                providers[index] = brp;
-                bundleResourceProviderMap.put(bundle.getBundleId(), providers);
-
-                log.debug("addBundleResourceProvider: Service ID = {}", id);
-                index++;
+        BundleResourceProvider[] providers = null;
+        try {
+            synchronized ( this ) {
+                // on startup we might get here twice for a bundle (listener and activator)
+                if ( bundleResourceProviderMap.get(bundle.getBundleId()) != null ) {
+                    return;
+                }
+                final String prefixes = bundle.getHeaders().get(BUNDLE_RESOURCE_ROOTS);
+                if (prefixes != null) {
+                    log.debug(
+                        "addBundleResourceProvider: Registering resources '{}' for bundle
{}:{} ({}) as service ",
+                        new Object[] { prefixes, bundle.getSymbolicName(), bundle.getVersion(),
+                            bundle.getBundleId() });
+
+                    final MappedPath[] roots = BundleResourceProvider.getRoots(bundle, prefixes);
+                    providers = new BundleResourceProvider[roots.length];
+
+                    int index = 0;
+                    for(final MappedPath path : roots) {
+                        final BundleResourceProvider brp = new BundleResourceProvider(bundle,
path);
+                        providers[index] = brp;
+
+                        index++;
+                    }
+                    bundleResourceProviderMap.put(bundle.getBundleId(), providers);
+                }
+            }
+            if ( providers != null ) {
+                for(final BundleResourceProvider provider : providers) {
+                    final long id = provider.registerService();
+                    log.debug("addBundleResourceProvider: Service ID = {}", id);
+                }
             }
+        } catch (final Throwable t) {
+            log.error(
+                "activate: Problem while registering bundle resources for bundle "
+                     + bundle.getSymbolicName() + ":" + bundle.getVersion() + " (" + bundle.getBundleId()
+ ")",
+                t);
         }
     }
 
     private void removeBundleResourceProvider(final Bundle bundle) {
-        final BundleResourceProvider[] brp = bundleResourceProviderMap.remove(bundle.getBundleId());
+        final BundleResourceProvider[] brp;
+        synchronized ( this ) {
+            brp = bundleResourceProviderMap.remove(bundle.getBundleId());
+        }
         if (brp != null) {
             log.debug(
-                "removeBundleResourceProvider: Unregistering resources for bundle {}/{}",
-                new Object[] { bundle.getSymbolicName(), bundle.getBundleId() });
+                "removeBundleResourceProvider: Unregistering resources for bundle {}:{} ({})",
+                new Object[] { bundle.getSymbolicName(), bundle.getVersion(), bundle.getBundleId()
});
             for(final BundleResourceProvider provider : brp) {
-                provider.unregisterService();
+                try {
+                    provider.unregisterService();
+                } catch ( final IllegalStateException ise) {
+                    // might happen on shutdown
+                }
             }
         }
     }

Modified: sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java?rev=1806138&r1=1806137&r2=1806138&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
(original)
+++ sling/trunk/bundles/extensions/bundleresource/src/main/java/org/apache/sling/bundleresource/impl/BundleResourceProvider.java
Fri Aug 25 09:33:13 2017
@@ -31,7 +31,6 @@ import org.apache.sling.spi.resource.pro
 import org.apache.sling.spi.resource.provider.ResourceContext;
 import org.apache.sling.spi.resource.provider.ResourceProvider;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 
@@ -76,7 +75,7 @@ public class BundleResourceProvider exte
 
     //---------- Service Registration
 
-    long registerService(final BundleContext context) {
+    long registerService() {
         final Dictionary<String, Object> props = new Hashtable<>();
         props.put(Constants.SERVICE_DESCRIPTION,
             "Provider of bundle based resources");
@@ -84,9 +83,8 @@ public class BundleResourceProvider exte
         props.put(ResourceProvider.PROPERTY_ROOT, getRoot());
         props.put(PROP_BUNDLE, this.bundle.getBundle().getBundleId());
 
-        serviceRegistration = context.registerService(ResourceProvider.class, this, props);
-        return (Long) serviceRegistration.getReference().getProperty(
-            Constants.SERVICE_ID);
+        serviceRegistration = this.bundle.getBundle().getBundleContext().registerService(ResourceProvider.class,
this, props);
+        return (Long) serviceRegistration.getReference().getProperty(Constants.SERVICE_ID);
     }
 
     void unregisterService() {



Mime
View raw message