Author: cziegeler
Date: Sun Aug 15 15:14:31 2010
New Revision: 985687

URL: http://svn.apache.org/viewvc?rev=985687&view=rev
Log:
Delayed activation if engine bundle is available but not started yet.

Modified:
    
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/Activator.java
    
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java

Modified: 
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/Activator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/Activator.java?rev=985687&r1=985686&r2=985687&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/Activator.java
 (original)
+++ 
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/Activator.java
 Sun Aug 15 15:14:31 2010
@@ -24,6 +24,8 @@ import java.util.Hashtable;
 import org.apache.sling.settings.SlingSettingsService;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 
@@ -32,45 +34,79 @@ import org.osgi.framework.ServiceRegistr
  * It registers the SlingSettingsService.
  *
  */
-public class Activator implements BundleActivator {
+public class Activator implements BundleActivator, BundleListener {
 
     /** The service registration */
     private ServiceRegistration serviceRegistration;
 
+    /** The bundle context. */
+    private BundleContext bundleContext;
+
+    /** The settings service. */
+    private SlingSettingsServiceImpl settingsService;
+
     /**
      * @see 
org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
     public void start(BundleContext context) throws Exception {
-        final SlingSettingsService service = new 
SlingSettingsServiceImpl(context);
+        this.bundleContext = context;
+        this.settingsService = new SlingSettingsServiceImpl(context);
+        // for compatibility, we might have to wait for the engine bundle
+        // to be started to get the Sling ID
+
+        if ( this.settingsService.isDelayedStart() ) {
+            this.bundleContext.addBundleListener(this);
+        } else {
+            this.startService();
+        }
+    }
+
+    /**
+     * @param event
+     */
+    public void bundleChanged(BundleEvent event) {
+        if ( 
SlingSettingsServiceImpl.ENGINE_SYMBOLIC_NAME.equals(event.getBundle().getSymbolicName()))
 {
+            this.settingsService.initDelayed(this.bundleContext);
+            if ( !this.settingsService.isDelayedStart() ) {
+                this.bundleContext.removeBundleListener(this);
+                this.startService();
+            }
+        }
+
+    }
+
+    private void startService() {
         final Dictionary<String, String> props = new Hashtable<String, 
String>();
-        props.put(Constants.SERVICE_PID, service.getClass().getName());
+        props.put(Constants.SERVICE_PID, 
this.settingsService.getClass().getName());
         props.put(Constants.SERVICE_DESCRIPTION,
             "Apache Sling Settings Service");
         props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
-        serviceRegistration = context.registerService(new String[] {
+        serviceRegistration = this.bundleContext.registerService(new String[] {
                                                
SlingSettingsService.class.getName()},
-                                           service, props);
+                                               this.settingsService, props);
         try {
-            SlingPropertiesPrinter.initPlugin(context);
+            SlingPropertiesPrinter.initPlugin(this.bundleContext);
         } catch (Throwable ignore) {
             // we just ignore this
         }
         try {
-            SlingSettingsPrinter.initPlugin(context, service);
+            SlingSettingsPrinter.initPlugin(this.bundleContext, 
this.settingsService);
         } catch (Throwable ignore) {
             // we just ignore this
         }
         try {
-            RunModeCommand.initPlugin(context, service.getRunModes());
+            RunModeCommand.initPlugin(this.bundleContext, 
this.settingsService.getRunModes());
         } catch (Throwable ignore) {
             // we just ignore this
         }
+
     }
 
     /**
      * @see 
org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
      */
     public void stop(BundleContext context) throws Exception {
+        this.bundleContext.removeBundleListener(this);
         try {
             RunModeCommand.destroyPlugin();
         } catch (Throwable ignore) {
@@ -90,5 +126,6 @@ public class Activator implements Bundle
             serviceRegistration.unregister();
             serviceRegistration = null;
         }
+        this.settingsService = null;
     }
 }

Modified: 
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java?rev=985687&r1=985686&r2=985687&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
 (original)
+++ 
sling/trunk/bundles/extensions/settings/src/main/java/org/apache/sling/settings/impl/SlingSettingsServiceImpl.java
 Sun Aug 15 15:14:31 2010
@@ -41,6 +41,8 @@ import org.slf4j.LoggerFactory;
 public class SlingSettingsServiceImpl
     implements SlingSettingsService {
 
+    public static final String ENGINE_SYMBOLIC_NAME = 
"org.apache.sling.engine";
+
     /** The logger */
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -58,6 +60,9 @@ public class SlingSettingsServiceImpl
     /** The name of the data file holding the sling id. */
     private static final String DATA_FILE = "sling.id.file";
 
+    /** Flag indicating a delayed start of this service. */
+    private boolean delayedStart = false;
+
     /**
      * Create the service and search the Sling home urls and
      * get/create a sling id.
@@ -101,9 +106,14 @@ public class SlingSettingsServiceImpl
         // if we don't have an id yet, we look for the engine bundle for 
compatibility reasons
         if ( this.slingId == null ) {
             final Bundle engineBundle = this.searchEngineBundle(context);
-            // TODO - we need the bundle context, maybe we should wait for the 
engine bundle to become active?
-            if ( engineBundle != null && engineBundle.getBundleContext() != 
null ) {
-                final File engineIdFile = 
engineBundle.getBundleContext().getDataFile(DATA_FILE);
+            if ( engineBundle != null && engineBundle.getState() != 
Bundle.UNINSTALLED ) {
+                final BundleContext engineCtx = 
engineBundle.getBundleContext();
+                if ( engineCtx == null ) {
+                    // we need a delayed start
+                    this.delayedStart = true;
+                    return;
+                }
+                final File engineIdFile = engineCtx.getDataFile(DATA_FILE);
                 this.slingId = this.readSlingId(engineIdFile);
                 if ( this.slingId != null ) {
                     this.writeSlingId(idFile, this.slingId);
@@ -199,7 +209,7 @@ public class SlingSettingsServiceImpl
     private Bundle searchEngineBundle(final BundleContext bc) {
         final Bundle[] bundles = bc.getBundles();
         for(final Bundle b : bundles) {
-            if ( "org.apache.sling.engine".equals(b.getSymbolicName()) ) {
+            if ( ENGINE_SYMBOLIC_NAME.equals(b.getSymbolicName()) ) {
                 return b;
             }
         }
@@ -233,4 +243,13 @@ public class SlingSettingsServiceImpl
     public Set<String> getRunModes() {
         return this.runModes;
     }
+
+    public boolean isDelayedStart() {
+        return this.delayedStart;
+    }
+
+    public void initDelayed(final BundleContext context) {
+        this.delayedStart = false;
+        this.setupSlingId(context);
+    }
 }


Reply via email to