[jboss-osgi-commits] JBoss-OSGI SVN: r92547 - in projects/jboss-osgi/trunk: reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework and 1 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Tue Aug 18 17:32:42 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-08-18 17:32:41 -0400 (Tue, 18 Aug 2009)
New Revision: 92547

Added:
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleLifecycle.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleLifecycleImpl.java
Modified:
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleClassLoader.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleRegistry.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleClassLoaderImpl.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleContextImpl.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleImpl.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleRegistryImpl.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceEventManagerImpl.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceReferenceImpl.java
   projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceRegistryImpl.java
   projects/jboss-osgi/trunk/testsuite/pom.xml
Log:
Add BundleLifecycle. Example SimpleTestCase - ok

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleClassLoader.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleClassLoader.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleClassLoader.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -21,6 +21,10 @@
  */
 package org.jboss.osgi.jbossmc.api;
 
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+
 //$Id: SystemBundleContext.java 91789 2009-07-29 20:49:03Z thomas.diesler at jboss.com $
 
 /**
@@ -31,5 +35,9 @@
  */
 public interface BundleClassLoader
 {
-   Class loadClass(String name) throws ClassNotFoundException;
+   Class<?> loadClass(String name) throws ClassNotFoundException;
+
+   URL getResource(String name);
+
+   Enumeration<URL> getResources(String name) throws IOException;
 }
\ No newline at end of file

Added: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleLifecycle.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleLifecycle.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleLifecycle.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.jbossmc.api;
+
+//$Id$
+
+import org.osgi.framework.BundleException;
+
+/**
+ * An abstraction the OSGi bundle lifecycle
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 29-Jul-2009
+ */
+public interface BundleLifecycle 
+{
+   void start(int options) throws BundleException;
+
+   void stop(int options) throws BundleException;
+
+   void uninstall() throws BundleException;
+}
\ No newline at end of file


Property changes on: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleLifecycle.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleRegistry.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleRegistry.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/api/BundleRegistry.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -21,10 +21,11 @@
  */
 package org.jboss.osgi.jbossmc.api;
 
-import org.osgi.framework.Bundle;
+// $Id: SystemBundleContext.java 91789 2009-07-29 20:49:03Z thomas.diesler at jboss.com $
 
-//$Id: SystemBundleContext.java 91789 2009-07-29 20:49:03Z thomas.diesler at jboss.com $
+import java.util.List;
 
+import org.osgi.framework.Bundle;
 
 /**
  * An abstraction of a bundle registry.
@@ -37,4 +38,8 @@
    long installBundle(Bundle bundle);
    
    void uninstallBundle(Bundle bundle);
+
+   Bundle getBundleById(long id);
+
+   List<Bundle> getBundles();
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleClassLoaderImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleClassLoaderImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleClassLoaderImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -28,6 +28,7 @@
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Enumeration;
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.jbossmc.api.BundleClassLoader;
@@ -66,9 +67,19 @@
       loader = new URLClassLoader(new URL[]{ location });
    }
 
-   public Class loadClass(String name) throws ClassNotFoundException
+   public Class<?> loadClass(String name) throws ClassNotFoundException
    {
       return loader.loadClass(name);
    }
 
+   public URL getResource(String name)
+   {
+      return loader.getResource(name);
+   }
+
+   public Enumeration<URL> getResources(String name) throws IOException
+   {
+      return loader.getResources(name);
+   }
+
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleContextImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleContextImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleContextImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -26,6 +26,7 @@
 import java.io.File;
 import java.io.InputStream;
 import java.util.Dictionary;
+import java.util.List;
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.jbossmc.api.BundleEventManager;
@@ -113,12 +114,16 @@
 
    public Bundle getBundle(long id)
    {
-      throw new NotImplementedException();
+      BundleRegistry bundleRegistry = getFramework().getBundleRegistry();
+      return bundleRegistry.getBundleById(id);
    }
 
    public Bundle[] getBundles()
    {
-      throw new NotImplementedException();
+      BundleRegistry bundleRegistry = getFramework().getBundleRegistry();
+      List<Bundle> bundleList = bundleRegistry.getBundles();
+      Bundle[] bundles = new Bundle[bundleList.size()];
+      return bundleList.toArray(bundles);
    }
 
    public File getDataFile(String filename)

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -35,22 +35,19 @@
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.jbossmc.api.BundleClassLoader;
-import org.jboss.osgi.jbossmc.api.BundleEventManager;
+import org.jboss.osgi.jbossmc.api.BundleLifecycle;
 import org.jboss.osgi.jbossmc.api.BundleResolver;
 import org.jboss.osgi.jbossmc.api.FrameworkEventManager;
 import org.jboss.osgi.spi.NotImplementedException;
 import org.jboss.virtual.VirtualFile;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
 
-import aQute.lib.osgi.Constants;
-
 /**
  * An implementation of an OSGi Bundle
  * 
@@ -62,22 +59,27 @@
    // Provide logging
    final Logger log = Logger.getLogger(BundleImpl.class);
 
-   private int state; 
+   private int state;
    private VirtualFile vFile;
    private long bundleId;
-   private BundleContextImpl context;
+   private BundleContext context;
    private Dictionary<String, String> headers;
+   private BundleLifecycle bundleLifecycle;
    private BundleClassLoader classLoader;
+   private String toStringValue;
 
    public BundleImpl()
    {
       // ctor used by the framework
-      // [TODO] remove when we have a VFS root for the framework 
+      // [TODO] remove when we have a VFS root for the framework
+      
+      this.bundleLifecycle = new BundleLifecycleImpl(this);
    }
 
    public BundleImpl(VirtualFile vFile)
    {
       this.vFile = vFile;
+      this.bundleLifecycle = new BundleLifecycleImpl(this);
    }
 
    public VirtualFile getVirtualFile()
@@ -139,93 +141,22 @@
 
    public void start(int options) throws BundleException
    {
-      // If this bundle's state is UNINSTALLED then an IllegalStateException is thrown.
-      if (state == Bundle.UNINSTALLED)
-         throw new IllegalStateException("Bundle already uninstalled: " + this);
-      
-      // [TODO] Implement the optional Start Level service 
-      
-      // [TODO] If this bundle is in the process of being activated or deactivated then this method must wait 
-      // for activation or deactivation to complete before continuing.
-      if (state == Bundle.STARTING || state == Bundle.STARTING)
-         throw new IllegalStateException("Bundle already staring/stopping: " + this);
-      
-      // If this bundle's state is not RESOLVED, an attempt is made to resolve this bundle.
-      if (state != Bundle.RESOLVED)
-      {
-         BundleResolver resolver = getFramework().getBundleResolver();
-         resolver.resolveBundle(this);
-      }
-      
-      // [TODO] Lazy bundle activation
-      
-      // This bundle's state is set to STARTING. 
-      state = Bundle.STARTING;
-      
-      // A bundle event of type BundleEvent.STARTING is fired.
-      BundleEventManager eventManager = getFramework().getBundleEventManager();
-      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STARTING, this));
-      
-      // The BundleActivator is called
-      String activatorClass = getHeader(Constants.BUNDLE_ACTIVATOR);
-      if (activatorClass != null)
-      {
-         BundleActivator activator;
-         try
-         {
-            activator = (BundleActivator)loadClass(activatorClass).newInstance();
-            activator.start(context);
-         }
-         catch (Exception ex)
-         {
-            // This bundle's state is set to STOPPING.
-            state = Bundle.STOPPING;
-            
-            // A bundle event of type BundleEvent.STOPPING is fired
-            eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STOPPING, this));
-            
-            // [TODO] Any services registered by this bundle must be unregistered.
-            
-            // [TODO] Any services used by this bundle must be released.
-            
-            // [TODO] Any listeners registered by this bundle must be removed.
-            
-            // This bundle's state is set to RESOLVED.
-            state = Bundle.RESOLVED;
-            
-            // A bundle event of type BundleEvent.STOPPED is fired.
-            eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STOPPED, this));
-            
-            // A BundleException is then thrown. 
-            throw new BundleException("Cannot call BundleActivator: " + activatorClass, ex);
-         }
-      }
-
-      // If this bundle's state is UNINSTALLED, because this bundle was uninstalled while 
-      // the BundleActivator.start was running 
-      if (state == Bundle.UNINSTALLED)
-         throw new IllegalStateException("Bundle already uninstalled: " + this);
-
-      // This bundle's state is set to ACTIVE
-      state = Bundle.ACTIVE;
-      
-      // A bundle event of type BundleEvent.STARTED is fired
-      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STARTED, this));
+      bundleLifecycle.start(options);
    }
 
    public void stop() throws BundleException
    {
-      throw new NotImplementedException();
+      stop(0);
    }
 
    public void stop(int options) throws BundleException
    {
-      throw new NotImplementedException();
+      bundleLifecycle.stop(options);
    }
 
    public void uninstall() throws BundleException
    {
-      throw new NotImplementedException();
+      bundleLifecycle.uninstall();
    }
 
    public void update() throws BundleException
@@ -304,18 +235,18 @@
 
    public URL getResource(String name)
    {
-      throw new NotImplementedException();
+      return getClassLoader().getResource(name);
    }
 
-   public Enumeration getResources(String name) throws IOException
+   public Enumeration<URL> getResources(String name) throws IOException
    {
-      throw new NotImplementedException();
+      return getClassLoader().getResources(name);
    }
 
    public ServiceReference[] getServicesInUse()
    {
       throw new NotImplementedException();
-      
+
    }
 
    public Map getSignerCertificates(int signersType)
@@ -336,7 +267,7 @@
    public Class<?> loadClass(String name) throws ClassNotFoundException
    {
       // [TODO] If this bundle is a fragment bundle then this method must throw a ClassNotFoundException
-      
+
       // If this bundle's state is INSTALLED, this method must attempt to resolve this bundle 
       // before attempting to load the class.
       if (state == Bundle.INSTALLED)
@@ -351,11 +282,11 @@
             // If this bundle cannot be resolved, a Framework event of type FrameworkEvent.ERROR is fired
             FrameworkEventManager eventManager = getFramework().getFrameworkEventManager();
             eventManager.fireFrameworkEvent(new FrameworkEvent(FrameworkEvent.ERROR, this, ex));
-            
+
             throw new ClassNotFoundException("Cannot load class: " + name, ex);
          }
       }
-      
+
       return getClassLoader().loadClass(name);
    }
 
@@ -366,6 +297,20 @@
 
    private FrameworkImpl getFramework()
    {
-      return context.getFramework();
+      BundleContextImpl contextImpl = (BundleContextImpl)context;
+      return contextImpl.getFramework();
    }
+
+   @Override
+   public String toString()
+   {
+      if (toStringValue == null)
+      {
+         long id = getBundleId();
+         String name = getSymbolicName();
+         String version = getHeader(Constants.BUNDLE_VERSION);
+         toStringValue = "[id=" + id + ",name=" + name + ",version=" + version + "]";
+      }
+      return toStringValue;
+   }
 }
\ No newline at end of file

Added: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleLifecycleImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleLifecycleImpl.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleLifecycleImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -0,0 +1,244 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.jbossmc.framework;
+
+//$Id$
+
+import org.jboss.logging.Logger;
+import org.jboss.osgi.jbossmc.api.BundleEventManager;
+import org.jboss.osgi.jbossmc.api.BundleLifecycle;
+import org.jboss.osgi.jbossmc.api.BundleRegistry;
+import org.jboss.osgi.jbossmc.api.BundleResolver;
+import org.jboss.osgi.jbossmc.api.FrameworkEventManager;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
+
+/**
+ * An implementation of the OSGi bundle lifecycle
+ * 
+ * @author thomas.diesler at jboss.com
+ * @since 29-Jul-2009
+ */
+public class BundleLifecycleImpl implements BundleLifecycle
+{
+   // Provide logging
+   final Logger log = Logger.getLogger(BundleLifecycleImpl.class);
+
+   private Bundle bundle;
+   private BundleActivator activator;
+
+   public BundleLifecycleImpl(Bundle bundle)
+   {
+      this.bundle = bundle;
+   }
+   
+   public void start(int options) throws BundleException
+   {
+      // If this bundle's state is UNINSTALLED then an IllegalStateException is thrown.
+      if (getState() == Bundle.UNINSTALLED)
+         throw new IllegalStateException("Bundle already uninstalled: " + this);
+
+      // [TODO] Implement the optional Start Level service 
+
+      // [TODO] If this bundle is in the process of being activated or deactivated then this method must wait 
+      // for activation or deactivation to complete before continuing.
+      if (getState() == Bundle.STARTING || getState() == Bundle.STARTING)
+         throw new IllegalStateException("Bundle already staring/stopping: " + this);
+
+      // If this bundle's state is not RESOLVED, an attempt is made to resolve this bundle.
+      if (getState() != Bundle.RESOLVED)
+      {
+         BundleResolver resolver = getFramework().getBundleResolver();
+         resolver.resolveBundle(bundle);
+      }
+
+      // [TODO] Lazy bundle activation
+
+      // This bundle's state is set to STARTING. 
+      setState(Bundle.STARTING);
+
+      // A bundle event of type BundleEvent.STARTING is fired.
+      BundleEventManager eventManager = getFramework().getBundleEventManager();
+      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STARTING, bundle));
+
+      // The BundleActivator is called
+      String activatorClass = (String)bundle.getHeaders().get(Constants.BUNDLE_ACTIVATOR);
+      if (activatorClass != null)
+      {
+         try
+         {
+            activator = (BundleActivator)bundle.loadClass(activatorClass).newInstance();
+            activator.start(bundle.getBundleContext());
+         }
+         catch (Exception ex)
+         {
+            // This bundle's state is set to STOPPING.
+            setState(Bundle.STOPPING);
+
+            // A bundle event of type BundleEvent.STOPPING is fired
+            eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STOPPING, bundle));
+
+            // [TODO] Any services registered by this bundle must be unregistered.
+
+            // [TODO] Any services used by this bundle must be released.
+
+            // [TODO] Any listeners registered by this bundle must be removed.
+
+            // This bundle's state is set to RESOLVED.
+            setState(Bundle.RESOLVED);
+
+            // A bundle event of type BundleEvent.STOPPED is fired.
+            eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STOPPED, bundle));
+
+            // A BundleException is then thrown. 
+            throw new BundleException("Cannot call BundleActivator: " + activatorClass, ex);
+         }
+      }
+
+      // If this bundle's state is UNINSTALLED, because this bundle was uninstalled while 
+      // the BundleActivator.start was running 
+      if (getState() == Bundle.UNINSTALLED)
+         throw new IllegalStateException("Bundle already uninstalled: " + this);
+
+      // This bundle's state is set to ACTIVE
+      setState(Bundle.ACTIVE);
+
+      // A bundle event of type BundleEvent.STARTED is fired
+      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STARTED, bundle));
+      
+      log.debug("Bundle STARTED " + bundle);
+   }
+
+   public void stop(int options) throws BundleException
+   {
+      // 1. If this bundle's state is UNINSTALLED then an IllegalStateException is thrown.
+      if (getState() == Bundle.UNINSTALLED)
+         throw new IllegalStateException("Bundle already unistalled: " + this);
+      
+      // [TODO] 2. If this bundle is in the process of being activated or deactivated then this method must wait for activation or deactivation to complete
+      
+      // [TODO] 3. If the STOP_TRANSIENT option is not set then then set this bundle's persistent autostart setting to to Stopped. When the Framework is restarted and this bundle's autostart setting is Stopped, this bundle must not be automatically started.
+      
+      // 4. If this bundle's state is not STARTING or ACTIVE then this method returns immediately.
+      if (!(getState() == Bundle.STARTING || getState() == Bundle.ACTIVE))
+         return;
+         
+      // 5. This bundle's state is set to STOPPING.
+      int statePriorToStopping = getState();
+      setState(Bundle.STOPPING);
+      
+      // 6. A bundle event of type BundleEvent.STOPPING is fired.
+      BundleEventManager eventManager = getFramework().getBundleEventManager();
+      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STOPPING, bundle));
+      
+      // 7. If this bundle's state was ACTIVE prior to setting the state to STOPPING, the BundleActivator.stop(org.osgi.framework.BundleContext) method of this bundle's BundleActivator, if one is specified, is called.
+      Exception activatorStopException = null;
+      if (statePriorToStopping == Bundle.ACTIVE && activator != null)
+      {
+         try
+         {
+            activator.stop(bundle.getBundleContext());
+         }
+         catch (Exception ex)
+         {
+            // This method must continue to stop this bundle and a BundleException must be thrown after completion of the remaining steps.
+            activatorStopException = ex;
+         }
+      }
+      
+      // [TODO] 8. Any services registered by this bundle must be unregistered.
+      
+      // [TODO] 9. Any services used by this bundle must be released.
+      
+      // [TODO] 10. Any listeners registered by this bundle must be removed.
+      
+      // 11. If this bundle's state is UNINSTALLED, because this bundle was uninstalled while the BundleActivator.stop method was running, a BundleException must be thrown.
+      if (getState() == Bundle.UNINSTALLED)
+         throw new IllegalStateException("Bundle already unistalled: " + this);
+      
+      // 12. This bundle's state is set to RESOLVED.
+      setState(Bundle.RESOLVED);
+      
+      // 13. A bundle event of type BundleEvent.STOPPED is fired.    
+      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.STOPPED, bundle));
+      
+      log.debug("Bundle STOPPED " + bundle);
+   }
+
+   public void uninstall() throws BundleException
+   {
+      // 1. If this bundle's state is UNINSTALLED then an IllegalStateException is thrown.
+      if (getState() == Bundle.UNINSTALLED)
+         throw new IllegalStateException("Bundle already unistalled: " + this);
+
+      // 2. If this bundle's state is ACTIVE, STARTING or STOPPING, this bundle is stopped as described in the Bundle.stop method. 
+      if (getState() == Bundle.ACTIVE || getState() == Bundle.STARTING || getState() == Bundle.STOPPING)
+      {
+         try
+         {
+            stop(0);
+         }
+         catch (Exception ex)
+         {
+            // If Bundle.stop throws an exception, a Framework event of type FrameworkEvent.ERROR is fired containing the exception.
+            FrameworkEventManager eventManager = getFramework().getFrameworkEventManager();
+            eventManager.fireFrameworkEvent(new FrameworkEvent(FrameworkEvent.ERROR, bundle, ex));
+         }
+      }
+
+      // 3. This bundle's state is set to UNINSTALLED.
+      setState(Bundle.UNINSTALLED);
+
+      // 4. A bundle event of type BundleEvent.UNINSTALLED is fired.
+      BundleEventManager eventManager = getFramework().getBundleEventManager();
+      eventManager.fireBundleEvent(new BundleEvent(BundleEvent.UNINSTALLED, bundle));
+
+      // 5. This bundle and any persistent storage area provided for this bundle by the Framework are removed.
+      BundleRegistry bundleRegistry = getFramework().getBundleRegistry();
+      bundleRegistry.uninstallBundle(bundle);
+
+      // [TODO] Any persistent storage area provided for this bundle are removed.
+      
+      log.debug("Bundle UNINSTALLED " + bundle);
+   }
+
+   private int getState()
+   {
+      return bundle.getState();
+   }
+
+   private void setState(int state)
+   {
+      BundleImpl bundleImpl = (BundleImpl)bundle;
+      bundleImpl.setState(state);
+   }
+
+   private FrameworkImpl getFramework()
+   {
+      BundleContextImpl context = (BundleContextImpl)bundle.getBundleContext();
+      return context.getFramework();
+   }
+}
\ No newline at end of file


Property changes on: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleLifecycleImpl.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleRegistryImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleRegistryImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/BundleRegistryImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -23,7 +23,12 @@
 
 //$Id: SystemBundleContext.java 91789 2009-07-29 20:49:03Z thomas.diesler at jboss.com $
 
-import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.jbossmc.api.BundleRegistry;
@@ -40,40 +45,45 @@
    // Provide logging
    final Logger log = Logger.getLogger(BundleRegistryImpl.class);
    
-   private long bundleId;
-   private FrameworkImpl framework;
-   private HashMap<String, Bundle> registry = new HashMap<String, Bundle>();
+   private long globalId;
+   private Map<Long, Bundle> registry = new LinkedHashMap<Long, Bundle>();
 
    public BundleRegistryImpl(FrameworkImpl framework)
    {
-      this.framework = framework;
    }
 
    public long installBundle(Bundle bundle)
    {
-      String location = getBundleLocation(bundle);
-      registry.put(location, bundle);
+      if (bundle == null)
+         throw new IllegalArgumentException("Cannot install null bundle");
       
-      ((BundleImpl)bundle).setBundleId(bundleId++);
-      return bundle.getBundleId();
+      BundleImpl bundleImpl = (BundleImpl)bundle;
+      bundleImpl.setBundleId(globalId++);
+      
+      Long bundleId = new Long(bundle.getBundleId());
+      registry.put(bundleId, bundle);
+      
+      return bundleId;
    }
 
    public void uninstallBundle(Bundle bundle)
    {
-      String location = getBundleLocation(bundle);
-      if (registry.remove(location) == null)
+      if (bundle == null)
+         throw new IllegalArgumentException("Cannot uninstall null bundle");
+      
+      Long bundleId = new Long(bundle.getBundleId());
+      if (registry.remove(bundleId) == null)
          throw new IllegalStateException("Cannot unregister bundle: " + bundle);
    }
 
-   private String getBundleLocation(Bundle bundle)
+   public Bundle getBundleById(long id)
    {
-      if (bundle == null)
-         throw new IllegalArgumentException("Cannot unregister null bundle");
-      
-      String location = bundle.getLocation();
-      if (location == null)
-         throw new IllegalArgumentException("Cannot obtain bundle location");
-      
-      return location;
+      return registry.get(new Long(id));
    }
+
+   public List<Bundle> getBundles()
+   {
+      ArrayList<Bundle> bundles = new ArrayList<Bundle>(registry.values());
+      return Collections.unmodifiableList(bundles);
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceEventManagerImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceEventManagerImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceEventManagerImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -28,8 +28,13 @@
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.jbossmc.api.ServiceEventManager;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
 
 /**
  * A simple implementation of a BundleEventManager
@@ -43,7 +48,7 @@
    final Logger log = Logger.getLogger(ServiceEventManagerImpl.class);
    
    private FrameworkImpl framework;
-   private List<ServiceListener> listeners = new ArrayList<ServiceListener>();
+   private List<ListenerRegistration> listeners = new ArrayList<ListenerRegistration>();
 
    public ServiceEventManagerImpl(FrameworkImpl framework)
    {
@@ -52,20 +57,65 @@
    
    public void fireServiceEvent(ServiceEvent event)
    {
+      ServiceReference sref = event.getServiceReference();
+      String[] clazzes = (String[])sref.getProperty(Constants.OBJECTCLASS);
+      if (clazzes == null)
+         throw new IllegalStateException("Cannot obtain property '" + Constants.OBJECTCLASS + "' from: " + sref);
+      
       // When a ServiceEvent is fired, it is synchronously delivered to a ServiceListener.
-      for (ServiceListener listener : listeners)
-         listener.serviceChanged(event);
+      for (ListenerRegistration reg : listeners)
+      {
+         ServiceListener listener = reg.getListener();
+         
+         // ServiceEvent object delivery to ServiceListener objects is filtered
+         Filter filter = reg.getFilter();
+         if (filter == null || filter.match(sref))
+            listener.serviceChanged(event);
+      }
       
-      // [TODO] ServiceEvent object delivery to ServiceListener objects is filtered
+      // [TODO] ServiceEvent object delivery to ServiceListener objects is further filtered according to package sources
    }
 
    public void addServiceListener(ServiceListener listener)
    {
-      listeners.add(listener);
+      listeners.add(new ListenerRegistration(listener, null));
    }
 
    public void addServiceListener(ServiceListener listener, String filter)
    {
-      listeners.add(listener);
+      listeners.add(new ListenerRegistration(listener, filter));
    }
+   
+   static class ListenerRegistration
+   {
+      ServiceListener listener; 
+      Filter filter;
+      
+      ListenerRegistration(ServiceListener listener, String filterStr) 
+      {
+         this.listener = listener;
+         
+         if (filterStr != null)
+         {
+            try
+            {
+               filter = FrameworkUtil.createFilter(filterStr);
+            }
+            catch (InvalidSyntaxException ex)
+            {
+               throw new IllegalArgumentException(ex);
+            }
+         }
+      }
+
+      public ServiceListener getListener()
+      {
+         return listener;
+      }
+
+      public Filter getFilter()
+      {
+         return filter;
+      }
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceReferenceImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceReferenceImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceReferenceImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -47,15 +47,15 @@
 
    private Bundle bundle;
    private Object service;
-   private Dictionary<String, String> properties;
+   private Dictionary<String, Object> properties;
    private ServiceRegistration registration;
    private boolean unregistered;
    
-   public ServiceReferenceImpl(Bundle bundle, Object service, Dictionary<String, String> properties)
+   public ServiceReferenceImpl(Bundle bundle, Object service, Dictionary<String, Object> props)
    {
       this.bundle = bundle;
       this.service = service;
-      this.properties = properties;
+      this.properties = props;
    }
 
    public boolean isUnregistered()

Modified: projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceRegistryImpl.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceRegistryImpl.java	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/reactor/runtime/jbossmc/src/main/java/org/jboss/osgi/jbossmc/framework/ServiceRegistryImpl.java	2009-08-18 21:32:41 UTC (rev 92547)
@@ -25,15 +25,17 @@
 
 import java.util.ArrayList;
 import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.jbossmc.api.ServiceEventManager;
 import org.jboss.osgi.jbossmc.api.ServiceRegistry;
-import org.jboss.osgi.spi.NotImplementedException;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceReference;
@@ -52,6 +54,7 @@
    
    private Map<String, List<ServiceReference>> registry = new HashMap<String, List<ServiceReference>>();
    private FrameworkImpl framework;
+   private long serviceId; 
 
    public ServiceRegistryImpl(FrameworkImpl framework)
    {
@@ -60,12 +63,12 @@
 
    public ServiceRegistration registerService(Bundle bundle, String clazz, Object service, Dictionary properties)
    {
-      return registerServiceInternal(bundle, clazz, service, properties);
+      return registerServiceInternal(bundle, new String[] {clazz}, service, properties);
    }
 
    public ServiceRegistration registerService(Bundle bundle, String[] clazzes, Object service, Dictionary properties)
    {
-      throw new NotImplementedException();
+      return registerServiceInternal(bundle, clazzes, service, properties);
    }
 
    public ServiceReference getServiceReference(String clazz)
@@ -117,46 +120,65 @@
       if (service instanceof ServiceFactory)
       {
          ServiceFactory factory = (ServiceFactory)service;
-         factory.getService(bundle, srefImpl.getServiceRegistration());
+         service = factory.getService(bundle, srefImpl.getServiceRegistration());
       }
       
       // The service object for the service is returned.
-      
       return service;
    }
    
-   private ServiceRegistration registerServiceInternal(Bundle bundle, String clazz, Object service, Dictionary properties)
+   private ServiceRegistration registerServiceInternal(Bundle bundle, String[] clazzes, Object service, Dictionary<String, String> props)
    {
       // If service is not a ServiceFactory, an IllegalArgumentException is thrown if service 
       // is not an instanceof all the specified class names.
       if ((service instanceof ServiceFactory) == false)
       {
-         try
+         for (String clazz : clazzes)
          {
-            Class<?> interf = bundle.loadClass(clazz);
-            if (interf.isAssignableFrom(service.getClass()) == false)
-               throw new IllegalArgumentException("Service is not assignable to: " + clazz);
+            try
+            {
+               Class<?> interf = bundle.loadClass(clazz);
+               if (interf.isAssignableFrom(service.getClass()) == false)
+                  throw new IllegalArgumentException("Service is not assignable to: " + clazz);
+            }
+            catch (ClassNotFoundException ex)
+            {
+               throw new IllegalArgumentException("Cannot load service interface: " + clazz, ex);
+            }
          }
-         catch (ClassNotFoundException ex)
-         {
-            throw new IllegalArgumentException("Cannot load service interface: " + clazz, ex);
-         }
       }
       
-      ServiceReferenceImpl sref = new ServiceReferenceImpl(bundle, service, properties);
-      
       // 2. The Framework adds the following service properties to the service properties from the specified Dictionary (which may be null):
       //    A property named Constants.SERVICE_ID identifying the registration number of the service
       //    A property named Constants.OBJECTCLASS containing all the specified classes.
+      Dictionary<String, Object> properties = new Hashtable<String, Object>();
+      properties.put(Constants.SERVICE_ID, new Long(serviceId++));
+      properties.put(Constants.OBJECTCLASS, clazzes);
       
+      if (props != null)
+      {
+         Enumeration<String> keys = props.keys();
+         while(keys.hasMoreElements())
+         {
+            String key = keys.nextElement();
+            Object value = props.get(key);
+            properties.put(key, value);
+         }
+      }
+      
       // 3. The service is added to the Framework service registry and may now be used by other bundles.
-      List<ServiceReference> list = registry.get(clazz);
-      if (list == null)
+      ServiceReferenceImpl sref = new ServiceReferenceImpl(bundle, service, properties);
+      for (String clazz : clazzes)
       {
-         list = new ArrayList<ServiceReference>();
-         registry.put(clazz, list);
+         log.debug("Register Service: " + clazz);
+         List<ServiceReference> list = registry.get(clazz);
+         if (list == null)
+         {
+            list = new ArrayList<ServiceReference>();
+            registry.put(clazz, list);
+         }
+         list.add(sref);
       }
-      list.add(sref);
       
       // 4. A service event of type ServiceEvent.REGISTERED is fired.
       ServiceEventManager eventManager = framework.getServiceEventManager();

Modified: projects/jboss-osgi/trunk/testsuite/pom.xml
===================================================================
--- projects/jboss-osgi/trunk/testsuite/pom.xml	2009-08-18 20:38:10 UTC (rev 92546)
+++ projects/jboss-osgi/trunk/testsuite/pom.xml	2009-08-18 21:32:41 UTC (rev 92547)
@@ -183,8 +183,24 @@
           <name>!framework</name>
         </property>
       </activation>
+      
+      <!-- default to jbossmc 
+      <properties>
+        <jboss.osgi.framework.properties>jboss-osgi-jbossmc.properties</jboss.osgi.framework.properties>
+      </properties>
       <dependencies>
         <dependency>
+          <groupId>org.jboss.osgi.runtime</groupId>
+          <artifactId>jboss-osgi-runtime-jbossmc</artifactId>
+          <version>${version.jboss.osgi.runtime.jbossmc}</version>
+          <scope>provided</scope>
+        </dependency>
+      </dependencies>
+      -->
+      
+      <!-- default to felix -->
+      <dependencies>
+        <dependency>
           <groupId>org.jboss.osgi</groupId>
           <artifactId>jboss-osgi-runtime-felix</artifactId>
           <version>${version.jboss.osgi.runtime.felix}</version>



More information about the jboss-osgi-commits mailing list