Permalink
Browse files

Allow nsvalue in filter if not found as attribute on the requirement

  • Loading branch information...
tdiesler committed May 23, 2012
1 parent c9d90ea commit 2d560d55ee031f0b59e08c1e1fc0a3a5b4590bc1
@@ -77,4 +77,7 @@
@Message(id = 20512, value = "Cannot write repository element")
IllegalStateException illegalStateCannotWriteRepositoryElement(@Cause Throwable th);
@Message(id = 20513, value = "Invalid filter directive: %s")
IllegalArgumentException illegalArgumentInvalidFilterDirective(String filter);
}
@@ -0,0 +1,87 @@
/*
* #%L
* JBossOSGi Resolver API
* %%
* Copyright (C) 2010 - 2012 JBoss by Red Hat
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.jboss.osgi.repository;
import static org.jboss.osgi.resolver.XResourceConstants.MAVEN_IDENTITY_NAMESPACE;
import static org.jboss.osgi.resolver.XResourceConstants.MODULE_IDENTITY_NAMESPACE;
import java.util.Collections;
import java.util.Map;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.osgi.resolver.MavenCoordinates;
import org.jboss.osgi.resolver.XRequirement;
import org.jboss.osgi.resolver.XResource;
import org.jboss.osgi.resolver.XResourceBuilder;
import org.jboss.osgi.resolver.XResourceBuilderFactory;
/**
* A builder for resource requirements
*
* @author thomas.diesler@jboss.com
* @since 16-Jan-2012
*/
public final class XRequirementBuilder {
private final XResourceBuilder builder;
private final XRequirement requirement;
public static XRequirementBuilder create(ModuleIdentifier moduleId) {
return create(MODULE_IDENTITY_NAMESPACE, moduleId.toString());
}
public static XRequirementBuilder create(MavenCoordinates coordinates) {
return create(MAVEN_IDENTITY_NAMESPACE, coordinates.toExternalForm());
}
public static XRequirementBuilder create(String namespace) {
return create(namespace, null);
}
public static XRequirementBuilder create(String namespace, String value) {
XResourceBuilder builder = XResourceBuilderFactory.create();
Map<String, Object> attr = value != null ? Collections.singletonMap(namespace, (Object)value) : null;
XRequirement requirement = builder.addGenericRequirement(namespace, attr, null);
return new XRequirementBuilder(builder, requirement);
}
private XRequirementBuilder(XResourceBuilder builder, XRequirement requirement) {
this.builder = builder;
this.requirement = requirement;
}
public Map<String, Object> getAttributes() {
return requirement.getAttributes();
}
public Map<String, String> getDirectives() {
return requirement.getDirectives();
}
public XRequirement getRequirement() {
XResource resource = builder.getResource();
String namespace = requirement.getNamespace();
return (XRequirement) resource.getRequirements(namespace).get(0);
}
}
@@ -39,6 +39,10 @@
import org.jboss.osgi.repository.XRepository;
import org.jboss.osgi.resolver.XIdentityCapability;
import org.jboss.osgi.resolver.XResource;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.namespace.AbstractWiringNamespace;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
@@ -182,20 +186,44 @@ public boolean removeResource(XResource res) throws RepositoryStorageException {
/**
* Create a cache key from the given capability
*/
static CacheKey create(Capability capability) {
String namespace = capability.getNamespace();
return new CacheKey(namespace, (String) capability.getAttributes().get(namespace));
static CacheKey create(Capability cap) {
String namespace = cap.getNamespace();
return new CacheKey(namespace, (String) cap.getAttributes().get(namespace));
}
/**
* Create a cache key from the given requirement
*/
static CacheKey create(Requirement requirement) {
String namespace = requirement.getNamespace();
return new CacheKey(namespace, (String) requirement.getAttributes().get(namespace));
static CacheKey create(Requirement req) {
String namespace = req.getNamespace();
String value = (String) req.getAttributes().get(namespace);
if (value == null) {
Filter filter = getRequiredFilter(req);
String filterstr = filter.toString();
int index = filterstr.indexOf("(" + namespace + "=");
if (index >= 0) {
value = filterstr.substring(index + namespace.length() + 2);
index = value.indexOf(")");
value = value.substring(0, index);
}
}
return new CacheKey(namespace, value);
}
private static Filter getRequiredFilter(Requirement req) {
String filterdir = req.getDirectives().get(AbstractWiringNamespace.REQUIREMENT_FILTER_DIRECTIVE);
try {
return FrameworkUtil.createFilter("" + filterdir);
} catch (InvalidSyntaxException e) {
throw MESSAGES.illegalArgumentInvalidFilterDirective(filterdir);
}
}
private CacheKey(String namespace, String value) {
if (namespace == null)
throw MESSAGES.illegalArgumentNull("namespace");
if (value == null)
throw MESSAGES.illegalArgumentNull("value");
key = namespace + ":" + value;
}
@@ -31,11 +31,11 @@
import org.jboss.osgi.repository.RepositoryReader;
import org.jboss.osgi.repository.RepositoryStorage;
import org.jboss.osgi.repository.XRepository;
import org.jboss.osgi.repository.XRequirementBuilder;
import org.jboss.osgi.repository.core.FileBasedRepositoryStorage;
import org.jboss.osgi.resolver.XCapability;
import org.jboss.osgi.resolver.XPackageCapability;
import org.jboss.osgi.resolver.XRequirement;
import org.jboss.osgi.resolver.XRequirementBuilder;
import org.jboss.osgi.resolver.XResource;
import org.jboss.osgi.spi.OSGiManifestBuilder;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -110,7 +110,7 @@ private void verifyResource(XResource resource) throws MalformedURLException {
URL fileURL = new URL((String) ccap.getAttribute(ContentNamespace.CAPABILITY_URL_ATTRIBUTE));
Assert.assertTrue("File exists: " + fileURL, new File(fileURL.getPath()).exists());
XRequirement req = XRequirementBuilder.createRequirement(PackageNamespace.PACKAGE_NAMESPACE, "org.acme.foo");
XRequirement req = XRequirementBuilder.create(PackageNamespace.PACKAGE_NAMESPACE, "org.acme.foo").getRequirement();
Collection<Capability> providers = storage.findProviders(req);
Assert.assertNotNull(providers);
Assert.assertEquals(1, providers.size());
@@ -31,10 +31,10 @@
import org.jboss.osgi.metadata.OSGiMetaData;
import org.jboss.osgi.metadata.OSGiMetaDataBuilder;
import org.jboss.osgi.repository.XRepository;
import org.jboss.osgi.repository.XRequirementBuilder;
import org.jboss.osgi.repository.core.MavenArtifactRepository;
import org.jboss.osgi.resolver.MavenCoordinates;
import org.jboss.osgi.resolver.XIdentityCapability;
import org.jboss.osgi.resolver.XRequirementBuilder;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Version;
@@ -61,7 +61,7 @@ public void setUp() throws IOException {
@Test
public void testFindProvidersByMavenId() throws Exception {
MavenCoordinates mavenid = MavenCoordinates.parse("org.apache.felix:org.apache.felix.configadmin:1.2.8");
Requirement req = XRequirementBuilder.createArtifactRequirement(mavenid);
Requirement req = XRequirementBuilder.create(mavenid).getRequirement();
Collection<Capability> caps = repository.findProviders(req);
assertEquals("One capability", 1, caps.size());
Capability cap = caps.iterator().next();
@@ -26,10 +26,10 @@
import org.jboss.osgi.repository.RepositoryReader;
import org.jboss.osgi.repository.RepositoryStorage;
import org.jboss.osgi.repository.XRepository;
import org.jboss.osgi.repository.XRequirementBuilder;
import org.jboss.osgi.repository.spi.MemoryRepositoryStorage;
import org.jboss.osgi.resolver.XCapability;
import org.jboss.osgi.resolver.XRequirement;
import org.jboss.osgi.resolver.XRequirementBuilder;
import org.jboss.osgi.resolver.XResource;
import org.junit.Before;
import org.junit.Test;
@@ -43,7 +43,7 @@
* @author thomas.diesler@jboss.com
* @since 16-Jan-2012
*/
public class DefaultRepositoryStorageTestCase extends AbstractRepositoryTest {
public class MemoryRepositoryStorageTestCase extends AbstractRepositoryTest {
private RepositoryStorage storage;
@@ -62,7 +62,8 @@ public void testRequireBundle() throws Exception {
Assert.assertNotNull("Resource not null", resource);
Assert.assertNull("One resource only", reader.nextResource());
XRequirement req = XRequirementBuilder.createRequirement(BundleNamespace.BUNDLE_NAMESPACE, "org.acme.pool");
XRequirementBuilder builder = XRequirementBuilder.create(BundleNamespace.BUNDLE_NAMESPACE, "org.acme.pool");
XRequirement req = builder.getRequirement();
Collection<Capability> providers = storage.findProviders(req);
Assert.assertNotNull(providers);
@@ -72,4 +73,19 @@ public void testRequireBundle() throws Exception {
Assert.assertNotNull(cap);
Assert.assertSame(resource, cap.getResource());
}
@Test
public void testRequireBundleWithFilter() throws Exception {
XRequirementBuilder builder = XRequirementBuilder.create(BundleNamespace.BUNDLE_NAMESPACE);
builder.getDirectives().put(BundleNamespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.wiring.bundle=org.acme.pool)");
XRequirement req = builder.getRequirement();
Collection<Capability> providers = storage.findProviders(req);
Assert.assertNotNull(providers);
Assert.assertEquals(1, providers.size());
XCapability cap = (XCapability) providers.iterator().next();
Assert.assertNotNull(cap);
}
}
@@ -35,13 +35,13 @@
import org.jboss.osgi.repository.RepositoryStorage;
import org.jboss.osgi.repository.RepositoryStorageFactory;
import org.jboss.osgi.repository.XRepository;
import org.jboss.osgi.repository.XRequirementBuilder;
import org.jboss.osgi.repository.core.FileBasedRepositoryStorage;
import org.jboss.osgi.repository.core.MavenArtifactRepository;
import org.jboss.osgi.repository.spi.AbstractPersistentRepository;
import org.jboss.osgi.resolver.MavenCoordinates;
import org.jboss.osgi.resolver.XCapability;
import org.jboss.osgi.resolver.XIdentityCapability;
import org.jboss.osgi.resolver.XRequirementBuilder;
import org.jboss.osgi.resolver.XResource;
import org.junit.Before;
import org.junit.Test;
@@ -78,7 +78,7 @@ public RepositoryStorage create(XRepository repository) {
public void testFindProvidersByMavenId() throws Exception {
MavenCoordinates mavenid = MavenCoordinates.parse("org.apache.felix:org.apache.felix.configadmin:1.2.8");
Requirement req = XRequirementBuilder.createArtifactRequirement(mavenid);
Requirement req = XRequirementBuilder.create(mavenid).getRequirement();
Collection<Capability> caps = repository.findProviders(req);
assertEquals("One capability", 1, caps.size());
XCapability cap = (XCapability) caps.iterator().next();
@@ -32,11 +32,11 @@
import org.jboss.osgi.repository.RepositoryStorage;
import org.jboss.osgi.repository.XPersistentRepository;
import org.jboss.osgi.repository.XRepository;
import org.jboss.osgi.repository.XRequirementBuilder;
import org.jboss.osgi.resolver.MavenCoordinates;
import org.jboss.osgi.resolver.XIdentityCapability;
import org.jboss.osgi.resolver.XPackageCapability;
import org.jboss.osgi.resolver.XRequirement;
import org.jboss.osgi.resolver.XRequirementBuilder;
import org.jboss.osgi.resolver.XResource;
import org.jboss.osgi.spi.OSGiManifestBuilder;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -91,7 +91,7 @@ public InputStream openStream() {
public void testMavenCoordinates() throws Exception {
MavenCoordinates mavenid = MavenCoordinates.parse("org.apache.felix:org.apache.felix.configadmin:1.2.8");
Requirement req = XRequirementBuilder.createArtifactRequirement(mavenid);
Requirement req = XRequirementBuilder.create(mavenid).getRequirement();
Assert.assertNotNull("Requirement not null", req);
Collection<Capability> providers = getRepository().findProviders(req);
@@ -140,8 +140,9 @@ public void testRepositoryRestart() throws Exception {
bundle.start();
Assert.assertNotNull(getRepository());
XRequirement req = XRequirementBuilder.createRequirement(PackageNamespace.PACKAGE_NAMESPACE, "org.apache.felix.cm");
req.getAttributes().put(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, Version.parseVersion("1.0"));
XRequirementBuilder builder = XRequirementBuilder.create(PackageNamespace.PACKAGE_NAMESPACE, "org.apache.felix.cm");
builder.getAttributes().put(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, Version.parseVersion("1.0"));
XRequirement req = builder.getRequirement();
Collection<Capability> providers = getRepository().findProviders(req);
Assert.assertEquals("Capability not null", 1, providers.size());
@@ -53,7 +53,7 @@
<version.jboss.osgi.equinox>3.5.2.SP1</version.jboss.osgi.equinox>
<version.jboss.osgi.framework>1.4.0.CR1</version.jboss.osgi.framework>
<version.jboss.osgi.metadata>2.1.1.CR1</version.jboss.osgi.metadata>
<version.jboss.osgi.resolver>2.1.1.CR2</version.jboss.osgi.resolver>
<version.jboss.osgi.resolver>2.1.1.CR3</version.jboss.osgi.resolver>
<version.jboss.osgi.spi>3.1.1.CR1</version.jboss.osgi.spi>
<version.jboss.osgi.testing>1.0.4</version.jboss.osgi.testing>
<version.jboss.shrinkwrap>1.0.0-cr-1</version.jboss.shrinkwrap>

0 comments on commit 2d560d5

Please sign in to comment.