Return-Path: X-Original-To: apmail-aries-commits-archive@www.apache.org Delivered-To: apmail-aries-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id ADAEC5AD7 for ; Thu, 12 May 2011 11:27:40 +0000 (UTC) Received: (qmail 57745 invoked by uid 500); 12 May 2011 11:27:40 -0000 Delivered-To: apmail-aries-commits-archive@aries.apache.org Received: (qmail 57654 invoked by uid 500); 12 May 2011 11:27:40 -0000 Mailing-List: contact commits-help@aries.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@aries.apache.org Delivered-To: mailing list commits@aries.apache.org Received: (qmail 57637 invoked by uid 99); 12 May 2011 11:27:40 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 May 2011 11:27:40 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 May 2011 11:27:33 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B73932388A6B; Thu, 12 May 2011 11:27:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1102244 [3/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 -0000 To: commits@aries.apache.org From: gcc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110512112713.B73932388A6B@eris.apache.org> Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java (original) +++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemResourceProcessor.java Thu May 12 11:27:11 2011 @@ -28,31 +28,28 @@ import static org.osgi.framework.Constan import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -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.Scope; -import org.apache.aries.subsystem.scope.ScopeUpdate; -import org.apache.aries.subsystem.scope.SharePolicy; +import org.apache.aries.subsystem.core.ResourceResolver; import org.apache.aries.subsystem.spi.Resource; +import org.apache.aries.subsystem.spi.ResourceOperation; 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.eclipse.equinox.region.Region; +import org.eclipse.equinox.region.RegionFilter; +import org.eclipse.equinox.region.RegionFilterBuilder; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.Filter; @@ -60,442 +57,50 @@ import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.Version; import org.osgi.framework.wiring.BundleRevision; +import org.osgi.service.coordinator.Coordination; +import org.osgi.service.coordinator.Participant; import org.osgi.util.tracker.ServiceTracker; public class SubsystemResourceProcessor implements ResourceProcessor { - - private static final Version SUBSYSTEM_MANIFEST_VERSION = new Version("1.0"); - - 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 Map installed = new HashMap(); - /* - * Map to keep track of composite bundle headers before the update and - * the updated composite bundle. This is needed for rollback - */ - private final Map, Subsystem> updated = new HashMap, Subsystem>(); - private final Map removed = new HashMap(); - private final List stopped = new ArrayList(); - private final Map trackers = new HashMap(); - private final Map> sessions = new HashMap>(); - private final BundleContext context; - - public SubsystemSession(SubsystemAdmin subsystemAdmin) { - this.subsystemAdmin = subsystemAdmin; - this.context = Activator.getBundleContext(); - } - - /** - * internal used in this class only. When a manifest is passed in, we - * won't attempt to read the manifest from the resource - * - * @param res - * @param manifest - */ - private void process(Resource res, Manifest manifest) { - - try { - SubsystemAdminImpl adminImpl = (SubsystemAdminImpl)subsystemAdmin; - Scope admin = adminImpl.getScopeAdmin(); - ResourceResolver resolver = getService(ResourceResolver.class); - - if (manifest == null) { - // Unpack the subsystem archive into a temporary directory - File dir = File.createTempFile("subsystem", "", null); - if (dir == null || !dir.delete() || !dir.mkdir()) { - throw new Exception("Unable to create temporary dir"); - } - FileUtils.unpackArchive(res.open(), dir); - - manifest = new Manifest(); - InputStream mis = new FileInputStream(new File(dir, - JarFile.MANIFEST_NAME)); - try { - manifest.read(mis); - } finally { - closeQuietly(mis); - } - - } - - List resource = new ArrayList(); - String resourceHeader = manifest.getMainAttributes().getValue( - SUBSYSTEM_RESOURCES); - Clause[] resourceClauses = Parser.parseHeader(resourceHeader); - for (Clause c : resourceClauses) { - Resource r = resolver.find(c.toString()); - resource.add(r); - } - - List content = new ArrayList(); - String contentHeader = manifest.getMainAttributes().getValue( - SUBSYSTEM_CONTENT); - Clause[] contentClauses = Parser.parseHeader(contentHeader); - for (Clause c : contentClauses) { - Resource r = resolver.find(c.toString()); - content.add(r); - } - - List previous = new ArrayList(); - - // TODO: convert resources before calling the resolver? - - List additional = resolver.resolve(content, resource); - - // check manifest header to see if they are valid - String ssn = manifest.getMainAttributes().getValue( - SUBSYSTEM_SYMBOLICNAME); - String sv = manifest.getMainAttributes().getValue( - SUBSYSTEM_VERSION); - checkManifestHeaders(manifest, ssn, sv); - - Map headers = computeSubsystemHeaders(manifest, - ssn, sv); - - // Check existing bundles - Subsystem subsystem = findSubsystem(res); - ScopeUpdate scopeUpdate = admin.newScopeUpdate(); - ScopeUpdate childScopeUpdate; - if (subsystem == null) { - // brand new install - - childScopeUpdate = scopeUpdate.newChild(headers.get(Constants.BUNDLE_SYMBOLICNAME) + "_" + headers.get(Constants.BUNDLE_VERSION), res.getLocation()); - Map> exportSharePolicies = childScopeUpdate.getSharePolicies(SharePolicy.TYPE_EXPORT); - Map> importSharePolicies = childScopeUpdate.getSharePolicies(SharePolicy.TYPE_IMPORT); - - setupSharePolicies(exportSharePolicies, importSharePolicies, headers); - scopeUpdate.commit(); - - // ScopeAdmin childScopeAdmin = getService(ScopeAdmin.class, "ScopeId=" + childScopeUpdate.getScope().getId()); - - Scope childScopeAdmin = childScopeUpdate.getScope(); - - 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 data before update - Map 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); - } - - subsystem.updateHeaders(headers); - updated.put(subsystemHeaders, subsystem); - } - - // 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) { - if (r2.getSymbolicName().equals(r.getSymbolicName()) - && r2.getVersion().equals(r.getVersion())) { - stillHere = true; - break; - } - } - if (!stillHere) { - getSession(childAdmin, - r.getType()).dropped(r); - } - } - - // additional resource is installed outside of the subsystem. - for (Resource r : additional) { - getSession(subsystemAdmin, r.getType()).process(r); - } - - - for (Resource r : content) { - getSession(childAdmin, r.getType()) - .process(r); - } - - } catch (SubsystemException e) { - throw e; - } catch (Exception e) { - throw new SubsystemException("Unable to process subsystem", e); - } - } - - private void setupSharePolicies( - Map> exportSharePolicies, - Map> importSharePolicies, - Map 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 importPackagePolicies = importSharePolicies.get(BundleRevision.PACKAGE_NAMESPACE); - if (importPackagePolicies == null) { - importPackagePolicies = new ArrayList(); - importSharePolicies.put(BundleRevision.PACKAGE_NAMESPACE,importPackagePolicies); - } - - importPackagePolicies.add(new SharePolicy(SharePolicy.TYPE_IMPORT, BundleRevision.PACKAGE_NAMESPACE, createFilter(importPackage, BundleRevision.PACKAGE_NAMESPACE))); - } - - if (importService != null && !importService.trim().isEmpty()) { - List importServicePolicies = importSharePolicies.get("scope.share.service"); - if (importServicePolicies == null) { - importServicePolicies = new ArrayList(); - importSharePolicies.put("scope.share.service",importServicePolicies); - } - - importServicePolicies.add(new SharePolicy(SharePolicy.TYPE_IMPORT, "scope.share.service", createFilter(importService, "scope.share.service"))); - } - - } - - private Filter createFilter(String packages, String namespace) { - if (namespace.equals(BundleRevision.PACKAGE_NAMESPACE)) { - // split packages - List pkgs = ManifestHeaderProcessor.split(packages, ","); - StringBuffer sb = new StringBuffer(); - sb.append("(|"); - for (String pkg : pkgs) { - sb.append("(" + BundleRevision.PACKAGE_NAMESPACE + "=" + pkg + ")"); - } - sb.append(")"); - try { - return FrameworkUtil.createFilter(sb.toString()); - } catch (InvalidSyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - if (namespace.equals("scope.share.service")) { - // split packages - List pkgs = ManifestHeaderProcessor.split(packages, ","); - StringBuffer sb = new StringBuffer(); - sb.append("(|"); - for (String pkg : pkgs) { - sb.append("(scope.share.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 { - Subsystem subsystem = findSubsystem(res); - if (subsystem == null) { - throw new SubsystemException( - "Unable to find matching subsystem to uninstall"); - } - // TODO: iterate through all resources and ask for a removal on - // each one - subsystemAdmin.uninstall(subsystem); - removed.put(res, subsystem); - - } - - protected Subsystem findSubsystem(Resource resource) { - for (Subsystem subsystem : this.subsystemAdmin.getSubsystems()) { - if (resource.getLocation().equals(subsystem.getLocation())) { - return subsystem; - } - } - return null; - } - - public void prepare() throws SubsystemException { - for (Map sm : sessions.values()) { - for (Session s : sm.values()) { - s.prepare(); - } - } - for (Subsystem subsystem : stopped) { - subsystem.start(); - } - } - - public void commit() { - for (Map sm : sessions.values()) { - for (Session s : sm.values()) { - s.commit(); - } - } - installed.clear(); - updated.clear(); - removed.clear(); - closeTrackers(); - } - - public void rollback() { - - for (Subsystem subsystem : installed.values()) { - subsystemAdmin.uninstall(subsystem); - } - installed.clear(); - - // Handle updated subsystems - Set, Subsystem>> updatedSet = updated - .entrySet(); - for (Entry, Subsystem> entry : updatedSet) { - Map oldHeader = entry.getKey(); - Subsystem subsystem = entry.getValue(); - - // let's build a Manifest from oldDict - Manifest manifest = new Manifest(); - Attributes attributes = manifest.getMainAttributes(); - 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, subsystem - .getLocation(), Collections - . emptyMap()); - try { - Session session = getSession(subsystemAdmin, subsystemResource - .getType()); - if (session instanceof SubsystemSession) { - ((SubsystemSession) session).process(subsystemResource, - manifest); - } else { - throw new SubsystemException( - "Invalid subsystem session - Unable to rollback subsystem"); - } - } catch (SubsystemException e) { - throw e; - } catch (Exception e) { - throw new SubsystemException( - "Unable to rollback subsystem", e); - } - - } - - // handle uninstalled subsystem - Set> set = removed.entrySet(); - for (Map.Entry entry : set) { - Resource res = entry.getKey(); - try { - getSession(subsystemAdmin, res.getType()).process(res); - } catch (SubsystemException e) { - throw e; - } catch (Exception e) { - throw new SubsystemException( - "Unable to rollback subsystem", e); - } - } - - removed.clear(); - - for (Map sm : sessions.values()) { - for (Session s : sm.values()) { - s.rollback(); - } - } - - closeTrackers(); - } - - protected Session getSession(SubsystemAdmin admin, String type) - throws InvalidSyntaxException, InterruptedException { - Map sm = this.sessions.get(admin); - if (sm == null) { - sm = new HashMap(); - this.sessions.put(admin, sm); - } - Session session = sm.get(type); - if (session == null) { - session = getProcessor(type).createSession(admin); - sm.put(type, session); - } - return session; - } - - protected ResourceProcessor getProcessor(String type) - throws InvalidSyntaxException, InterruptedException { - return getService(ResourceProcessor.class, - SubsystemConstants.SERVICE_RESOURCE_TYPE + "=" + type); - } - - protected T getService(Class clazz) - throws InvalidSyntaxException, InterruptedException { - return getService(clazz, null); - } - - protected T getService(Class clazz, String filter) - throws InvalidSyntaxException, InterruptedException { - Filter flt; - if (filter != null) { - if (!filter.startsWith("(")) { - flt = context.createFilter("(&(" + Constants.OBJECTCLASS - + "=" + clazz.getName() + ")(" + filter + "))"); - } else { - flt = context.createFilter("(&(" + Constants.OBJECTCLASS - + "=" + clazz.getName() + ")" + filter + ")"); - } - } else { - flt = context.createFilter("(" + Constants.OBJECTCLASS + "=" - + clazz.getName() + ")"); - } - ServiceTracker tracker = trackers.get(flt.toString()); - if (tracker == null) { - tracker = new ServiceTracker(context, flt, null); - tracker.open(); - trackers.put(flt.toString(), tracker); - } - T t = (T) tracker.waitForService(TIMEOUT); - if (t == null) { - throw new SubsystemException("No service available: " + flt); - } - return t; - } - - private void closeTrackers() { - for (ServiceTracker t : trackers.values()) { - t.close(); - } - trackers.clear(); - } - } - - private static void checkManifestHeaders(Manifest manifest, String ssn, - String sv) { + private static final Version SUBSYSTEM_MANIFEST_VERSION = new Version("1.0"); + private static final long TIMEOUT = 30000; + + private final BundleContext bundleContext; + private final Map installed = new HashMap(); + private final Map trackers = new HashMap(); + private final Map, Subsystem> updated = new HashMap, Subsystem>(); + + public SubsystemResourceProcessor(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + public void process(final ResourceOperation operation) throws SubsystemException { + switch (operation.getType()) { + case INSTALL: + installOrUpdate(operation); + break; + case START: + start(operation); + break; + case STOP: + stop(operation); + break; + case UNINSTALL: + uninstall(operation); + break; + case UPDATE: + installOrUpdate(operation); + break; + default: + throw new SubsystemException("Unsupported resource opertaion type: " + operation.getType()); + } + } + + private static void checkManifestHeaders(Manifest manifest, String ssn, String sv) { // Check subsystem manifest required headers - String mfv = manifest.getMainAttributes().getValue( - SUBSYSTEM_MANIFESTVERSION); + String mfv = manifest.getMainAttributes().getValue(SUBSYSTEM_MANIFESTVERSION); if (mfv == null || mfv.length() == 0) { - throw new SubsystemException("Invalid subsystem manifest version: " - + mfv); + throw new SubsystemException("Invalid subsystem manifest version: " + mfv); } try { Version v = Version.parseVersion(mfv); @@ -506,14 +111,12 @@ public class SubsystemResourceProcessor + SubsystemConstants.SUBSYSTEM_MANIFESTVERSION + " is " + SUBSYSTEM_MANIFEST_VERSION); } - } catch (IllegalArgumentException e) { - throw new SubsystemException("Invalid subsystem manifest version: " - + mfv, e); + } + catch (IllegalArgumentException e) { + throw new SubsystemException("Invalid subsystem manifest version: " + mfv, e); } - if (ssn == null || ssn.length() == 0) { - throw new SubsystemException("Invalid subsystem symbolic name: " - + ssn); + throw new SubsystemException("Invalid subsystem symbolic name: " + ssn); } // check attributes / directives on the subsystem symbolic name // TODO add any other symbolic name to check @@ -524,38 +127,20 @@ public class SubsystemResourceProcessor + " directive in " + SUBSYSTEM_SYMBOLICNAME + ": " + ssDirective); } - if (sv == null || sv.length() == 0) { throw new SubsystemException("Invalid subsystem version: " + sv); } try { new Version(sv); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { throw new SubsystemException("Invalid subsystem version: " + sv, e); } - // TODO: do we want to check other headers such as // subsystem-importpackage, subsystem-exportpackage, etc. - - } - - // if the ssn already contains COMPOSITE_DIRECTIVE or SUBSYSTEM_DIRECTIVE - // directive - // let's not add them again - private static String getSubsystemSymbolicName(String ssn) { - Clause[] ssnClauses = Parser.parseHeader(ssn); - String ssDirective = ssnClauses[0].getDirective(SUBSYSTEM_DIRECTIVE); - - if (ssDirective == null ) { - ssn = ssn + ";" - + SUBSYSTEM_DIRECTIVE + ":=true"; - } - - return ssn; } - - private static Map computeSubsystemHeaders( - Manifest manifest, String ssn, String sv) { + + private static Map computeSubsystemHeaders(Manifest manifest, String ssn, String sv) { // Grab all headers Map headers = new HashMap(); Iterator it = manifest.getMainAttributes().entrySet().iterator(); @@ -565,24 +150,346 @@ public class SubsystemResourceProcessor String value = e.getValue().toString(); headers.put(name, value); } - // Create the required composite headers headers.put(BUNDLE_SYMBOLICNAME, getSubsystemSymbolicName(ssn)); headers.put(BUNDLE_VERSION, sv); - String subImportPkg = headers.get(SUBSYSTEM_IMPORTPACKAGE); String subExportPkg = headers.get(SUBSYSTEM_EXPORTPACKAGE); - - // TODO: compute other composite manifest entries // TODO: compute list of bundles - return headers; } - - public Session createSession(BundleContext arg0) { - // TODO Auto-generated method stub + + private Filter createFilter(String packages, String namespace) { + if (namespace.equals(BundleRevision.PACKAGE_NAMESPACE)) { + // split packages + List pkgs = ManifestHeaderProcessor.split(packages, ","); + StringBuffer sb = new StringBuffer(); + sb.append("(|"); + for (String pkg : pkgs) { + sb.append("(" + BundleRevision.PACKAGE_NAMESPACE + "=" + pkg + ")"); + } + sb.append(")"); + try { + return FrameworkUtil.createFilter(sb.toString()); + } + catch (InvalidSyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + if (namespace.equals("scope.share.service")) { + // split packages + List pkgs = ManifestHeaderProcessor.split(packages, ","); + StringBuffer sb = new StringBuffer(); + sb.append("(|"); + for (String pkg : pkgs) { + sb.append("(scope.share.service=" + pkg + ")"); + } + sb.append(")"); + try { + return FrameworkUtil.createFilter(sb.toString()); + } + catch (InvalidSyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } return null; } - + + private Subsystem findSubsystem(Resource resource, Subsystem s) { + for (Subsystem subsystem : s.getChildren()) { + if (resource.getAttributes().get(Resource.LOCATION_ATTRIBUTE).equals(subsystem.getLocation())) { + return subsystem; + } + } + return null; + } + + private Manifest getManifest(ResourceOperation operation) throws Exception { + Manifest manifest = (Manifest)operation.getContext().get("manifest"); + if (manifest == null) { + // Unpack the subsystem archive into a temporary directory + File dir = File.createTempFile("subsystem", "", null); + if (dir == null || !dir.delete() || !dir.mkdir()) { + throw new Exception("Unable to create temporary dir"); + } + String location = String.valueOf(operation.getResource().getAttributes().get(Resource.LOCATION_ATTRIBUTE)); + FileUtils.unpackArchive(new URL(location).openStream(), dir); + manifest = new Manifest(); + InputStream mis = new FileInputStream(new File(dir, JarFile.MANIFEST_NAME)); + try { + manifest.read(mis); + } + finally { + closeQuietly(mis); + } + } + return manifest; + } + + private T getService(Class clazz) throws InvalidSyntaxException, InterruptedException { + return getService(clazz, null); + } + + private T getService(Class clazz, String filter) throws InvalidSyntaxException, InterruptedException { + Filter flt; + if (filter != null) { + if (!filter.startsWith("(")) { + flt = bundleContext.createFilter("(&(" + Constants.OBJECTCLASS + + "=" + clazz.getName() + ")(" + filter + "))"); + } + else { + flt = bundleContext.createFilter("(&(" + Constants.OBJECTCLASS + + "=" + clazz.getName() + ")" + filter + ")"); + } + } + else { + flt = bundleContext.createFilter("(" + Constants.OBJECTCLASS + "=" + + clazz.getName() + ")"); + } + ServiceTracker tracker = trackers.get(flt.toString()); + if (tracker == null) { + tracker = new ServiceTracker(bundleContext, flt, null); + tracker.open(); + trackers.put(flt.toString(), tracker); + } + T t = (T) tracker.waitForService(TIMEOUT); + if (t == null) { + throw new SubsystemException("No service available: " + flt); + } + return t; + } + + // if the ssn already contains COMPOSITE_DIRECTIVE or SUBSYSTEM_DIRECTIVE + // directive + // let's not add them again + private static String getSubsystemSymbolicName(String ssn) { + Clause[] ssnClauses = Parser.parseHeader(ssn); + String ssDirective = ssnClauses[0].getDirective(SUBSYSTEM_DIRECTIVE); + if (ssDirective == null ) { + ssn = ssn /*+ ";" + + SUBSYSTEM_DIRECTIVE + ":=true"*/; + } + return ssn; + } + + private void installOrUpdate(final ResourceOperation operation) { + try { + SubsystemImpl subsystemInContext = (SubsystemImpl)operation.getContext().get("subsystem"); + final Region subsystemInContextRegion = subsystemInContext.getRegion(); + ResourceResolver resolver = getService(ResourceResolver.class); + Manifest manifest = getManifest(operation); + List resource = new ArrayList(); + String resourceHeader = manifest.getMainAttributes().getValue(SUBSYSTEM_RESOURCES); + Clause[] resourceClauses = Parser.parseHeader(resourceHeader); + for (Clause c : resourceClauses) { + Resource r = resolver.find(c.toString()); + resource.add(r); + } + List content = new ArrayList(); + String contentHeader = manifest.getMainAttributes().getValue(SUBSYSTEM_CONTENT); + Clause[] contentClauses = Parser.parseHeader(contentHeader); + for (Clause c : contentClauses) { + Resource r = resolver.find(c.toString()); + content.add(r); + } + List previous = new ArrayList(); + // TODO: convert resources before calling the resolver? + List additional = resolver.resolve(content, resource); + // check manifest header to see if they are valid + String ssn = manifest.getMainAttributes().getValue(SUBSYSTEM_SYMBOLICNAME); + String sv = manifest.getMainAttributes().getValue(SUBSYSTEM_VERSION); + checkManifestHeaders(manifest, ssn, sv); + Map headers = computeSubsystemHeaders(manifest, ssn, sv); + // Check existing bundles + Subsystem subsystemToProcess = findSubsystem(operation.getResource(), subsystemInContext); + final Region childRegion; + if (subsystemToProcess == null) { + // brand new install + String location = String.valueOf(operation.getResource().getAttributes().get(Resource.LOCATION_ATTRIBUTE)); + childRegion = subsystemInContextRegion.getRegionDigraph().createRegion(headers.get(Constants.BUNDLE_SYMBOLICNAME) + "_" + headers.get(Constants.BUNDLE_VERSION)); + setupSharePolicies(subsystemInContextRegion, childRegion, headers); + // ScopeAdmin childScopeAdmin = getService(ScopeAdmin.class, "ScopeId=" + childScopeUpdate.getScope().getId()); + subsystemToProcess = new SubsystemImpl(childRegion, headers, subsystemInContext, location); + bundleContext.registerService(Subsystem.class.getName(), subsystemToProcess, DictionaryBuilder.build("SubsystemParentId", subsystemInContext.getSubsystemId(), "SubsystemId", subsystemToProcess.getSubsystemId())); + installed.put(operation.getResource(), subsystemToProcess); + } + else { + childRegion = null; + // update + // capture data before update + Map subsystemHeaders = subsystemToProcess.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); + } + ((SubsystemImpl)subsystemToProcess).updateHeaders(headers); + updated.put(subsystemHeaders, subsystemToProcess); + } + // content is installed in the scope, so need to find the subsystemAdmin for the scope first + long scopeId = subsystemToProcess.getSubsystemId(); + Subsystem childSubsystem = getService(Subsystem.class, "SubsystemId=" + scopeId); + for (final Resource r : previous) { + boolean stillHere = false; + for (Resource r2 : content) { + if (r2.getAttributes().get(Resource.SYMBOLIC_NAME_ATTRIBUTE).equals(r.getAttributes().get(Resource.SYMBOLIC_NAME_ATTRIBUTE)) + && r2.getAttributes().get(Resource.VERSION_ATTRIBUTE).equals(r.getAttributes().get(Resource.VERSION_ATTRIBUTE))) { + stillHere = true; + break; + } + } + if (!stillHere) { + // TODO Get this from the service registry. + new BundleResourceProcessor(bundleContext).process(new ResourceOperation() { + public void completed() { + // TODO Send event. + } + + public Coordination getCoordination() { + return operation.getCoordination(); + } + + public Resource getResource() { + return r; + } + + public Map getContext() { + Map context = new HashMap(); + context.put("region", childRegion == null ? subsystemInContextRegion : childRegion); + return context; + } + + public Type getType() { + return Type.UNINSTALL; + } + }); + } + } + // additional resource is installed outside of the subsystem. + for (final Resource r : additional) { + // TODO Get this from the service registry. + new BundleResourceProcessor(bundleContext).process(new ResourceOperation() { + public void completed() { + // TODO Send event. + } + + public Coordination getCoordination() { + return operation.getCoordination(); + } + + public Resource getResource() { + return r; + } + + public Map getContext() { + Map context = new HashMap(); + context.put("region", childRegion == null ? subsystemInContextRegion : childRegion); + return context; + } + + public Type getType() { + return Type.INSTALL; + } + }); + } + for (final Resource r : content) { + // TODO Get this from the service registry. + new BundleResourceProcessor(bundleContext).process(new ResourceOperation() { + public void completed() { + // TODO Send event. + } + + public Coordination getCoordination() { + return operation.getCoordination(); + } + + public Resource getResource() { + return r; + } + + public Map getContext() { + Map context = new HashMap(); + context.put("region", childRegion == null ? subsystemInContextRegion : childRegion); + return context; + } + + public Type getType() { + return Type.INSTALL; + } + }); + } + } + catch (Exception e) { + throw new SubsystemException("Unable to process subsystem", e); + } + } + + private void setupSharePolicies(Region parent, Region child, Map headers) throws Exception { + 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); + RegionFilterBuilder builder = child.getRegionDigraph().createRegionFilterBuilder(); + if (importPackage != null && !importPackage.trim().isEmpty()) { + builder.allow(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, importPackage); + } + if (importService != null && !importService.trim().isEmpty()) { + builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, importService); + } + child.connectRegion(parent, builder.build()); + builder = parent.getRegionDigraph().createRegionFilterBuilder(); + if (exportPackage != null && !exportPackage.trim().isEmpty()) { + builder.allow(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, exportPackage); + } + if (exportService != null && !exportService.trim().isEmpty()) { + builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, exportService); + } + parent.connectRegion(child, builder.build()); + } + + private void start(final ResourceOperation operation) { + final Subsystem subsystem = (Subsystem)operation.getContext().get("subsystem"); + subsystem.start(); + operation.getCoordination().addParticipant(new Participant() { + public void ended(Coordination arg0) throws Exception { + operation.completed(); + } + + public void failed(Coordination arg0) throws Exception { + subsystem.stop(); + } + }); + } + + private void stop(final ResourceOperation operation) { + final Subsystem subsystem = (Subsystem)operation.getContext().get("subsystem"); + subsystem.stop(); + operation.getCoordination().addParticipant(new Participant() { + public void ended(Coordination arg0) throws Exception { + operation.completed(); + } + + public void failed(Coordination arg0) throws Exception { + subsystem.start(); + } + }); + } + + private void uninstall(final ResourceOperation operation) { + final Subsystem subsystem = (Subsystem)operation.getContext().get("subsystem"); + subsystem.uninstall(); + operation.getCoordination().addParticipant(new Participant() { + public void ended(Coordination arg0) throws Exception { + operation.completed(); + } + + public void failed(Coordination arg0) throws Exception { + // TODO Rollback? + } + }); + } } \ No newline at end of file Added: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemServiceFactory.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemServiceFactory.java?rev=1102244&view=auto ============================================================================== --- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemServiceFactory.java (added) +++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/SubsystemServiceFactory.java Thu May 12 11:27:11 2011 @@ -0,0 +1,32 @@ +package org.apache.aries.subsystem.core.internal; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.aries.subsystem.Subsystem; +import org.eclipse.equinox.region.Region; +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceFactory; +import org.osgi.framework.ServiceRegistration; + +public class SubsystemServiceFactory implements ServiceFactory { + private final Map bundlesToSubsystems = new HashMap(); + private final Subsystem rootSubsystem; + + public SubsystemServiceFactory(Region region) { + rootSubsystem = new RootSubsystem(region); + } + + public Subsystem getService(Bundle bundle, ServiceRegistration registration) { + Subsystem result = bundlesToSubsystems.get(bundle); + if (result == null) { + result = rootSubsystem; + bundlesToSubsystems.put(bundle, result); + } + return result; + } + + public void ungetService(Bundle bundle, ServiceRegistration registration, Subsystem service) { + bundlesToSubsystems.remove(bundle); + } +} Modified: aries/trunk/subsystem/subsystem-install/src/main/java/org/apache/aries/subsystem/install/internal/SubsystemInstaller.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-install/src/main/java/org/apache/aries/subsystem/install/internal/SubsystemInstaller.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-install/src/main/java/org/apache/aries/subsystem/install/internal/SubsystemInstaller.java (original) +++ aries/trunk/subsystem/subsystem-install/src/main/java/org/apache/aries/subsystem/install/internal/SubsystemInstaller.java Thu May 12 11:27:11 2011 @@ -21,36 +21,36 @@ import java.util.jar.JarFile; import java.util.jar.Manifest; import org.apache.aries.subsystem.Subsystem; -import org.apache.aries.subsystem.SubsystemAdmin; import org.apache.aries.subsystem.SubsystemConstants; import org.apache.felix.fileinstall.ArtifactInstaller; public class SubsystemInstaller implements ArtifactInstaller { - private SubsystemAdmin subsystemAdmin; + private Subsystem root; - public SubsystemAdmin getSubsystemAdmin() { - return subsystemAdmin; + public Subsystem getRootSubsystem() { + return root; } - public void setSubsystemAdmin(SubsystemAdmin subsystemAdmin) { - this.subsystemAdmin = subsystemAdmin; + public void setRootSubsystem(Subsystem subsystem) { + this.root = subsystem; } public void install(File file) throws Exception { - subsystemAdmin.install(getLocation(file)); + root.install(getLocation(file)); } public void update(File file) throws Exception { - subsystemAdmin.update(getSubsystem(getLocation(file))); + String location = getLocation(file); + getSubsystem(location).update(); } public void uninstall(File file) throws Exception { - subsystemAdmin.uninstall(getSubsystem(getLocation(file))); + getSubsystem(getLocation(file)).uninstall(); } protected Subsystem getSubsystem(String location) { - for (Subsystem s : subsystemAdmin.getSubsystems()) { + for (Subsystem s : root.getChildren()) { if (s.getLocation().equals(location)) { return s; } Modified: aries/trunk/subsystem/subsystem-itests/pom.xml URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-itests/pom.xml?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-itests/pom.xml (original) +++ aries/trunk/subsystem/subsystem-itests/pom.xml Thu May 12 11:27:11 2011 @@ -1,22 +1,15 @@ - - + + 4.0.0 org.apache.aries @@ -46,40 +39,109 @@ org.apache.aries.subsystem.api test 0.1-SNAPSHOT + + + + + + org.osgi + + org.osgi.core + + + + + org.apache.aries.subsystem org.apache.aries.subsystem.core test 0.1-SNAPSHOT + + + org.apache.aries.subsystem + org.apache.aries.subsystem.scope.api + 0.1-SNAPSHOT + + + + + + org.osgi + + org.osgi.core + + + + + org.apache.aries.subsystem org.apache.aries.subsystem.scope.impl 0.1-SNAPSHOT - + org.apache.aries.subsystem org.apache.aries.subsystem.executor 0.1-SNAPSHOT - + + + + + org.osgi + + org.osgi.core + + + + + + org.apache.aries.testsupport org.apache.aries.testsupport.unit test 0.3 + + + + + + org.osgi + + org.osgi.core + + + + + org.apache.aries org.apache.aries.util test 0.3 - + + + + + + org.osgi + + org.osgi.core + + + + + + org.ops4j.pax.exam pax-exam @@ -114,6 +176,19 @@ org.apache.felix.configadmin test 1.2.8 + + + + + + org.osgi + + org.osgi.core + + + + + org.ops4j.pax.logging @@ -135,24 +210,75 @@ org.osgi.service.obr test 1.0.2 + + + + + + org.osgi + + org.osgi.core + + + + + org.apache.felix org.apache.felix.bundlerepository test 1.6.4 + + + + + + org.osgi + + org.osgi.core + + + + + org.apache.aries.application org.apache.aries.application.utils test 0.3 + + + + + + org.osgi + + org.osgi.core + + + + + org.apache.aries.subsystem org.apache.aries.subsystem.example.helloIsolation 0.1-SNAPSHOT test + + + + + org.osgi + + org.osgi.core + + + + + @@ -205,10 +331,8 @@ org.apache.maven.plugins maven-surefire-plugin - + -Dorg.ops4j.pax.url.mvn.localRepository=${maven.repo.local} Modified: aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdmin2Test.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdmin2Test.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdmin2Test.java (original) +++ aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdmin2Test.java Thu May 12 11:27:11 2011 @@ -30,8 +30,6 @@ import java.io.FileOutputStream; import java.util.Collection; import org.apache.aries.subsystem.Subsystem; -import org.apache.aries.subsystem.SubsystemAdmin; -import org.apache.aries.subsystem.scope.Scope; import org.apache.aries.unittest.fixture.ArchiveFixture; import org.apache.aries.unittest.fixture.ArchiveFixture.ZipFixture; import org.junit.Before; @@ -69,25 +67,20 @@ public class SubsystemAdmin2Test extends @Test public void test() throws Exception { - // make sure we are using a framework that provides scope admin service - Scope scopeA = getOsgiService(Scope.class); - assertNotNull("scope admin should not be null", scopeA); - System.out.println("able to get scope admin service"); - // obtain subsystem admin service - SubsystemAdmin sa = getOsgiService(SubsystemAdmin.class); - assertNotNull("subsystem admin should not be null", sa); + Subsystem s = getOsgiService(Subsystem.class); + assertNotNull("subsystem admin should not be null", s); System.out.println("able to get subsytem admin service"); File f = new File("test.eba"); - Subsystem subsystem = sa.install(f.toURI().toURL().toExternalForm()); + Subsystem subsystem = s.install(f.toURI().toURL().toExternalForm()); assertNotNull("subsystem should not be null", subsystem); assertTrue("subsystem should have a unique id", subsystem.getSubsystemId() > 0); assertTrue(subsystem.getLocation().indexOf("test.eba") != -1); assertTrue(subsystem.getSymbolicName().indexOf("felix-file-install2") != -1); assertEquals("2.0.8", subsystem.getVersion().toString()); - Collection bundles = subsystem.getBundles(); + Collection bundles = subsystem.getConstituents(); assertEquals("check bundles' size", 1, bundles.size()); } @@ -108,13 +101,13 @@ public class SubsystemAdmin2Test extends systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("DEBUG"), // Bundles - mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.scope.api"), - mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.scope.impl"), + mavenBundle("org.eclipse.equinox", "region"), mavenBundle("org.apache.aries.testsupport", "org.apache.aries.testsupport.unit"), mavenBundle("org.apache.aries.application", "org.apache.aries.application.api"), mavenBundle("org.apache.aries", "org.apache.aries.util"), mavenBundle("org.apache.aries.application", "org.apache.aries.application.utils"), mavenBundle("org.apache.felix", "org.apache.felix.bundlerepository"), + mavenBundle("org.eclipse.equinox", "coordinator"), mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.api"), mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.core"), Modified: aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdminTest.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdminTest.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdminTest.java (original) +++ aries/trunk/subsystem/subsystem-itests/src/test/java/org/apache/aries/subsystem/itests/SubsystemAdminTest.java Thu May 12 11:27:11 2011 @@ -30,8 +30,6 @@ import java.io.FileOutputStream; import java.util.Collection; import org.apache.aries.subsystem.Subsystem; -import org.apache.aries.subsystem.SubsystemAdmin; -import org.apache.aries.subsystem.scope.Scope; import org.apache.aries.unittest.fixture.ArchiveFixture; import org.apache.aries.unittest.fixture.ArchiveFixture.ZipFixture; import org.junit.Before; @@ -71,27 +69,22 @@ public class SubsystemAdminTest extends // Disable this test since it is designed to use the NoOpResolver which isn't registered. //@Test public void test() throws Exception { - // make sure we are using a framework that provides composite admin service - Scope scopeA = getOsgiService(Scope.class); - assertNotNull("composite admin should not be null", scopeA); - System.out.println("able to get composite admin service"); - // obtain subsystem admin service - SubsystemAdmin sa = getOsgiService(SubsystemAdmin.class); - assertNotNull("subsystem admin should not be null", sa); + Subsystem s = getOsgiService(Subsystem.class); + assertNotNull("subsystem admin should not be null", s); System.out.println("able to get subsytem admin service"); File f = new File("test.eba"); // capture initial bundle size int init = bundleContext.getBundles().length; - Subsystem subsystem = sa.install(f.toURI().toURL().toExternalForm()); + Subsystem subsystem = s.install(f.toURI().toURL().toExternalForm()); assertNotNull("subsystem should not be null", subsystem); assertTrue("subsystem should have a unique id", subsystem.getSubsystemId() > 0); assertTrue(subsystem.getLocation().indexOf("test.eba") != -1); assertEquals("felix-file-install", subsystem.getSymbolicName()); assertEquals("2.0.8", subsystem.getVersion().toString()); - Collection bundles = subsystem.getBundles(); + Collection bundles = subsystem.getConstituents(); assertEquals("check constituents' size", 1, bundles.size()); // recapture bundle size int later = bundleContext.getBundles().length; @@ -121,10 +114,9 @@ public class SubsystemAdminTest extends mavenBundle("org.apache.aries.application", "org.apache.aries.application.api"), mavenBundle("org.apache.aries.application", "org.apache.aries.application.utils"), mavenBundle("org.apache.felix", "org.apache.felix.bundlerepository"), + mavenBundle("org.eclipse.equinox", "coordinator"), mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.api"), - mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.scope.api"), mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.core"), - mavenBundle("org.apache.aries.subsystem", "org.apache.aries.subsystem.scope.impl"), //org.ops4j.pax.exam.container.def.PaxRunnerOptions.vmOption("-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"), Modified: aries/trunk/subsystem/subsystem-obr/pom.xml URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/pom.xml?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-obr/pom.xml (original) +++ aries/trunk/subsystem/subsystem-obr/pom.xml Thu May 12 11:27:11 2011 @@ -65,6 +65,11 @@ 0.1-SNAPSHOT + org.apache.aries.subsystem + org.apache.aries.subsystem.core + 0.1-SNAPSHOT + + org.apache.felix org.apache.felix.bundlerepository 1.6.4 Modified: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/Activator.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/Activator.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/Activator.java (original) +++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/Activator.java Thu May 12 11:27:11 2011 @@ -18,7 +18,7 @@ */ package org.apache.aries.subsystem.obr.internal; -import org.apache.aries.subsystem.spi.ResourceResolver; +import org.apache.aries.subsystem.core.ResourceResolver; import org.apache.felix.bundlerepository.RepositoryAdmin; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; Modified: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceImpl.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceImpl.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceImpl.java (original) +++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceImpl.java Thu May 12 11:27:11 2011 @@ -21,8 +21,11 @@ package org.apache.aries.subsystem.obr.i import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.List; import java.util.Map; +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; @@ -30,9 +33,9 @@ public class ObrResourceImpl implements private final org.apache.felix.bundlerepository.Resource resource; private final String type; - private final Map attributes; + private final Map attributes; - public ObrResourceImpl(org.apache.felix.bundlerepository.Resource resource, String type, Map attributes) { + public ObrResourceImpl(org.apache.felix.bundlerepository.Resource resource, String type, Map attributes) { this.resource = resource; this.type = type; this.attributes = attributes; @@ -54,11 +57,21 @@ public class ObrResourceImpl implements return resource.getURI(); } - public Map getAttributes() { + public Map getAttributes() { return attributes; } public InputStream open() throws IOException { return new URL(getLocation()).openStream(); } + + public List getCapabilities(String namespace) { + // TODO Auto-generated method stub + return null; + } + + public List getRequirements(String namespace) { + // TODO Auto-generated method stub + return null; + } } Modified: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceResolver.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceResolver.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceResolver.java (original) +++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceResolver.java Thu May 12 11:27:11 2011 @@ -27,8 +27,8 @@ 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.bundlerepository.Capability; import org.apache.felix.bundlerepository.Repository; import org.apache.felix.bundlerepository.RepositoryAdmin; @@ -76,7 +76,7 @@ public class ObrResourceResolver impleme String ver = clauses[0].getAttribute(Constants.VERSION_ATTRIBUTE); String typ = clauses[0].getAttribute(SubsystemConstants.RESOURCE_TYPE_ATTRIBUTE); String loc = clauses[0].getAttribute(SubsystemConstants.RESOURCE_LOCATION_ATTRIBUTE); - Map attributes = new HashMap(); + Map attributes = new HashMap(); for (Attribute a : clauses[0].getAttributes()) { String name = a.getName(); if (!Constants.VERSION_ATTRIBUTE.equals(name) @@ -116,10 +116,10 @@ public class ObrResourceResolver impleme List obrResources = new ArrayList(); for (Resource res : subsystemResources) { - if (res.getType().equals(SubsystemConstants.RESOURCE_TYPE_BUNDLE)) { + if (res.getAttributes().get(Resource.NAMESPACE_ATTRIBUTE).equals(SubsystemConstants.RESOURCE_TYPE_BUNDLE)) { try { - obrResources.add(admin.getHelper().createResource(new URL(res.getLocation()))); + obrResources.add(admin.getHelper().createResource(new URL(String.valueOf(res.getAttributes().get(Resource.LOCATION_ATTRIBUTE))))); } catch (Exception e) { // TODO: log exception @@ -136,8 +136,10 @@ public class ObrResourceResolver impleme for (Resource res : subsystemContent) { - if (res.getType().equals(SubsystemConstants.RESOURCE_TYPE_BUNDLE)) { - resolver.add(admin.getHelper().requirement(Capability.BUNDLE, "(&(symbolicname=" + res.getSymbolicName() + ")" + (res.getVersion() != null ? "(version=" + res.getVersion() + ")" : "") + ")")); + if (res.getAttributes().get(Resource.NAMESPACE_ATTRIBUTE).equals(SubsystemConstants.RESOURCE_TYPE_BUNDLE)) { + String symbolicName = String.valueOf(res.getAttributes().get(Resource.SYMBOLIC_NAME_ATTRIBUTE)); + Version version = (Version)res.getAttributes().get(Resource.VERSION_ATTRIBUTE); + resolver.add(admin.getHelper().requirement(Capability.BUNDLE, "(&(symbolicname=" + symbolicName + ")" + (version != null ? "(version=" + version + ")" : "") + ")")); } } if (!resolver.resolve(Resolver.NO_OPTIONAL_RESOURCES)) { @@ -146,7 +148,7 @@ public class ObrResourceResolver impleme List resolved = new ArrayList(); for (org.apache.felix.bundlerepository.Resource res : resolver.getRequiredResources()) { - resolved.add(new ObrResourceImpl(res, SubsystemConstants.RESOURCE_TYPE_BUNDLE, new HashMap())); + resolved.add(new ObrResourceImpl(res, SubsystemConstants.RESOURCE_TYPE_BUNDLE, new HashMap())); } return resolved; } Modified: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ResourceImpl.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ResourceImpl.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ResourceImpl.java (original) +++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ResourceImpl.java Thu May 12 11:27:11 2011 @@ -21,8 +21,11 @@ package org.apache.aries.subsystem.obr.i import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.List; import java.util.Map; +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; @@ -32,9 +35,9 @@ public class ResourceImpl implements Res private final Version version; private final String type; private final String location; - private final Map attributes; + private final Map attributes; - public ResourceImpl(String symbolicName, Version version, String type, String location, Map attributes) { + public ResourceImpl(String symbolicName, Version version, String type, String location, Map attributes) { this.symbolicName = symbolicName; this.version = version; this.type = type; @@ -58,7 +61,7 @@ public class ResourceImpl implements Res return location; } - public Map getAttributes() { + public Map getAttributes() { return attributes; } @@ -66,4 +69,14 @@ public class ResourceImpl implements Res return new URL(getLocation()).openStream(); } + public List getCapabilities(String namespace) { + // TODO Auto-generated method stub + return null; + } + + public List getRequirements(String namespace) { + // TODO Auto-generated method stub + return null; + } + } Added: aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/IdGenerator.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/IdGenerator.java?rev=1102244&view=auto ============================================================================== --- aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/IdGenerator.java (added) +++ aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/IdGenerator.java Thu May 12 11:27:11 2011 @@ -0,0 +1,17 @@ +package org.apache.aries.subsystem.scope.impl; + +public class IdGenerator { + private long lastId; + + public IdGenerator() { + this(0); + } + + public IdGenerator(long firstId) { + lastId = firstId; + } + + public synchronized long nextId() { + return lastId++; + } +} Modified: aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/ScopeImpl.java URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/ScopeImpl.java?rev=1102244&r1=1102243&r2=1102244&view=diff ============================================================================== --- aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/ScopeImpl.java (original) +++ aries/trunk/subsystem/subsystem-scope-impl/src/main/java/org/apache/aries/subsystem/scope/impl/ScopeImpl.java Thu May 12 11:27:11 2011 @@ -2,7 +2,6 @@ package org.apache.aries.subsystem.scope import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -12,115 +11,31 @@ import org.apache.aries.subsystem.scope. import org.apache.aries.subsystem.scope.ScopeUpdate; import org.apache.aries.subsystem.scope.SharePolicy; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; public class ScopeImpl implements Scope { - private static class UnmodifiableSharePolicyMap implements Map> { - private final Map> map; - - public UnmodifiableSharePolicyMap(Map> map) { - this.map = map; - } - - public void clear() { - throw new UnsupportedOperationException(); - } - - public boolean containsKey(Object key) { - return map.containsKey(key); - } - - public boolean containsValue(Object value) { - return map.containsValue(value); - } - - public Set>> entrySet() { - Set>> result = new HashSet>>(map.size()); - for (final Map.Entry> entry : map.entrySet()) { - result.add(new Map.Entry>() { - public String getKey() { - return entry.getKey(); - } - - public List getValue() { - return entry.getValue(); - } - - public List setValue(List object) { - throw new UnsupportedOperationException(); - } - }); - } - return Collections.unmodifiableSet(result); - } - - public List get(Object key) { - List result = map.get(key); - return result == null ? null : Collections.unmodifiableList(result); - } - - public boolean isEmpty() { - return map.isEmpty(); - } - - public Set keySet() { - return Collections.unmodifiableSet(map.keySet()); - } - - public List put(String key, List value) { - throw new UnsupportedOperationException(); - } - - public void putAll(Map> map) { - throw new UnsupportedOperationException(); - } - - public List remove(Object key) { - throw new UnsupportedOperationException(); - } - - public int size() { - return map.size(); - } - - public Collection> values() { - return Collections.unmodifiableCollection(map.values()); - } - } + private volatile boolean updating; - private static long lastId = -1; - - private static synchronized long nextId() { - if (lastId == Long.MAX_VALUE) - throw new IllegalStateException("The next ID would exceed Long.MAX_VALUE"); - return ++lastId; - } - - volatile boolean updating; - - long lastUpdate; - - final BundleContext bundleContext; - final Set bundles = Collections.synchronizedSet(new HashSet()); - final Set children = Collections.synchronizedSet(new HashSet()); - final Map> exportPolicies = Collections.synchronizedMap(new HashMap>()); - final Map> importPolicies = Collections.synchronizedMap(new HashMap>()); + private long lastUpdate; + private final Collection bundles = Collections.synchronizedSet(new HashSet()); + private final Set children = Collections.synchronizedSet(new HashSet()); private final long id; private final String location; private final String name; - private final Scope parent; + private final long parentId; + private final Scopes scopes; + private final SharePolicies sharePolicies; - public ScopeImpl( - BundleContext bundleContext, - String name, - String location, - Scope parent) { - this.bundleContext = bundleContext; + public ScopeImpl(long id, String name, String location, long parentId, Collection bundles, SharePolicies sharePolicies, Scopes scopes) { + this.id = id; this.name = name; this.location = location; - this.parent = parent; - id = nextId(); + this.parentId = parentId; + if (bundles != null) { + this.bundles.addAll(bundles); + } + this.sharePolicies = sharePolicies; + this.scopes = scopes; } public Collection getBundles() { @@ -144,18 +59,54 @@ public class ScopeImpl implements Scope } public Scope getParent() { - return parent; + return scopes.getScope(parentId); } public Map> getSharePolicies(String type) { - if (SharePolicy.TYPE_EXPORT.equals(type)) - return new UnmodifiableSharePolicyMap(exportPolicies); - else if (SharePolicy.TYPE_IMPORT.equals(type)) - return new UnmodifiableSharePolicyMap(importPolicies); - throw new IllegalArgumentException(type); + return Collections.unmodifiableMap(sharePolicies.getSharePolicies(type)); } public ScopeUpdate newScopeUpdate() { return ScopeUpdateImpl.newInstance(this); } + + void addBundle(Bundle bundle) { + bundles.add(bundle); + } + + void addChild(ScopeImpl child) { + children.add(child); + } + + synchronized long getLastUpdate() { + return lastUpdate; + } + + Scopes getScopes() { + return scopes; + } + + SharePolicies getSharePolicies() { + return sharePolicies; + } + + boolean isUpdating() { + return updating; + } + + void removeBundle(Bundle bundle) { + bundles.remove(bundle); + } + + void removeChild(ScopeImpl scope) { + children.remove(scope); + } + + synchronized void setLastUpdate(long value) { + lastUpdate = value; + } + + void setUpdating(boolean value) { + updating = value; + } }