Author: davidb Date: Thu May 1 15:22:19 2014 New Revision: 1591679 URL: http://svn.apache.org/r1591679 Log: [FELIX-4370] Additional work to support the OSGi Repository Service.
Improved the mapping between OBR and OSGi namespaces. This also fixes an OSGi Repository CT failure: RepositoryTest.testRepositoryContent() Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixCapabilityAdapter.java felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixResourceAdapter.java felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixCapabilityAdapter.java URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixCapabilityAdapter.java?rev=1591679&r1=1591678&r2=1591679&view=diff ============================================================================== --- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixCapabilityAdapter.java (original) +++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixCapabilityAdapter.java Thu May 1 15:22:19 2014 @@ -15,8 +15,11 @@ package org.apache.felix.bundlerepositor import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.osgi.framework.namespace.BundleNamespace; +import org.osgi.framework.namespace.PackageNamespace; import org.osgi.resource.Capability; import org.osgi.resource.Resource; @@ -39,17 +42,42 @@ public class FelixCapabilityAdapter impl if (convertedAttributes == null) { Map<String, Object> orgMap = capability.getPropertiesAsMap(); - HashMap<String, Object> converted = new HashMap<String, Object>(orgMap.size()); + HashMap<String, Object> converted = new HashMap<String, Object>(orgMap.size() + 2); for (Map.Entry<String, Object> entry : orgMap.entrySet()) { converted.put(NamespaceTranslator.getOSGiNamespace(entry.getKey()), entry.getValue()); } - convertedAttributes = converted; // Cache the result + + if (BundleNamespace.BUNDLE_NAMESPACE.equals(getNamespace())) + { + defaultAttribute(orgMap, converted, BundleNamespace.BUNDLE_NAMESPACE, + orgMap.get(org.apache.felix.bundlerepository.Resource.SYMBOLIC_NAME)); + defaultAttribute(orgMap, converted, BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, + orgMap.get(org.apache.felix.bundlerepository.Resource.VERSION)); + } + else if (PackageNamespace.PACKAGE_NAMESPACE.equals(getNamespace())) + { + Capability bundleCap = getBundleCapability(); + if (bundleCap != null) + { + defaultAttribute(orgMap, converted, PackageNamespace.CAPABILITY_BUNDLE_SYMBOLICNAME_ATTRIBUTE, + bundleCap.getAttributes().get(BundleNamespace.BUNDLE_NAMESPACE)); + defaultAttribute(orgMap, converted, PackageNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, + bundleCap.getAttributes().get(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE)); + } + } + convertedAttributes = converted; } return convertedAttributes; } + private void defaultAttribute(Map<String, Object> orgMap, Map<String, Object> converted, String newAttr, Object defVal) + { + if (converted.get(newAttr) == null) + converted.put(newAttr, defVal); + } + public Map<String, String> getDirectives() { return Collections.emptyMap(); @@ -65,6 +93,18 @@ public class FelixCapabilityAdapter impl return resource; } + private Capability getBundleCapability() + { + if (resource == null) + return null; + + List<Capability> caps = resource.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE); + if (caps.size() > 0) + return caps.get(0); + else + return null; + } + @Override public boolean equals(Object o) { Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixResourceAdapter.java URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixResourceAdapter.java?rev=1591679&r1=1591678&r2=1591679&view=diff ============================================================================== --- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixResourceAdapter.java (original) +++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixResourceAdapter.java Thu May 1 15:22:19 2014 @@ -16,7 +16,6 @@ package org.apache.felix.bundlerepositor import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -38,25 +37,23 @@ public class FelixResourceAdapter implem public List<Capability> getCapabilities(String namespace) { + ArrayList<Capability> result = new ArrayList<Capability>(); + if (namespace == null || namespace.equals(IdentityNamespace.IDENTITY_NAMESPACE)) { - // TODO cater for null request - Object type = resource.getProperties().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE); OSGiCapabilityImpl c = OSGiRepositoryImpl.newOSGiIdentityCapability(resource); c.setResource(this); - return Collections.<Capability>singletonList(c); + result.add(c); } - if (namespace.equals(ContentNamespace.CONTENT_NAMESPACE)) + if (namespace == null || namespace.equals(ContentNamespace.CONTENT_NAMESPACE)) { - // TODO cater for null request OSGiCapabilityImpl c = OSGiRepositoryImpl.newOSGiContentCapability(resource.getURI(), resource.getSize()); c.setResource(this); - return Collections.<Capability>singletonList(c); + result.add(c); } namespace = NamespaceTranslator.getFelixNamespace(namespace); 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)) Modified: felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java?rev=1591679&r1=1591678&r2=1591679&view=diff ============================================================================== --- felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java (original) +++ felix/trunk/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java Thu May 1 15:22:19 2014 @@ -25,7 +25,6 @@ import java.security.DigestInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -41,7 +40,6 @@ import org.osgi.framework.namespace.Iden import org.osgi.resource.Capability; import org.osgi.resource.Namespace; import org.osgi.resource.Requirement; -import org.osgi.resource.Resource; import org.osgi.service.repository.ContentNamespace; import org.osgi.service.repository.Repository; @@ -107,28 +105,21 @@ class OSGiRepositoryImpl implements Repo private void addResourceForIdentity(final org.apache.felix.bundlerepository.Resource res, Filter filter, List<Capability> caps) throws Exception { - OSGiCapabilityImpl idCap = newOSGiIdentityCapability(res); + List<Capability> idCaps = new FelixResourceAdapter(res).getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE); + if (idCaps.size() == 0) + return; + + Capability idCap = idCaps.get(0); // there should only be one osgi.identity anyway if (filter != null) { if (!filter.matches(idCap.getAttributes())) return; } - - OSGiCapabilityImpl contentCap = newOSGiContentCapability(res.getURI(), res.getSize()); - - List<OSGiCapabilityImpl> capabilities = Arrays.<OSGiCapabilityImpl>asList(idCap, contentCap); - Resource resource = - new OSGiResourceImpl(capabilities, Collections.<Requirement>emptyList()); - - for (OSGiCapabilityImpl c : capabilities) - { - c.setResource(resource); - } - caps.add(idCap); } - static OSGiCapabilityImpl newOSGiIdentityCapability(org.apache.felix.bundlerepository.Resource res) { + static OSGiCapabilityImpl newOSGiIdentityCapability(org.apache.felix.bundlerepository.Resource res) + { @SuppressWarnings("unchecked") Map<String, Object> idAttrs = new HashMap<String, Object>(res.getProperties()); @@ -144,7 +135,8 @@ class OSGiRepositoryImpl implements Repo static OSGiCapabilityImpl newOSGiContentCapability(final String uri, long size) { LazyValue<String, Object> lazyValue = - new LazyValue<String, Object>(ContentNamespace.CONTENT_NAMESPACE, new Callable<Object>() { + new LazyValue<String, Object>(ContentNamespace.CONTENT_NAMESPACE, new Callable<Object>() + { public Object call() throws Exception { // This is expensive to do, so only compute it when actually obtained... Modified: felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java URL: http://svn.apache.org/viewvc/felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java?rev=1591679&r1=1591678&r2=1591679&view=diff ============================================================================== --- felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java (original) +++ felix/trunk/bundlerepository/src/test/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImplTest.java Thu May 1 15:22:19 2014 @@ -34,7 +34,9 @@ import org.mockito.Mockito; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Version; +import org.osgi.framework.namespace.BundleNamespace; import org.osgi.framework.namespace.IdentityNamespace; +import org.osgi.framework.namespace.PackageNamespace; import org.osgi.resource.Capability; import org.osgi.resource.Requirement; import org.osgi.resource.Resource; @@ -44,7 +46,7 @@ import org.osgi.service.repository.Repos public class OSGiRepositoryImplTest extends TestCase { - public void testIdentityAndContentCapabilities() throws Exception + public void testCapabilities() throws Exception { RepositoryAdminImpl repoAdmin = createRepositoryAdmin(); URL url = getClass().getResource("/another_repository.xml"); @@ -74,7 +76,10 @@ public class OSGiRepositoryImplTest exte assertEquals(0, res.getRequirements(null).size()); assertEquals(1, res.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).size()); assertEquals(1, res.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).size()); - assertEquals(2, res.getCapabilities(null).size()); + assertEquals(1, res.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE).size()); + assertEquals(8, res.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE).size()); + assertEquals(1, res.getCapabilities("foo").size()); + assertEquals(12, res.getCapabilities(null).size()); Capability contentCap = res.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).iterator().next(); assertEquals("4b68ab3847feda7d6c62c1fbcbeebfa35eab7351ed5e78f4ddadea5df64b8015", @@ -83,6 +88,21 @@ public class OSGiRepositoryImplTest exte contentCap.getAttributes().get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE)); assertEquals(1L, contentCap.getAttributes().get(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE)); assertEquals("application/vnd.osgi.bundle", contentCap.getAttributes().get(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE)); + + Capability bundleCap = res.getCapabilities(BundleNamespace.BUNDLE_NAMESPACE).iterator().next(); + assertEquals("2", bundleCap.getAttributes().get("manifestversion")); + assertEquals("dummy", bundleCap.getAttributes().get(BundleNamespace.BUNDLE_NAMESPACE)); + assertEquals(Version.parseVersion("1.0.0.SNAPSHOT"), bundleCap.getAttributes().get(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE)); + assertEquals("Unnamed - dummy", bundleCap.getAttributes().get("presentationname")); + + Capability packageCap = res.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE).get(7); + assertEquals("org.apache.commons.logging", packageCap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE)); + assertEquals(Version.parseVersion("1.0.4"), packageCap.getAttributes().get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE)); + assertEquals("dummy", packageCap.getAttributes().get(PackageNamespace.CAPABILITY_BUNDLE_SYMBOLICNAME_ATTRIBUTE)); + assertEquals(Version.parseVersion("1.0.0.SNAPSHOT"), packageCap.getAttributes().get(PackageNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE)); + + Capability fooCap = res.getCapabilities("foo").iterator().next(); + assertEquals("someVal", fooCap.getAttributes().get("someKey")); } public void testIdentityCapabilityFilter() throws Exception