incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lin...@apache.org
Subject svn commit: r1040641 - in /incubator/aries/trunk/subsystem/subsystem-core: ./ src/main/java/org/apache/aries/subsystem/core/internal/ src/main/java/org/eclipse/ src/main/java/org/eclipse/equinox/ src/main/java/org/eclipse/equinox/internal/ src/main/jav...
Date Tue, 30 Nov 2010 16:54:57 GMT
Author: linsun
Date: Tue Nov 30 16:54:56 2010
New Revision: 1040641

URL: http://svn.apache.org/viewvc?rev=1040641&view=rev
Log:
(ARIES-498) Sybsystem: subsystem-core updated to use scope instead of composite bundle

Added:
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/eclipse/
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/eclipse/equinox/
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/eclipse/equinox/internal/
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/eclipse/equinox/internal/scope/
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/eclipse/equinox/scope/
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/eclipse/equinox/scope/impl/
Modified:
    incubator/aries/trunk/subsystem/subsystem-core/pom.xml
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemAdminImpl.java
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java
    incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java

Modified: incubator/aries/trunk/subsystem/subsystem-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/subsystem/subsystem-core/pom.xml?rev=1040641&r1=1040640&r2=1040641&view=diff
==============================================================================
--- incubator/aries/trunk/subsystem/subsystem-core/pom.xml (original)
+++ incubator/aries/trunk/subsystem/subsystem-core/pom.xml Tue Nov 30 16:54:56 2010
@@ -38,6 +38,7 @@
         </aries.osgi.activator>
         <aries.osgi.private.pkg>
             org.apache.aries.subsystem.core.internal,
+            org.apache.aries.subsystem.core.obr,
             org.apache.felix.utils.manifest
         </aries.osgi.private.pkg>
     </properties>
@@ -48,6 +49,10 @@
             <artifactId>org.apache.aries.subsystem.api</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.aries.subsystem</groupId>
+            <artifactId>org.apache.aries.subsystem.scope.api</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.apache.aries.application</groupId>
             <artifactId>org.apache.aries.application.api</artifactId>
         </dependency>
@@ -63,6 +68,7 @@
         <dependency>
             <groupId>org.eclipse</groupId>
             <artifactId>osgi</artifactId>
+            <version>3.7.0.v20100910</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java?rev=1040641&r1=1040640&r2=1040641&view=diff
==============================================================================
--- incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java (original)
+++ incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java Tue Nov 30 16:54:56 2010
@@ -14,21 +14,31 @@
 package org.apache.aries.subsystem.core.internal;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.aries.subsystem.Subsystem;
 import org.apache.aries.subsystem.SubsystemAdmin;
 import org.apache.aries.subsystem.SubsystemConstants;
+import org.apache.aries.subsystem.SubsystemException;
+import org.apache.aries.subsystem.scope.ScopeAdmin;
 import org.apache.aries.subsystem.spi.ResourceProcessor;
 import org.apache.aries.subsystem.spi.ResourceResolver;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,18 +50,19 @@ import org.slf4j.LoggerFactory;
 public class Activator implements BundleActivator {
     private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
     
-    private BundleContext context;
+    private static BundleContext context;
     private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
     private static SubsystemEventDispatcher eventDispatcher;
-
+    private static SubsystemAdminFactory adminFactory;
+    
     public void start(BundleContext context) throws Exception {
         if (LOGGER.isDebugEnabled()) {
             LOGGER.debug("subsystem activator starting");
         }
-        this.context = context;
-        eventDispatcher = new SubsystemEventDispatcher(context);
-        
-        register(SubsystemAdmin.class, new SubsystemAdminFactory(), null);
+        Activator.context = context;
+        Activator.eventDispatcher = new SubsystemEventDispatcher(context);
+        adminFactory = new SubsystemAdminFactory();
+        register(SubsystemAdmin.class, adminFactory, null);
         register(ResourceResolver.class,
                  new NoOpResolver(),
                  DictionaryBuilder.build(Constants.SERVICE_RANKING, Integer.MIN_VALUE));
@@ -86,27 +97,121 @@ public class Activator implements Bundle
             }
         }
         eventDispatcher.destroy();
+        if (adminFactory!= null) {
+            adminFactory.destroy();
+        }
+        
+    }
+    
+    public static BundleContext getBundleContext() {
+        return context;
+    }
+    
+    public static SubsystemEventDispatcher getEventDispatcher() {
+        return eventDispatcher;
     }
 
 
     public static class SubsystemAdminFactory implements ServiceFactory {
+        //private final List<ScopeAdmin> scopeAdmins = new ArrayList<ScopeAdmin>();
+        private final List<SubsystemAdmin> admins = new ArrayList<SubsystemAdmin>();
+        private final Map<SubsystemAdmin, Long> references = new HashMap<SubsystemAdmin, Long>();
+        private ScopeAdmin scopeAdmin; // scope admin for the root scope.
+        private static ServiceTracker serviceTracker;
+        private SubsystemAdmin defaultAdmin;
+        private ServiceRegistration rootAdminReg;
+        
+        public SubsystemAdminFactory() throws InvalidSyntaxException  {
+            context = Activator.getBundleContext();
+            
+            ServiceReference[] reference = Activator.getBundleContext().getServiceReferences(ScopeAdmin.class.getName(), 
+            "(&(ScopeName=root))");
+            if (reference != null && reference.length == 1) {
+                ScopeAdmin scopeAdmin = (ScopeAdmin)Activator.getBundleContext().getService(reference[0]);
+                Subsystem subsystem = new SubsystemImpl(scopeAdmin.getScope(), new HashMap<String, String>());
+                defaultAdmin = new SubsystemAdminImpl(scopeAdmin, subsystem, null);
+                rootAdminReg = context.registerService(SubsystemAdmin.class.getName(), 
+                        defaultAdmin, 
+                        DictionaryBuilder.build("Subsystem", subsystem.getSubsystemId(), "SubsystemParentId", 0));
+                admins.add(defaultAdmin);
+            } else {
+                throw new RuntimeException("Unable to locate service reference for the root scope admin");
+            }
+            
+            Filter filter = FrameworkUtil.createFilter("(&("
+                    + Constants.OBJECTCLASS + "=" + SubsystemAdmin.class.getName() + "))");
+            serviceTracker = new ServiceTracker(context, filter,
+                    new ServiceTrackerCustomizer() {
+
+                        public Object addingService(ServiceReference reference) {
+                            // adding new service, update admins map
+                            SubsystemAdmin sa = (SubsystemAdmin) context
+                                    .getService(reference);
+                            admins.add(sa);
+
+                            return sa;
+                        }
+
+                        public void modifiedService(ServiceReference reference,
+                                Object service) {
+                            // TODO Auto-generated method stub
+
+                        }
+
+                        public void removedService(ServiceReference reference,
+                                Object service) {
+                            SubsystemAdmin sa = (SubsystemAdmin) service;
+                            admins.remove(sa);
+                        }
 
-        private final Map<BundleContext, SubsystemAdminImpl> admins = new HashMap<BundleContext, SubsystemAdminImpl>();
-        private final Map<SubsystemAdminImpl, Long> references = new HashMap<SubsystemAdminImpl, Long>();
-
+                    });
+        }
+        
+        public void destroy() {
+            serviceTracker.close();
+        }
+        
+        private SubsystemAdmin getSubsystemAdmin(Bundle bundle) {
+            // first check if it is in root framework
+            Bundle[] bundles = Activator.getBundleContext().getBundles();
+            for (Bundle b : bundles) {
+                if (b == bundle) {
+                    return defaultAdmin;
+                }
+            }
+            // check if they are bundles in the 
+            for (SubsystemAdmin admin : admins) {
+                Collection<Subsystem> subsystems = admin.getSubsystems();
+                for (Subsystem subsystem : subsystems) {
+                    Collection<Bundle> subsystemBundles = subsystem.getBundles();
+                    for (Bundle b : subsystemBundles) {
+                        if (b == bundle) {
+                            return admin;
+                        }
+                    }
+                }
+            }
+            
+            return null;
+        }
         public synchronized Object getService(Bundle bundle, ServiceRegistration registration) {
             if (LOGGER.isDebugEnabled()) {
                 LOGGER.debug("Get SubsystemAdmin service from bundle symbolic name {} version {}", bundle.getSymbolicName(), bundle.getVersion());
             }
-            BundleContext systemBundleContext = bundle.getBundleContext().getBundle(0).getBundleContext();
-            SubsystemAdminImpl admin = admins.get(systemBundleContext);
+            
             long ref = 0;
+            
+            // figure out the subsystemAdmin for the bundle           
+            SubsystemAdmin admin = getSubsystemAdmin(bundle);
+            
             if (admin == null) {
-                admin = new SubsystemAdminImpl(systemBundleContext, eventDispatcher);
-                admins.put(systemBundleContext, admin);
-            } else {
-                ref = references.get(admin);
+                throw new SubsystemException("Unable to locate the Subsystem admin for the bundle " + bundle.toString());
+            }
+
+            if (references.get(admin) == null) {
+                ref = 0;
             }
+            
             references.put(admin, ref + 1);
             return admin;
         }

Modified: incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java?rev=1040641&r1=1040640&r2=1040641&view=diff
==============================================================================
--- incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java (original)
+++ incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java Tue Nov 30 16:54:56 2010
@@ -13,6 +13,7 @@
  */
 package org.apache.aries.subsystem.core.internal;
 
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -20,30 +21,36 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Map.Entry;
 
+import org.apache.aries.subsystem.SubsystemAdmin;
 import org.apache.aries.subsystem.SubsystemConstants;
 import org.apache.aries.subsystem.SubsystemException;
+import org.apache.aries.subsystem.scope.InstallInfo;
+import org.apache.aries.subsystem.scope.Scope;
+import org.apache.aries.subsystem.scope.ScopeAdmin;
+import org.apache.aries.subsystem.scope.ScopeUpdate;
 import org.apache.aries.subsystem.spi.Resource;
 import org.apache.aries.subsystem.spi.ResourceProcessor;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
-import org.osgi.service.composite.CompositeBundle;
 
 public class BundleResourceProcessor implements ResourceProcessor {
 
-    public Session createSession(BundleContext context) {
-        return new BundleSession(context);
+    public Session createSession(SubsystemAdmin subsystemAdmin) {
+        return new BundleSession(subsystemAdmin);
     }
 
     public static class BundleSession implements Session {
 
-        private final BundleContext context;
+        private final ScopeAdmin scopeAdmin;
         private final List<Bundle> installed = new ArrayList<Bundle>();
         private final Map<Resource, Bundle> updated = new HashMap<Resource, Bundle>();
         private final Map<Resource, Bundle> removed = new HashMap<Resource, Bundle>();
+        
 
-        public BundleSession(BundleContext context) {
-            this.context = context;
+        public BundleSession(SubsystemAdmin subsystemAdmin) {
+            SubsystemAdminImpl subsystemAdminImpl = (SubsystemAdminImpl)subsystemAdmin;
+            this.scopeAdmin = subsystemAdminImpl.getScopeAdmin();
         }
 
         public void process(Resource resource) throws SubsystemException {
@@ -52,9 +59,11 @@ public class BundleResourceProcessor imp
                 Bundle bundle = findBundle(resource);
                 
                 if (bundle == null) {
-                    // fresh install
-                    bundle = context.installBundle(resource.getLocation(), resource.open());
-                    installed.add(bundle);
+                    // fresh install 
+                    InstallInfo installInfo = new InstallInfo(new URL(resource.getLocation()), resource.getLocation());
+                    ScopeUpdate scopeUpdate = scopeAdmin.newScopeUpdate();
+                    scopeUpdate.getBundlesToInstall().add(installInfo);
+                    scopeUpdate.commit();
                 } else {
                     // update only if RESOURCE_UPDATE_ATTRIBUTE is set to true
                     String updateAttribute = resource.getAttributes().get(SubsystemConstants.RESOURCE_UPDATE_ATTRIBUTE);
@@ -63,6 +72,11 @@ public class BundleResourceProcessor imp
                         updated.put(resource, bundle);
                     }
                 }
+                
+                if (bundle == null) {
+                    bundle = findBundle(resource);
+                    installed.add(bundle);
+                }
 
                 String startAttribute = resource.getAttributes().get(SubsystemConstants.RESOURCE_START_ATTRIBUTE);
                 
@@ -71,8 +85,6 @@ public class BundleResourceProcessor imp
                     startAttribute = "true";
                 }
                 if ("true".equals(startAttribute)) {
-                    // This will only mark the bundle as persistently started as the composite is supposed
-                    // to be stopped
                     bundle.start();
                 }
             } catch (SubsystemException e) {
@@ -126,7 +138,10 @@ public class BundleResourceProcessor imp
                     Bundle bundle = entry.getValue();
                     Resource res = entry.getKey();
                     try {
-                        context.installBundle(res.getLocation(), res.open());
+                        InstallInfo installInfo = new InstallInfo(res.open(), res.getLocation());
+                        ScopeUpdate scopeUpdate = scopeAdmin.newScopeUpdate();
+                        scopeUpdate.getBundlesToInstall().add(installInfo);
+                        scopeUpdate.commit();
                     } catch (Exception e) {
                         // Ignore
                     }
@@ -137,14 +152,10 @@ public class BundleResourceProcessor imp
         }
         
         protected Bundle findBundle(Resource resource) {
-            for (Bundle b : context.getBundles()) {
-                if (resource.getLocation().equals(b.getLocation())) {
-                    if (b instanceof CompositeBundle) {
-                        throw new SubsystemException("A bundle with the same location already exists!");
-                    } else {
-                        return b;
-
-                    }
+            Scope scope = scopeAdmin.getScope();
+            for (Bundle b : scope.getBundles()) {
+                if (resource.getLocation().equals(scope.getLocation())) {
+                    return b;
                 }
             }
             
@@ -158,5 +169,10 @@ public class BundleResourceProcessor imp
         }
     }
 
+    public Session createSession(BundleContext arg0) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 
 }

Modified: incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemAdminImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemAdminImpl.java?rev=1040641&r1=1040640&r2=1040641&view=diff
==============================================================================
--- incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemAdminImpl.java (original)
+++ incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemAdminImpl.java Tue Nov 30 16:54:56 2010
@@ -28,18 +28,13 @@ import org.apache.aries.subsystem.Subsys
 import org.apache.aries.subsystem.SubsystemEvent;
 import org.apache.aries.subsystem.SubsystemException;
 import org.apache.aries.subsystem.SubsystemListener;
+import org.apache.aries.subsystem.scope.ScopeAdmin;
 import org.apache.aries.subsystem.spi.Resource;
 import org.apache.aries.subsystem.spi.ResourceResolver;
-import org.apache.felix.utils.manifest.Clause;
-import org.apache.felix.utils.manifest.Parser;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.Constants;
-import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
-import org.osgi.service.composite.CompositeAdmin;
-import org.osgi.service.composite.CompositeBundle;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -49,94 +44,57 @@ public class SubsystemAdminImpl implemen
     private static final Version SUBSYSTEM_MANIFEST_VERSION = new Version("1.0");
 
     final BundleContext context;
+    final ScopeAdmin scopeAdmin;
     final Map<Long, Subsystem> subsystems = new HashMap<Long, Subsystem>();
-    final ServiceTracker compositeAdminTracker;
     final ServiceTracker resourceResolverTracker;
     final SubsystemEventDispatcher eventDispatcher;
     final ServiceTracker listenersTracker;
+    final Subsystem subsystem;
+    final Subsystem parentSubsystem;
     
-    public SubsystemAdminImpl(BundleContext context, SubsystemEventDispatcher eventDispatcher) {
-        this.context = context;
-        this.eventDispatcher = eventDispatcher;
-        this.compositeAdminTracker = new ServiceTracker(context, CompositeAdmin.class.getName(), null);
-        this.compositeAdminTracker.open();
+    public SubsystemAdminImpl(ScopeAdmin scopeAdmin, Subsystem subsystem, Subsystem parentSubsystem) {
+        context = Activator.getBundleContext();
+        this.eventDispatcher = Activator.getEventDispatcher();
+        this.scopeAdmin = scopeAdmin;
+        this.subsystem = subsystem;
+        this.parentSubsystem = parentSubsystem;
         this.resourceResolverTracker = new ServiceTracker(context, ResourceResolver.class.getName(), null);
         this.resourceResolverTracker.open();
         this.listenersTracker = new ServiceTracker(context, SubsystemListener.class.getName(), null);
         this.listenersTracker.open();
-        // Track subsystems
-        synchronized (subsystems) {
-            this.context.addBundleListener(new SynchronousBundleListener() {
-                public void bundleChanged(BundleEvent event) {
-                    SubsystemAdminImpl.this.bundleChanged(event);
-                }
-            });
-            loadSubsystems();
-        }
     }
 
     public void dispose() {
-        compositeAdminTracker.close();
         resourceResolverTracker.close();
         listenersTracker.close();
     }
-
-    public void bundleChanged(BundleEvent event) {
-        synchronized (subsystems) {
-            Bundle bundle = event.getBundle();
-            if (event.getType() == BundleEvent.UPDATED || event.getType() == BundleEvent.UNINSTALLED) {
-                if (LOGGER.isDebugEnabled()) {
-                    LOGGER.debug("Removing bundle symbolic name {} version {} from subsystems map being tracked", bundle.getSymbolicName(), bundle.getVersion());
-                }
-                subsystems.remove(bundle.getBundleId());
-            }
-            if (event.getType() == BundleEvent.INSTALLED || event.getType() == BundleEvent.UPDATED) {
-                Subsystem s = isSubsystem(bundle);
-                if (s != null) {
-                    if (LOGGER.isDebugEnabled()) {
-                        LOGGER.debug("Adding bundle symbolic name {} version {} to subsystems map being tracked", bundle.getSymbolicName(), bundle.getVersion());
-                    }
-                    subsystems.put(s.getSubsystemId(), s);
-                }
-            }
-            if (event.getType() == BundleEvent.RESOLVED) {
-                Subsystem s = isSubsystem(bundle);
-                if (s != null) {
-                    // emit the subsystem resolved event
-                    eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.RESOLVED, System.currentTimeMillis(), s));
-                }
-            }
-        }
-    }
-
-    protected void loadSubsystems() {
-        synchronized (subsystems) {
-            subsystems.clear();
-            for (Bundle bundle : context.getBundles()) {
-                Subsystem s = isSubsystem(bundle);
-                if (s != null) {
-                    if (LOGGER.isDebugEnabled()) {
-                        LOGGER.debug("Adding bundle symbolic name {} version {} to subsystems map being tracked", bundle.getSymbolicName(), bundle.getVersion());
-                    }
-                    subsystems.put(s.getSubsystemId(), s);
+    
+    private synchronized void refreshSubsystems() {
+        subsystems.clear();
+        /*for (Subsystem sub : subsystem.getChildrenSubsystems()) {
+            subsystems.put(sub.getSubsystemId(), sub);
+        }*/
+        
+        final String filter = "(SubsystemParentId=" + subsystem.getSubsystemId() + ")";
+                  
+        try {
+            ServiceReference[] srs = context.getServiceReferences(SubsystemAdmin.class.getName(), filter);
+            if (srs != null) {
+                for (ServiceReference sr : srs) {
+                    SubsystemAdmin childSubAdmin = (SubsystemAdmin)context.getService(sr);
+                    Subsystem childSub = childSubAdmin.getSubsystem();
+                    subsystems.put(childSub.getSubsystemId(), childSub);
+                    context.ungetService(sr);
                 }
+                
             }
+        } catch (InvalidSyntaxException e) {
+            // ignore
         }
     }
-
-    protected Subsystem isSubsystem(Bundle bundle) {
-        if (bundle instanceof CompositeBundle) {
-            // it is important not to use bundle.getSymbolicName() here as that would not contain the directives we need.
-            String bsn = (String) bundle.getHeaders().get(Constants.BUNDLE_SYMBOLICNAME);
-            Clause[] bsnClauses = Parser.parseHeader(bsn);
-            if ("true".equals(bsnClauses[0].getDirective(SubsystemConstants.SUBSYSTEM_DIRECTIVE))) {
-                return new SubsystemImpl(this, (CompositeBundle) bundle, eventDispatcher);
-            }
-        }
-        return null;
-    }
-
+    
     public Subsystem getSubsystem(long id) {
+        refreshSubsystems();
         synchronized (subsystems) {
             for (Subsystem s : subsystems.values()) {
                 if (s.getSubsystemId() == id) {
@@ -148,6 +106,7 @@ public class SubsystemAdminImpl implemen
     }
 
     public Subsystem getSubsystem(String symbolicName, Version version) {
+        refreshSubsystems();
         synchronized (subsystems) {
             for (Subsystem s : subsystems.values()) {
                 if (s.getSymbolicName().equals(symbolicName) && s.getVersion().equals(version)) {
@@ -157,8 +116,9 @@ public class SubsystemAdminImpl implemen
             return null;
         }
     }
-
+    
     public Collection<Subsystem> getSubsystems() {
+        refreshSubsystems();
         synchronized (subsystems) {
             return Collections.unmodifiableCollection(new ArrayList(subsystems.values()));
         }
@@ -191,7 +151,7 @@ public class SubsystemAdminImpl implemen
             }
         };
         SubsystemResourceProcessor processor = new SubsystemResourceProcessor();
-        SubsystemResourceProcessor.SubsystemSession session = processor.createSession(context);
+        SubsystemResourceProcessor.SubsystemSession session = processor.createSession(this);
         boolean success = false;
         try {
             session.process(subsystemResource);
@@ -256,7 +216,7 @@ public class SubsystemAdminImpl implemen
             }
         };
         SubsystemResourceProcessor processor = new SubsystemResourceProcessor();
-        SubsystemResourceProcessor.SubsystemSession session = processor.createSession(context);
+        SubsystemResourceProcessor.SubsystemSession session = processor.createSession(this);
         boolean success = false;
         try {
             session.process(subsystemResource);
@@ -279,12 +239,15 @@ public class SubsystemAdminImpl implemen
 
     public void uninstall(Subsystem subsystem) {
         if (subsystem.getState().equals(Subsystem.State.UNINSTALLED)) {
-            throw new IllegalStateException("Unable to uninstall subsystem as subsystem is already uninstalled");
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("Unable to uninstall subsystem {} as subsystem is already uninstalled", subsystem.getSymbolicName());
+            }
+            return;
         }
         
         Resource subsystemResource = new ResourceImpl(subsystem.getSymbolicName(), subsystem.getVersion(), SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM, subsystem.getLocation(), Collections.<String, String>emptyMap());
         SubsystemResourceProcessor processor = new SubsystemResourceProcessor();
-        SubsystemResourceProcessor.SubsystemSession session = processor.createSession(context);
+        SubsystemResourceProcessor.SubsystemSession session = processor.createSession(this);
         boolean success = false;
         try {
             session.dropped(subsystemResource);
@@ -318,4 +281,18 @@ public class SubsystemAdminImpl implemen
         }
         return null;
     }
+    
+    // return the scope admin associated with the subsystemadmin.
+    protected ScopeAdmin getScopeAdmin() {
+        return this.scopeAdmin;
+    }
+    
+    public Subsystem getSubsystem() {
+        return this.subsystem;
+    }
+
+    public Subsystem getParentSubsystem() {
+        return this.parentSubsystem;
+    }
 }
+

Modified: incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java?rev=1040641&r1=1040640&r2=1040641&view=diff
==============================================================================
--- incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java (original)
+++ incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java Tue Nov 30 16:54:56 2010
@@ -1,4 +1,5 @@
 /*
+ /*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -16,50 +17,126 @@ package org.apache.aries.subsystem.core.
 import java.util.*;
 
 import org.apache.aries.subsystem.Subsystem;
+import org.apache.aries.subsystem.SubsystemAdmin;
+import org.apache.aries.subsystem.SubsystemConstants;
 import org.apache.aries.subsystem.SubsystemEvent;
 import org.apache.aries.subsystem.SubsystemException;
+import org.apache.aries.subsystem.Subsystem.State;
+import org.apache.aries.subsystem.scope.Scope;
+import org.apache.aries.subsystem.scope.ScopeAdmin;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
-import org.osgi.service.composite.CompositeBundle;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 public class SubsystemImpl implements Subsystem {
 
     final long id;
-    final SubsystemAdminImpl admin;
-    final CompositeBundle composite;
     final SubsystemEventDispatcher eventDispatcher;
+    final Scope scope;
+    Map<String, String> headers;
+    private final ServiceTracker serviceTracker;
+    private final BundleContext context;
+    private final Map<Long, Subsystem> subsystems = new HashMap<Long, Subsystem>();
+
+    public SubsystemImpl(Scope scope, Map<String, String> headers) {
+        this.scope = scope;
+        this.id = this.scope.getId();
+        this.eventDispatcher = Activator.getEventDispatcher();
+        this.headers = headers;
+        Filter filter = null;
+        try {
+            filter = FrameworkUtil.createFilter("(&("
+                    + Constants.OBJECTCLASS + "=" + SubsystemAdmin.class.getName() + "))");
+        } catch (InvalidSyntaxException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        this.context = Activator.getBundleContext();
+        serviceTracker = new ServiceTracker(context, filter,
+                new ServiceTrackerCustomizer() {
+
+                    public Object addingService(ServiceReference reference) {
+                        // adding new service, update admins map
+                        SubsystemAdmin sa = (SubsystemAdmin)context.getService(reference);
+                        if (sa.getParentSubsystem().getSubsystemId() == id) {
+                            // it is the child subsystems for the current subsystem
+                            synchronized (subsystems) {
+                                subsystems.put(sa.getSubsystem().getSubsystemId(), sa.getSubsystem());
+                            }
+                        }
+
+                        return sa;
+                    }
+
+                    public void modifiedService(ServiceReference reference,
+                            Object service) {
+                        // TODO Auto-generated method stub
+
+                    }
+
+                    public void removedService(ServiceReference reference,
+                            Object service) {
+                        SubsystemAdmin sa = (SubsystemAdmin) service;
+                        if (sa.getParentSubsystem().getSubsystemId() == id) {
+                            // it is the child subsystems for the current subsystem
+                            synchronized (subsystems) {
+                                subsystems.remove(sa.getSubsystem().getSubsystemId());
+                            }
+                        }
+                    }
 
-    public SubsystemImpl(SubsystemAdminImpl admin, CompositeBundle composite, SubsystemEventDispatcher eventDispatcher) {
-        this.admin = admin;
-        this.composite = composite;
-        this.id = composite.getBundleId();
-        this.eventDispatcher = eventDispatcher;
+                });
     }
 
     public State getState() {
-        switch (composite.getState())
-        {
-            case Bundle.UNINSTALLED:
-                return State.UNINSTALLED;
-            case Bundle.INSTALLED:
-                return State.INSTALLED;
-            case Bundle.RESOLVED:
-                return State.RESOLVED;
-            case Bundle.STARTING:
-                return State.STARTING;
-            case Bundle.ACTIVE:
-                return State.ACTIVE;
-            case Bundle.STOPPING:
-                return State.STOPPING;
-        }
+        // check bundles status
+        Collection<Bundle> bundles = getBundles();
+        if (checkBundlesStatus(bundles, Bundle.UNINSTALLED)) {
+            return State.UNINSTALLED;
+        } else if (checkBundlesStatus(bundles, Bundle.INSTALLED)) {
+            return State.INSTALLED;
+        } else if (checkBundlesStatus(bundles, Bundle.RESOLVED)) {
+            return State.RESOLVED;
+        } else if (checkBundlesStatus(bundles, Bundle.STARTING)) {
+            return State.STARTING;
+        } else if (checkBundlesStatus(bundles, Bundle.ACTIVE)) {
+            return State.ACTIVE;
+        } else if (checkBundlesStatus(bundles, Bundle.STOPPING)) {
+            return State.STOPPING;
+        } 
+        
         throw new SubsystemException("Unable to retrieve subsystem state");
     }
 
+    /**
+     * check if all bundles in the collection has the state
+     * 
+     * @return
+     */
+    private boolean checkBundlesStatus(Collection<Bundle> bundles, int state) {
+        for (Bundle b : bundles) {
+            if (b.getState() != state) {
+                return false;
+            }
+        }
+     
+        return true;
+    }
     public void start() throws SubsystemException {
         try {
             eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.STARTING, System.currentTimeMillis(), this));
-            composite.start();
+            Collection<Bundle> bundles = this.scope.getBundles();
+            for (Bundle b : bundles) {
+                b.start();
+            }
             eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.STARTED, System.currentTimeMillis(), this));
         } catch (BundleException e) {
             throw new SubsystemException("Unable to start subsystem", e);
@@ -69,7 +146,10 @@ public class SubsystemImpl implements Su
     public void stop() throws SubsystemException {
         try {
             eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.STOPPING, System.currentTimeMillis(), this));
-            composite.stop();
+            Collection<Bundle> bundles = this.scope.getBundles();
+            for (Bundle b : bundles) {
+                b.start();
+            }
             eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.STOPPED, System.currentTimeMillis(), this));
         } catch (BundleException e) {
             throw new SubsystemException("Unable to stop subsystem", e);
@@ -77,38 +157,44 @@ public class SubsystemImpl implements Su
     }
 
     public long getSubsystemId() {
-        return composite.getBundleId();
+        return this.id;
     }
 
     public String getLocation() {
-        return composite.getLocation();
+        return scope.getLocation();
     }
 
     public String getSymbolicName() {
-        return composite.getSymbolicName();
+        return scope.getName();
     }
 
     public Version getVersion() {
-        return composite.getVersion();
+        return Version.parseVersion(headers.get(SubsystemConstants.SUBSYSTEM_VERSION));
     }
 
     public Map<String, String> getHeaders() {
-        return getHeaders(null);
+        return Collections.unmodifiableMap(headers);
     }
 
+    public void updateHeaders(Map<String, String> headers) {
+        this.headers = headers;
+    }
+    
     public Map<String, String> getHeaders(String locale) {
-        final Dictionary dict = composite.getHeaders(locale);
-        return new DictionaryAsMap(dict);
+        return null;
     }
 
-    public Collection<Bundle> getConstituents() {
-        List<Bundle> list = new ArrayList<Bundle>();
-        Bundle[] bundles = composite.getSystemBundleContext().getBundles();
-        for (Bundle bundle : bundles) {
-            if (bundle.getBundleId() != 0) {
-                list.add(bundle);
-            }
+    public Collection<Bundle> getBundles() {
+        return this.scope.getBundles();
+    }
+    
+    protected Scope getScope() {
+        return scope;
+    }
+
+    public Collection<Subsystem> getChildrenSubsystems() {
+        synchronized (subsystems) {
+            return Collections.unmodifiableCollection(new ArrayList(subsystems.values()));
         }
-        return list;
     }
 }
\ No newline at end of file

Modified: incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java?rev=1040641&r1=1040640&r2=1040641&view=diff
==============================================================================
--- incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java (original)
+++ incubator/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java Tue Nov 30 16:54:56 2010
@@ -13,12 +13,23 @@
  */
 package org.apache.aries.subsystem.core.internal;
 
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_CONTENT;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_DIRECTIVE;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_EXPORTPACKAGE;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_IMPORTPACKAGE;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_MANIFESTVERSION;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_RESOURCES;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_SYMBOLICNAME;
+import static org.apache.aries.subsystem.SubsystemConstants.SUBSYSTEM_VERSION;
+import static org.apache.aries.subsystem.core.internal.FileUtils.closeQuietly;
+import static org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME;
+import static org.osgi.framework.Constants.BUNDLE_VERSION;
+
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
@@ -31,58 +42,57 @@ import java.util.jar.Attributes;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 
+import org.apache.aries.application.utils.manifest.ManifestHeaderProcessor;
+import org.apache.aries.subsystem.Subsystem;
 import org.apache.aries.subsystem.SubsystemAdmin;
 import org.apache.aries.subsystem.SubsystemConstants;
 import org.apache.aries.subsystem.SubsystemException;
+import org.apache.aries.subsystem.scope.InstallInfo;
+import org.apache.aries.subsystem.scope.ScopeAdmin;
+import org.apache.aries.subsystem.scope.ScopeUpdate;
+import org.apache.aries.subsystem.scope.SharePolicy;
 import org.apache.aries.subsystem.spi.Resource;
 import org.apache.aries.subsystem.spi.ResourceProcessor;
 import org.apache.aries.subsystem.spi.ResourceResolver;
 import org.apache.felix.utils.manifest.Clause;
 import org.apache.felix.utils.manifest.Parser;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.Version;
-import org.osgi.service.composite.CompositeAdmin;
-import org.osgi.service.composite.CompositeBundle;
-import org.osgi.service.composite.CompositeConstants;
+import org.osgi.framework.wiring.Capability;
 import org.osgi.util.tracker.ServiceTracker;
 
-import static org.apache.aries.subsystem.core.internal.FileUtils.closeQuietly;
-
-import static org.osgi.framework.Constants.*;
-import static org.osgi.service.composite.CompositeConstants.*;
-import static org.apache.aries.subsystem.SubsystemConstants.*;
-
 public class SubsystemResourceProcessor implements ResourceProcessor {
 
     private static final Version SUBSYSTEM_MANIFEST_VERSION = new Version("1.0");
 
-    public SubsystemSession createSession(BundleContext context) {
-        return new SubsystemSession(context);
+    public SubsystemSession createSession(SubsystemAdmin subsystemAdmin) {
+        return new SubsystemSession(subsystemAdmin);
     }
 
     public static class SubsystemSession implements Session {
 
         private static final long TIMEOUT = 30000;
+        private final SubsystemAdmin subsystemAdmin;
 
-        private final BundleContext context;
-        private final Map<Resource, CompositeBundle> installed = new HashMap<Resource, CompositeBundle>();
+        private final Map<Resource, Subsystem> installed = new HashMap<Resource, Subsystem>();
         /*
          * Map to keep track of composite bundle headers before the update and
          * the updated composite bundle. This is needed for rollback
          */
-        private final Map<Dictionary, CompositeBundle> updated = new HashMap<Dictionary, CompositeBundle>();
-        private final Map<Resource, CompositeBundle> removed = new HashMap<Resource, CompositeBundle>();
-        private final List<CompositeBundle> stopped = new ArrayList<CompositeBundle>();
+        private final Map<Map<String, String>, Subsystem> updated = new HashMap<Map<String, String>, Subsystem>();
+        private final Map<Resource, Subsystem> removed = new HashMap<Resource, Subsystem>();
+        private final List<Subsystem> stopped = new ArrayList<Subsystem>();
         private final Map<String, ServiceTracker> trackers = new HashMap<String, ServiceTracker>();
-        private final Map<BundleContext, Map<String, Session>> sessions = new HashMap<BundleContext, Map<String, Session>>();
-
-        public SubsystemSession(BundleContext context) {
-            this.context = context;
+        private final Map<SubsystemAdmin, Map<String, Session>> sessions = new HashMap<SubsystemAdmin, Map<String, Session>>();
+        private final BundleContext context;
+        
+        public SubsystemSession(SubsystemAdmin subsystemAdmin) {
+            this.subsystemAdmin = subsystemAdmin;
+            this.context = Activator.getBundleContext();
         }
 
         /**
@@ -95,8 +105,8 @@ public class SubsystemResourceProcessor 
         private void process(Resource res, Manifest manifest) {
 
             try {
-
-                CompositeAdmin admin = getService(CompositeAdmin.class);
+                SubsystemAdminImpl adminImpl = (SubsystemAdminImpl)subsystemAdmin;
+                ScopeAdmin admin = adminImpl.getScopeAdmin();
                 ResourceResolver resolver = getService(ResourceResolver.class);
 
                 if (manifest == null) {
@@ -149,40 +159,52 @@ public class SubsystemResourceProcessor 
                         SUBSYSTEM_VERSION);
                 checkManifestHeaders(manifest, ssn, sv);
 
-                Map<String, String> headers = computeCompositeHeaders(manifest,
+                Map<String, String> headers = computeSubsystemHeaders(manifest,
                         ssn, sv);
 
                 // Check existing bundles
-                CompositeBundle composite = findSubsystemComposite(res);
-                if (composite == null) {
+                Subsystem subsystem = findSubsystem(res);
+                ScopeUpdate scopeUpdate = admin.newScopeUpdate();
+                ScopeUpdate childScopeUpdate;
+                if (subsystem == null) {
                     // brand new install
-                    composite = admin.installCompositeBundle(res.getLocation(),
-                            headers, Collections.<String, String> emptyMap());
-                    installed.put(res, composite);
+
+                    childScopeUpdate = scopeUpdate.newChild(headers.get(Constants.BUNDLE_SYMBOLICNAME) + "_" + headers.get(Constants.BUNDLE_VERSION), res.getLocation());
+                    Map<String, List<SharePolicy>> exportSharePolicies = childScopeUpdate.getSharePolicies(SharePolicy.TYPE_EXPORT);
+                    Map<String, List<SharePolicy>> importSharePolicies = childScopeUpdate.getSharePolicies(SharePolicy.TYPE_IMPORT);
+                    
+                    setupSharePolicies(exportSharePolicies, importSharePolicies, headers);
+                    scopeUpdate.commit();
+                    
+                    ScopeAdmin childScopeAdmin = getService(ScopeAdmin.class, "ScopeId=" + childScopeUpdate.getScope().getId());
+                    
+                    subsystem = new SubsystemImpl(childScopeUpdate.getScope(), headers);
+                    SubsystemAdmin childSubsystemAdmin = new SubsystemAdminImpl(childScopeAdmin, subsystem, subsystemAdmin.getSubsystem());
+                    context.registerService(SubsystemAdmin.class.getName(), childSubsystemAdmin, DictionaryBuilder.build("SubsystemParentId", childSubsystemAdmin.getParentSubsystem().getSubsystemId(), "SubsystemId", subsystem.getSubsystemId()));
+                    
+                    installed.put(res, subsystem);
+                    
+                    
                 } else {
                     // update
-                    // capture composite headers before update
-                    Dictionary dictionary = composite.getHeaders();
-
-                    String previousContentHeader = (String) composite
-                            .getHeaders().get(SUBSYSTEM_CONTENT);
+                    // capture data before update
+                    Map<String, String> subsystemHeaders = subsystem.getHeaders();
+                    String previousContentHeader = (String) subsystemHeaders.get(SUBSYSTEM_CONTENT);
                     Clause[] previousContentClauses = Parser
                             .parseHeader(previousContentHeader);
                     for (Clause c : previousContentClauses) {
                         Resource r = resolver.find(c.toString());
                         previous.add(r);
                     }
-                    if (composite.getState() == Bundle.ACTIVE) {
-                        composite.stop();
-                        stopped.add(composite);
-                    }
-                    composite.update(headers);
-                    updated.put(dictionary, composite);
+                    
+                    subsystem.updateHeaders(headers);
+                    updated.put(subsystemHeaders, subsystem);
                 }
-                composite.getSystemBundleContext().registerService(
-                        SubsystemAdmin.class.getName(),
-                        new Activator.SubsystemAdminFactory(), null);
 
+                // content is installed in the scope, so need to find the subsystemAdmin for the scope first
+                long scopeId = subsystem.getSubsystemId();
+                SubsystemAdmin childAdmin = getService(SubsystemAdmin.class, "SubsystemId=" + scopeId);
+                
                 for (Resource r : previous) {
                     boolean stillHere = false;
                     for (Resource r2 : content) {
@@ -193,15 +215,19 @@ public class SubsystemResourceProcessor 
                         }
                     }
                     if (!stillHere) {
-                        getSession(composite.getSystemBundleContext(),
+                        getSession(childAdmin,
                                 r.getType()).dropped(r);
                     }
                 }
+                
+                // additional resource is installed outside of the subsystem.
                 for (Resource r : additional) {
-                    getSession(context, r.getType()).process(r);
+                    getSession(subsystemAdmin, r.getType()).process(r);
                 }
+                
+
                 for (Resource r : content) {
-                    getSession(composite.getSystemBundleContext(), r.getType())
+                    getSession(childAdmin, r.getType())
                             .process(r);
                 }
 
@@ -212,45 +238,97 @@ public class SubsystemResourceProcessor 
             }
         }
 
+        private void setupSharePolicies(
+                Map<String, List<SharePolicy>> exportSharePolicies,
+                Map<String, List<SharePolicy>> importSharePolicies,
+                Map<String, String> headers) {
+            String importPackage = headers.get(SubsystemConstants.SUBSYSTEM_IMPORTPACKAGE);
+            String exportPackage = headers.get(SubsystemConstants.SUBSYSTEM_EXPORTPACKAGE);
+            String importService = headers.get(SubsystemConstants.SUBSYSTEM_IMPORTSERVICE);
+            String exportService = headers.get(SubsystemConstants.SUBSYSTEM_EXPORTSERVICE);
+            if (importPackage != null && !importPackage.trim().isEmpty()) {
+                List<SharePolicy> importPackagePolicies = importSharePolicies.get(Capability.PACKAGE_CAPABILITY);
+                if (importPackagePolicies == null) {
+                    importPackagePolicies = new ArrayList<SharePolicy>();
+                    importSharePolicies.put(Capability.PACKAGE_CAPABILITY,importPackagePolicies);
+                }
+                
+                importPackagePolicies.add(new SharePolicy(SharePolicy.TYPE_IMPORT, Capability.PACKAGE_CAPABILITY, createFilter(importPackage, Capability.PACKAGE_CAPABILITY)));
+            }
+            
+            if (importService != null && !importService.trim().isEmpty()) {
+                List<SharePolicy> importServicePolicies = importSharePolicies.get("osgi.service");
+                if (importServicePolicies == null) {
+                    importServicePolicies = new ArrayList<SharePolicy>();
+                    importSharePolicies.put("osgi.service",importServicePolicies);
+                }
+                
+                importServicePolicies.add(new SharePolicy(SharePolicy.TYPE_IMPORT, "osgi.service", createFilter(importService, "osgi.service")));
+            }
+            
+        }
+        
+        private Filter createFilter(String packages, String namespace) {
+            if (namespace.equals(Capability.PACKAGE_CAPABILITY)) {
+                // split packages
+                List<String> pkgs = ManifestHeaderProcessor.split(packages, ",");
+                StringBuffer sb = new StringBuffer();
+                sb.append("(|");
+                for (String pkg : pkgs) {
+                    sb.append("(" + Capability.PACKAGE_CAPABILITY + "=" + pkg + ")");
+                }
+                sb.append(")");
+                try {
+                    return FrameworkUtil.createFilter(sb.toString());
+                } catch (InvalidSyntaxException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+                
+            }
+            if (namespace.equals("osgi.service")) {
+                // split packages
+                List<String> pkgs = ManifestHeaderProcessor.split(packages, ",");
+                StringBuffer sb = new StringBuffer();
+                sb.append("(|");
+                for (String pkg : pkgs) {
+                    sb.append("(osgi.service=" + pkg + ")");
+                }
+                sb.append(")");
+                try {
+                    return FrameworkUtil.createFilter(sb.toString());
+                } catch (InvalidSyntaxException e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
+            
+            
+            return null;
+        }
+
         public void process(Resource res) throws SubsystemException {
             process(res, null);
 
         }
 
         public void dropped(Resource res) throws SubsystemException {
-            CompositeBundle composite = findSubsystemComposite(res);
-            if (composite == null) {
+            Subsystem subsystem = findSubsystem(res);
+            if (subsystem == null) {
                 throw new SubsystemException(
                         "Unable to find matching subsystem to uninstall");
             }
-            try {
-                // TODO: iterate through all resources and ask for a removal on
-                // each one
-                composite.uninstall();
-                removed.put(res, composite);
-            } catch (BundleException e) {
-                throw new SubsystemException("Unable to uninstall subsystem", e);
-            }
-        }
+            // TODO: iterate through all resources and ask for a removal on
+            // each one
+            subsystemAdmin.uninstall(subsystem);
+            removed.put(res, subsystem);
 
-        protected CompositeBundle findSubsystemComposite(Resource resource) {
-            for (Bundle bundle : context.getBundles()) {
-                if (resource.getLocation().equals(bundle.getLocation())) {
-                    if (bundle instanceof CompositeBundle) {
-                        CompositeBundle composite = (CompositeBundle) bundle;
-                        String bsn = (String) bundle.getHeaders().get(
-                                Constants.BUNDLE_SYMBOLICNAME);
-                        Clause[] bsnClauses = Parser.parseHeader(bsn);
-                        if ("true"
-                                .equals(bsnClauses[0]
-                                        .getDirective(SubsystemConstants.SUBSYSTEM_DIRECTIVE))) {
-                            return composite;
-                        } else {
-                            throw new SubsystemException(
-                                    "A bundle with the same location already exists!");
-                        }
+        }
 
-                    }
+        protected Subsystem findSubsystem(Resource resource) {
+            for (Subsystem subsystem : this.subsystemAdmin.getSubsystems()) {
+                if (resource.getLocation().equals(subsystem.getLocation())) {
+                    return subsystem;
                 }
             }
             return null;
@@ -262,12 +340,8 @@ public class SubsystemResourceProcessor 
                     s.prepare();
                 }
             }
-            for (CompositeBundle composite : stopped) {
-                try {
-                    composite.start();
-                } catch (BundleException e) {
-                    throw new SubsystemException(e);
-                }
+            for (Subsystem subsystem : stopped) {
+                subsystem.start();  
             }
         }
 
@@ -285,38 +359,32 @@ public class SubsystemResourceProcessor 
 
         public void rollback() {
 
-            for (CompositeBundle c : installed.values()) {
-                try {
-                    c.uninstall();
-                } catch (BundleException e) {
-                    // Ignore
-                }
+            for (Subsystem subsystem : installed.values()) {
+                subsystemAdmin.uninstall(subsystem);
             }
             installed.clear();
 
             // Handle updated subsystems
-            Set<Map.Entry<Dictionary, CompositeBundle>> updatedSet = updated
+            Set<Map.Entry<Map<String, String>, Subsystem>> updatedSet = updated
                     .entrySet();
-            for (Entry<Dictionary, CompositeBundle> entry : updatedSet) {
-                Dictionary oldDic = entry.getKey();
-                CompositeBundle cb = entry.getValue();
+            for (Entry<Map<String, String>, Subsystem> entry : updatedSet) {
+                Map<String, String> oldHeader = entry.getKey();
+                Subsystem subsystem = entry.getValue();
 
                 // let's build a Manifest from oldDict
                 Manifest manifest = new Manifest();
                 Attributes attributes = manifest.getMainAttributes();
-                Map<String, String> headerMap = new DictionaryAsMap(oldDic);
-
-                attributes.putAll(headerMap);
+                attributes.putAll(oldHeader);
                 String symbolicName = attributes
                         .getValue(Constants.BUNDLE_SYMBOLICNAME);
                 Version v = Version.parseVersion(attributes
                         .getValue(Constants.BUNDLE_VERSION));
                 Resource subsystemResource = new ResourceImpl(symbolicName, v,
-                        SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM, cb
+                        SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM, subsystem
                                 .getLocation(), Collections
                                 .<String, String> emptyMap());
                 try {
-                    Session session = getSession(context, subsystemResource
+                    Session session = getSession(subsystemAdmin, subsystemResource
                             .getType());
                     if (session instanceof SubsystemSession) {
                         ((SubsystemSession) session).process(subsystemResource,
@@ -335,11 +403,11 @@ public class SubsystemResourceProcessor 
             }
 
             // handle uninstalled subsystem
-            Set<Map.Entry<Resource, CompositeBundle>> set = removed.entrySet();
-            for (Map.Entry<Resource, CompositeBundle> entry : set) {
+            Set<Map.Entry<Resource, Subsystem>> set = removed.entrySet();
+            for (Map.Entry<Resource, Subsystem> entry : set) {
                 Resource res = entry.getKey();
                 try {
-                    getSession(context, res.getType()).process(res);
+                    getSession(subsystemAdmin, res.getType()).process(res);
                 } catch (SubsystemException e) {
                     throw e;
                 } catch (Exception e) {
@@ -359,16 +427,16 @@ public class SubsystemResourceProcessor 
             closeTrackers();
         }
 
-        protected Session getSession(BundleContext context, String type)
+        protected Session getSession(SubsystemAdmin admin, String type)
                 throws InvalidSyntaxException, InterruptedException {
-            Map<String, Session> sm = this.sessions.get(context);
+            Map<String, Session> sm = this.sessions.get(admin);
             if (sm == null) {
                 sm = new HashMap<String, Session>();
-                this.sessions.put(context, sm);
+                this.sessions.put(admin, sm);
             }
             Session session = sm.get(type);
             if (session == null) {
-                session = getProcessor(type).createSession(context);
+                session = getProcessor(type).createSession(admin);
                 sm.put(type, session);
             }
             return session;
@@ -452,19 +520,12 @@ public class SubsystemResourceProcessor 
         // TODO add any other symbolic name to check
         Clause[] ssnClauses = Parser.parseHeader(ssn);
         String ssDirective = ssnClauses[0].getDirective(SUBSYSTEM_DIRECTIVE);
-        String comDirective = ssnClauses[0].getDirective(COMPOSITE_DIRECTIVE);
         if (ssDirective != null && ssDirective.equalsIgnoreCase("false")) {
             throw new SubsystemException("Invalid " + SUBSYSTEM_DIRECTIVE
                     + " directive in " + SUBSYSTEM_SYMBOLICNAME + ": "
                     + ssDirective);
         }
 
-        if (ssDirective != null && comDirective.equalsIgnoreCase("false")) {
-            throw new SubsystemException("Invalid " + COMPOSITE_DIRECTIVE
-                    + " directive in " + SUBSYSTEM_SYMBOLICNAME + ": "
-                    + comDirective);
-        }
-
         if (sv == null || sv.length() == 0) {
             throw new SubsystemException("Invalid subsystem version: " + sv);
         }
@@ -482,24 +543,19 @@ public class SubsystemResourceProcessor 
     // if the ssn already contains COMPOSITE_DIRECTIVE or SUBSYSTEM_DIRECTIVE
     // directive
     // let's not add them again
-    private static String getCompositeSymbolicName(String ssn) {
+    private static String getSubsystemSymbolicName(String ssn) {
         Clause[] ssnClauses = Parser.parseHeader(ssn);
         String ssDirective = ssnClauses[0].getDirective(SUBSYSTEM_DIRECTIVE);
-        String comDirective = ssnClauses[0].getDirective(COMPOSITE_DIRECTIVE);
 
-        if (ssDirective == null && comDirective == null) {
-            ssn = ssn + ";" + COMPOSITE_DIRECTIVE + ":=true;"
+        if (ssDirective == null ) {
+            ssn = ssn + ";"
                     + SUBSYSTEM_DIRECTIVE + ":=true";
-        } else if (ssDirective == null) {
-            ssn = ssn + ";" + COMPOSITE_DIRECTIVE + ":=true;";
-        } else if (comDirective == null) {
-            ssn = ssn + ";" + SUBSYSTEM_DIRECTIVE + ":=true;";
-        }
+        } 
 
         return ssn;
     }
 
-    private static Map<String, String> computeCompositeHeaders(
+    private static Map<String, String> computeSubsystemHeaders(
             Manifest manifest, String ssn, String sv) {
         // Grab all headers
         Map<String, String> headers = new HashMap<String, String>();
@@ -512,23 +568,12 @@ public class SubsystemResourceProcessor 
         }
 
         // Create the required composite headers
-        headers.put(BUNDLE_SYMBOLICNAME, getCompositeSymbolicName(ssn));
+        headers.put(BUNDLE_SYMBOLICNAME, getSubsystemSymbolicName(ssn));
         headers.put(BUNDLE_VERSION, sv);
 
         String subImportPkg = headers.get(SUBSYSTEM_IMPORTPACKAGE);
         String subExportPkg = headers.get(SUBSYSTEM_EXPORTPACKAGE);
-        if (subImportPkg != null && subImportPkg.length() > 0) {
-            // use subsystem-importpackage for composite-importpackage
-            headers.put(CompositeConstants.COMPOSITE_PACKAGE_IMPORT_POLICY,
-                    subImportPkg);
-        } else {
-            // TODO: let's compute the import package for the subsystem
-        }
-        if (subExportPkg != null && subExportPkg.length() > 0) {
-            // use subsystem-importpackage for composite-importpackage
-            headers.put(CompositeConstants.COMPOSITE_PACKAGE_EXPORT_POLICY,
-                    subExportPkg);
-        }
+
 
         // TODO: compute other composite manifest entries
         // TODO: compute list of bundles
@@ -536,4 +581,9 @@ public class SubsystemResourceProcessor 
         return headers;
     }
 
+    public Session createSession(BundleContext arg0) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }
\ No newline at end of file



Mime
View raw message