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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Aug 27 04:39:00 EDT 2009


Author: thomas.diesler at jboss.com
Date: 2009-08-27 04:38:59 -0400 (Thu, 27 Aug 2009)
New Revision: 92849

Added:
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemState.java
Removed:
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemBundle.java
Modified:
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleContextWrapper.java
   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/OSGiBundleState.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleWrapper.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java
   projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiClassLoaderSystem.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
Log:
[JBOSGI-138] Proper system BundleContext implementation
Add better abstraction for the bundle states

Copied: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java (from rev 92848, projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java)
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java	                        (rev 0)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/AbstractBundleState.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -0,0 +1,967 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.plugins.facade.bundle;
+
+import java.io.File;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.Permission;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.jboss.logging.Logger;
+import org.jboss.osgi.plugins.facade.api.BundleStoragePlugin;
+import org.jboss.osgi.plugins.filter.NoFilter;
+import org.jboss.osgi.spi.NotImplementedException;
+import org.jboss.osgi.spi.metadata.OSGiMetaData;
+import org.jboss.util.collection.ConcurrentSet;
+import org.osgi.framework.AdminPermission;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.FrameworkListener;
+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;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.framework.Version;
+
+/**
+ * BundleState.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author Thomas.Diesler at jboss.com
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractBundleState implements Bundle, BundleContext
+{
+   /** The log */
+   private static final Logger log = Logger.getLogger(AbstractBundleState.class);
+
+   /** The last modified time stamp */
+   private long lastModified = System.currentTimeMillis();
+
+   /** The bundle manager */
+   private OSGiBundleManager bundleManager;
+
+   /** The osgi metadata */
+   private OSGiMetaData osgiMetaData;
+
+   /** The bundle context */
+   private BundleContext bundleContext;
+
+   /** The bundle */
+   private Bundle bundle;
+
+   /** The bundle state */
+   private AtomicInteger state = new AtomicInteger(Bundle.UNINSTALLED);
+
+   /** The registered services in use */
+   protected Set<OSGiServiceState> registeredServices = new ConcurrentSet<OSGiServiceState>();
+
+   /** The services in use */
+   protected Map<OSGiServiceState, Integer> servicesInUse = new ConcurrentHashMap<OSGiServiceState, Integer>();
+
+   /** The bundle listeners */
+   private List<BundleListener> bundleListeners = new CopyOnWriteArrayList<BundleListener>();
+
+   /** The framework listeners */
+   private List<FrameworkListener> frameworkListeners = new CopyOnWriteArrayList<FrameworkListener>();
+
+   /** The service listeners */
+   private Map<ServiceListener, ServiceListenerRegistration> serviceListeners = new ConcurrentHashMap<ServiceListener, ServiceListenerRegistration>();
+
+   /**
+    * Create a new BundleState for the system bundle.
+    * 
+    * @param osgiMetaData the osgi metadata
+    * @throws IllegalArgumentException for a null parameter
+    */
+   AbstractBundleState(OSGiMetaData osgiMetaData)
+   {
+      if (osgiMetaData == null)
+         throw new IllegalArgumentException("Null osgi metadata");
+      this.osgiMetaData = osgiMetaData;
+   }
+
+   /**
+    * Get the bundleManager.
+    * 
+    * @return the bundleManager.
+    */
+   public OSGiBundleManager getBundleManager()
+   {
+      return bundleManager;
+   }
+
+   public String getSymbolicName()
+   {
+      return osgiMetaData.getBundleSymbolicName();
+   }
+
+   public Version getVersion()
+   {
+      return osgiMetaData.getBundleVersion();
+   }
+
+   public int getState()
+   {
+      return state.get();
+   }
+
+   public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType)
+   {
+      throw new NotImplementedException();
+   }
+
+   public synchronized BundleContext getBundleContext()
+   {
+      checkAdminPermission(AdminPermission.CONTEXT);
+      return bundleContext;
+   }
+
+   public synchronized BundleContext createBundleContext()
+   {
+      if (bundleContext == null)
+         bundleContext = new OSGiBundleContextWrapper(this);
+      return bundleContext;
+   }
+
+   public synchronized void destroyBundleContext()
+   {
+      bundleContext = null;
+   }
+
+   public synchronized Bundle getBundle()
+   {
+      checkValidBundleContext();
+      return getBundleInternal();
+   }
+
+   public synchronized Bundle getBundleInternal()
+   {
+      if (bundle == null)
+         bundle = new OSGiBundleWrapper(this);
+      return bundle;
+   }
+
+   public Bundle getBundle(long id)
+   {
+      checkValidBundleContext();
+      OSGiBundleManager bundleManager = getBundleManager();
+      if (bundleManager == null)
+         return null;
+
+      AbstractBundleState bundleState = bundleManager.getBundle(id);
+      if (bundleState == null)
+         return null;
+      return bundleState.getBundleInternal();
+   }
+
+   public Bundle[] getBundles()
+   {
+      checkValidBundleContext();
+      OSGiBundleManager bundleManager = getBundleManager();
+      if (bundleManager == null)
+         return null;
+
+      Collection<AbstractBundleState> bundleStates = bundleManager.getBundles();
+      if (bundleStates.isEmpty())
+         return new Bundle[0];
+
+      List<Bundle> bundles = new ArrayList<Bundle>(bundleStates.size());
+      for (AbstractBundleState bundleState : bundleStates)
+         bundles.add(bundleState.getBundleInternal());
+      return bundles.toArray(new Bundle[bundles.size()]);
+   }
+
+   public long getLastModified()
+   {
+      return lastModified;
+   }
+
+   public void modified()
+   {
+      lastModified = System.currentTimeMillis();
+   }
+
+   /**
+    * Get the osgiMetaData.
+    * 
+    * @return the osgiMetaData.
+    */
+   public OSGiMetaData getOSGiMetaData()
+   {
+      return osgiMetaData;
+   }
+
+   @SuppressWarnings("unchecked")
+   public Dictionary getHeaders()
+   {
+      return getHeaders(null);
+   }
+
+   @SuppressWarnings("unchecked")
+   public Dictionary getHeaders(String locale)
+   {
+      checkAdminPermission(AdminPermission.METADATA);
+      return getOSGiMetaData().getHeaders(locale);
+   }
+
+   public String getProperty(String key)
+   {
+      checkValidBundleContext();
+      return bundleManager.getProperty(key);
+   }
+
+   public File getDataFile(String filename)
+   {
+      checkValidBundleContext();
+      BundleStoragePlugin storagePlugin = bundleManager.getOptionalPlugin(BundleStoragePlugin.class);
+      return storagePlugin != null ? storagePlugin.getDataFile(this, filename) : null;
+   }
+
+   public boolean hasPermission(Object permission)
+   {
+      if (permission == null || permission instanceof Permission == false)
+         return false;
+
+      SecurityManager sm = System.getSecurityManager();
+      if (sm == null)
+         return true;
+
+      // [TODO] hasPermission
+      return true;
+   }
+
+   public Filter createFilter(String filter) throws InvalidSyntaxException
+   {
+      checkValidBundleContext();
+      return FrameworkUtil.createFilter(filter);
+   }
+
+   public void addServiceListener(ServiceListener listener)
+   {
+      addServiceListenerInternal(listener, null);
+   }
+
+   public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException
+   {
+      Filter theFilter = null;
+      if (filter != null)
+         theFilter = createFilter(filter);
+      addServiceListenerInternal(listener, theFilter);
+   }
+
+   public void addServiceListenerInternal(ServiceListener listener, Filter filter)
+   {
+      if (listener == null)
+         throw new IllegalArgumentException("Null listener");
+      if (filter == null)
+         filter = NoFilter.INSTANCE;
+      checkValidBundleContext();
+
+      serviceListeners.put(listener, new ServiceListenerRegistration(filter));
+   }
+
+   public void removeServiceListener(ServiceListener listener)
+   {
+      if (listener == null)
+         throw new IllegalArgumentException("Null listener");
+
+      checkValidBundleContext();
+
+      serviceListeners.remove(listener);
+   }
+
+   /**
+    * Add a registered service
+    * 
+    * @param serviceState the service
+    */
+   void addRegisteredService(OSGiServiceState serviceState)
+   {
+      registeredServices.add(serviceState);
+   }
+
+   /**
+    * Remove a registered service
+    * 
+    * @param serviceState the service
+    */
+   void removeRegisteredService(OSGiServiceState serviceState)
+   {
+      registeredServices.remove(serviceState);
+   }
+
+   public ServiceReference[] getRegisteredServices()
+   {
+      checkInstalled();
+
+      if (registeredServices.isEmpty())
+         return null;
+
+      Set<ServiceReference> result = new HashSet<ServiceReference>(registeredServices.size());
+      for (OSGiServiceState service : registeredServices)
+      {
+         if (service.hasPermission())
+            result.add(service.getReferenceInternal());
+      }
+      if (result.isEmpty())
+         return null;
+      return result.toArray(new ServiceReference[result.size()]);
+   }
+
+   /**
+    * Increment the use count of a service for this bundle
+    * 
+    * @param serviceState the service
+    */
+   void addServiceInUse(OSGiServiceState serviceState)
+   {
+      synchronized (servicesInUse)
+      {
+         Integer count = servicesInUse.get(serviceState);
+         if (count == null)
+            servicesInUse.put(serviceState, 1);
+         else
+            servicesInUse.put(serviceState, ++count);
+      }
+      serviceState.addUsingBundle(this);
+   }
+
+   /**
+    * Decrement the use count of a service for this bundle
+    * 
+    * @param serviceState the service
+    * @return true when the service is still in use by the bundle
+    */
+   boolean removeServiceInUse(OSGiServiceState serviceState)
+   {
+      synchronized (servicesInUse)
+      {
+         Integer count = servicesInUse.get(serviceState);
+         if (count == null)
+         {
+            return false;
+         }
+         else if (count == 1)
+         {
+            servicesInUse.remove(serviceState);
+            serviceState.removeUsingBundle(this);
+            return false;
+         }
+         else
+         {
+            servicesInUse.put(serviceState, --count);
+         }
+      }
+      return true;
+   }
+
+   public ServiceReference[] getServicesInUse()
+   {
+      checkInstalled();
+
+      synchronized (servicesInUse)
+      {
+         Collection<OSGiServiceState> inUse = servicesInUse.keySet();
+         if (inUse.isEmpty())
+            return null;
+
+         Set<ServiceReference> result = new HashSet<ServiceReference>(inUse.size());
+         for (OSGiServiceState service : inUse)
+         {
+            if (service.hasPermission())
+               result.add(service.getReferenceInternal());
+         }
+         if (result.isEmpty())
+            return null;
+         return result.toArray(new ServiceReference[result.size()]);
+      }
+   }
+
+   public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException
+   {
+      checkValidBundleContext();
+      return getBundleManager().getServiceReferences(this, clazz, filter, false);
+   }
+
+   public Object getService(ServiceReference reference)
+   {
+      checkValidBundleContext();
+
+      if (reference == null)
+         throw new IllegalArgumentException("Null reference");
+
+      return getBundleManager().getService(this, reference);
+   }
+
+   public ServiceReference getServiceReference(String clazz)
+   {
+      checkValidBundleContext();
+      if (clazz == null)
+         throw new IllegalArgumentException("Null clazz");
+      return getBundleManager().getServiceReference(this, clazz);
+   }
+
+   public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException
+   {
+      checkValidBundleContext();
+      return getBundleManager().getServiceReferences(this, clazz, filter, true);
+   }
+
+   @SuppressWarnings("unchecked")
+   public ServiceRegistration registerService(String clazz, Object service, Dictionary properties)
+   {
+      if (clazz == null)
+         throw new IllegalArgumentException("Null class");
+      return registerService(new String[] { clazz }, service, properties);
+   }
+
+   @SuppressWarnings("unchecked")
+   public ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties)
+   {
+      checkValidBundleContext();
+
+      OSGiServiceState serviceState = getBundleManager().registerService(this, clazzes, service, properties);
+      return serviceState.getRegistration();
+   }
+
+   /**
+    * Unregister a service
+    * 
+    * @param serviceState the service state
+    */
+   void unregisterService(OSGiServiceState serviceState)
+   {
+      getBundleManager().unregisterService(serviceState);
+   }
+
+   public boolean ungetService(ServiceReference reference)
+   {
+      checkValidBundleContext();
+
+      return getBundleManager().ungetService(this, reference);
+   }
+
+   boolean ungetService(OSGiServiceState state)
+   {
+      return getBundleManager().ungetService(this, state);
+   }
+
+   public void addBundleListener(BundleListener listener)
+   {
+      if (listener == null)
+         throw new IllegalArgumentException("Null listener");
+
+      checkValidBundleContext();
+
+      if (listener instanceof SynchronousBundleListener)
+         checkAdminPermission(AdminPermission.LISTENER);
+
+      if (bundleListeners.contains(listener))
+         return;
+
+      bundleListeners.add(listener);
+   }
+
+   public void removeBundleListener(BundleListener listener)
+   {
+      if (listener == null)
+         throw new IllegalArgumentException("Null listener");
+
+      checkValidBundleContext();
+
+      if (listener instanceof SynchronousBundleListener)
+         checkAdminPermission(AdminPermission.LISTENER);
+
+      bundleListeners.remove(listener);
+   }
+
+   public void start() throws BundleException
+   {
+      start(0);
+   }
+
+   public void stop() throws BundleException
+   {
+      stop(0);
+   }
+
+   public void update() throws BundleException
+   {
+      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
+      // [TODO] update
+      throw new UnsupportedOperationException("update");
+   }
+
+   public void update(InputStream in) throws BundleException
+   {
+      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
+      // [TODO] update
+      throw new UnsupportedOperationException("update");
+   }
+
+   public void uninstall() throws BundleException
+   {
+      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
+
+      OSGiBundleManager bundleManager = getBundleManager();
+      if (bundleManager == null)
+         return;
+
+      bundleManager.uninstall(this);
+   }
+
+   void uninstallInternal()
+   {
+      changeState(Bundle.UNINSTALLED);
+
+      frameworkListeners.clear();
+      bundleListeners.clear();
+      serviceListeners.clear();
+   }
+
+   public void addFrameworkListener(FrameworkListener listener)
+   {
+      if (listener == null)
+         throw new IllegalArgumentException("Null listener");
+
+      checkValidBundleContext();
+
+      if (frameworkListeners.contains(listener))
+         return;
+
+      frameworkListeners.add(listener);
+   }
+
+   public void removeFrameworkListener(FrameworkListener listener)
+   {
+      if (listener == null)
+         throw new IllegalArgumentException("Null listener");
+
+      checkValidBundleContext();
+
+      frameworkListeners.remove(listener);
+   }
+
+   public Bundle installBundle(String location, InputStream input) throws BundleException
+   {
+      checkValidBundleContext();
+      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
+      // [TODO] installBundle
+      throw new UnsupportedOperationException("installBundle");
+   }
+
+   public Bundle installBundle(String location) throws BundleException
+   {
+      checkValidBundleContext();
+      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
+
+      OSGiBundleManager bundleManager = getBundleManager();
+      if (bundleManager == null)
+         throw new IllegalStateException("Bundle " + getCanonicalName() + " is not valid");
+
+      URL url = getLocationURL(location);
+      AbstractBundleState bundleState = bundleManager.install(url);
+      return bundleState.getBundleInternal();
+   }
+
+   private URL getLocationURL(String location) throws BundleException
+   {
+      // Try location as URL
+      URL url = null;
+      try
+      {
+         url = new URL(location);
+      }
+      catch (MalformedURLException e)
+      {
+         // ignore
+      }
+
+      // Try location as File
+      if (url == null)
+      {
+         try
+         {
+            File file = new File(location);
+            if (file.exists())
+               url = file.toURL();
+         }
+         catch (MalformedURLException e)
+         {
+            // ignore
+         }
+      }
+
+      if (url == null)
+         throw new BundleException("Unable to handle location=" + location);
+      
+      return url;
+   }
+
+   @Override
+   public String toString()
+   {
+      return "Bundle{" + getCanonicalName() + "}";
+   }
+
+   /**
+    * Get the canonical name of the bundle
+    * 
+    * @return the canonical name
+    */
+   String getCanonicalName()
+   {
+      return getSymbolicName() + ":" + osgiMetaData.getBundleVersion();
+   }
+
+   /**
+    * Set the bundle manager
+    * 
+    * @param bundleManager the bundle manager or null to uninstall the bundle
+    */
+   void setBundleManager(OSGiBundleManager bundleManager)
+   {
+      if (bundleManager != null && this.bundleManager != null)
+         throw new IllegalStateException("Bundle " + this + " is already installed");
+      this.bundleManager = bundleManager;
+      if (bundleManager == null)
+         changeState(Bundle.UNINSTALLED);
+   }
+
+   /**
+    * Get the source of a class for ServiceReference.isAssignable()
+    * 
+    * @param className the class name
+    * @return the source or null if no source
+    */
+   Object getSource(String className)
+   {
+      // [TODO] some more efficient way than using the class?
+      try
+      {
+         return loadClass(className);
+      }
+      catch (ClassNotFoundException e)
+      {
+         return null;
+      }
+   }
+
+   /**
+    * Change the state of the bundle
+    * 
+    * @param state the new state
+    */
+   public void changeState(int state)
+   {
+      int previous = getState();
+      int type = 0;
+      switch (state)
+      {
+         case Bundle.STARTING:
+            type = BundleEvent.STARTING;
+            break;
+         case Bundle.ACTIVE:
+            type = BundleEvent.STARTED;
+            break;
+         case Bundle.STOPPING:
+            type = BundleEvent.STOPPING;
+            break;
+         case Bundle.UNINSTALLED:
+            type = BundleEvent.UNINSTALLED;
+            break;
+         case Bundle.INSTALLED:
+         {
+            if (previous == Bundle.RESOLVED)
+               type = BundleEvent.UNRESOLVED;
+            else
+               type = BundleEvent.INSTALLED;
+            break;
+         }
+         case Bundle.RESOLVED:
+         {
+            if (previous == Bundle.STOPPING)
+               type = BundleEvent.STOPPED;
+            else
+               type = BundleEvent.RESOLVED;
+            break;
+         }
+         default:
+            throw new IllegalArgumentException("Unknown bundle state: " + state);
+      }
+      this.state.set(state);
+      log.debug(this + " change state=" + toHumanReadableStateString(state));
+      BundleEvent event = new BundleEvent(type, getBundleInternal());
+      fireBundleEvent(event);
+   }
+
+   /**
+    * Fire a bundle event
+    * 
+    * @param event the event
+    */
+   void fireBundleEvent(BundleEvent event)
+   {
+      // Nobody is interested
+      if (bundleListeners.isEmpty())
+         return;
+
+      // Are we active?
+      if (getBundleManager().isActive() == false)
+         return;
+
+      // Synchronous listeners first
+      for (BundleListener listener : bundleListeners)
+      {
+         try
+         {
+            if (listener instanceof SynchronousBundleListener)
+               listener.bundleChanged(event);
+         }
+         catch (Throwable t)
+         {
+            log.warn("Error while firing bundle event: " + event.getType() + " for bundle " + getCanonicalName(), t);
+         }
+      }
+      // Normal listeners after, if required
+      int type = event.getType();
+      if (type != BundleEvent.STARTING && type != BundleEvent.STOPPING && type != BundleEvent.LAZY_ACTIVATION)
+      {
+         for (BundleListener listener : bundleListeners)
+         {
+            try
+            {
+               if (listener instanceof SynchronousBundleListener == false)
+                  listener.bundleChanged(event);
+            }
+            catch (Throwable t)
+            {
+               log.warn("Error while firing bundle event: " + event.getType() + " for bundle " + this, t);
+            }
+         }
+      }
+   }
+
+   /**
+    * Fire a framework event
+    * 
+    * @param type the type of event
+    * @param throwable any throwable
+    */
+   void fireFrameworkEvent(int type, Throwable throwable)
+   {
+      // Nobody is interested
+      if (frameworkListeners.isEmpty())
+         return;
+
+      // Are we active?
+      if (getBundleManager().isActive() == false)
+         return;
+
+      // Call the listeners
+      FrameworkEvent event = new FrameworkEvent(type, getBundleInternal(), throwable);
+      for (FrameworkListener listener : frameworkListeners)
+      {
+         try
+         {
+            listener.frameworkEvent(event);
+         }
+         catch (Throwable t)
+         {
+            log.warn("Error while firing framework event: " + event.getType() + " for bundle " + getCanonicalName(), t);
+         }
+      }
+   }
+
+   /**
+    * Fire a service event
+    * 
+    * @param type the type
+    * @param service the service
+    */
+   void fireServiceEvent(int type, OSGiServiceState service)
+   {
+      if (service == null)
+         throw new IllegalArgumentException("No serviceReference");
+
+      // Nobody is interested
+      if (serviceListeners.isEmpty())
+         return;
+
+      // Are we active?
+      if (getBundleManager().isActive() == false)
+         return;
+
+      ServiceEvent event = new ServiceEvent(type, service.getReferenceInternal());
+
+      // Call the listeners
+      for (Map.Entry<ServiceListener, ServiceListenerRegistration> entry : serviceListeners.entrySet())
+      {
+         ServiceListener listener = entry.getKey();
+         ServiceListenerRegistration registration = entry.getValue();
+         try
+         {
+            if (registration.filter.match(service))
+            {
+               AccessControlContext accessControlContext = registration.accessControlContext;
+               if (accessControlContext == null || service.hasPermission(accessControlContext))
+                  listener.serviceChanged(event);
+            }
+         }
+         catch (Throwable t)
+         {
+            log.warn("Error while firing service event: " + type + " for service " + service, t);
+         }
+      }
+   }
+
+   /**
+    * Check the bundle is installed
+    * 
+    * @throws IllegalStateException when the bundle is not installed
+    */
+   protected void checkInstalled()
+   {
+      if ((getState() & Bundle.UNINSTALLED) != 0)
+         throw new IllegalStateException("Bundle " + getCanonicalName() + " is not installed");
+   }
+
+   /**
+    * Check a bundle context is still valid
+    * 
+    * @return the bundle context
+    * @throws IllegalArgumentException when the context is no longer valid
+    */
+   protected synchronized BundleContext checkValidBundleContext()
+   {
+      BundleContext result = this.bundleContext;
+      if (result == null)
+         throw new IllegalStateException("Bundle context is no longer valid");
+      return result;
+   }
+
+   /**
+    * Check the admin permission
+    * 
+    * @param what what permission to check
+    * @throws SecurityException when the caller does not have the AdminPermission and a security manager is installed
+    */
+   protected void checkAdminPermission(String what)
+   {
+      SecurityManager sm = System.getSecurityManager();
+      if (sm != null)
+         sm.checkPermission(new AdminPermission(this, what));
+   }
+
+   /**
+    * Checks if we have the admin permission
+    * 
+    * @param what the permission to check
+    * @return true if the caller doesn't have the permission
+    */
+   protected boolean noAdminPermission(String what)
+   {
+      try
+      {
+         checkAdminPermission(what);
+         return false;
+      }
+      catch (SecurityException e)
+      {
+         return true;
+      }
+   }
+
+   /**
+    * Get a human readable version of the state
+    * 
+    * @param state the state
+    * @return the human readable form
+    */
+   private static String toHumanReadableStateString(int state)
+   {
+      switch (state)
+      {
+         case Bundle.INSTALLED:
+            return "INSTALLED";
+         case Bundle.RESOLVED:
+            return "RESOLVED";
+         case Bundle.STARTING:
+            return "STARTING";
+         case Bundle.ACTIVE:
+            return "ACTIVE";
+         case Bundle.STOPPING:
+            return "STOPPING";
+         case Bundle.UNINSTALLED:
+            return "UNINSTALLED";
+      }
+      return "???" + state;
+   }
+
+   /**
+    * ServiceListenerRegistration.
+    */
+   private static class ServiceListenerRegistration
+   {
+      // Any filter
+      Filter filter;
+
+      // Any access control context
+      AccessControlContext accessControlContext;
+
+      /**
+       * Create a new ServiceListenerRegistration.
+       * 
+       * @param filter the filter
+       */
+      public ServiceListenerRegistration(Filter filter)
+      {
+         this.filter = filter;
+
+         if (System.getSecurityManager() != null)
+            accessControlContext = AccessController.getContext();
+      }
+   }
+}

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleContextWrapper.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleContextWrapper.java	2009-08-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleContextWrapper.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -45,7 +45,7 @@
 public class OSGiBundleContextWrapper implements BundleContext
 {
    /** The bundle state */
-   private OSGiBundleState bundleState;
+   private AbstractBundleState bundleState;
    
    /**
     * Create a new OSGiBundleContextWrapper.
@@ -53,7 +53,7 @@
     * @param bundleState the bundle state
     * @throws IllegalArgumentException for a null parameter
     */
-   public OSGiBundleContextWrapper(OSGiBundleState bundleState)
+   public OSGiBundleContextWrapper(AbstractBundleState bundleState)
    {
       if (bundleState == null)
          throw new IllegalArgumentException("Null bundle state");

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-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -99,7 +99,7 @@
    private static String OSGi_FRAMEWORK_PROCESSOR;
    
    /** The bundles by id */
-   private Map<Long, OSGiBundleState> bundlesById = new ConcurrentHashMap<Long, OSGiBundleState>();
+   private Map<Long, AbstractBundleState> bundlesById = new ConcurrentHashMap<Long, AbstractBundleState>();
 
    /** The registered services */
    private Set<OSGiServiceState> registeredServices = new ConcurrentSet<OSGiServiceState>();
@@ -114,7 +114,7 @@
    private Executor executor;
 
    /** The system bundle */
-   private OSGiSystemBundle systemBundle;
+   private OSGiSystemState systemBundle;
    
    /** The registered manager plugins */
    private Map<Class<?>, AbstractPlugin> plugins = new HashMap<Class<?>, AbstractPlugin>();
@@ -184,7 +184,7 @@
       attributes.put(new Name(Constants.BUNDLE_NAME), Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
       attributes.put(new Name(Constants.BUNDLE_SYMBOLICNAME), Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
       OSGiMetaData systemMetaData = new AbstractOSGiMetaData(manifest);
-      this.systemBundle = new OSGiSystemBundle(systemMetaData);
+      this.systemBundle = new OSGiSystemState(systemMetaData);
       addBundle(systemBundle);
    }
 
@@ -272,7 +272,7 @@
    public boolean isActive()
    {
       // We are active if the system bundle is at least in the starting/stopping state
-      OSGiBundleState bundleState = getSystemBundle();
+      AbstractBundleState bundleState = getSystemBundle();
       return bundleState.getState() >= Bundle.STARTING;
    }
    
@@ -283,7 +283,7 @@
     * @return the bundle state
     * @throws BundleException for any error
     */
-   public OSGiBundleState install(URL url) throws BundleException
+   public AbstractBundleState install(URL url) throws BundleException
    {
       if (url == null)
          throw new BundleException("Null url");
@@ -296,7 +296,7 @@
          try
          {
             DeploymentUnit unit = deployerStructure.getDeploymentUnit(deployment.getName());
-            OSGiBundleState bundleState = unit.getAttachment(OSGiBundleState.class);
+            AbstractBundleState bundleState = unit.getAttachment(OSGiBundleState.class);
             if (bundleState == null)
                throw new IllegalStateException("Unable to determine bundle state for " + deployment.getName());
             return bundleState;
@@ -320,7 +320,7 @@
     * @return the bundle state
     * @throws BundleException for any error
     */
-   public OSGiBundleState install(VirtualFile file) throws BundleException
+   public AbstractBundleState install(VirtualFile file) throws BundleException
    {
       if (file == null)
          throw new BundleException("Null file");
@@ -332,7 +332,7 @@
          try
          {
             DeploymentUnit unit = deployerStructure.getDeploymentUnit(deployment.getName());
-            OSGiBundleState bundleState = unit.getAttachment(OSGiBundleState.class);
+            AbstractBundleState bundleState = unit.getAttachment(OSGiBundleState.class);
             if (bundleState == null)
                throw new IllegalStateException("Unable to determine bundle state for " + deployment.getName());
             return bundleState;
@@ -358,9 +358,13 @@
    public void uninstall(Bundle bundle) throws BundleException
    {
       long id = bundle.getBundleId();
-      OSGiBundleState state = getBundle(id);
+      if (id == 0)
+         throw new IllegalArgumentException("Cannot uninstall system bundle");
+      
+      OSGiBundleState state = (OSGiBundleState)getBundle(id);
       if (state == null)
          throw new BundleException(bundle + " not installed");
+      
       uninstall(state);
    }
    
@@ -448,7 +452,7 @@
     * @param bundleState the bundle state
     * @throws IllegalArgumentException for a null bundle state
     */
-   public void addBundle(OSGiBundleState bundleState)
+   public void addBundle(AbstractBundleState bundleState)
    {
       if (bundleState == null)
          throw new IllegalArgumentException("Null bundle state");
@@ -458,7 +462,7 @@
       if (metaData.isSingleton())
       {
          String symbolicName = metaData.getBundleSymbolicName();
-         for (OSGiBundleState bundle : getBundles())
+         for (AbstractBundleState bundle : getBundles())
          {
             OSGiMetaData other = bundle.getOSGiMetaData();
             if (other.isSingleton() && symbolicName.equals(other.getBundleSymbolicName()))
@@ -477,7 +481,7 @@
     * @param bundleState the bundle state
     * @throws IllegalArgumentException for a null bundle state
     */
-   public void removeBundle(OSGiBundleState bundleState)
+   public void removeBundle(AbstractBundleState bundleState)
    {
       if (bundleState == null)
          throw new IllegalArgumentException("Null bundle state");
@@ -494,7 +498,7 @@
     * @param id the id of the bundle
     * @return the bundle or null if there is no bundle with that id
     */
-   public OSGiBundleState getBundle(long id)
+   public AbstractBundleState getBundle(long id)
    {
       return bundlesById.get(id);
    }
@@ -504,7 +508,7 @@
     * 
     * @return the system bundle
     */
-   public OSGiSystemBundle getSystemBundle()
+   public OSGiSystemState getSystemBundle()
    {
       return systemBundle;
    }
@@ -517,9 +521,13 @@
     */
    public DeploymentUnit getDeployment(long id)
    {
-      OSGiBundleState bundleState = getBundle(id);
+      if (id == 0)
+         throw new IllegalArgumentException("Cannot get deployment from system bundle");
+      
+      OSGiBundleState bundleState = (OSGiBundleState)getBundle(id);
       if (bundleState == null)
          return null;
+      
       return bundleState.getDeploymentUnit();
    }
 
@@ -528,7 +536,7 @@
     * 
     * @return the bundles
     */
-   public Collection<OSGiBundleState> getBundles()
+   public Collection<AbstractBundleState> getBundles()
    {
       return bundlesById.values();
    }
@@ -636,7 +644,7 @@
     * @param checkAssignable whether to check isAssignable
     * @return the services
     */
-   Collection<OSGiServiceState> getServices(OSGiBundleState bundleState, String clazz, Filter filter, boolean checkAssignable) 
+   Collection<OSGiServiceState> getServices(AbstractBundleState bundleState, String clazz, Filter filter, boolean checkAssignable) 
    {
       if (filter == null)
          filter = NoFilter.INSTANCE;
@@ -671,7 +679,7 @@
     * @param clazz any class
     * @return the reference
     */
-   ServiceReference getServiceReference(OSGiBundleState bundleState, String clazz) 
+   ServiceReference getServiceReference(AbstractBundleState bundleState, String clazz) 
    {
       // Ignore isAssignable for the system context
       boolean checkAssignable = (bundleState.getBundleId() != 0);
@@ -693,7 +701,7 @@
     * @param checkAssignable whether to check isAssignable
     * @return the services
     */
-   ServiceReference[] getServiceReferences(OSGiBundleState bundleState, String clazz, Filter filter, boolean checkAssignable) 
+   ServiceReference[] getServiceReferences(AbstractBundleState bundleState, String clazz, Filter filter, boolean checkAssignable) 
    {
       Collection<OSGiServiceState> services = getServices(bundleState, clazz, filter, checkAssignable);
       if (services == null || services.isEmpty())
@@ -715,7 +723,7 @@
     * @return the services
     * @throws InvalidSyntaxException when the filter is invalid
     */
-   ServiceReference[] getServiceReferences(OSGiBundleState bundleState, String clazz, String filter, boolean checkAssignable) throws InvalidSyntaxException
+   ServiceReference[] getServiceReferences(AbstractBundleState bundleState, String clazz, String filter, boolean checkAssignable) throws InvalidSyntaxException
    {
       Filter f = NoFilter.INSTANCE;
       if (filter != null)
@@ -733,7 +741,7 @@
     * @return the service state
     */
    @SuppressWarnings("unchecked")
-   OSGiServiceState registerService(OSGiBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
+   OSGiServiceState registerService(AbstractBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
    {
       OSGiServiceState result = new OSGiServiceState(bundleState, clazzes, service, properties);
       result.internalRegister();
@@ -763,7 +771,7 @@
     * @param reference the service reference
     * @return the service
     */
-   Object getService(OSGiBundleState bundleState, ServiceReference reference)
+   Object getService(AbstractBundleState bundleState, ServiceReference reference)
    {
       OSGiServiceReferenceWrapper serviceReference = (OSGiServiceReferenceWrapper) reference;
       OSGiServiceState serviceState = serviceReference.getServiceState();
@@ -780,7 +788,7 @@
     * @param reference the service reference
     * @return true when the service is still in use by the bundle
     */
-   boolean ungetService(OSGiBundleState bundleState, ServiceReference reference)
+   boolean ungetService(AbstractBundleState bundleState, ServiceReference reference)
    {
       if (reference == null)
          throw new IllegalArgumentException("Null reference");
@@ -796,7 +804,7 @@
     * @param service the service
     * @return true when the service is still in use by the bundle
     */
-   boolean ungetService(OSGiBundleState bundleState, OSGiServiceState service)
+   boolean ungetService(AbstractBundleState bundleState, OSGiServiceState service)
    {
       return bundleState.removeServiceInUse(service);
    }
@@ -870,12 +878,12 @@
     */
    public void stopFramework()
    {
-      OSGiBundleState systemBundle = getSystemBundle();
+      AbstractBundleState systemBundle = getSystemBundle();
       if (systemBundle.getState() != Bundle.ACTIVE)
          return;
       
       systemBundle.changeState(Bundle.STOPPING);
-      for (OSGiBundleState bundle : getBundles())
+      for (AbstractBundleState bundle : getBundles())
       {
          if (bundle != systemBundle)
          {
@@ -898,11 +906,11 @@
     */
    public void restartFramework()
    {
-      OSGiBundleState systemBundle = getSystemBundle();
+      AbstractBundleState systemBundle = getSystemBundle();
       if (systemBundle.getState() != Bundle.ACTIVE)
          return;
 
-      for (OSGiBundleState bundle : getBundles())
+      for (AbstractBundleState bundle : getBundles())
       {
          if (bundle != systemBundle && bundle.getState() == Bundle.ACTIVE)
          {
@@ -932,9 +940,9 @@
     * 
     * @param t the throwable
     */
-   void fireError(OSGiBundleState bundleState, String context, Throwable t)
+   void fireError(AbstractBundleState bundleState, String context, Throwable t)
    {
-      OSGiSystemBundle systemBundle = getSystemBundle();
+      OSGiSystemState systemBundle = getSystemBundle();
       if (t instanceof BundleException)
          systemBundle.fireFrameworkEvent(FrameworkEvent.ERROR, t);
       else if (bundleState != null)
@@ -948,9 +956,9 @@
     * 
     * @param t the throwable
     */
-   void fireWarning(OSGiBundleState bundleState, String context, Throwable t)
+   void fireWarning(AbstractBundleState bundleState, String context, Throwable t)
    {
-      OSGiSystemBundle systemBundle = getSystemBundle();
+      OSGiSystemState systemBundle = getSystemBundle();
       if (t instanceof BundleException)
          systemBundle.fireFrameworkEvent(FrameworkEvent.WARNING, t);
       else if (bundleState != null)

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java	2009-08-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleState.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -21,26 +21,10 @@
 */
 package org.jboss.osgi.plugins.facade.bundle;
 
-import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.net.URL;
-import java.security.AccessControlContext;
-import java.security.AccessController;
-import java.security.Permission;
-import java.security.cert.X509Certificate;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Dictionary;
 import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.jboss.beans.info.spi.BeanInfo;
@@ -50,84 +34,35 @@
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
 import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.logging.Logger;
-import org.jboss.osgi.plugins.facade.api.BundleStoragePlugin;
-import org.jboss.osgi.plugins.filter.NoFilter;
-import org.jboss.osgi.spi.NotImplementedException;
 import org.jboss.osgi.spi.metadata.OSGiMetaData;
-import org.jboss.util.collection.ConcurrentSet;
 import org.jboss.virtual.VirtualFile;
 import org.osgi.framework.AdminPermission;
 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.BundleListener;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkEvent;
-import org.osgi.framework.FrameworkListener;
-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;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.framework.SynchronousBundleListener;
-import org.osgi.framework.Version;
 
 /**
  * BundleState.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author Thomas.Diesler at jboss.com
  * @version $Revision: 1.1 $
  */
-public class OSGiBundleState implements Bundle, BundleContext
+public class OSGiBundleState extends AbstractBundleState
 {
    /** The log */
    private static final Logger log = Logger.getLogger(OSGiBundleState.class);
-
+   
    /** Used to generate a unique id */
    private static final AtomicLong bundleIDGenerator = new AtomicLong();
 
    /** The bundle id */
    private long bundleId;
 
-   /** The last modified time stamp */
-   private long lastModified = System.currentTimeMillis();
-
-   /** The bundle manager */
-   private OSGiBundleManager bundleManager;
-
-   /** The osgi metadata */
-   private OSGiMetaData osgiMetaData;
-
    /** The deployment unit */
    private DeploymentUnit unit;
 
-   /** The bundle context */
-   private BundleContext bundleContext;
-
-   /** The bundle */
-   private Bundle bundle;
-
-   /** The bundle state */
-   private AtomicInteger state = new AtomicInteger(Bundle.UNINSTALLED);
-
-   /** The registered services in use */
-   private Set<OSGiServiceState> registeredServices = new ConcurrentSet<OSGiServiceState>();
-
-   /** The services in use */
-   private Map<OSGiServiceState, Integer> servicesInUse = new ConcurrentHashMap<OSGiServiceState, Integer>();
-
-   /** The bundle listeners */
-   private List<BundleListener> bundleListeners = new CopyOnWriteArrayList<BundleListener>();
-
-   /** The framework listeners */
-   private List<FrameworkListener> frameworkListeners = new CopyOnWriteArrayList<FrameworkListener>();
-
-   /** The service listeners */
-   private Map<ServiceListener, ServiceListenerRegistration> serviceListeners = new ConcurrentHashMap<ServiceListener, ServiceListenerRegistration>();
-
    /**
     * Create a new BundleState.
     * 
@@ -137,133 +72,30 @@
     */
    public OSGiBundleState(OSGiMetaData osgiMetaData, DeploymentUnit unit)
    {
-      if (osgiMetaData == null)
-         throw new IllegalArgumentException("Null osgi metadata");
+      super(osgiMetaData);
       if (unit == null)
          throw new IllegalArgumentException("Null deployment unit");
-      this.osgiMetaData = osgiMetaData;
       this.unit = unit;
       this.bundleId = bundleIDGenerator.incrementAndGet();
       unit.getMutableMetaData().addMetaData(unit, DeploymentUnit.class);
       changeState(Bundle.INSTALLED);
    }
 
-   /**
-    * Create a new BundleState for the system bundle.
-    * 
-    * @param osgiMetaData the osgi metadata
-    * @throws IllegalArgumentException for a null parameter
-    */
-   OSGiBundleState(OSGiMetaData osgiMetaData)
+   public long getBundleId()
    {
-      if (osgiMetaData == null)
-         throw new IllegalArgumentException("Null osgi metadata");
-      this.osgiMetaData = osgiMetaData;
-      this.bundleId = 0;
-      changeState(Bundle.INSTALLED);
+      return bundleId;
    }
 
    /**
-    * Get the bundleManager.
+    * Get the unit.
     * 
-    * @return the bundleManager.
+    * @return the unit.
     */
-   public OSGiBundleManager getBundleManager()
+   DeploymentUnit getDeploymentUnit()
    {
-      return bundleManager;
+      return unit;
    }
 
-   public long getBundleId()
-   {
-      return bundleId;
-   }
-
-   public String getSymbolicName()
-   {
-      return osgiMetaData.getBundleSymbolicName();
-   }
-
-   public Version getVersion()
-   {
-      return osgiMetaData.getBundleVersion();
-   }
-
-   public int getState()
-   {
-      return state.get();
-   }
-
-   public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType)
-   {
-      throw new NotImplementedException();
-   }
-
-   public synchronized BundleContext getBundleContext()
-   {
-      checkAdminPermission(AdminPermission.CONTEXT);
-      return bundleContext;
-   }
-
-   public synchronized BundleContext createBundleContext()
-   {
-      if (bundleContext == null)
-         bundleContext = new OSGiBundleContextWrapper(this);
-      return bundleContext;
-   }
-
-   public synchronized Bundle getBundle()
-   {
-      checkValidBundleContext();
-      return getBundleInternal();
-   }
-
-   public synchronized Bundle getBundleInternal()
-   {
-      if (bundle == null)
-         bundle = new OSGiBundleWrapper(this);
-      return bundle;
-   }
-
-   public Bundle getBundle(long id)
-   {
-      checkValidBundleContext();
-      OSGiBundleManager bundleManager = getBundleManager();
-      if (bundleManager == null)
-         return null;
-
-      OSGiBundleState bundleState = bundleManager.getBundle(id);
-      if (bundleState == null)
-         return null;
-      return bundleState.getBundleInternal();
-   }
-
-   public Bundle[] getBundles()
-   {
-      checkValidBundleContext();
-      OSGiBundleManager bundleManager = getBundleManager();
-      if (bundleManager == null)
-         return null;
-
-      Collection<OSGiBundleState> bundleStates = bundleManager.getBundles();
-      if (bundleStates.isEmpty())
-         return new Bundle[0];
-
-      List<Bundle> bundles = new ArrayList<Bundle>(bundleStates.size());
-      for (OSGiBundleState bundleState : bundleStates)
-         bundles.add(bundleState.getBundleInternal());
-      return bundles.toArray(new Bundle[bundles.size()]);
-   }
-
-   public long getLastModified()
-   {
-      return lastModified;
-   }
-
-   public void modified()
-   {
-      lastModified = System.currentTimeMillis();
-   }
-
    public String getLocation()
    {
       checkAdminPermission(AdminPermission.METADATA);
@@ -283,42 +115,6 @@
       return null;
    }
 
-   /**
-    * Get the osgiMetaData.
-    * 
-    * @return the osgiMetaData.
-    */
-   public OSGiMetaData getOSGiMetaData()
-   {
-      return osgiMetaData;
-   }
-
-   @SuppressWarnings("unchecked")
-   public Dictionary getHeaders()
-   {
-      return getHeaders(null);
-   }
-
-   @SuppressWarnings("unchecked")
-   public Dictionary getHeaders(String locale)
-   {
-      checkAdminPermission(AdminPermission.METADATA);
-      return getOSGiMetaData().getHeaders(locale);
-   }
-
-   public String getProperty(String key)
-   {
-      checkValidBundleContext();
-      return bundleManager.getProperty(key);
-   }
-
-   public File getDataFile(String filename)
-   {
-      checkValidBundleContext();
-      BundleStoragePlugin storagePlugin = bundleManager.getOptionalPlugin(BundleStoragePlugin.class);
-      return storagePlugin != null ? storagePlugin.getDataFile(this, filename) : null;
-   }
-
    public URL getEntry(String path)
    {
       checkInstalled();
@@ -423,6 +219,20 @@
       return classLoader.loadClass(name);
    }
 
+   /**
+    * Try to resolve the bundle
+    * 
+    * @param errorOnFail whether to throw an error when not installed
+    * @return true when resolved
+    */
+   boolean resolve(boolean errorOnFail)
+   {
+      if (getState() != INSTALLED)
+         return true;
+
+      return getBundleManager().resolve(this, errorOnFail);
+   }
+
    public URL getResource(String name)
    {
       checkInstalled();
@@ -448,293 +258,28 @@
       return getDeploymentUnit().getClassLoader().getResources(name);
    }
 
-   public boolean hasPermission(Object permission)
+   // [TODO] options
+   public void start(int options) throws BundleException
    {
-      if (permission == null || permission instanceof Permission == false)
-         return false;
-
-      SecurityManager sm = System.getSecurityManager();
-      if (sm == null)
-         return true;
-
-      // [TODO] hasPermission
-      return true;
-   }
-
-   public Filter createFilter(String filter) throws InvalidSyntaxException
-   {
-      checkValidBundleContext();
-      return FrameworkUtil.createFilter(filter);
-   }
-
-   public void addServiceListener(ServiceListener listener)
-   {
-      addServiceListenerInternal(listener, null);
-   }
-
-   public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException
-   {
-      Filter theFilter = null;
-      if (filter != null)
-         theFilter = createFilter(filter);
-      addServiceListenerInternal(listener, theFilter);
-   }
-
-   public void addServiceListenerInternal(ServiceListener listener, Filter filter)
-   {
-      if (listener == null)
-         throw new IllegalArgumentException("Null listener");
-      if (filter == null)
-         filter = NoFilter.INSTANCE;
-      checkValidBundleContext();
-
-      serviceListeners.put(listener, new ServiceListenerRegistration(filter));
-   }
-
-   public void removeServiceListener(ServiceListener listener)
-   {
-      if (listener == null)
-         throw new IllegalArgumentException("Null listener");
-
-      checkValidBundleContext();
-
-      serviceListeners.remove(listener);
-   }
-
-   /**
-    * Add a registered service
-    * 
-    * @param serviceState the service
-    */
-   void addRegisteredService(OSGiServiceState serviceState)
-   {
-      registeredServices.add(serviceState);
-   }
-
-   /**
-    * Remove a registered service
-    * 
-    * @param serviceState the service
-    */
-   void removeRegisteredService(OSGiServiceState serviceState)
-   {
-      registeredServices.remove(serviceState);
-   }
-
-   public ServiceReference[] getRegisteredServices()
-   {
       checkInstalled();
+      checkAdminPermission(AdminPermission.EXECUTE);
 
-      if (registeredServices.isEmpty())
-         return null;
-
-      Set<ServiceReference> result = new HashSet<ServiceReference>(registeredServices.size());
-      for (OSGiServiceState service : registeredServices)
-      {
-         if (service.hasPermission())
-            result.add(service.getReferenceInternal());
-      }
-      if (result.isEmpty())
-         return null;
-      return result.toArray(new ServiceReference[result.size()]);
-   }
-
-   /**
-    * Increment the use count of a service for this bundle
-    * 
-    * @param serviceState the service
-    */
-   void addServiceInUse(OSGiServiceState serviceState)
-   {
-      synchronized (servicesInUse)
-      {
-         Integer count = servicesInUse.get(serviceState);
-         if (count == null)
-            servicesInUse.put(serviceState, 1);
-         else
-            servicesInUse.put(serviceState, ++count);
-      }
-      serviceState.addUsingBundle(this);
-   }
-
-   /**
-    * Decrement the use count of a service for this bundle
-    * 
-    * @param serviceState the service
-    * @return true when the service is still in use by the bundle
-    */
-   boolean removeServiceInUse(OSGiServiceState serviceState)
-   {
-      synchronized (servicesInUse)
-      {
-         Integer count = servicesInUse.get(serviceState);
-         if (count == null)
-         {
-            return false;
-         }
-         else if (count == 1)
-         {
-            servicesInUse.remove(serviceState);
-            serviceState.removeUsingBundle(this);
-            return false;
-         }
-         else
-         {
-            servicesInUse.put(serviceState, --count);
-         }
-      }
-      return true;
-   }
-
-   public ServiceReference[] getServicesInUse()
-   {
-      checkInstalled();
-
-      synchronized (servicesInUse)
-      {
-         Collection<OSGiServiceState> inUse = servicesInUse.keySet();
-         if (inUse.isEmpty())
-            return null;
-
-         Set<ServiceReference> result = new HashSet<ServiceReference>(inUse.size());
-         for (OSGiServiceState service : inUse)
-         {
-            if (service.hasPermission())
-               result.add(service.getReferenceInternal());
-         }
-         if (result.isEmpty())
-            return null;
-         return result.toArray(new ServiceReference[result.size()]);
-      }
-   }
-
-   public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException
-   {
-      checkValidBundleContext();
-      return getBundleManager().getServiceReferences(this, clazz, filter, false);
-   }
-
-   public Object getService(ServiceReference reference)
-   {
-      checkValidBundleContext();
-
-      if (reference == null)
-         throw new IllegalArgumentException("Null reference");
-
-      return getBundleManager().getService(this, reference);
-   }
-
-   public ServiceReference getServiceReference(String clazz)
-   {
-      checkValidBundleContext();
-      if (clazz == null)
-         throw new IllegalArgumentException("Null clazz");
-      return getBundleManager().getServiceReference(this, clazz);
-   }
-
-   public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException
-   {
-      checkValidBundleContext();
-      return getBundleManager().getServiceReferences(this, clazz, filter, true);
-   }
-
-   @SuppressWarnings("unchecked")
-   public ServiceRegistration registerService(String clazz, Object service, Dictionary properties)
-   {
-      if (clazz == null)
-         throw new IllegalArgumentException("Null class");
-      return registerService(new String[] { clazz }, service, properties);
-   }
-
-   @SuppressWarnings("unchecked")
-   public ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties)
-   {
-      checkValidBundleContext();
-
-      OSGiServiceState serviceState = getBundleManager().registerService(this, clazzes, service, properties);
-      return serviceState.getRegistration();
-   }
-
-   /**
-    * Unregister a service
-    * 
-    * @param serviceState the service state
-    */
-   void unregisterService(OSGiServiceState serviceState)
-   {
-      getBundleManager().unregisterService(serviceState);
-   }
-
-   public boolean ungetService(ServiceReference reference)
-   {
-      checkValidBundleContext();
-
-      return getBundleManager().ungetService(this, reference);
-   }
-
-   boolean ungetService(OSGiServiceState state)
-   {
-      return getBundleManager().ungetService(this, state);
-   }
-
-   public void addBundleListener(BundleListener listener)
-   {
-      if (listener == null)
-         throw new IllegalArgumentException("Null listener");
-
-      checkValidBundleContext();
-
-      if (listener instanceof SynchronousBundleListener)
-         checkAdminPermission(AdminPermission.LISTENER);
-
-      if (bundleListeners.contains(listener))
+      if (getState() == ACTIVE)
          return;
 
-      bundleListeners.add(listener);
+      getBundleManager().start(this);
    }
 
-   public void removeBundleListener(BundleListener listener)
-   {
-      if (listener == null)
-         throw new IllegalArgumentException("Null listener");
-
-      checkValidBundleContext();
-
-      if (listener instanceof SynchronousBundleListener)
-         checkAdminPermission(AdminPermission.LISTENER);
-
-      bundleListeners.remove(listener);
-   }
-
-   /**
-    * Try to resolve the bundle
-    * 
-    * @param errorOnFail whether to throw an error when not installed
-    * @return true when resolved
-    */
-   boolean resolve(boolean errorOnFail)
-   {
-      if (getState() != INSTALLED)
-         return true;
-
-      return getBundleManager().resolve(this, errorOnFail);
-   }
-
-   public void start() throws BundleException
-   {
-      start(0);
-   }
-
    // [TODO] options
-   public void start(int options) throws BundleException
+   public void stop(int options) throws BundleException
    {
       checkInstalled();
       checkAdminPermission(AdminPermission.EXECUTE);
 
-      if (getState() == ACTIVE)
+      if (getState() != ACTIVE)
          return;
 
-      getBundleManager().start(this);
+      getBundleManager().stop(this);
    }
 
    /**
@@ -747,7 +292,7 @@
    public void startInternal() throws Throwable
    {
       changeState(STARTING);
-      bundleContext = createBundleContext();
+      createBundleContext();
       try
       {
          OSGiMetaData metaData = getOSGiMetaData();
@@ -772,12 +317,12 @@
                BundleActivator bundleActivator = (BundleActivator)result;
                getDeploymentUnit().addAttachment(BundleActivator.class, bundleActivator);
 
-               bundleActivator.start(bundleContext);
+               bundleActivator.start(getBundleContext());
             }
          }
 
          if (getState() != STARTING)
-            throw new BundleException("Bundle has been uninstalled: " + bundle);
+            throw new BundleException("Bundle has been uninstalled: " + this);
 
          changeState(ACTIVE);
       }
@@ -785,29 +330,12 @@
       {
          changeState(STOPPING);
          // TODO stop the bundle
-         this.bundleContext = null;
+         destroyBundleContext();
          changeState(RESOLVED);
          throw t;
       }
    }
 
-   public void stop() throws BundleException
-   {
-      stop(0);
-   }
-
-   // [TODO] options
-   public void stop(int options) throws BundleException
-   {
-      checkInstalled();
-      checkAdminPermission(AdminPermission.EXECUTE);
-
-      if (getState() != ACTIVE)
-         return;
-
-      getBundleManager().stop(this);
-   }
-
    /**
     * Stop Internal
     * 
@@ -823,7 +351,7 @@
       try
       {
          BundleActivator bundleActivator = getDeploymentUnit().getAttachment(BundleActivator.class);
-         BundleContext bundleContext = this.bundleContext;
+         BundleContext bundleContext = getBundleContext();
          if (bundleActivator != null && bundleContext != null)
          {
             try
@@ -872,459 +400,11 @@
       {
          if (getState() == STOPPING)
             changeState(RESOLVED);
-         bundleContext = null;
+         destroyBundleContext();
          getDeploymentUnit().removeAttachment(BundleActivator.class);
       }
 
       if (rethrow != null)
          throw rethrow;
    }
-
-   public void update() throws BundleException
-   {
-      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
-      // [TODO] update
-      throw new UnsupportedOperationException("update");
-   }
-
-   public void update(InputStream in) throws BundleException
-   {
-      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
-      // [TODO] update
-      throw new UnsupportedOperationException("update");
-   }
-
-   public void uninstall() throws BundleException
-   {
-      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
-
-      OSGiBundleManager bundleManager = getBundleManager();
-      if (bundleManager == null)
-         return;
-
-      bundleManager.uninstall(this);
-   }
-
-   void uninstallInternal()
-   {
-      changeState(Bundle.UNINSTALLED);
-
-      frameworkListeners.clear();
-      bundleListeners.clear();
-      serviceListeners.clear();
-   }
-
-   public void addFrameworkListener(FrameworkListener listener)
-   {
-      if (listener == null)
-         throw new IllegalArgumentException("Null listener");
-
-      checkValidBundleContext();
-
-      if (frameworkListeners.contains(listener))
-         return;
-
-      frameworkListeners.add(listener);
-   }
-
-   public void removeFrameworkListener(FrameworkListener listener)
-   {
-      if (listener == null)
-         throw new IllegalArgumentException("Null listener");
-
-      checkValidBundleContext();
-
-      frameworkListeners.remove(listener);
-   }
-
-   public Bundle installBundle(String location, InputStream input) throws BundleException
-   {
-      checkValidBundleContext();
-      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
-      // [TODO] installBundle
-      throw new UnsupportedOperationException("installBundle");
-   }
-
-   public Bundle installBundle(String location) throws BundleException
-   {
-      checkValidBundleContext();
-      checkAdminPermission(AdminPermission.LIFECYCLE); // [TODO] extension bundles
-
-      OSGiBundleManager bundleManager = getBundleManager();
-      if (bundleManager == null)
-         throw new IllegalStateException("Bundle " + getCanonicalName() + " is not valid");
-
-      URL url = getLocationURL(location);
-      OSGiBundleState bundleState = bundleManager.install(url);
-      return bundleState.getBundleInternal();
-   }
-
-   private URL getLocationURL(String location) throws BundleException
-   {
-      // Try location as URL
-      URL url = null;
-      try
-      {
-         url = new URL(location);
-      }
-      catch (MalformedURLException e)
-      {
-         // ignore
-      }
-
-      // Try location as File
-      if (url == null)
-      {
-         try
-         {
-            File file = new File(location);
-            if (file.exists())
-               url = file.toURL();
-         }
-         catch (MalformedURLException e)
-         {
-            // ignore
-         }
-      }
-
-      if (url == null)
-         throw new BundleException("Unable to handle location=" + location);
-      
-      return url;
-   }
-
-   @Override
-   public String toString()
-   {
-      return "Bundle{" + getCanonicalName() + "}";
-   }
-
-   /**
-    * Get the canonical name of the bundle
-    * 
-    * @return the canonical name
-    */
-   String getCanonicalName()
-   {
-      return getSymbolicName() + ":" + osgiMetaData.getBundleVersion();
-   }
-
-   /**
-    * Set the bundle manager
-    * 
-    * @param bundleManager the bundle manager or null to uninstall the bundle
-    */
-   void setBundleManager(OSGiBundleManager bundleManager)
-   {
-      if (bundleManager != null && this.bundleManager != null)
-         throw new IllegalStateException("Bundle " + this + " is already installed");
-      this.bundleManager = bundleManager;
-      if (bundleManager == null)
-         changeState(Bundle.UNINSTALLED);
-   }
-
-   /**
-    * Get the unit.
-    * 
-    * @return the unit.
-    */
-   DeploymentUnit getDeploymentUnit()
-   {
-      return unit;
-   }
-
-   /**
-    * Get the source of a class for ServiceReference.isAssignable()
-    * 
-    * @param className the class name
-    * @return the source or null if no source
-    */
-   Object getSource(String className)
-   {
-      // [TODO] some more efficient way than using the class?
-      try
-      {
-         return loadClass(className);
-      }
-      catch (ClassNotFoundException e)
-      {
-         return null;
-      }
-   }
-
-   /**
-    * Change the state of the bundle
-    * 
-    * @param state the new state
-    */
-   public void changeState(int state)
-   {
-      int previous = getState();
-      int type = 0;
-      switch (state)
-      {
-         case Bundle.STARTING:
-            type = BundleEvent.STARTING;
-            break;
-         case Bundle.ACTIVE:
-            type = BundleEvent.STARTED;
-            break;
-         case Bundle.STOPPING:
-            type = BundleEvent.STOPPING;
-            break;
-         case Bundle.UNINSTALLED:
-            type = BundleEvent.UNINSTALLED;
-            break;
-         case Bundle.INSTALLED:
-         {
-            if (previous == Bundle.RESOLVED)
-               type = BundleEvent.UNRESOLVED;
-            else
-               type = BundleEvent.INSTALLED;
-            break;
-         }
-         case Bundle.RESOLVED:
-         {
-            if (previous == Bundle.STOPPING)
-               type = BundleEvent.STOPPED;
-            else
-               type = BundleEvent.RESOLVED;
-            break;
-         }
-         default:
-            throw new IllegalArgumentException("Unknown bundle state: " + state);
-      }
-      this.state.set(state);
-      log.debug(this + " change state=" + toHumanReadableStateString(state));
-      BundleEvent event = new BundleEvent(type, getBundleInternal());
-      fireBundleEvent(event);
-   }
-
-   /**
-    * Fire a bundle event
-    * 
-    * @param event the event
-    */
-   void fireBundleEvent(BundleEvent event)
-   {
-      // Nobody is interested
-      if (bundleListeners.isEmpty())
-         return;
-
-      // Are we active?
-      if (getBundleManager().isActive() == false)
-         return;
-
-      // Synchronous listeners first
-      for (BundleListener listener : bundleListeners)
-      {
-         try
-         {
-            if (listener instanceof SynchronousBundleListener)
-               listener.bundleChanged(event);
-         }
-         catch (Throwable t)
-         {
-            log.warn("Error while firing bundle event: " + event.getType() + " for bundle " + getCanonicalName(), t);
-         }
-      }
-      // Normal listeners after, if required
-      int type = event.getType();
-      if (type != BundleEvent.STARTING && type != BundleEvent.STOPPING && type != BundleEvent.LAZY_ACTIVATION)
-      {
-         for (BundleListener listener : bundleListeners)
-         {
-            try
-            {
-               if (listener instanceof SynchronousBundleListener == false)
-                  listener.bundleChanged(event);
-            }
-            catch (Throwable t)
-            {
-               log.warn("Error while firing bundle event: " + event.getType() + " for bundle " + this, t);
-            }
-         }
-      }
-   }
-
-   /**
-    * Fire a framework event
-    * 
-    * @param type the type of event
-    * @param throwable any throwable
-    */
-   void fireFrameworkEvent(int type, Throwable throwable)
-   {
-      // Nobody is interested
-      if (frameworkListeners.isEmpty())
-         return;
-
-      // Are we active?
-      if (getBundleManager().isActive() == false)
-         return;
-
-      // Call the listeners
-      FrameworkEvent event = new FrameworkEvent(type, getBundleInternal(), throwable);
-      for (FrameworkListener listener : frameworkListeners)
-      {
-         try
-         {
-            listener.frameworkEvent(event);
-         }
-         catch (Throwable t)
-         {
-            log.warn("Error while firing framework event: " + event.getType() + " for bundle " + getCanonicalName(), t);
-         }
-      }
-   }
-
-   /**
-    * Fire a service event
-    * 
-    * @param type the type
-    * @param service the service
-    */
-   void fireServiceEvent(int type, OSGiServiceState service)
-   {
-      if (service == null)
-         throw new IllegalArgumentException("No serviceReference");
-
-      // Nobody is interested
-      if (serviceListeners.isEmpty())
-         return;
-
-      // Are we active?
-      if (getBundleManager().isActive() == false)
-         return;
-
-      ServiceEvent event = new ServiceEvent(type, service.getReferenceInternal());
-
-      // Call the listeners
-      for (Map.Entry<ServiceListener, ServiceListenerRegistration> entry : serviceListeners.entrySet())
-      {
-         ServiceListener listener = entry.getKey();
-         ServiceListenerRegistration registration = entry.getValue();
-         try
-         {
-            if (registration.filter.match(service))
-            {
-               AccessControlContext accessControlContext = registration.accessControlContext;
-               if (accessControlContext == null || service.hasPermission(accessControlContext))
-                  listener.serviceChanged(event);
-            }
-         }
-         catch (Throwable t)
-         {
-            log.warn("Error while firing service event: " + type + " for service " + service, t);
-         }
-      }
-   }
-
-   /**
-    * Check the bundle is installed
-    * 
-    * @throws IllegalStateException when the bundle is not installed
-    */
-   protected void checkInstalled()
-   {
-      if ((getState() & Bundle.UNINSTALLED) != 0)
-         throw new IllegalStateException("Bundle " + getCanonicalName() + " is not installed");
-   }
-
-   /**
-    * Check a bundle context is still valid
-    * 
-    * @return the bundle context
-    * @throws IllegalArgumentException when the context is no longer valid
-    */
-   protected synchronized BundleContext checkValidBundleContext()
-   {
-      BundleContext result = this.bundleContext;
-      if (result == null)
-         throw new IllegalStateException("Bundle context is no longer valid");
-      return result;
-   }
-
-   /**
-    * Check the admin permission
-    * 
-    * @param what what permission to check
-    * @throws SecurityException when the caller does not have the AdminPermission and a security manager is installed
-    */
-   protected void checkAdminPermission(String what)
-   {
-      SecurityManager sm = System.getSecurityManager();
-      if (sm != null)
-         sm.checkPermission(new AdminPermission(this, what));
-   }
-
-   /**
-    * Checks if we have the admin permission
-    * 
-    * @param what the permission to check
-    * @return true if the caller doesn't have the permission
-    */
-   private boolean noAdminPermission(String what)
-   {
-      try
-      {
-         checkAdminPermission(what);
-         return false;
-      }
-      catch (SecurityException e)
-      {
-         return true;
-      }
-   }
-
-   /**
-    * Get a human readable version of the state
-    * 
-    * @param state the state
-    * @return the human readable form
-    */
-   private static String toHumanReadableStateString(int state)
-   {
-      switch (state)
-      {
-         case Bundle.INSTALLED:
-            return "INSTALLED";
-         case Bundle.RESOLVED:
-            return "RESOLVED";
-         case Bundle.STARTING:
-            return "STARTING";
-         case Bundle.ACTIVE:
-            return "ACTIVE";
-         case Bundle.STOPPING:
-            return "STOPPING";
-         case Bundle.UNINSTALLED:
-            return "UNINSTALLED";
-      }
-      return "???" + state;
-   }
-
-   /**
-    * ServiceListenerRegistration.
-    */
-   private static class ServiceListenerRegistration
-   {
-      // Any filter
-      Filter filter;
-
-      // Any access control context
-      AccessControlContext accessControlContext;
-
-      /**
-       * Create a new ServiceListenerRegistration.
-       * 
-       * @param filter the filter
-       */
-      public ServiceListenerRegistration(Filter filter)
-      {
-         this.filter = filter;
-
-         if (System.getSecurityManager() != null)
-            accessControlContext = AccessController.getContext();
-      }
-   }
 }

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleWrapper.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleWrapper.java	2009-08-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleWrapper.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -45,7 +45,7 @@
 public class OSGiBundleWrapper implements Bundle
 {
    /** The bundle state */
-   private OSGiBundleState bundleState;
+   private AbstractBundleState bundleState;
    
    /**
     * Create a new OSGiBundleImpl.
@@ -53,7 +53,7 @@
     * @param bundleState the bundle state
     * @throws IllegalArgumentException for a null parameter
     */
-   public OSGiBundleWrapper(OSGiBundleState bundleState)
+   public OSGiBundleWrapper(AbstractBundleState bundleState)
    {
       if (bundleState == null)
          throw new IllegalArgumentException("Null bundle state");
@@ -65,7 +65,7 @@
     * 
     * @return the bundle state
     */
-   OSGiBundleState getBundleState()
+   AbstractBundleState getBundleState()
    {
       return bundleState;
    }

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java	2009-08-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -58,7 +58,7 @@
    private static final AtomicLong serviceIDGenerator = new AtomicLong();
 
    /** The bundle state */
-   private OSGiBundleState bundleState;
+   private AbstractBundleState bundleState;
    
    /** The service reference */
    private OSGiServiceReferenceWrapper serviceReference;
@@ -82,7 +82,7 @@
    private CaseInsensitiveDictionary properties;
 
    /** The using bundles */
-   private Set<OSGiBundleState> usingBundles = new ConcurrentSet<OSGiBundleState>(); 
+   private Set<AbstractBundleState> usingBundles = new ConcurrentSet<AbstractBundleState>(); 
    
    /**
     * Create a new OSGiServiceState.
@@ -94,7 +94,7 @@
     * @throws IllegalArgumentException for a null parameter
     */
    @SuppressWarnings("unchecked")
-   public OSGiServiceState(OSGiBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
+   public OSGiServiceState(AbstractBundleState bundleState, String[] clazzes, Object service, Dictionary properties)
    {
       if (bundleState == null)
          throw new IllegalArgumentException("Null bundle state");
@@ -226,7 +226,7 @@
     * 
     * @return the bundleState.
     */
-   public OSGiBundleState getBundleState()
+   public AbstractBundleState getBundleState()
    {
       return bundleState;
    }
@@ -275,7 +275,7 @@
     * 
     * @param bundleState the bundle
     */
-   void addUsingBundle(OSGiBundleState bundleState)
+   void addUsingBundle(AbstractBundleState bundleState)
    {
       usingBundles.add(bundleState);
    }
@@ -285,7 +285,7 @@
     * 
     * @param bundleState the bundle
     */
-   void removeUsingBundle(OSGiBundleState bundleState)
+   void removeUsingBundle(AbstractBundleState bundleState)
    {
       usingBundles.remove(bundleState);
    }
@@ -296,7 +296,7 @@
          return null;
 
       Set<Bundle> result = new HashSet<Bundle>();
-      for (OSGiBundleState bundleState : usingBundles)
+      for (AbstractBundleState bundleState : usingBundles)
          result.add(bundleState.getBundleInternal());
       return result.toArray(new Bundle[result.size()]);
    }
@@ -311,7 +311,7 @@
          throw new IllegalArgumentException("Unknown bundle: " + bundle);
       
       OSGiBundleWrapper wrapper = (OSGiBundleWrapper) bundle;
-      OSGiBundleState other = wrapper.getBundleState();
+      AbstractBundleState other = wrapper.getBundleState();
       return isAssignableTo(other, className);
    }
 
@@ -322,7 +322,7 @@
     * @param className the class name
     * @return true when assignable
     */
-   boolean isAssignableTo(OSGiBundleState other, String className)
+   boolean isAssignableTo(AbstractBundleState other, String className)
    {
       if (className == null)
          throw new IllegalArgumentException("Null class name");
@@ -360,7 +360,7 @@
     * @param other the bundle state
     * @return true when assignable
     */
-   boolean isAssignable(OSGiBundleState other)
+   boolean isAssignable(AbstractBundleState other)
    {
       if (other == bundleState)
          return true;
@@ -382,7 +382,7 @@
     * @param className the class name
     * @return true when the class name matches
     */
-   boolean matchClass(OSGiBundleState other, String className)
+   boolean matchClass(AbstractBundleState other, String className)
    {
       if (clazzes == null || clazzes.length == 0)
          return false;
@@ -511,7 +511,7 @@
    {
       if (usingBundles.isEmpty() == false)
       {
-         for (OSGiBundleState using : usingBundles)
+         for (AbstractBundleState using : usingBundles)
             using.ungetService(this);
       }
 

Deleted: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemBundle.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemBundle.java	2009-08-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemBundle.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -1,99 +0,0 @@
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2009, Red Hat Middleware LLC, and individual contributors
-* as indicated by the @author tags. See the copyright.txt file 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.plugins.facade.bundle;
-
-import java.io.InputStream;
-
-import org.jboss.osgi.spi.NotImplementedException;
-import org.jboss.osgi.spi.metadata.OSGiMetaData;
-import org.osgi.framework.AdminPermission;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.Constants;
-
-/**
- * OSGiSystemBundle.
- * 
- * @author <a href="adrian at jboss.com">Adrian Brock</a>
- * @version $Revision: 1.1 $
- */
-class OSGiSystemBundle extends OSGiBundleState
-{
-   /**
-    * Create a new OSGiSystemBundle.
-    * 
-    * @param osgiMetaData the metadata for the system bundle
-    */
-   public OSGiSystemBundle(OSGiMetaData osgiMetaData)
-   {
-      super(osgiMetaData);
-   }
-
-   @Override
-   public String getLocation()
-   {
-      return Constants.SYSTEM_BUNDLE_LOCATION;
-   }
-
-   public Class<?> loadClass(String name) throws ClassNotFoundException
-   {
-      // [JBOSGI-138] Proper system BundleContext implementation
-      throw new NotImplementedException();
-   }
-   
-   @Override
-   public void stop(int options) throws BundleException
-   {
-      final OSGiBundleManager bundleManager = getBundleManager();
-      getBundleManager().getExecutor().execute(new Runnable()
-      {
-         public void run()
-         {
-            bundleManager.stopFramework();
-         }
-      });
-   }
-
-   @Override
-   public void update() throws BundleException
-   {
-      final OSGiBundleManager bundleManager = getBundleManager();
-      getBundleManager().getExecutor().execute(new Runnable()
-      {
-         public void run()
-         {
-            bundleManager.restartFramework();
-         }
-      });
-   }
-
-   @Override
-   public void update(InputStream in) throws BundleException
-   {
-      throw new BundleException("The system bundle cannot be updated from a stream");
-   }
-
-   @Override
-   public void uninstall() throws BundleException
-   {
-      throw new BundleException("The system bundle cannot be uninstalled");
-   }
-}

Copied: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemState.java (from rev 92848, projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemBundle.java)
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemState.java	                        (rev 0)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiSystemState.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -0,0 +1,155 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.plugins.facade.bundle;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Enumeration;
+
+import org.jboss.osgi.spi.NotImplementedException;
+import org.jboss.osgi.spi.metadata.OSGiMetaData;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+
+/**
+ * OSGiSystemBundle.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author Thomas.Diesler at jboss.com
+ * @version $Revision: 1.1 $
+ */
+public class OSGiSystemState extends AbstractBundleState
+{
+   /**
+    * Create a new OSGiSystemBundle.
+    * 
+    * @param osgiMetaData the metadata for the system bundle
+    */
+   public OSGiSystemState(OSGiMetaData osgiMetaData)
+   {
+      super(osgiMetaData);
+      changeState(Bundle.INSTALLED);
+   }
+
+   @Override
+   public long getBundleId()
+   {
+      return 0;
+   }
+   
+   @Override
+   public String getLocation()
+   {
+      return Constants.SYSTEM_BUNDLE_LOCATION;
+   }
+
+   public Class<?> loadClass(String name) throws ClassNotFoundException
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+   
+   @Override
+   @SuppressWarnings("unchecked")
+   public Enumeration findEntries(String path, String filePattern, boolean recurse)
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+
+   @Override
+   public URL getEntry(String path)
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public Enumeration getEntryPaths(String path)
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+
+   @Override
+   public URL getResource(String name)
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+
+   @Override
+   @SuppressWarnings("unchecked")
+   public Enumeration getResources(String name) throws IOException
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+
+   @Override
+   public void start(int options) throws BundleException
+   {
+      // [JBOSGI-138] Proper system BundleContext implementation
+      throw new NotImplementedException();
+   }
+
+   @Override
+   public void stop(int options) throws BundleException
+   {
+      final OSGiBundleManager bundleManager = getBundleManager();
+      getBundleManager().getExecutor().execute(new Runnable()
+      {
+         public void run()
+         {
+            bundleManager.stopFramework();
+         }
+      });
+   }
+
+   @Override
+   public void update() throws BundleException
+   {
+      final OSGiBundleManager bundleManager = getBundleManager();
+      getBundleManager().getExecutor().execute(new Runnable()
+      {
+         public void run()
+         {
+            bundleManager.restartFramework();
+         }
+      });
+   }
+
+   @Override
+   public void update(InputStream in) throws BundleException
+   {
+      throw new BundleException("The system bundle cannot be updated from a stream");
+   }
+
+   @Override
+   public void uninstall() throws BundleException
+   {
+      throw new BundleException("The system bundle cannot be uninstalled");
+   }
+}

Modified: projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiClassLoaderSystem.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiClassLoaderSystem.java	2009-08-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/classloading/OSGiClassLoaderSystem.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -33,6 +33,7 @@
 import org.jboss.classloader.spi.filter.ClassFilter;
 import org.jboss.classloader.spi.filter.PackageClassFilter;
 import org.jboss.osgi.plugins.facade.api.SystemPackagesPlugin;
+import org.jboss.osgi.plugins.facade.bundle.AbstractBundleState;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleManager;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
 
@@ -63,6 +64,7 @@
       filter.setIncludeJava(true);
       
       domain.setParentPolicy(new ParentPolicy(filter, ClassFilter.NOTHING));
+      AbstractJDKChecker.getExcluded().add(AbstractBundleState.class);
       AbstractJDKChecker.getExcluded().add(OSGiBundleState.class);
    }
 

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-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFramework.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -28,8 +28,8 @@
 
 import org.jboss.logging.Logger;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleManager;
-import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleWrapper;
+import org.jboss.osgi.plugins.facade.bundle.OSGiSystemState;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.launch.Framework;
@@ -47,7 +47,7 @@
    
    private OSGiBundleManager bundleManager;
    
-   public OSGiFramework(OSGiBundleManager bundleManager, OSGiBundleState bundleState)
+   public OSGiFramework(OSGiBundleManager bundleManager, OSGiSystemState bundleState)
    {
       super(bundleState);
       this.bundleManager = bundleManager;

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-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/main/java/org/jboss/osgi/plugins/facade/launch/OSGiFrameworkFactory.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -35,7 +35,8 @@
 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
 import org.jboss.logging.Logger;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleManager;
-import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
+import org.jboss.osgi.plugins.facade.bundle.AbstractBundleState;
+import org.jboss.osgi.plugins.facade.bundle.OSGiSystemState;
 import org.osgi.framework.launch.Framework;
 import org.osgi.framework.launch.FrameworkFactory;
 
@@ -86,7 +87,7 @@
          throw new IllegalStateException("Cannot obtain installed bean: " + OSGiBundleManager.BEAN_BUNDLE_MANAGER);
 
       OSGiBundleManager manager = (OSGiBundleManager)managerContext.getTarget();
-      OSGiBundleState sysBundle = manager.getBundle(0);
+      OSGiSystemState sysBundle = (OSGiSystemState)manager.getBundle(0);
       
       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-27 00:39:26 UTC (rev 92848)
+++ projects/jboss-osgi/projects/runtime/microcontainer/trunk/src/test/java/org/jboss/test/osgi/OSGiTestDelegate.java	2009-08-27 08:38:59 UTC (rev 92849)
@@ -33,7 +33,7 @@
 import org.jboss.deployers.plugins.main.MainDeployerImpl;
 import org.jboss.deployers.structure.spi.DeploymentUnit;
 import org.jboss.osgi.plugins.facade.bundle.OSGiBundleManager;
-import org.jboss.osgi.plugins.facade.bundle.OSGiBundleState;
+import org.jboss.osgi.plugins.facade.bundle.AbstractBundleState;
 import org.jboss.test.kernel.junit.MicrocontainerTestDelegate;
 import org.jboss.virtual.AssembledDirectory;
 import org.jboss.virtual.VFS;
@@ -125,8 +125,8 @@
    protected void undeployBundles() 
    {
       OSGiBundleManager bundleManager = getBundleManager();
-      Collection<OSGiBundleState> bundles = bundleManager.getBundles();
-      for (OSGiBundleState bundleState : bundles)
+      Collection<AbstractBundleState> bundles = bundleManager.getBundles();
+      for (AbstractBundleState bundleState : bundles)
       {
          try
          {
@@ -189,7 +189,7 @@
       if (resourceRoot == null)
          throw new AssertionFailedError("Resource not found: " + root);
       URL childResource = new URL(resourceRoot, child);
-      OSGiBundleState bundleState = getBundleManager().install(childResource);
+      AbstractBundleState bundleState = getBundleManager().install(childResource);
       return bundleState.getBundleInternal();
    }
 
@@ -202,7 +202,7 @@
     */
    public Bundle addBundle(VirtualFile file) throws Exception
    {
-      OSGiBundleState bundleState = getBundleManager().install(file);
+      AbstractBundleState bundleState = getBundleManager().install(file);
       return bundleState.getBundleInternal();
    }
 




More information about the jboss-cvs-commits mailing list