You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by jw...@apache.org on 2012/12/05 17:53:06 UTC
svn commit: r1417524 - in /aries/trunk/subsystem: ./ subsystem-obr/
subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/
Author: jwross
Date: Wed Dec 5 16:53:04 2012
New Revision: 1417524
URL: http://svn.apache.org/viewvc?rev=1417524&view=rev
Log:
Integrate Felix Repository Admin with Subsystems.
The subsystem-obr bundle provides an org.osgi.service.repository.Repository wrapper around registered RepositoryAdmin services.
This makes bundles within Felix OBR available to resolving subsystems.
Added:
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixCapabilityAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixProperty.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRepositoryAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRequirementAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixResourceAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/NamespaceTranslator.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiCapabilityAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiRequirementAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiResourceAdapter.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/RepositoryAdminRepository.java
Removed:
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceImpl.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ObrResourceResolver.java
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/ResourceImpl.java
Modified:
aries/trunk/subsystem/pom.xml
aries/trunk/subsystem/subsystem-obr/pom.xml
aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/Activator.java
Modified: aries/trunk/subsystem/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/pom.xml?rev=1417524&r1=1417523&r2=1417524&view=diff
==============================================================================
--- aries/trunk/subsystem/pom.xml (original)
+++ aries/trunk/subsystem/pom.xml Wed Dec 5 16:53:04 2012
@@ -41,7 +41,8 @@
</activation>
<modules>
<module>subsystem-api</module>
- <module>subsystem-core</module>
+ <module>subsystem-core</module>
+ <module>subsystem-obr</module>
<module>subsystem-itests</module>
<module>subsystem-itests-api-bundle</module>
<module>subsystem-bundle</module>
Modified: aries/trunk/subsystem/subsystem-obr/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/pom.xml?rev=1417524&r1=1417523&r2=1417524&view=diff
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/pom.xml (original)
+++ aries/trunk/subsystem/subsystem-obr/pom.xml Wed Dec 5 16:53:04 2012
@@ -21,8 +21,8 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.aries</groupId>
- <artifactId>java5-parent</artifactId>
- <version>0.4</version>
+ <artifactId>java6-parent</artifactId>
+ <version>1.0.0</version>
<relativePath />
</parent>
@@ -30,8 +30,8 @@
<artifactId>org.apache.aries.subsystem.obr</artifactId>
<packaging>bundle</packaging>
<name>Apache Aries Subsystem OBR</name>
- <version>0.1-SNAPSHOT</version>
- <description>Subsystems OBR support</description>
+ <version>1.0.0-SNAPSHOT</version>
+ <description>Subsystems OBR support.</description>
<scm>
<connection>scm:svn:http://svn.apache.org/repos/asf/aries/trunk/subsystem/subsystem-obr</connection>
@@ -49,25 +49,20 @@
</properties>
<dependencies>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- <scope>provided</scope>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
</dependency>
<dependency>
- <groupId>org.apache.aries.subsystem</groupId>
- <artifactId>org.apache.aries.subsystem.api</artifactId>
- <version>0.1-SNAPSHOT</version>
+ <groupId>org.apache.aries.application</groupId>
+ <artifactId>org.apache.aries.application.utils</artifactId>
+ <version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.aries.subsystem</groupId>
<artifactId>org.apache.aries.subsystem.core</artifactId>
- <version>0.1-SNAPSHOT</version>
+ <version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
@@ -75,20 +70,24 @@
<version>1.6.4</version>
</dependency>
<dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>5.0.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.enterprise</artifactId>
+ <version>5.0.0</version>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
- <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
</dependencies>
</project>
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=1417524&r1=1417523&r2=1417524&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 Wed Dec 5 16:53:04 2012
@@ -18,37 +18,49 @@
*/
package org.apache.aries.subsystem.obr.internal;
-import org.apache.aries.subsystem.core.ResourceResolver;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.repository.Repository;
import org.osgi.util.tracker.ServiceTracker;
public class Activator implements BundleActivator {
+ private final Map<ServiceReference<RepositoryAdmin>, ServiceRegistration<Repository>> registrations = Collections.synchronizedMap(new HashMap<ServiceReference<RepositoryAdmin>, ServiceRegistration<Repository>>());
+
+ private ServiceTracker<RepositoryAdmin, RepositoryAdmin> tracker;
- private BundleContext bundleContext;
- private ServiceRegistration registration;
- private ServiceTracker repositoryAdminTracker;
-
- public void start(BundleContext context) throws Exception {
- bundleContext = context;
- repositoryAdminTracker = new ServiceTracker(bundleContext,
- RepositoryAdmin.class.getName(),
- null) {
- public Object addingService(ServiceReference reference) {
- registration = bundleContext.registerService(ResourceResolver.class.getName(),
- new ObrResourceResolver(repositoryAdminTracker),
- null);
+ @Override
+ public void start(final BundleContext context) {
+ tracker = new ServiceTracker<RepositoryAdmin, RepositoryAdmin>(context, RepositoryAdmin.class.getName(), null) {
+ @Override
+ public RepositoryAdmin addingService(ServiceReference<RepositoryAdmin> reference) {
+ registrations.put(reference, context.registerService(
+ Repository.class,
+ new RepositoryAdminRepository(context.getService(reference)),
+ null));
return super.addingService(reference);
}
+
+ @Override
+ public void removedService(ServiceReference<RepositoryAdmin> reference, RepositoryAdmin service) {
+ ServiceRegistration<Repository> registration = registrations.get(reference);
+ if (registration == null)
+ return;
+ registration.unregister();
+ super.removedService(reference, service);
+ }
};
- repositoryAdminTracker.open();
+ tracker.open();
}
- public void stop(BundleContext context) throws Exception {
- registration.unregister();
- repositoryAdminTracker.close();
+ @Override
+ public void stop(BundleContext context) {
+ tracker.close();
}
}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixCapabilityAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixCapabilityAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixCapabilityAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixCapabilityAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,55 @@
+/*
+ * 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.obr.internal;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.aries.subsystem.core.internal.AbstractCapability;
+import org.osgi.namespace.service.ServiceNamespace;
+import org.osgi.resource.Resource;
+
+public class FelixCapabilityAdapter extends AbstractCapability {
+ private final org.apache.felix.bundlerepository.Capability capability;
+ private final Resource resource;
+
+ public FelixCapabilityAdapter(org.apache.felix.bundlerepository.Capability capability, Resource resource) {
+ if (capability == null)
+ throw new NullPointerException("Missing required parameter: capability");
+ this.capability = capability;
+ this.resource = resource;
+ }
+
+ public Map<String, Object> getAttributes() {
+ Map<String, Object> result = capability.getPropertiesAsMap();
+ String namespace = getNamespace();
+ if (ServiceNamespace.SERVICE_NAMESPACE.equals(namespace))
+ result.put(ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE, result.get("objectclass"));
+ else
+ result.put(namespace, result.get(capability.getName()));
+ return result;
+ }
+
+ public Map<String, String> getDirectives() {
+ return Collections.emptyMap();
+ }
+
+ public String getNamespace() {
+ return NamespaceTranslator.translate(capability.getName());
+ }
+
+ public Resource getResource() {
+ return resource;
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixProperty.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixProperty.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixProperty.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixProperty.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,71 @@
+/*
+ * 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.obr.internal;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.felix.bundlerepository.Property;
+import org.osgi.framework.Version;
+
+public class FelixProperty implements Property {
+ private static Set<?> asSet(List<?> list) {
+ return new HashSet<Object>(list);
+ }
+
+ private final String name;
+ private final Object value;
+
+ public FelixProperty(String name, Object value) {
+ if (name == null)
+ throw new NullPointerException("Missing required parameter: name");
+ if (value == null)
+ throw new NullPointerException("Missing required parameter: value");
+ this.name = name;
+ this.value = value;
+ }
+
+ public FelixProperty(Map.Entry<String, Object> entry) {
+ this(entry.getKey(), entry.getValue());
+ }
+
+ public Object getConvertedValue() {
+ if (value instanceof List)
+ return asSet((List<?>)value);
+ return value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getType() {
+ if (value instanceof Version)
+ return Property.VERSION;
+ if (value instanceof Long)
+ return Property.LONG;
+ if (value instanceof Double)
+ return Property.DOUBLE;
+ if (value instanceof List<?>)
+ return Property.SET;
+ return null;
+ }
+
+ public String getValue() {
+ return String.valueOf(value);
+ }
+
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRepositoryAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRepositoryAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRepositoryAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRepositoryAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,144 @@
+/*
+ * 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.obr.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.aries.subsystem.core.internal.OsgiIdentityCapability;
+import org.apache.aries.subsystem.core.internal.ResourceHelper;
+import org.osgi.framework.Constants;
+import org.osgi.framework.namespace.IdentityNamespace;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+import org.osgi.resource.Resource;
+import org.osgi.service.repository.Repository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class FelixRepositoryAdapter implements Repository {
+ private static class IdentityRequirementFilter {
+ private static final String REGEX = "\\(osgi.identity=([^\\)]*)\\)";
+ private static final Pattern PATTERN = Pattern.compile(REGEX);
+
+ private final String symbolicName;
+
+ public IdentityRequirementFilter(String filter) {
+ Matcher matcher = PATTERN.matcher(filter);
+ if (!matcher.find())
+ throw new IllegalArgumentException("Could not find pattern '" + REGEX + "' in filter string '" + filter + "'");
+ symbolicName = matcher.group(1);
+ }
+
+ public String getSymbolicName() {
+ return symbolicName;
+ }
+ }
+
+ private static final Logger logger = LoggerFactory.getLogger(FelixRepositoryAdapter.class);
+
+ private final Map<String, Collection<Capability>> identityIndex = Collections.synchronizedMap(new HashMap<String, Collection<Capability>>());
+ private final org.apache.felix.bundlerepository.Repository repository;
+
+ private long lastUpdated;
+
+ public FelixRepositoryAdapter(org.apache.felix.bundlerepository.Repository repository) {
+ if (repository == null)
+ throw new NullPointerException("Missing required parameter: repository");
+ this.repository = repository;
+ }
+
+ public Collection<Capability> findProviders(Requirement requirement) {
+ update();
+ List<Capability> result = Collections.emptyList();
+ if (IdentityNamespace.IDENTITY_NAMESPACE.equals(requirement.getNamespace())) {
+ String symbolicName = new IdentityRequirementFilter(requirement.getDirectives().get(Constants.FILTER_DIRECTIVE)).getSymbolicName();
+ logger.debug("Looking for symbolic name {}", symbolicName);
+ Collection<Capability> capabilities = identityIndex.get(symbolicName);
+ if (capabilities != null) {
+ result = new ArrayList<Capability>(capabilities.size());
+ for (Capability capability : capabilities) {
+ if (ResourceHelper.matches(requirement, capability)) {
+ result.add(capability);
+ }
+ }
+ ((ArrayList<Capability>)result).trimToSize();
+ }
+ }
+ else {
+ org.apache.felix.bundlerepository.Resource[] resources = repository.getResources();
+ if (resources != null && resources.length != 0) {
+ result = new ArrayList<Capability>(resources.length);
+ for (final org.apache.felix.bundlerepository.Resource resource : resources) {
+ Resource r = new FelixResourceAdapter(resource);
+ for (Capability capability : r.getCapabilities(requirement.getNamespace()))
+ if (ResourceHelper.matches(requirement, capability))
+ result.add(capability);
+ }
+ ((ArrayList<Capability>)result).trimToSize();
+ }
+ }
+ return result;
+ }
+
+ @Override
+ public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
+ Map<Requirement, Collection<Capability>> result = new HashMap<Requirement, Collection<Capability>>(requirements.size());
+ for (Requirement requirement : requirements)
+ result.put(requirement, findProviders(requirement));
+ return result;
+ }
+
+ private synchronized void update() {
+ long lastModified = repository.getLastModified();
+ logger.debug("The repository adaptor was last updated at {}. The repository was last modified at {}", lastUpdated, lastModified);
+ if (lastModified > lastUpdated) {
+ logger.debug("Updating the adapter with the modified repository contents...");
+ lastUpdated = lastModified;
+ synchronized (identityIndex) {
+ identityIndex.clear();
+ org.apache.felix.bundlerepository.Resource[] resources = repository.getResources();
+ logger.debug("There are {} resources to evaluate", resources == null ? 0 : resources.length);
+ if (resources != null && resources.length != 0) {
+ for (org.apache.felix.bundlerepository.Resource resource : resources) {
+ logger.debug("Evaluating resource {}", resource);
+ String symbolicName = resource.getSymbolicName();
+ Collection<Capability> capabilities = identityIndex.get(symbolicName);
+ if (capabilities == null) {
+ capabilities = new HashSet<Capability>();
+ identityIndex.put(symbolicName, capabilities);
+ }
+ OsgiIdentityCapability capability =
+ new OsgiIdentityCapability(
+ new FelixResourceAdapter(resource),
+ symbolicName,
+ resource.getVersion(),
+ // TODO Assuming all resources are bundles. Need to support
+ // type fragment as well, but how do we know?
+ IdentityNamespace.TYPE_BUNDLE);
+ logger.debug("Indexing capability {}", capability);
+ capabilities.add(capability);
+ }
+ }
+ }
+ }
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRequirementAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRequirementAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRequirementAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixRequirementAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,66 @@
+/*
+ * 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.obr.internal;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.aries.subsystem.core.internal.AbstractRequirement;
+import org.osgi.framework.Constants;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Resource;
+
+public class FelixRequirementAdapter extends AbstractRequirement {
+ private final org.apache.felix.bundlerepository.Requirement requirement;
+ private final Resource resource;
+
+ public FelixRequirementAdapter(org.apache.felix.bundlerepository.Requirement requirement, Resource resource) {
+ if (requirement == null)
+ throw new NullPointerException("Missing required parameter: requirement");
+ if (resource == null)
+ throw new NullPointerException("Missing required parameter: resource");
+ this.requirement = requirement;
+ this.resource = resource;
+ }
+
+ public Map<String, Object> getAttributes() {
+ return Collections.emptyMap();
+ }
+
+ public Map<String, String> getDirectives() {
+ Map<String, String> result = new HashMap<String, String>(1);
+ /* (1) The Felix OBR specific "mandatory:<*" syntax must be stripped out of the filter.
+ * (2) The namespace must be translated.
+ */
+ result.put(Constants.FILTER_DIRECTIVE, requirement.getFilter()
+ .replaceAll("\\(mandatory\\:\\<\\*[^\\)]*\\)", "")
+ .replaceAll("\\(service\\=[^\\)]*\\)", "")
+ .replaceAll("objectclass", "objectClass")
+ .replaceAll(requirement.getName() + '=', getNamespace() + '='));
+ return result;
+ }
+
+ public String getNamespace() {
+ return NamespaceTranslator.translate(requirement.getName());
+ }
+
+ public Resource getResource() {
+ return resource;
+ }
+
+ public boolean matches(Capability capability) {
+ return requirement.isSatisfied(new OsgiCapabilityAdapter(capability));
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixResourceAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixResourceAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixResourceAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/FelixResourceAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,119 @@
+/*
+ * 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.obr.internal;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.aries.subsystem.core.internal.OsgiContentCapability;
+import org.apache.aries.subsystem.core.internal.OsgiIdentityCapability;
+import org.apache.aries.subsystem.core.internal.ResourceHelper;
+import org.osgi.framework.namespace.IdentityNamespace;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+import org.osgi.resource.Resource;
+import org.osgi.service.repository.RepositoryContent;
+
+public class FelixResourceAdapter implements Resource, RepositoryContent {
+ private final org.apache.felix.bundlerepository.Resource resource;
+
+ public FelixResourceAdapter(final org.apache.felix.bundlerepository.Resource resource) {
+ this.resource = resource;
+ }
+
+ public boolean equals(Object o) {
+ if (o == this)
+ return true;
+ if (!(o instanceof Resource))
+ return false;
+ Resource that = (Resource)o;
+ if (!ResourceHelper.getTypeAttribute(that).equals(ResourceHelper.getTypeAttribute(this)))
+ return false;
+ if (!ResourceHelper.getSymbolicNameAttribute(that).equals(ResourceHelper.getSymbolicNameAttribute(this)))
+ return false;
+ if (!ResourceHelper.getVersionAttribute(that).equals(ResourceHelper.getVersionAttribute(this)))
+ return false;
+ return true;
+ }
+
+ public int hashCode() {
+ int result = 17;
+ result = 31 * result + ResourceHelper.getTypeAttribute(this).hashCode();
+ result = 31 * result + ResourceHelper.getSymbolicNameAttribute(this).hashCode();
+ result = 31 * result + ResourceHelper.getVersionAttribute(this).hashCode();
+ return result;
+ }
+
+ public List<Capability> getCapabilities(String namespace) {
+ namespace = NamespaceTranslator.translate(namespace);
+ if (namespace == null || namespace.equals(IdentityNamespace.IDENTITY_NAMESPACE)) {
+ Capability c = new OsgiIdentityCapability(this, resource.getSymbolicName(), resource.getVersion());
+ return Collections.singletonList(c);
+ }
+ // TODO Add to constants.
+ if (namespace.equals("osgi.content")) {
+ Capability c = new OsgiContentCapability(this, resource.getURI());
+ return Collections.singletonList(c);
+ }
+ org.apache.felix.bundlerepository.Capability[] capabilities = resource.getCapabilities();
+ ArrayList<Capability> result = new ArrayList<Capability>(capabilities.length);
+ for (org.apache.felix.bundlerepository.Capability capability : capabilities) {
+ if (namespace != null && !capability.getName().equals(namespace)) continue;
+ result.add(new FelixCapabilityAdapter(capability, this));
+ }
+ result.trimToSize();
+ return result;
+ }
+
+ @Override
+ public InputStream getContent() {
+ try {
+ return new URL(resource.getURI()).openStream();
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public List<Requirement> getRequirements(String namespace) {
+ namespace = NamespaceTranslator.translate(namespace);
+ org.apache.felix.bundlerepository.Requirement[] requirements = resource.getRequirements();
+ ArrayList<Requirement> result = new ArrayList<Requirement>(requirements.length);
+ for (final org.apache.felix.bundlerepository.Requirement requirement : requirements) {
+ if (namespace == null || requirement.getName().equals(namespace))
+ result.add(new FelixRequirementAdapter(requirement, this));
+ }
+ result.trimToSize();
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ Capability c = getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE)
+ .iterator().next();
+ Map<String, Object> atts = c.getAttributes();
+ return new StringBuilder()
+ .append(atts.get(IdentityNamespace.IDENTITY_NAMESPACE))
+ .append(';')
+ .append(atts
+ .get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE))
+ .append(';')
+ .append(atts.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE))
+ .toString();
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/NamespaceTranslator.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/NamespaceTranslator.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/NamespaceTranslator.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/NamespaceTranslator.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,47 @@
+/*
+ * 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.obr.internal;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.namespace.BundleNamespace;
+import org.osgi.framework.namespace.HostNamespace;
+import org.osgi.framework.namespace.PackageNamespace;
+import org.osgi.namespace.service.ServiceNamespace;
+
+public class NamespaceTranslator {
+ private static final Map<String, String> namespaces = computeNamespaces();
+
+ private static Map<String, String> computeNamespaces() {
+ Map<String, String> result = new HashMap<String, String>(8);
+ result.put(PackageNamespace.PACKAGE_NAMESPACE, org.apache.felix.bundlerepository.Capability.PACKAGE);
+ result.put(ServiceNamespace.SERVICE_NAMESPACE, org.apache.felix.bundlerepository.Capability.SERVICE);
+ result.put(BundleNamespace.BUNDLE_NAMESPACE, org.apache.felix.bundlerepository.Capability.BUNDLE);
+ result.put(HostNamespace.HOST_NAMESPACE, org.apache.felix.bundlerepository.Capability.FRAGMENT);
+ result.put(org.apache.felix.bundlerepository.Capability.PACKAGE, PackageNamespace.PACKAGE_NAMESPACE);
+ result.put(org.apache.felix.bundlerepository.Capability.SERVICE, ServiceNamespace.SERVICE_NAMESPACE);
+ result.put(org.apache.felix.bundlerepository.Capability.BUNDLE, BundleNamespace.BUNDLE_NAMESPACE);
+ result.put(org.apache.felix.bundlerepository.Capability.FRAGMENT, HostNamespace.HOST_NAMESPACE);
+ return Collections.unmodifiableMap(result);
+ }
+
+ public static String translate(String namespace) {
+ String result = namespaces.get(namespace);
+ if (result == null)
+ result = namespace;
+ return result;
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiCapabilityAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiCapabilityAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiCapabilityAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiCapabilityAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,66 @@
+/*
+ * 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.obr.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.bundlerepository.Capability;
+import org.apache.felix.bundlerepository.Property;
+
+public class OsgiCapabilityAdapter implements Capability {
+ private final org.osgi.resource.Capability capability;
+
+ public OsgiCapabilityAdapter(org.osgi.resource.Capability capability) {
+ if (capability == null)
+ throw new NullPointerException("Missing required parameter: capability");
+ this.capability = capability;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return capability.equals(o);
+ }
+
+ public String getName() {
+ return NamespaceTranslator.translate(capability.getNamespace());
+ }
+
+ public Property[] getProperties() {
+ Map<String, Object> attributes = capability.getAttributes();
+ Collection<Property> result = new ArrayList<Property>(attributes.size());
+ for (final Map.Entry<String, Object> entry : capability.getAttributes().entrySet()) {
+ if (entry.getKey().equals(capability.getNamespace())) {
+ result.add(new FelixProperty(getName(), entry.getValue()));
+ continue;
+ }
+ result.add(new FelixProperty(entry));
+ }
+ return result.toArray(new Property[result.size()]);
+ }
+
+ @SuppressWarnings("rawtypes")
+ public Map getPropertiesAsMap() {
+ Map<String, Object> result = new HashMap<String, Object>(capability.getAttributes());
+ result.put(getName(), result.get(capability.getNamespace()));
+ return result;
+ }
+
+ @Override
+ public int hashCode() {
+ return capability.hashCode();
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiRequirementAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiRequirementAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiRequirementAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiRequirementAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,69 @@
+/*
+ * 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.obr.internal;
+
+import static org.apache.aries.application.utils.AppConstants.LOG_ENTRY;
+import static org.apache.aries.application.utils.AppConstants.LOG_EXIT;
+
+import org.apache.aries.subsystem.core.internal.ResourceHelper;
+import org.apache.felix.bundlerepository.Capability;
+import org.apache.felix.bundlerepository.Requirement;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OsgiRequirementAdapter implements Requirement {
+ private static final Logger logger = LoggerFactory.getLogger(OsgiRequirementAdapter.class);
+
+ private final org.osgi.resource.Requirement requirement;
+
+ public OsgiRequirementAdapter(org.osgi.resource.Requirement requirement) {
+ if (requirement == null)
+ throw new NullPointerException("Missing required parameter: requirement");
+ this.requirement = requirement;
+ }
+
+ public String getComment() {
+ return null;
+ }
+
+ public String getFilter() {
+ return requirement.getDirectives().get(Constants.FILTER_DIRECTIVE);
+ }
+
+ public String getName() {
+ return NamespaceTranslator.translate(requirement.getNamespace());
+ }
+
+ public boolean isExtend() {
+ return false;
+ }
+
+ public boolean isMultiple() {
+ return false;
+ }
+
+ public boolean isOptional() {
+ String resolution = requirement.getDirectives().get(Constants.RESOLUTION_DIRECTIVE);
+ return Constants.RESOLUTION_OPTIONAL.equals(resolution);
+ }
+
+ public boolean isSatisfied(Capability capability) {
+ logger.debug(LOG_ENTRY, "isSatisfied", capability);
+ boolean result = ResourceHelper.matches(requirement, new FelixCapabilityAdapter(capability, null));
+ logger.debug(LOG_EXIT, "isSatisfied", result);
+ return result;
+ }
+
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiResourceAdapter.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiResourceAdapter.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiResourceAdapter.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/OsgiResourceAdapter.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,90 @@
+/*
+ * 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.obr.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.aries.subsystem.core.internal.ResourceHelper;
+import org.apache.felix.bundlerepository.Capability;
+import org.apache.felix.bundlerepository.Requirement;
+import org.apache.felix.bundlerepository.Resource;
+import org.osgi.framework.Version;
+
+// TODO Need to distinguish between resources that have already been deployed (local) and those that have not.
+public class OsgiResourceAdapter implements Resource {
+ private final org.osgi.resource.Resource resource;
+
+ public OsgiResourceAdapter(org.osgi.resource.Resource resource) {
+ if (resource == null)
+ throw new NullPointerException("Missing required parameter: resource");
+ this.resource = resource;
+ }
+
+ public Capability[] getCapabilities() {
+ Collection<org.osgi.resource.Capability> capabilities = resource.getCapabilities(null);
+ Collection<Capability> result = new ArrayList<Capability>(capabilities.size());
+ for (org.osgi.resource.Capability capability : capabilities)
+ result.add(new OsgiCapabilityAdapter(capability));
+ return result.toArray(new Capability[result.size()]);
+ }
+
+ public String[] getCategories() {
+ return new String[0];
+ }
+
+ public String getId() {
+ String symbolicName = ResourceHelper.getSymbolicNameAttribute(resource);
+ Version version = ResourceHelper.getVersionAttribute(resource);
+ return symbolicName + ";version=" + version;
+ }
+
+ public String getPresentationName() {
+ return ResourceHelper.getSymbolicNameAttribute(resource);
+ }
+
+ public Map getProperties() {
+ return Collections.emptyMap();
+ }
+
+ public Requirement[] getRequirements() {
+ Collection<org.osgi.resource.Requirement> requirements = resource.getRequirements(null);
+ Collection<Requirement> result = new ArrayList<Requirement>(requirements.size());
+ for (org.osgi.resource.Requirement requirement : requirements)
+ result.add(new OsgiRequirementAdapter(requirement));
+ return result.toArray(new Requirement[result.size()]);
+ }
+
+ public Long getSize() {
+ return -1L;
+ }
+
+ public String getSymbolicName() {
+ return ResourceHelper.getSymbolicNameAttribute(resource);
+ }
+
+ public String getURI() {
+ return ResourceHelper.getContentAttribute(resource);
+ }
+
+ public Version getVersion() {
+ return ResourceHelper.getVersionAttribute(resource);
+ }
+
+ public boolean isLocal() {
+ return false;
+ }
+}
Added: aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/RepositoryAdminRepository.java
URL: http://svn.apache.org/viewvc/aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/RepositoryAdminRepository.java?rev=1417524&view=auto
==============================================================================
--- aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/RepositoryAdminRepository.java (added)
+++ aries/trunk/subsystem/subsystem-obr/src/main/java/org/apache/aries/subsystem/obr/internal/RepositoryAdminRepository.java Wed Dec 5 16:53:04 2012
@@ -0,0 +1,90 @@
+/*
+ * 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.obr.internal;
+
+import static org.apache.aries.application.utils.AppConstants.LOG_ENTRY;
+import static org.apache.aries.application.utils.AppConstants.LOG_EXIT;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.bundlerepository.RepositoryAdmin;
+import org.apache.felix.bundlerepository.Resource;
+import org.osgi.framework.namespace.IdentityNamespace;
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+import org.osgi.service.repository.Repository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RepositoryAdminRepository implements Repository {
+ private static final Logger logger = LoggerFactory.getLogger(RepositoryAdminRepository.class);
+
+ private final RepositoryAdmin repositoryAdmin;
+
+ public RepositoryAdminRepository(RepositoryAdmin repositoryAdmin) {
+ this.repositoryAdmin = repositoryAdmin;
+ }
+
+ public Collection<Capability> findProviders(Requirement requirement) {
+ logger.debug(LOG_ENTRY, "findProviders", requirement);
+ Collection<Capability> result = Collections.emptyList();
+ if (IdentityNamespace.IDENTITY_NAMESPACE.equals(requirement.getNamespace())) {
+ result = new ArrayList<Capability>();
+ for (org.apache.felix.bundlerepository.Repository r : repositoryAdmin.listRepositories()) {
+ FelixRepositoryAdapter repository = new FelixRepositoryAdapter(r);
+ Map<Requirement, Collection<Capability>> map = repository.findProviders(Arrays.asList(requirement));
+ Collection<Capability> capabilities = map.get(requirement);
+ if (capabilities != null)
+ result.addAll(capabilities);
+ }
+ return result;
+ }
+ else {
+ Resource[] resources = repositoryAdmin.discoverResources(
+ new org.apache.felix.bundlerepository.Requirement[]{
+ new OsgiRequirementAdapter(requirement)});
+ logger.debug("Found {} resources with capabilities satisfying {}", resources == null ? 0 : resources.length, requirement);
+ if (resources != null && resources.length != 0) {
+ result = new ArrayList<Capability>(result.size());
+ OsgiRequirementAdapter adapter = new OsgiRequirementAdapter(requirement);
+ for (Resource resource : resources) {
+ logger.debug("Evaluating resource {}", resource);
+ for (org.apache.felix.bundlerepository.Capability capability : resource.getCapabilities()) {
+ logger.debug("Evaluating capability {}", capability);
+ if (adapter.isSatisfied(capability)) {
+ logger.debug("Adding capability {}", capability);
+ result.add(new FelixCapabilityAdapter(capability, new FelixResourceAdapter(resource)));
+ }
+ }
+ }
+ }
+ }
+ logger.debug(LOG_EXIT, "findProviders", result);
+ return result;
+
+ }
+
+ @Override
+ public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
+ Map<Requirement, Collection<Capability>> result = new HashMap<Requirement, Collection<Capability>>(requirements.size());
+ for (Requirement requirement : requirements)
+ result.put(requirement, findProviders(requirement));
+ return result;
+ }
+}