Search in sources :

Example 16 with Resource

use of org.apache.felix.bundlerepository.Resource in project aries by apache.

the class OBRAriesResolver method doResolve.

private Collection<ModelledResource> doResolve(Resolver obrResolver, String appName) throws ResolverException {
    log.debug(LOG_ENTRY, "doResolve");
    Collection<ModelledResource> toReturn = new ArrayList<ModelledResource>();
    if (obrResolver.resolve()) {
        List<Resource> requiredResources = retrieveRequiredResources(obrResolver);
        if (requiredResources == null) {
            log.debug("resolver.getRequiredResources() returned null");
        } else {
            for (Resource r : requiredResources) {
                Map<String, String> attribs = new HashMap<String, String>();
                attribs.put(Constants.VERSION_ATTRIBUTE, "[" + r.getVersion() + ',' + r.getVersion() + "]");
                ModelledResource modelledResourceForThisMatch = null;
                // list of packages available in the target runtime environment. If the resource has no symbolic name, we can ignore it
                if (r.getSymbolicName() != null) {
                    try {
                        modelledResourceForThisMatch = new ModelledBundleResource(r, modellingManager, modellingHelper);
                    } catch (InvalidAttributeException iax) {
                        ResolverException re = new ResolverException("Internal error occurred: " + iax);
                        log.debug(LOG_EXIT, "doResolve", re);
                        throw re;
                    }
                    toReturn.add(modelledResourceForThisMatch);
                }
            }
        }
        log.debug(LOG_EXIT, toReturn);
        return toReturn;
    } else {
        Reason[] reasons = obrResolver.getUnsatisfiedRequirements();
        // let's refine the list by removing the indirect unsatisfied bundles that are caused by unsatisfied packages or other bundles
        Map<String, Set<String>> refinedReqs = refineUnsatisfiedRequirements(obrResolver, reasons);
        StringBuffer reqList = new StringBuffer();
        Map<String, String> unsatisfiedRequirements = extractConsumableMessageInfo(refinedReqs);
        for (String reason : unsatisfiedRequirements.keySet()) {
            reqList.append('\n');
            reqList.append(reason);
        }
        ResolverException re = new ResolverException(MessageUtil.getMessage("RESOLVER_UNABLE_TO_RESOLVE", new Object[] { appName, reqList }));
        re.setUnsatisfiedRequirementsAndReasons(unsatisfiedRequirements);
        log.debug(LOG_EXIT, "doResolve", re);
        throw re;
    }
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) Set(java.util.Set) HashSet(java.util.HashSet) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) Reason(org.apache.felix.bundlerepository.Reason) ModelledResource(org.apache.aries.application.modelling.ModelledResource) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource)

Example 17 with Resource

use of org.apache.felix.bundlerepository.Resource in project aries by apache.

the class OBRAriesResolver method retrieveRequiredResources.

/**
 * Get the list of resources returned by the resolver
 * @param resolver OBR resolver
 * @return a list of required resources
 */
protected List<Resource> retrieveRequiredResources(Resolver resolver) {
    log.debug(LOG_ENTRY, "retrieveRequiredResources", resolver);
    Map<String, List<Resource>> resourcesByName = new HashMap<String, List<Resource>>();
    for (Resource r : resolver.getRequiredResources()) {
        resourcesByName.put(r.getSymbolicName(), mergeResource(resolver, r, resourcesByName.get(r.getSymbolicName())));
    }
    List<Resource> result = new ArrayList<Resource>();
    for (List<Resource> res : resourcesByName.values()) {
        result.addAll(res);
    }
    log.debug(LOG_EXIT, "retrieveRequiredResources", result);
    return result;
}
Also used : HashMap(java.util.HashMap) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 18 with Resource

use of org.apache.felix.bundlerepository.Resource in project aries by apache.

the class OBRAriesResolver method mergeResource.

/**
 * Get rid of the redundant resources
 * @param resolver OBR resolver
 * @param r a resource
 * @param list similar resources
 * @return the list of minimum resources
 */
protected List<Resource> mergeResource(Resolver resolver, Resource r, List<Resource> list) {
    log.debug(LOG_ENTRY, "mergeResource", new Object[] { resolver, r, list });
    if (list == null) {
        log.debug(LOG_EXIT, "mergeResource", Arrays.asList(r));
        return Arrays.asList(r);
    } else {
        List<Resource> result = new ArrayList<Resource>();
        for (Resource old : list) {
            boolean oldRedundant = satisfiesAll(r, resolver.getReason(old));
            boolean newRedundant = satisfiesAll(old, resolver.getReason(r));
            if (oldRedundant && newRedundant) {
                int comp = old.getVersion().compareTo(r.getVersion());
                oldRedundant = comp < 0;
                newRedundant = comp >= 0;
            }
            if (newRedundant) {
                log.debug(LOG_EXIT, "mergeResource", list);
                return list;
            } else if (oldRedundant) {
            // do nothing -> so don't add the old resource to the new list
            } else {
                result.add(old);
            }
        }
        result.add(r);
        log.debug(LOG_EXIT, "mergeResource", result);
        return result;
    }
}
Also used : ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) ArrayList(java.util.ArrayList) ResolveConstraint(org.apache.aries.application.management.ResolveConstraint)

Example 19 with Resource

use of org.apache.felix.bundlerepository.Resource in project aries by apache.

the class Aries1608Test method createRepositoryXml.

private URL createRepositoryXml(DataModelHelper helper) throws Exception {
    File dir;
    String cwd = new File("").getAbsolutePath();
    if (cwd.endsWith(File.separator + "target")) {
        dir = new File("test-classes/aries1608");
    } else {
        dir = new File("target/test-classes/aries1608");
    }
    File jar = new File(dir, "aries1608provider.jar");
    assertTrue("The bundle jar does not exist: " + jar.getAbsolutePath(), jar.exists());
    Resource resource = helper.createResource(jar.toURI().toURL());
    Repository repository = helper.repository(new Resource[] { resource });
    File file = new File(dir, "repository.xml");
    FileWriter fw = new FileWriter(file);
    try {
        helper.writeRepository(repository, fw);
        return file.toURI().toURL();
    } finally {
        fw.close();
    }
}
Also used : Repository(org.apache.felix.bundlerepository.Repository) FileWriter(java.io.FileWriter) Resource(org.apache.felix.bundlerepository.Resource) File(java.io.File)

Example 20 with Resource

use of org.apache.felix.bundlerepository.Resource in project aries by apache.

the class Aries1523Test method createRepositoryXml.

private URL createRepositoryXml(DataModelHelper helper) throws Exception {
    File dir;
    String cwd = new File("").getAbsolutePath();
    if (cwd.endsWith(File.separator + "target")) {
        dir = new File("test-classes/aries1523");
    } else {
        dir = new File("target/test-classes/aries1523");
    }
    File jar = new File(dir, "aries1523host.jar");
    assertTrue("The host jar does not exist: " + jar.getAbsolutePath(), jar.exists());
    Resource resource = helper.createResource(jar.toURI().toURL());
    Repository repository = helper.repository(new Resource[] { resource });
    File file = new File(dir, "repository.xml");
    FileWriter fw = new FileWriter(file);
    try {
        helper.writeRepository(repository, fw);
        return file.toURI().toURL();
    } finally {
        fw.close();
    }
}
Also used : Repository(org.apache.felix.bundlerepository.Repository) FileWriter(java.io.FileWriter) Resource(org.apache.felix.bundlerepository.Resource) File(java.io.File)

Aggregations

Resource (org.apache.felix.bundlerepository.Resource)45 Repository (org.apache.felix.bundlerepository.Repository)19 Capability (org.apache.felix.bundlerepository.Capability)9 Resolver (org.apache.felix.bundlerepository.Resolver)9 ArrayList (java.util.ArrayList)8 ModelledResource (org.apache.aries.application.modelling.ModelledResource)8 ModelledBundleResource (org.apache.aries.application.resolver.obr.ext.ModelledBundleResource)8 Reason (org.apache.felix.bundlerepository.Reason)7 RepositoryAdmin (org.apache.felix.bundlerepository.RepositoryAdmin)7 HashMap (java.util.HashMap)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 URL (java.net.URL)5 File (java.io.File)4 DataModelHelper (org.apache.felix.bundlerepository.DataModelHelper)4 FileWriter (java.io.FileWriter)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Requirement (org.apache.felix.bundlerepository.Requirement)3 BundleContext (org.osgi.framework.BundleContext)3