[jboss-cvs] JBossAS SVN: r92733 - in projects/jboss-osgi/projects/runtime/microcontainer/trunk/src: main/java/org/jboss/osgi/plugins/facade/launch and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Aug 24 05:40:32 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-08-24 05:40:32 -0400 (Mon, 24 Aug 2009)
New Revision: 92733

Modified:
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFramework.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFrameworkFactory.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestDelegate.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/launch/FrameworkLaunchTestCase.java
Log:
Implement proper framework life cycle states on init() and start()

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java	2009-08-24 09:18:23 UTC (rev 92732)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java	2009-08-24 09:40:32 UTC (rev 92733)
@@ -55,6 +55,8 @@
 import org.jboss.deployers.vfs.spi.client.VFSDeploymentFactory;
 import org.jboss.logging.Logger;
 import org.jboss.osgi.plugins.facade.api.AbstractPlugin;
+import org.jboss.osgi.plugins.facade.api.AutoInstallPlugin;
+import org.jboss.osgi.plugins.facade.api.BundleStoragePlugin;
 import org.jboss.osgi.plugins.filter.NoFilter;
 import org.jboss.osgi.plugins.metadata.AbstractOSGiMetaData;
 import org.jboss.osgi.spi.metadata.OSGiMetaData;
@@ -162,16 +164,24 @@
    {
       if (deployerClient == null)
          throw new IllegalArgumentException("Null deployerClient");
-      this.deployerClient = deployerClient;
       if (deployerClient instanceof MainDeployerStructure == false)
          throw new IllegalArgumentException("Deployer client does not implement " + MainDeployerStructure.class.getName());
-      deployerStructure = (MainDeployerStructure) deployerClient;
+      
+      this.deployerClient = deployerClient;
+      this.deployerStructure = (MainDeployerStructure)deployerClient;
+      
+      // TODO thread factory
       if (executor == null)
-         // TODO thread factory
          executor = Executors.newFixedThreadPool(10);
+      
       this.executor = executor;
       
-      // [TODO] populate metadata for system bundle
+      // Create the system bundle
+      createSystemBundle();
+   }
+
+   private void createSystemBundle()
+   {
       Manifest manifest = new Manifest();
       Attributes attributes = manifest.getMainAttributes();
       attributes.put(new Name(Constants.BUNDLE_NAME), Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
@@ -179,8 +189,6 @@
       OSGiMetaData systemMetaData = new AbstractOSGiMetaData(manifest);
       this.systemBundle = new OSGiSystemBundle(systemMetaData);
       addBundle(systemBundle);
-      // [TODO] integrate lifecycle with the underlying framework for stopping/updating
-      startFramework();
    }
    
    /**
@@ -773,21 +781,61 @@
    }
    
    /**
+    * Init the Framework
+    */
+   public void initFramework()
+   {
+      int state = systemBundle.getState();
+      
+      // This method does nothing if called when this Framework is in the STARTING, ACTIVE or STOPPING state
+      if (state == Bundle.STARTING || state == Bundle.ACTIVE || state == Bundle.STOPPING)
+         return;
+
+      // Put into the STARTING state
+      systemBundle.changeState(Bundle.STARTING);
+
+      // [TODO] Be at start level 0
+
+      // [TODO] Have event handling enabled
+
+      // Cleanup the storage area
+      String storageClean = (String)getProperty(Constants.FRAMEWORK_STORAGE_CLEAN);
+      BundleStoragePlugin storagePlugin = getOptionalPlugin(BundleStoragePlugin.class);
+      if (storagePlugin != null)
+         storagePlugin.cleanStorage(storageClean);
+   }
+
+   /**
     * Start the framework
     */
-   void startFramework()
+   public void startFramework() throws BundleException
    {
-      OSGiBundleState systemBundle = getSystemBundle();
-      systemBundle.changeState(Bundle.STARTING);
-      // [TODO] start the osgi framework, already installed bundles and start level, etc. (fire frameworkEvent.ERROR for errors)
+      // If this Framework is not in the STARTING state, initialize this Framework
+      if (systemBundle.getState() != Bundle.STARTING)
+         initFramework();
+      
+      // Create the system bundl context
+      systemBundle.createBundleContext();
+      
+      // All installed bundles must be started
+      AutoInstallPlugin autoInstall = getOptionalPlugin(AutoInstallPlugin.class);
+      if (autoInstall != null)
+      {
+         autoInstall.installBundles();
+         autoInstall.startBundles();
+      }
+
+      // This Framework's state is set to ACTIVE
       systemBundle.changeState(Bundle.ACTIVE);
+      
+      // A framework event of type STARTED is fired
       systemBundle.fireFrameworkEvent(FrameworkEvent.STARTED, null);
    }
    
    /**
     * Stop the framework
     */
-   void stopFramework()
+   public void stopFramework()
    {
       OSGiBundleState systemBundle = getSystemBundle();
       if (systemBundle.getState() != Bundle.ACTIVE)
@@ -817,7 +865,7 @@
     * 
     * @param systemBundle the system bundle
     */
-   void restartFramework()
+   public void restartFramework()
    {
       OSGiBundleState systemBundle = getSystemBundle();
       if (systemBundle.getState() != Bundle.ACTIVE)

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFramework.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFramework.java	2009-08-24 09:18:23 UTC (rev 92732)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFramework.java	2009-08-24 09:40:32 UTC (rev 92733)
@@ -46,30 +46,28 @@
    final Logger log = Logger.getLogger(OSGiFramework.class);
    
    private OSGiBundleManager bundleManager;
-   private OSGiBundleState bundleState;
    
    public OSGiFramework(OSGiBundleManager bundleManager, OSGiBundleState bundleState)
    {
       super(bundleState);
       this.bundleManager = bundleManager;
-      this.bundleState = bundleState;
    }
 
    public void init() throws BundleException
    {
-      initInternal();
+      bundleManager.initFramework();
    }
 
    @Override
    public void start() throws BundleException
    {
-      startInternal();
+      bundleManager.startFramework();
    }
 
    @Override
    public void start(int options) throws BundleException
    {
-      startInternal();
+      bundleManager.startFramework();
    }
 
    @Override
@@ -77,7 +75,7 @@
    {
       // [TODO] The method returns immediately to the caller after initiating the following steps
 
-      stopInternal();
+      bundleManager.stopFramework();
    }
 
    @Override
@@ -85,13 +83,13 @@
    {
       // [TODO] The method returns immediately to the caller after initiating the following steps
 
-      stopInternal();
+      bundleManager.stopFramework();
    }
 
    @Override
    public void update() throws BundleException
    {
-      updateInternal();
+      bundleManager.restartFramework();
    }
 
    /**
@@ -114,7 +112,7 @@
 
       // [TODO] The method returns immediately to the caller after initiating the following steps
 
-      updateInternal();
+      bundleManager.restartFramework();
    }
 
    /**
@@ -128,70 +126,6 @@
       throw new BundleException("The system bundle cannot be uninstalled");
    }
 
-   private void initInternal()
-   {
-      // This method does nothing if called when this Framework is in the
-      // STARTING, ACTIVE or STOPPING state
-      if (getState() == STARTING || getState() == ACTIVE || getState() == STOPPING)
-         return;
-
-      // Put into the STARTING state
-      bundleState.changeState(STARTING);
-
-      // Have a valid Bundle Context
-      bundleState.createBundleContext();
-
-      // [TODO] Be at start level 0
-
-      // [TODO] Have event handling enabled
-
-      // [TODO] Cleanup the storage area
-      
-      // [TODO] Have reified Bundle objects for all installed bundles
-
-      // [TODO] Have registered any framework services
-   }
-
-   private void startInternal() throws BundleException
-   {
-      // If this Framework is not in the STARTING state, initialize this Framework
-      if (getState() != STARTING)
-         initInternal();
-
-      // All installed bundles must be started
-
-      // This Framework's state is set to ACTIVE
-      bundleState.changeState(ACTIVE);
-
-      // [TODO] A framework event of type STARTED is fired
-   }
-
-   private void stopInternal()
-   {
-      // This Framework's state is set to STOPPING
-      bundleState.changeState(STOPPING);
-
-      // [TODO] All installed bundles must be stopped
-
-      // [TODO] Unregister all services registered by this Framework
-
-      // [TODO] Event handling is disabled
-
-      // This Framework's state is set to RESOLVED
-      bundleState.changeState(RESOLVED);
-
-      // [TODO] All resources held by this Framework are released.
-
-      // [TODO] Notify all threads that are waiting at waitForStop(long)
-   }
-
-   private void updateInternal() throws BundleException
-   {
-      stopInternal();
-
-      startInternal();
-   }
-
    public FrameworkEvent waitForStop(long timeout) throws InterruptedException
    {
       // [TODO] Wait until this Framework has completely stopped.

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFrameworkFactory.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFrameworkFactory.java	2009-08-24 09:18:23 UTC (rev 92732)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFrameworkFactory.java	2009-08-24 09:40:32 UTC (rev 92733)
@@ -35,7 +35,6 @@
 import org.jboss.logging.Logger;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleManager;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.launch.Framework;
 import org.osgi.framework.launch.FrameworkFactory;
 
@@ -87,11 +86,7 @@
 
       OSGiBundleManager manager = (OSGiBundleManager)managerContext.getTarget();
       OSGiBundleState sysBundle = manager.getBundle(0);
-
-      // [TODO] Remove hack that forces the initial state to installed
-      if (sysBundle.getState() != Bundle.INSTALLED)
-         sysBundle.changeState(Bundle.INSTALLED);
-
+      
       return new OSGiFramework(manager, sysBundle);
    }
 

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestDelegate.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestDelegate.java	2009-08-24 09:18:23 UTC (rev 92732)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestDelegate.java	2009-08-24 09:40:32 UTC (rev 92733)
@@ -41,6 +41,7 @@
 import org.jboss.virtual.plugins.context.jar.JarUtils;
 import org.jboss.virtual.plugins.vfs.helpers.SuffixesExcludeFilter;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
 
 /**
  * A OSGiTestDelegate
@@ -159,7 +160,18 @@
    protected OSGiBundleManager getBundleManager()
    {
       if (bundleManager == null)
+      {
          bundleManager = getBean("OSGiBundleManager", ControllerState.INSTALLED, OSGiBundleManager.class);
+         try
+         {
+            if (bundleManager.isActive() == false)
+               bundleManager.startFramework();
+         }
+         catch (BundleException ex)
+         {
+            throw new IllegalStateException("Cannot start bundle manager", ex);
+         }
+      }
       return bundleManager;
    }
 

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/launch/FrameworkLaunchTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/launch/FrameworkLaunchTestCase.java	2009-08-24 09:18:23 UTC (rev 92732)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/launch/FrameworkLaunchTestCase.java	2009-08-24 09:40:32 UTC (rev 92733)
@@ -25,8 +25,10 @@
 
 import static org.junit.Assert.assertEquals;
 
+import org.jboss.osgi.spi.util.ConstantsHelper;
 import org.jboss.osgi.spi.util.ServiceLoader;
 import org.junit.Test;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.launch.Framework;
 import org.osgi.framework.launch.FrameworkFactory;
 
@@ -39,12 +41,25 @@
 public class FrameworkLaunchTestCase 
 {
    @Test
-   public void testFrameworkLaunch()
+   public void testFrameworkLaunch() throws BundleException
    {
       FrameworkFactory factory = ServiceLoader.loadService(FrameworkFactory.class);
       Framework framework = factory.newFramework(null);
       
       assertEquals("BundleId == 0", 0, framework.getBundleId());
       assertEquals("SymbolicName", "system.bundle", framework.getSymbolicName());
+      
+      String state = ConstantsHelper.bundleState(framework.getState());
+      assertEquals("INSTALLED", state);
+      
+      framework.init();
+      
+      state = ConstantsHelper.bundleState(framework.getState());
+      assertEquals("STARTING", state);
+      
+      framework.start();
+      
+      state = ConstantsHelper.bundleState(framework.getState());
+      assertEquals("ACTIVE", state);
    }
 }
\ No newline at end of file




More information about the jboss-cvs-commits mailing list