aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g..@apache.org
Subject svn commit: r1102244 [2/4] - in /aries/trunk/subsystem: subsystem-api/ subsystem-api/src/main/java/org/apache/aries/subsystem/ subsystem-api/src/main/java/org/apache/aries/subsystem/spi/ subsystem-core/ subsystem-core/src/main/java/org/apache/aries/sub...
Date Thu, 12 May 2011 11:27:13 GMT
Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/Activator.java Thu May 12 11:27:11 2011
@@ -14,29 +14,21 @@
 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.Scope;
+import org.apache.aries.subsystem.core.ResourceResolver;
 import org.apache.aries.subsystem.spi.ResourceProcessor;
-import org.apache.aries.subsystem.spi.ResourceResolver;
-import org.osgi.framework.Bundle;
+import org.eclipse.equinox.region.RegionDigraph;
 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.service.coordinator.Coordinator;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
 import org.slf4j.Logger;
@@ -50,31 +42,61 @@ import org.slf4j.LoggerFactory;
 public class Activator implements BundleActivator {
     private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
     
+    static Coordinator getCoordinator() {
+    	return context.getService(context.getServiceReference(Coordinator.class));
+    }
+    
     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 {
+    private ServiceTracker scopeServiceTracker;
+    
+    public void start(final BundleContext context) throws Exception {
         if (LOGGER.isDebugEnabled()) {
             LOGGER.debug("subsystem activator starting");
         }
         Activator.context = context;
-        Activator.eventDispatcher = new SubsystemEventDispatcher(context);
-        adminFactory = new SubsystemAdminFactory();
-        register(SubsystemAdmin.class, adminFactory, null);
+        Activator.eventDispatcher = new SubsystemEventDispatcher(context);   
         register(ResourceResolver.class,
                  new NoOpResolver(),
                  DictionaryBuilder.build(Constants.SERVICE_RANKING, Integer.MIN_VALUE));
         register(ResourceResolver.class,
-                new ResourceResolverImpl(this.context),
+                new ResourceResolverImpl(context),
                 DictionaryBuilder.build(Constants.SERVICE_RANKING, 0));
         register(ResourceProcessor.class,
-                new BundleResourceProcessor(),
+                new BundleResourceProcessor(context),
                 DictionaryBuilder.build(SubsystemConstants.SERVICE_RESOURCE_TYPE, SubsystemConstants.RESOURCE_TYPE_BUNDLE));
         register(ResourceProcessor.class,
-                new SubsystemResourceProcessor(),
+                new SubsystemResourceProcessor(context),
                 DictionaryBuilder.build(SubsystemConstants.SERVICE_RESOURCE_TYPE, SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM));
+        scopeServiceTracker = new ServiceTracker(
+        		context,
+        		RegionDigraph.class.getName(),
+        		new ServiceTrackerCustomizer() {
+        			private ServiceReference<?> scopeRef;
+        			private ServiceRegistration<?> subsystemReg;
+        			
+					public synchronized Object addingService(ServiceReference reference) {
+						if (subsystemReg != null) return null;
+						RegionDigraph digraph = (RegionDigraph)context.getService(reference);
+						if (digraph == null) return null;
+						subsystemReg = context.registerService(Subsystem.class.getName(), new SubsystemServiceFactory(digraph.getRegion(context.getBundle())), null);
+						return digraph;
+					}
+
+					public void modifiedService(ServiceReference reference, Object service) {
+						// Do nothing.
+					}
+
+					public synchronized void removedService(ServiceReference reference, Object service) {
+						if (reference != scopeRef) return;
+						subsystemReg.unregister();
+						subsystemReg = null;
+						// TODO Look for another service?
+					}
+        		});
+        scopeServiceTracker.open();
     }
 
     protected <T> void register(Class<T> clazz, T service, Dictionary props) {
@@ -89,7 +111,8 @@ public class Activator implements Bundle
         if (LOGGER.isDebugEnabled()) {
             LOGGER.debug("subsystem activator stopping");
         }
-        for (ServiceRegistration r : registrations) {
+        scopeServiceTracker.close();
+        for (ServiceRegistration<?> r : registrations) {
             try {
                 r.unregister();
             } catch (Exception e) {
@@ -97,10 +120,6 @@ public class Activator implements Bundle
             }
         }
         eventDispatcher.destroy();
-        if (adminFactory!= null) {
-            adminFactory.destroy();
-        }
-        
     }
     
     public static BundleContext getBundleContext() {
@@ -110,126 +129,4 @@ public class Activator implements Bundle
     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 Scope 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().getServiceReference(Scope.class.getName());
-            if (reference != null) {
-                Scope scopeAdmin = (Scope)Activator.getBundleContext().getService(reference);
-                Subsystem subsystem = new SubsystemImpl(scopeAdmin, 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);
-                        }
-
-                    });
-        }
-        
-        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());
-            }
-            
-            long ref = 0;
-            
-            // figure out the subsystemAdmin for the bundle           
-            SubsystemAdmin admin = getSubsystemAdmin(bundle);
-            
-            if (admin == null) {
-                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;
-        }
-
-        public synchronized void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
-            if (LOGGER.isDebugEnabled()) {
-                LOGGER.debug("Unget SubsystemAdmin service {} from bundle symbolic name {} version {}", new Object[] {service, bundle.getSymbolicName(), bundle.getVersion()});
-            }
-            SubsystemAdminImpl admin = (SubsystemAdminImpl) service;
-            long ref = references.get(admin) - 1;
-            if (ref == 0) {
-                admin.dispose();
-                admins.remove(admin.context);
-                references.remove(admin);
-            } else {
-                references.put(admin, ref);
-            }
-        }
-
-    }
-
 }

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceProcessor.java Thu May 12 11:27:11 2011
@@ -14,164 +14,169 @@
 package org.apache.aries.subsystem.core.internal;
 
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 
-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.ScopeUpdate;
 import org.apache.aries.subsystem.spi.Resource;
+import org.apache.aries.subsystem.spi.ResourceOperation;
 import org.apache.aries.subsystem.spi.ResourceProcessor;
+import org.eclipse.equinox.region.Region;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
+import org.osgi.service.coordinator.Coordination;
+import org.osgi.service.coordinator.Participant;
 
 public class BundleResourceProcessor implements ResourceProcessor {
-
-    public Session createSession(SubsystemAdmin subsystemAdmin) {
-        return new BundleSession(subsystemAdmin);
-    }
-
-    public static class BundleSession implements Session {
-
-        private final Scope 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(SubsystemAdmin subsystemAdmin) {
-            SubsystemAdminImpl subsystemAdminImpl = (SubsystemAdminImpl)subsystemAdmin;
-            this.scopeAdmin = subsystemAdminImpl.getScopeAdmin();
-        }
-
-        public void process(Resource resource) throws SubsystemException {
-            try {
-                // find the bundle
-                Bundle bundle = findBundle(resource);
-                
-                if (bundle == null) {
-                    // fresh install 
-                    InstallInfo installInfo = new InstallInfo(resource.getLocation(), new URL(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);
-                    if ("update".equals(updateAttribute)) {
-                        bundle.update(resource.open());
-                        updated.put(resource, bundle);
-                    }
-                }
-                
-                if (bundle == null) {
-                    bundle = findBundle(resource);
-                    installed.add(bundle);
-                }
-
-                String startAttribute = resource.getAttributes().get(SubsystemConstants.RESOURCE_START_ATTRIBUTE);
-                
-                if (startAttribute == null || startAttribute.length() == 0) {
-                    // defaults to true
-                    startAttribute = "true";
-                }
-                if ("true".equals(startAttribute)) {
-                    bundle.start();
-                }
-            } catch (SubsystemException e) {
-                throw e;
-            } catch (Exception e) {
-//                throw new SubsystemException("Unable to process bundle resource", e);
-            	e.printStackTrace();
+	private final BundleContext bundleContext;
+	
+	public BundleResourceProcessor(BundleContext bundleContext) {
+		this.bundleContext = bundleContext;
+	}
+	
+	public void process(final ResourceOperation operation) throws SubsystemException {
+		switch (operation.getType()) {
+			case INSTALL:
+				install(operation);
+				break;
+			case START:
+				start(operation);
+				break;
+			case STOP:
+				stop(operation);
+				break;
+			case UNINSTALL:
+				uninstall(operation);
+				break;
+			case UPDATE:
+				update(operation);
+				break;
+			default:
+				throw new SubsystemException("Unsupported resource opertaion type: " + operation.getType());
+		}
+	}
+	
+	private Bundle findBundle(Resource resource, Region region) {        
+        Set<Long> bundleIds = region.getBundleIds();
+    	for (Long bundleId : bundleIds) {
+    		Bundle b = bundleContext.getBundle(bundleId);
+    		String location = String.valueOf(resource.getAttributes().get(Resource.LOCATION_ATTRIBUTE));
+            if (location.equals(b.getLocation())) {
+                return b;
             }
-        }
-
-        public void dropped(Resource resource) throws SubsystemException {
-            // find the bundle
-            Bundle bundle = findBundle(resource);
-            
-            if (bundle == null) {
-                throw new SubsystemException("Unable to find the resource to be dropped");
-            } else {
-                try {
-                    bundle.uninstall();
-                    removed.put(resource, bundle);
-                } catch (BundleException be) {
-                    throw new SubsystemException("Unable to drop resource", be);
-                }
-            }
-        }
-
-        public void prepare() throws SubsystemException {
-            // no-op
-        }
-
-        public void commit() {
-            clearAll();
-        }
-
-        public void rollback() {
-            // rollback installed bundle
-            for (Bundle bundle : installed) {
-                try {
-                    bundle.uninstall();
-                } catch (Exception e) {
-                    // Ignore
-                }
-            }
-            
-            // rollback updated bundle - not sure what we can do here
-            
-            // rollback removed bundle
-            if (!removed.isEmpty()) {
-                Set<Entry<Resource, Bundle>> removedSet = removed.entrySet();
-                for (Entry<Resource, Bundle> entry : removedSet) {
-                    Bundle bundle = entry.getValue();
-                    Resource res = entry.getKey();
-                    try {
-                        InstallInfo installInfo = new InstallInfo(res.getLocation(), res.open());
-                        ScopeUpdate scopeUpdate = scopeAdmin.newScopeUpdate();
-                        scopeUpdate.getBundlesToInstall().add(installInfo);
-                        scopeUpdate.commit();
-                    } catch (Exception e) {
-                        // Ignore
-                    }
-                }
-            }
-            
-            clearAll();
-        }
-        
-        protected Bundle findBundle(Resource resource) {
-            Scope scope = scopeAdmin;
-            for (Bundle b : scope.getBundles()) {
-                if (resource.getLocation().equals(scope.getLocation())) {
-                    return b;
-                }
-            }
-            
-            return null;
-        }
-        
-        private void clearAll() {
-            installed.clear();
-            updated.clear();
-            removed.clear();
-        }
-    }
-
-    public Session createSession(BundleContext arg0) {
-        // TODO Auto-generated method stub
+    	}
         return null;
     }
-
-
+	
+	private void install(final ResourceOperation operation) {
+		Coordination coordination = operation.getCoordination();
+		final Resource resource = operation.getResource();
+		final Region region = (Region)operation.getContext().get("region");
+		String location = String.valueOf(resource.getAttributes().get(Resource.LOCATION_ATTRIBUTE));
+        try {
+        	region.installBundle(location, new URL(location).openStream());
+            coordination.addParticipant(new Participant() {
+				public void ended(Coordination c) throws Exception {
+					operation.completed();
+				}
+
+				public void failed(Coordination c) throws Exception {
+					Bundle bundle = findBundle(resource, region);
+					region.removeBundle(bundle);
+				}
+            });
+        }
+        catch (Exception e) {
+        	coordination.fail(e);
+        }
+	}
+	
+	private void start(final ResourceOperation operation) {
+		Resource resource = operation.getResource();
+		String startAttribute = String.valueOf(resource.getAttributes().get(SubsystemConstants.RESOURCE_START_ATTRIBUTE));
+        if (!"true".equals(startAttribute)) return;
+		Coordination coordination = operation.getCoordination();
+		Region region = (Region)operation.getContext().get("region");
+		final Bundle bundle = findBundle(resource, region);
+		try {
+			bundle.start();
+			coordination.addParticipant(new Participant() {
+				public void ended(Coordination c) throws Exception {
+					operation.completed();
+				}
+
+				public void failed(Coordination c) throws Exception {
+					bundle.stop();
+				}
+			});
+		}
+		catch (Exception e) {
+			coordination.fail(e);
+		}
+	}
+	
+	private void stop(final ResourceOperation operation) {
+		Coordination coordination = operation.getCoordination();
+		Resource resource = operation.getResource();
+		Region region = (Region)operation.getContext().get("region");
+		final Bundle bundle = findBundle(resource, region);
+		try {
+			bundle.stop();
+			coordination.addParticipant(new Participant() {
+				public void ended(Coordination c) throws Exception {
+					operation.completed();
+				}
+
+				public void failed(Coordination c) throws Exception {
+					bundle.start();
+				}
+			});
+		}
+		catch (Exception e) {
+			coordination.fail(e);
+		}
+	}
+	
+	private void uninstall(final ResourceOperation operation) {
+		Coordination coordination = operation.getCoordination();
+		final Resource resource = operation.getResource();
+		final Region region = (Region)operation.getContext().get("region");
+		Bundle bundle = findBundle(resource, region);
+		try {
+			region.removeBundle(bundle);
+			coordination.addParticipant(new Participant() {
+				public void ended(Coordination c) throws Exception {
+					operation.completed();
+				}
+
+				public void failed(Coordination c) throws Exception {
+					String location = String.valueOf(resource.getAttributes().get(Resource.LOCATION_ATTRIBUTE));
+					region.installBundle(location);
+				}
+			});
+		}
+		catch (Exception e) {
+			coordination.fail(e);
+		}
+	}
+	
+	private void update(final ResourceOperation operation) {
+		Resource resource = operation.getResource();
+		String updateAttribute = String.valueOf(resource.getAttributes().get(SubsystemConstants.RESOURCE_UPDATE_ATTRIBUTE));
+		if (!"true".equals(updateAttribute)) return;
+		final String location = String.valueOf(resource.getAttributes().get(Resource.LOCATION_ATTRIBUTE));
+		Region region = (Region)operation.getContext().get("region");
+		final Bundle bundle = findBundle(resource, region);
+		Coordination coordination = operation.getCoordination();
+		coordination.addParticipant(new Participant() {
+			public void ended(Coordination c) throws Exception {
+				bundle.update(new URL(location).openStream());
+				operation.completed();
+			}
+			
+			public void failed(Coordination c) throws Exception {
+				// Do nothing.
+			}
+		});
+	}
 }

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/NoOpResolver.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/NoOpResolver.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/NoOpResolver.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/NoOpResolver.java Thu May 12 11:27:11 2011
@@ -13,18 +13,16 @@
  */
 package org.apache.aries.subsystem.core.internal;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.aries.subsystem.SubsystemConstants;
 import org.apache.aries.subsystem.SubsystemException;
+import org.apache.aries.subsystem.core.ResourceResolver;
 import org.apache.aries.subsystem.spi.Resource;
-import org.apache.aries.subsystem.spi.ResourceResolver;
 import org.apache.felix.utils.manifest.Attribute;
 import org.apache.felix.utils.manifest.Clause;
-import org.apache.felix.utils.manifest.Directive;
 import org.apache.felix.utils.manifest.Parser;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Version;
@@ -44,7 +42,7 @@ public class NoOpResolver implements Res
         if (loc == null) {
             throw new SubsystemException("Mandatory location missing on resource: " + resource);
         }
-        Map<String,String> attributes = new HashMap<String,String>();
+        Map<String,Object> attributes = new HashMap<String,Object>();
         for (Attribute a : clauses[0].getAttributes()) {
             String name = a.getName();
             if (!Constants.VERSION_ATTRIBUTE.equals(name)

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceImpl.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceImpl.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceImpl.java Thu May 12 11:27:11 2011
@@ -16,49 +16,65 @@ package org.apache.aries.subsystem.core.
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import org.apache.aries.subsystem.SubsystemConstants;
+import org.apache.aries.subsystem.spi.Capability;
+import org.apache.aries.subsystem.spi.Requirement;
 import org.apache.aries.subsystem.spi.Resource;
 import org.osgi.framework.Version;
 
 public class ResourceImpl implements Resource {
+    private final Map<String,Object> attributes;
 
-    private final String symbolicName;
-    private final Version version;
-    private final String type;
-    private final String location;
-    private final Map<String,String> attributes;
-
-    public ResourceImpl(String symbolicName, Version version, String type, String location, Map<String,String> attributes) {
-        this.symbolicName = symbolicName;
-        this.version = version;
-        this.type = type;
-        this.location = location;
+    public ResourceImpl(String symbolicName, Version version, String type, String location, Map<String,Object> attributes) {
+    	if (attributes == null) {
+    		attributes = new HashMap<String, Object>();
+    	}
+    	attributes.put(Resource.SYMBOLIC_NAME_ATTRIBUTE, symbolicName);
+    	attributes.put(Resource.VERSION_ATTRIBUTE, version);
+    	if (type == null || type.length() == 0) {
+    		type = SubsystemConstants.RESOURCE_NAMESPACE_BUNDLE;
+    	}
+    	attributes.put(Resource.NAMESPACE_ATTRIBUTE, type);
+    	attributes.put(Resource.LOCATION_ATTRIBUTE, location);
         this.attributes = attributes;
     }
 
     public String getSymbolicName() {
-        return symbolicName;
+        return (String)attributes.get(Resource.SYMBOLIC_NAME_ATTRIBUTE);
     }
 
     public Version getVersion() {
-        return version;
+        return (Version)attributes.get(Resource.VERSION_ATTRIBUTE);
     }
 
     public String getType() {
-        return type;
+        return (String)attributes.get(Resource.NAMESPACE_ATTRIBUTE);
     }
 
     public String getLocation() {
-        return location;
+        return (String)attributes.get(Resource.LOCATION_ATTRIBUTE);
     }
 
-    public Map<String, String> getAttributes() {
+    public Map<String, Object> getAttributes() {
         return attributes;
     }
 
     public InputStream open() throws IOException {
-        return new URL(location).openStream();
+        return new URL(getLocation()).openStream();
     }
 
+	public List<Capability> getCapabilities(String arg0) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public List<Requirement> getRequirements(String arg0) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
 }

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceResolverImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceResolverImpl.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceResolverImpl.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/ResourceResolverImpl.java Thu May 12 11:27:11 2011
@@ -17,8 +17,6 @@ package org.apache.aries.subsystem.core.
 import java.io.File;
 import java.io.FileOutputStream;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -33,20 +31,19 @@ import org.apache.aries.application.Cont
 import org.apache.aries.application.management.BundleInfo;
 import org.apache.aries.subsystem.SubsystemConstants;
 import org.apache.aries.subsystem.SubsystemException;
+import org.apache.aries.subsystem.core.ResourceResolver;
 import org.apache.aries.subsystem.core.obr.BundleInfoImpl;
 import org.apache.aries.subsystem.core.obr.ContentImpl;
 import org.apache.aries.subsystem.core.obr.Manve2Repository;
 import org.apache.aries.subsystem.core.obr.RepositoryDescriptorGenerator;
 import org.apache.aries.subsystem.spi.Resource;
-import org.apache.aries.subsystem.spi.ResourceResolver;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.Version;
+import org.apache.felix.bundlerepository.Reason;
 import org.apache.felix.bundlerepository.Repository;
 import org.apache.felix.bundlerepository.RepositoryAdmin;
-import org.apache.felix.bundlerepository.Requirement;
-import org.apache.felix.bundlerepository.Reason;
 import org.apache.felix.bundlerepository.Resolver;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -200,9 +197,9 @@ public class ResourceResolverImpl implem
      * the format of resource is like bundlesymbolicname;version=1.0.0, for example com.ibm.ws.eba.example.blog.api;version=1.0.0,
      */
     private org.apache.felix.bundlerepository.Resource findOBRResource(Resource resource) throws SubsystemException {
-        String symbolicName = resource.getSymbolicName();
+        String symbolicName = String.valueOf(resource.getAttributes().get(Resource.SYMBOLIC_NAME_ATTRIBUTE));
         // this version could possibly be a range
-        Version version = resource.getVersion();
+        Version version = (Version)resource.getAttributes().get(Resource.VERSION_ATTRIBUTE);
 
         //org.apache.felix.bundlerepository.Resource[] res = this.repositoryAdmin.discoverResources(filterString.toString());
         Repository[] repos = this.repositoryAdmin.listRepositories();

Added: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RootSubsystem.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RootSubsystem.java?rev=1102244&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RootSubsystem.java (added)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/RootSubsystem.java Thu May 12 11:27:11 2011
@@ -0,0 +1,64 @@
+package org.apache.aries.subsystem.core.internal;
+
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.aries.subsystem.Subsystem;
+import org.apache.aries.subsystem.SubsystemException;
+import org.eclipse.equinox.region.Region;
+import org.osgi.framework.Version;
+
+public class RootSubsystem extends SubsystemImpl {
+	public RootSubsystem(Region region) {
+		super(region, Collections.EMPTY_MAP, null, "root");
+	}
+
+	public Map<String, String> getHeaders() {
+		return Collections.emptyMap();
+	}
+
+	public Map<String, String> getHeaders(String locale) {
+		return Collections.emptyMap();
+	}
+
+	public Subsystem getParent() {
+		return null;
+	}
+
+	public State getState() {
+		return Subsystem.State.ACTIVE;
+	}
+
+	public long getSubsystemId() {
+		return 0;
+	}
+
+	public String getSymbolicName() {
+		return "org.osgi.service.subsystem.root";
+	}
+
+	public Version getVersion() {
+		return Version.emptyVersion;
+	}
+
+	public void start() throws SubsystemException {
+		throw new SubsystemException("The root subsystem may not be started");
+	}
+
+	public void stop() throws SubsystemException {
+		throw new SubsystemException("The root subsystem may not be stopped");
+	}
+
+	public void uninstall() throws SubsystemException {
+		throw new SubsystemException("The root subsystem may not be uninstalled");
+	}
+
+	public void update() throws SubsystemException {
+		update(null);
+	}
+
+	public void update(InputStream content) throws SubsystemException {
+		throw new SubsystemException("The root subsystem may not be updated");
+	}
+}

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemEventDispatcher.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemEventDispatcher.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemEventDispatcher.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemEventDispatcher.java Thu May 12 11:27:11 2011
@@ -14,8 +14,8 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 import org.apache.aries.subsystem.Subsystem;
-import org.apache.aries.subsystem.SubsystemEvent;
-import org.apache.aries.subsystem.SubsystemListener;
+import org.apache.aries.subsystem.core.SubsystemEvent;
+import org.apache.aries.subsystem.core.SubsystemListener;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.ServiceReference;

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java?rev=1102244&r1=1102243&r2=1102244&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java (original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemImpl.java Thu May 12 11:27:11 2011
@@ -14,18 +14,24 @@
  */
 package org.apache.aries.subsystem.core.internal;
 
+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.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 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.scope.Scope;
+import org.apache.aries.subsystem.core.SubsystemEvent;
+import org.apache.aries.subsystem.spi.Resource;
+import org.apache.aries.subsystem.spi.ResourceOperation;
+import org.eclipse.equinox.region.Region;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
@@ -35,28 +41,45 @@ import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
+import org.osgi.service.coordinator.Coordination;
 import org.osgi.util.tracker.ServiceTracker;
 import org.osgi.util.tracker.ServiceTrackerCustomizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SubsystemImpl implements Subsystem {
-
+	private static final Logger LOGGER = LoggerFactory.getLogger(SubsystemImpl.class);
+	
+	private static long lastId = -1;
+
+	private synchronized static long getNextId() {
+		if (Long.MAX_VALUE == lastId)
+			throw new IllegalStateException("Next ID would exceed Long.MAX_VALUE");
+		// First ID will be 1.
+		return ++lastId;
+	}
+	
     final long id;
     final SubsystemEventDispatcher eventDispatcher;
-    final Scope scope;
+    final Region region;
     Map<String, String> headers;
     private final ServiceTracker serviceTracker;
     private final BundleContext context;
     private final Map<Long, Subsystem> subsystems = new HashMap<Long, Subsystem>();
+    private final Subsystem parent;
+    private final String location;
 
-    public SubsystemImpl(Scope scope, Map<String, String> headers) {
-        this.scope = scope;
-        this.id = this.scope.getId();
+    public SubsystemImpl(Region region, Map<String, String> headers, Subsystem parent, String location) {
+        this.region = region;
+        this.id = getNextId();
         this.eventDispatcher = Activator.getEventDispatcher();
         this.headers = headers;
+        this.parent = parent;
+        this.location = location;
         Filter filter = null;
         try {
             filter = FrameworkUtil.createFilter("(&("
-                    + Constants.OBJECTCLASS + "=" + SubsystemAdmin.class.getName() + "))");
+                    + Constants.OBJECTCLASS + "=" + Subsystem.class.getName() + "))");
         } catch (InvalidSyntaxException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
@@ -67,15 +90,16 @@ public class SubsystemImpl implements Su
 
                     public Object addingService(ServiceReference reference) {
                         // adding new service, update admins map
-                        SubsystemAdmin sa = (SubsystemAdmin)context.getService(reference);
-                        if (sa.getParentSubsystem().getSubsystemId() == id) {
+                        Subsystem s = (Subsystem)context.getService(reference);
+                        if (s.getParent() != null && s.getParent().getSubsystemId() == id) {
                             // it is the child subsystems for the current subsystem
                             synchronized (subsystems) {
-                                subsystems.put(sa.getSubsystem().getSubsystemId(), sa.getSubsystem());
+                                subsystems.put(s.getSubsystemId(), s);
                             }
+                            return s;
                         }
 
-                        return sa;
+                        return null;
                     }
 
                     public void modifiedService(ServiceReference reference,
@@ -86,21 +110,23 @@ public class SubsystemImpl implements Su
 
                     public void removedService(ServiceReference reference,
                             Object service) {
-                        SubsystemAdmin sa = (SubsystemAdmin) service;
-                        if (sa.getParentSubsystem().getSubsystemId() == id) {
+                        Subsystem s = (Subsystem)service;
+                        if (s.getParent().getSubsystemId() == id) {
                             // it is the child subsystems for the current subsystem
                             synchronized (subsystems) {
-                                subsystems.remove(sa.getSubsystem().getSubsystemId());
+                                subsystems.remove(s.getSubsystemId());
                             }
                         }
                     }
 
                 });
+        // TODO This needs to be closed somewhere.
+        serviceTracker.open();
     }
 
     public State getState() {
         // check bundles status
-        Collection<Bundle> bundles = getBundles();
+        Collection<Bundle> bundles = getConstituents();
         if (checkBundlesStatus(bundles, Bundle.UNINSTALLED)) {
             return State.UNINSTALLED;
         } else if (checkBundlesStatus(bundles, Bundle.INSTALLED)) {
@@ -135,7 +161,7 @@ public class SubsystemImpl implements Su
     public void start() throws SubsystemException {
         try {
             eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.STARTING, System.currentTimeMillis(), this));
-            Collection<Bundle> bundles = this.scope.getBundles();
+            Collection<Bundle> bundles = getBundles();
             for (Bundle b : bundles) {
                 b.start();
             }
@@ -148,7 +174,7 @@ public class SubsystemImpl implements Su
     public void stop() throws SubsystemException {
         try {
             eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.STOPPING, System.currentTimeMillis(), this));
-            Collection<Bundle> bundles = this.scope.getBundles();
+            Collection<Bundle> bundles = getBundles();
             for (Bundle b : bundles) {
                 b.start();
             }
@@ -163,11 +189,11 @@ public class SubsystemImpl implements Su
     }
 
     public String getLocation() {
-        return scope.getLocation();
+        return location;
     }
 
     public String getSymbolicName() {
-        return scope.getName();
+        return region.getName();
     }
 
     public Version getVersion() {
@@ -186,17 +212,208 @@ public class SubsystemImpl implements Su
         return null;
     }
 
-    public Collection<Bundle> getBundles() {
-        return this.scope.getBundles();
+    public Collection<Bundle> getConstituents() {
+    	return getBundles();
     }
     
-    protected Scope getScope() {
-        return scope;
+    protected Region getRegion() {
+        return region;
     }
 
-    public Collection<Subsystem> getChildrenSubsystems() {
+    public Collection<Subsystem> getChildren() {
         synchronized (subsystems) {
             return Collections.unmodifiableCollection(new ArrayList(subsystems.values()));
         }
     }
+
+	public Subsystem getParent() {
+		return parent;
+	}
+
+	public Subsystem install(String url) throws SubsystemException {
+		return install(url, null);
+	}
+
+	public Subsystem install(final String url, final InputStream is) throws SubsystemException {
+		if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Installing subsystem url {}", url);
+        }
+        // let's check if the subsystem has been installed or not first before proceed installation
+        Subsystem toReturn = getInstalledSubsytem(url);      
+        if (toReturn != null) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("A subsystem containing the same location identifier {} is already installed", url);
+            }
+            return toReturn;
+        }
+        final Resource subsystemResource = new ResourceImpl(null, null, SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM, url, null) {
+            @Override
+            public InputStream open() throws IOException {
+                if (is != null) {
+                    return is;
+                }
+                return super.open();
+            }
+        };
+        SubsystemResourceProcessor processor = new SubsystemResourceProcessor(context);
+        final Coordination coordination = Activator.getCoordinator().create("subsystem", 0);
+        try {
+	        processor.process(new ResourceOperation() {
+				public void completed() {
+					eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.INSTALLED, System.currentTimeMillis(), getInstalledSubsytem(url)));
+				}
+	
+				public Coordination getCoordination() {
+					return coordination;
+				}
+	
+				public Resource getResource() {
+					return subsystemResource;
+				}
+	
+				public Map<String, Object> getContext() {
+					Map<String, Object> c = new HashMap<String, Object>();
+					c.put("subsystem", SubsystemImpl.this);
+					return c;
+				}
+	
+				public Type getType() {
+					return Type.INSTALL;
+				}
+	        });
+	        return getInstalledSubsytem(url); 
+        }
+        catch (Exception e) {
+        	coordination.fail(e);
+        }
+        finally {
+        	coordination.end();
+        }
+        throw new IllegalStateException();
+	}
+
+	public void uninstall() throws SubsystemException {
+		if (getState().equals(Subsystem.State.UNINSTALLED)) {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("Unable to uninstall subsystem {} as subsystem is already uninstalled", getSymbolicName());
+            }
+            return;
+        }
+        final Resource subsystemResource = new ResourceImpl(getSymbolicName(), getVersion(), SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM, getLocation(), Collections.<String, Object>emptyMap());
+        SubsystemResourceProcessor processor = new SubsystemResourceProcessor(context);
+        final Coordination coordination = Activator.getCoordinator().create("subsystem", 0);
+        try {
+	        processor.process(new ResourceOperation() {
+				public void completed() {
+					eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.UNINSTALLED, System.currentTimeMillis(), SubsystemImpl.this));
+				}
+	
+				public Coordination getCoordination() {
+					return coordination;
+				}
+	
+				public Resource getResource() {
+					return subsystemResource;
+				}
+	
+				public Map<String, Object> getContext() {
+					Map<String, Object> c = new HashMap<String, Object>();
+					c.put("subsystem", SubsystemImpl.this);
+					return c;
+				}
+	
+				public Type getType() {
+					return Type.UNINSTALL;
+				}
+	        }); 
+        }
+        catch (Exception e) {
+        	coordination.fail(e);
+        }
+        finally {
+        	coordination.end();
+        }
+	}
+
+	public void update() throws SubsystemException {
+		update(null);
+	}
+
+	public void update(final InputStream is) throws SubsystemException {
+		if (getState().equals(Subsystem.State.UNINSTALLED)) {
+            throw new IllegalStateException("Unable to update subsystem as subsystem is already uninstalled");
+        }
+        if (getState().equals(Subsystem.State.ACTIVE) 
+                || getState().equals(Subsystem.State.STARTING) 
+                || getState().equals(Subsystem.State.STOPPING)) {
+            stop();
+        }
+        final Resource subsystemResource = new ResourceImpl(getSymbolicName(), getVersion(), SubsystemConstants.RESOURCE_TYPE_SUBSYSTEM, getLocation(), Collections.<String, Object>emptyMap()) {
+            @Override
+            public InputStream open() throws IOException {
+                if (is != null) {
+                    return is;
+                }
+                // subsystem-updatelocation specified the manifest has higher priority than subsystem original location
+                String subsystemLoc = getHeaders().get(SubsystemConstants.SUBSYSTEM_UPDATELOCATION);
+                if (subsystemLoc != null && subsystemLoc.length() > 0) {
+                    // we have a subsystem location let us use it
+                    return new URL(subsystemLoc).openStream();
+                }
+                return super.open();
+            }
+        };
+        SubsystemResourceProcessor processor = new SubsystemResourceProcessor(context);
+        
+        final Coordination coordination = Activator.getCoordinator().create("subsystem", 0);
+        try {
+	        processor.process(new ResourceOperation() {
+				public void completed() {
+					eventDispatcher.subsystemEvent(new SubsystemEvent(SubsystemEvent.Type.UPDATED, System.currentTimeMillis(), SubsystemImpl.this));
+				}
+	
+				public Coordination getCoordination() {
+					return coordination;
+				}
+	
+				public Resource getResource() {
+					return subsystemResource;
+				}
+	
+				public Map<String, Object> getContext() {
+					Map<String, Object> c = new HashMap<String, Object>();
+					c.put("subsystem", SubsystemImpl.this);
+					return c;
+				}
+	
+				public Type getType() {
+					return Type.INSTALL;
+				}
+	        }); 
+        }
+        catch (Exception e) {
+        	coordination.fail(e);
+        }
+        finally {
+        	coordination.end();
+        }
+	}
+	
+	private Collection<Bundle> getBundles() {
+		Set<Long> bundleIds = region.getBundleIds();
+    	Collection<Bundle> bundles = new HashSet<Bundle>(bundleIds.size());
+    	for (Long bundleId : bundleIds) {
+    		bundles.add(context.getBundle(bundleId));
+    	}
+        return bundles;
+	}
+	
+	private Subsystem getInstalledSubsytem(String url) {
+        for (Subsystem ss : subsystems.values()) {
+            if (url.equals(ss.getLocation())) {
+                return ss;
+            }
+        }
+        return null;
+    }
 }
\ No newline at end of file



Mime
View raw message