aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwr...@apache.org
Subject svn commit: r1417523 - in /aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal: BundleDirectory.java BundleResourceInstaller.java BundleRevisionResource.java
Date Wed, 05 Dec 2012 16:52:52 GMT
Author: jwross
Date: Wed Dec  5 16:52:51 2012
New Revision: 1417523

URL: http://svn.apache.org/viewvc?rev=1417523&view=rev
Log:
Make service capabilities from preexisting root region bundles available to the system repository.

Added:
    aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleDirectory.java
    aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleRevisionResource.java
Modified:
    aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceInstaller.java

Added: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleDirectory.java?rev=1417523&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleDirectory.java
(added)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleDirectory.java
Wed Dec  5 16:52:51 2012
@@ -0,0 +1,223 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.aries.subsystem.core.internal;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.aries.util.filesystem.FileSystem;
+import org.apache.aries.util.filesystem.ICloseableDirectory;
+import org.apache.aries.util.filesystem.IDirectory;
+import org.apache.aries.util.filesystem.IFile;
+import org.osgi.framework.Bundle;
+import org.osgi.service.subsystem.SubsystemException;
+
+public class BundleDirectory implements IDirectory {
+	private static class BundleFile implements IFile {
+		private final BundleDirectory directory;
+		private final String name;
+		private final URL url;
+		
+		public BundleFile(String name, URL url, BundleDirectory directory) {
+			this.name = name;
+			this.url = url;
+			this.directory = directory;
+		}
+
+		@Override
+		public IDirectory convert() {
+			return null;
+		}
+
+		@Override
+		public IDirectory convertNested() {
+			try {
+				return FileSystem.getFSRoot(url.openStream());
+			}
+			catch (IOException e) {
+				throw new SubsystemException(e);
+			}
+		}
+
+		@Override
+		public long getLastModified() {
+			return 0;
+		}
+
+		@Override
+		public String getName() {
+			if (name.startsWith("/"))
+				return name.substring(1);
+			return name;
+		}
+
+		@Override
+		public IDirectory getParent() {
+			return directory;
+		}
+
+		@Override
+		public IDirectory getRoot() {
+			return directory;
+		}
+
+		@Override
+		public long getSize() {
+			return 0;
+		}
+
+		@Override
+		public boolean isDirectory() {
+			return false;
+		}
+
+		@Override
+		public boolean isFile() {
+			return true;
+		}
+
+		@Override
+		public InputStream open() throws IOException,
+		UnsupportedOperationException {
+			return url.openStream();
+		}
+
+		@Override
+		public URL toURL() throws MalformedURLException {
+			return url;
+		}
+	}
+	
+	private final Bundle bundle;
+	
+	public BundleDirectory(Bundle bundle) {
+		if (bundle == null)
+			throw new NullPointerException();
+		this.bundle = bundle;
+	}
+	
+	@Override
+	public Iterator<IFile> iterator() {
+		return listAllFiles().iterator();
+	}
+
+	@Override
+	public IDirectory convert() {
+		return this;
+	}
+
+	@Override
+	public IDirectory convertNested() {
+		return this;
+	}
+
+	@Override
+	public long getLastModified() {
+		return 0;
+	}
+
+	@Override
+	public String getName() {
+		return "";
+	}
+
+	@Override
+	public IDirectory getParent() {
+		return null;
+	}
+
+	@Override
+	public IDirectory getRoot() {
+		return this;
+	}
+
+	@Override
+	public long getSize() {
+		return 0;
+	}
+
+	@Override
+	public boolean isDirectory() {
+		return true;
+	}
+
+	@Override
+	public boolean isFile() {
+		return false;
+	}
+
+	@Override
+	public InputStream open() throws IOException, UnsupportedOperationException {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override
+	public URL toURL() throws MalformedURLException {
+		return bundle.getEntry("/");
+	}
+
+	@Override
+	public IFile getFile(final String name) {
+		if (name == null || name.length() == 0)
+			return null;
+		if ("/".equals(name))
+			return this;
+		URL entry = bundle.getEntry(name);
+		if (entry == null)
+			return null;
+		return new BundleFile(name, entry, this);
+	}
+
+	@Override
+	public boolean isRoot() {
+		return true;
+	}
+
+	@Override
+	public List<IFile> listAllFiles() {
+		return listFiles(true);
+	}
+
+	@Override
+	public List<IFile> listFiles() {
+		return listFiles(false);
+	}
+
+	@Override
+	public ICloseableDirectory toCloseable() {
+		return null;
+	}
+	
+	private List<IFile> listFiles(boolean recurse) {
+		Enumeration<URL> entries = bundle.findEntries("/", null, recurse);
+		if (entries == null)
+			return Collections.emptyList();
+		ArrayList<IFile> files = new ArrayList<IFile>();
+		while (entries.hasMoreElements()) {
+			URL entry = entries.nextElement();
+			if (entry.getPath().endsWith("/"))
+				continue;
+			files.add(new BundleFile(entry.getPath(), entry, this));
+		}
+		files.trimToSize();
+		return files;
+	}
+}

Modified: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceInstaller.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceInstaller.java?rev=1417523&r1=1417522&r2=1417523&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceInstaller.java
(original)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleResourceInstaller.java
Wed Dec  5 16:52:51 2012
@@ -51,7 +51,19 @@ public class BundleResourceInstaller ext
 		private final BundleRevision revision;
 		
 		public BundleConstituent(Resource resource, BundleRevision revision) {
-			this.resource = resource;
+			if (resource instanceof BundleRevision) {
+				try {
+					this.resource = new BundleRevisionResource((BundleRevision)resource);
+				}
+				catch (SubsystemException e) {
+					throw e;
+				}
+				catch (Exception e) {
+					throw new SubsystemException(e);
+				}
+			}
+			else
+				this.resource = resource;
 			this.revision = revision;
 		}
 

Added: aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleRevisionResource.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleRevisionResource.java?rev=1417523&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleRevisionResource.java
(added)
+++ aries/trunk/subsystem/subsystem-core/src/main/java/org/apache/aries/subsystem/core/internal/BundleRevisionResource.java
Wed Dec  5 16:52:51 2012
@@ -0,0 +1,129 @@
+package org.apache.aries.subsystem.core.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.aries.application.modelling.ExportedService;
+import org.apache.aries.application.modelling.ImportedService;
+import org.apache.aries.application.modelling.ModelledResource;
+import org.apache.aries.application.modelling.ModelledResourceManager;
+import org.apache.aries.application.modelling.ModellerException;
+import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.namespace.service.ServiceNamespace;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Namespace;
+import org.osgi.resource.Requirement;
+import org.osgi.resource.Resource;
+import org.osgi.service.subsystem.SubsystemException;
+
+public class BundleRevisionResource implements Resource {
+	private final BundleRevision revision;
+	
+	private volatile ModelledResource resource;
+	
+	public BundleRevisionResource(BundleRevision revision) {
+		if (revision == null)
+			throw new NullPointerException();
+		this.revision = revision;
+	}
+
+	@Override
+	public List<Capability> getCapabilities(String namespace) {
+		if (namespace == null) {
+			List<Capability> rCaps = revision.getCapabilities(namespace);
+			List<Capability> sCaps = computeServiceCapabilities();
+			List<Capability> result = new ArrayList<Capability>(rCaps.size() + sCaps.size());
+			result.addAll(rCaps);
+			result.addAll(sCaps);
+			return Collections.unmodifiableList(result);
+		}
+		if (ServiceNamespace.SERVICE_NAMESPACE.equals(namespace))
+			return Collections.unmodifiableList(computeServiceCapabilities());
+		return revision.getCapabilities(namespace);
+	}
+
+	@Override
+	public List<Requirement> getRequirements(String namespace) {
+		if (namespace == null) {
+			List<Requirement> rReqs = revision.getRequirements(namespace);
+			List<Requirement> sReqs = computeServiceRequirements();
+			List<Requirement> result = new ArrayList<Requirement>(rReqs.size() + sReqs.size());
+			result.addAll(rReqs);
+			result.addAll(sReqs);
+			return Collections.unmodifiableList(result);
+		}
+		if (ServiceNamespace.SERVICE_NAMESPACE.equals(namespace))
+			return Collections.unmodifiableList(computeServiceRequirements());
+		return revision.getRequirements(namespace);
+	}
+	
+	private ModelledResource computeModelledResource() {
+		Activator activator = Activator.getInstance();
+		ModelledResourceManager manager = activator.getModelledResourceManager();
+		BundleDirectory directory = new BundleDirectory(revision.getBundle());
+		try {
+			return manager.getModelledResource(directory);
+		}
+		catch (ModellerException e) {
+			throw new SubsystemException(e);
+		}
+	}
+	
+	private List<Capability> computeServiceCapabilities() {
+		ModelledResource resource = getModelledResource();
+		Collection<? extends ExportedService> services = resource.getExportedServices();
+		if (services.isEmpty())
+			return Collections.emptyList();
+		List<Capability> result = new ArrayList<Capability>(services.size());
+		for (ExportedService service : services)
+			result.add(new BasicCapability.Builder()
+					.namespace(ServiceNamespace.SERVICE_NAMESPACE)
+					.attribute(ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE, new ArrayList<String>(service.getInterfaces()))
+					.attributes(service.getServiceProperties())
+					.resource(this)
+					.build());
+		return result;
+	}
+	
+	private List<Requirement> computeServiceRequirements() {
+		ModelledResource resource = getModelledResource();
+		Collection<? extends ImportedService> services = resource.getImportedServices();
+		if (services.isEmpty())
+			return Collections.emptyList();
+		List<Requirement> result = new ArrayList<Requirement>(services.size());
+		for (ImportedService service : services) {
+			StringBuilder builder = new StringBuilder("(&(")
+					.append(ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE)
+					.append('=')
+					.append(service.getInterface())
+					.append(')');
+			String filter = service.getFilter();
+			if (filter != null)
+				builder.append('(').append(filter).append(')');
+			builder.append(')');
+			result.add(new BasicRequirement.Builder()
+					.namespace(ServiceNamespace.SERVICE_NAMESPACE)
+					.directive(Namespace.REQUIREMENT_FILTER_DIRECTIVE, builder.toString())
+					.directive(
+							Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, 
+							service.isOptional() ? Namespace.RESOLUTION_OPTIONAL : Namespace.RESOLUTION_MANDATORY)
+					.resource(this)
+					.build());
+		}
+		return result;
+	}
+
+	private ModelledResource getModelledResource() {
+		ModelledResource result = resource;
+		if (result == null) {
+			synchronized (this) {
+				result = resource;
+				if (result == null)
+					resource = result = computeModelledResource();
+			}
+		}
+		return result;
+	}
+}



Mime
View raw message