Search in sources :

Example 1 with FelixResourceAdapter

use of org.apache.aries.subsystem.util.felix.FelixResourceAdapter in project aries by apache.

the class RepositoryGenerator method resolve.

public List<Resource> resolve(List<Resource> subsystemContent, List<Resource> subsystemResources) throws SubsystemException {
    generateOBR();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Attempt to resolve subsystem content {} subsystem resource {}", subsystemContent.toString(), subsystemResources.toString());
    }
    Resolver obrResolver = this.repositoryAdmin.resolver();
    // add subsystem content to the resolver
    for (Resource res : subsystemContent) {
        // org.apache.felix.bundlerepository.Resource obrRes = findOBRResource(res);
        // obrResolver.add(obrRes);
        obrResolver.add(new OsgiResourceAdapter(res));
    }
    // add subsystem resource to the resolver
    for (Resource res : subsystemResources) {
        // org.apache.felix.bundlerepository.Resource obrRes = findOBRResource(res);
        // obrResolver.add(obrRes);
        obrResolver.add(new OsgiResourceAdapter(res));
    }
    // Question: do we need to create the repository.xml for the subsystem and add the repo to RepoAdmin?
    List<Resource> resources = new ArrayList<Resource>();
    if (obrResolver.resolve()) {
        for (org.apache.felix.bundlerepository.Resource res : obrResolver.getRequiredResources()) {
            // resources.add(toResource(res));
            resources.add(new FelixResourceAdapter(res));
        }
        // Question: should we handle optional resource differently?
        for (org.apache.felix.bundlerepository.Resource res : obrResolver.getOptionalResources()) {
            // resources.add(toResource(res));
            resources.add(new FelixResourceAdapter(res));
        }
    } else {
        // log the unsatisfied requirement
        Reason[] reasons = obrResolver.getUnsatisfiedRequirements();
        StringBuilder builder = new StringBuilder("Failed to resolve subsystem").append(System.getProperty("line.separator"));
        for (Reason reason : reasons) {
            LOGGER.warn("Unable to resolve subsystem content {} subsystem resource {} because of unsatisfied requirement {}", new Object[] { subsystemContent.toString(), subsystemResources.toString(), reason.getRequirement().getName() });
            builder.append("resource = ").append(reason.getResource().getSymbolicName()).append(", requirement = ").append(reason.getRequirement().getName()).append(System.getProperty("line.separator"));
        }
        throw new SubsystemException(builder.toString());
    }
    return resources;
}
Also used : Resolver(org.apache.felix.bundlerepository.Resolver) SubsystemException(org.osgi.service.subsystem.SubsystemException) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) FelixResourceAdapter(org.apache.aries.subsystem.util.felix.FelixResourceAdapter) Reason(org.apache.felix.bundlerepository.Reason) OsgiResourceAdapter(org.apache.aries.subsystem.util.felix.OsgiResourceAdapter)

Example 2 with FelixResourceAdapter

use of org.apache.aries.subsystem.util.felix.FelixResourceAdapter in project aries by apache.

the class RepositoryGenerator method find.

/**
 * the format of resource is like bundlesymbolicname;version=1.0.0, for example com.ibm.ws.eba.example.blog.api;version=1.0.0,
 */
@SuppressWarnings({ "rawtypes", "unused" })
public Resource find(String resource) throws SubsystemException {
    generateOBR();
    Content content = new ContentImpl(resource);
    String symbolicName = content.getContentName();
    // this version could possibly be a range
    String version = content.getVersion().toString();
    StringBuilder filterString = new StringBuilder();
    filterString.append("(&(name" + "=" + symbolicName + "))");
    filterString.append("(version" + "=" + version + "))");
    // org.apache.felix.bundlerepository.Resource[] res = this.repositoryAdmin.discoverResources(filterString.toString());
    Repository[] repos = this.repositoryAdmin.listRepositories();
    org.apache.felix.bundlerepository.Resource res = null;
    for (Repository repo : repos) {
        org.apache.felix.bundlerepository.Resource[] resources = repo.getResources();
        for (int i = 0; i < resources.length; i++) {
            if (resources[i].getSymbolicName().equals(symbolicName)) {
                if (resources[i].getVersion().compareTo(new Version(version)) == 0) {
                    res = resources[i];
                    break;
                }
            }
        }
    }
    if (res == null) {
        // throw new SubsystemException("unable to find the resource " + resource);
        return null;
    }
    Map props = res.getProperties();
    Object type = props.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE);
    return new FelixResourceAdapter(res);
}
Also used : Resource(org.osgi.resource.Resource) FelixResourceAdapter(org.apache.aries.subsystem.util.felix.FelixResourceAdapter) Repository(org.apache.felix.bundlerepository.Repository) Version(org.osgi.framework.Version) Content(org.apache.aries.application.Content) Map(java.util.Map)

Example 3 with FelixResourceAdapter

use of org.apache.aries.subsystem.util.felix.FelixResourceAdapter in project aries by apache.

the class FelixResourceAdapterTest method testGetCapabilitiesWithNullNamespace.

@Test
public void testGetCapabilitiesWithNullNamespace() {
    Resource resource = EasyMock.createNiceMock(Resource.class);
    Capability capability = EasyMock.createNiceMock(Capability.class);
    EasyMock.expect(capability.getName()).andReturn(Capability.PACKAGE);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(Capability.PACKAGE, "org.foo.bar");
    EasyMock.expect(capability.getPropertiesAsMap()).andReturn(properties);
    Capability[] capabilities = new Capability[] { capability };
    EasyMock.expect(resource.getCapabilities()).andReturn(capabilities);
    EasyMock.replay(resource);
    FelixResourceAdapter adapter = new FelixResourceAdapter(resource);
    List<org.osgi.resource.Capability> caps = adapter.getCapabilities(null);
    // osgi.identity, osgi.content. osgi.wiring.host, and osgi.wiring.package
    assertEquals("Null namespace should return all capabilities", 4, caps.size());
}
Also used : Capability(org.apache.felix.bundlerepository.Capability) HashMap(java.util.HashMap) Resource(org.apache.felix.bundlerepository.Resource) FelixResourceAdapter(org.apache.aries.subsystem.util.felix.FelixResourceAdapter) Test(org.junit.Test)

Example 4 with FelixResourceAdapter

use of org.apache.aries.subsystem.util.felix.FelixResourceAdapter in project aries by apache.

the class RepositoryAdminRepository method findProviders.

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;
}
Also used : Capability(org.osgi.resource.Capability) Resource(org.apache.felix.bundlerepository.Resource) OsgiRequirementAdapter(org.apache.aries.subsystem.util.felix.OsgiRequirementAdapter) FelixResourceAdapter(org.apache.aries.subsystem.util.felix.FelixResourceAdapter) Requirement(org.osgi.resource.Requirement) FelixCapabilityAdapter(org.apache.aries.subsystem.util.felix.FelixCapabilityAdapter) FelixRepositoryAdapter(org.apache.aries.subsystem.util.felix.FelixRepositoryAdapter) Collection(java.util.Collection)

Aggregations

FelixResourceAdapter (org.apache.aries.subsystem.util.felix.FelixResourceAdapter)4 Resource (org.apache.felix.bundlerepository.Resource)2 Resource (org.osgi.resource.Resource)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Content (org.apache.aries.application.Content)1 FelixCapabilityAdapter (org.apache.aries.subsystem.util.felix.FelixCapabilityAdapter)1 FelixRepositoryAdapter (org.apache.aries.subsystem.util.felix.FelixRepositoryAdapter)1 OsgiRequirementAdapter (org.apache.aries.subsystem.util.felix.OsgiRequirementAdapter)1 OsgiResourceAdapter (org.apache.aries.subsystem.util.felix.OsgiResourceAdapter)1 Capability (org.apache.felix.bundlerepository.Capability)1 Reason (org.apache.felix.bundlerepository.Reason)1 Repository (org.apache.felix.bundlerepository.Repository)1 Resolver (org.apache.felix.bundlerepository.Resolver)1 Test (org.junit.Test)1 Version (org.osgi.framework.Version)1 Capability (org.osgi.resource.Capability)1 Requirement (org.osgi.resource.Requirement)1