Search in sources :

Example 31 with Resource

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

the class DataModelHelperImpl method toXml.

private static void toXml(XmlWriter w, Repository repository) throws IOException {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss.SSS");
    w.element(RepositoryParser.REPOSITORY).attribute(RepositoryParser.NAME, repository.getName()).attribute(RepositoryParser.LASTMODIFIED, format.format(new Date(repository.getLastModified())));
    if (repository instanceof RepositoryImpl) {
        Referral[] referrals = ((RepositoryImpl) repository).getReferrals();
        for (int i = 0; referrals != null && i < referrals.length; i++) {
            w.element(RepositoryParser.REFERRAL).attribute(RepositoryParser.DEPTH, new Integer(referrals[i].getDepth())).attribute(RepositoryParser.URL, referrals[i].getUrl()).end();
        }
    }
    Resource[] resources = repository.getResources();
    for (int i = 0; resources != null && i < resources.length; i++) {
        toXml(w, resources[i]);
    }
    w.end();
}
Also used : Resource(org.apache.felix.bundlerepository.Resource) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 32 with Resource

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

the class OBRAriesResolver method getBundleInfo.

@Override
public BundleInfo getBundleInfo(String bundleSymbolicName, Version bundleVersion) {
    Map<String, String> attribs = new HashMap<String, String>();
    // bundleVersion is an exact version - so ensure right version filter is generated
    VersionRange range = ManifestHeaderProcessor.parseVersionRange(bundleVersion.toString(), true);
    attribs.put(Resource.VERSION, range.toString());
    String filterString = ManifestHeaderProcessor.generateFilter(Resource.SYMBOLIC_NAME, bundleSymbolicName, attribs);
    Resource[] resources;
    try {
        resources = repositoryAdmin.discoverResources(filterString);
        if (resources != null && resources.length > 0) {
            return toBundleInfo(resources[0], false);
        } else {
            return null;
        }
    } catch (InvalidSyntaxException e) {
        log.error("Invalid filter", e);
        return null;
    }
}
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) VersionRange(org.apache.aries.util.VersionRange) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 33 with Resource

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

the class OBRAriesResolver method addPlatformRepositories.

/* A 'platform repository' describes capabilities of the target runtime environment
   * These should be added to the resolver without being listed as coming from a particular 
   * repository or bundle.  
   */
private void addPlatformRepositories(Resolver obrResolver, String appName, PlatformRepository platformRepository) {
    log.debug(LOG_ENTRY, "addPlatformRepositories", new Object[] { obrResolver, appName });
    DataModelHelper helper = repositoryAdmin.getHelper();
    if (platformRepository != null) {
        Collection<URI> uris = platformRepository.getPlatformRepositoryURLs();
        if ((uris != null) && (!uris.isEmpty())) {
            for (URI uri : uris) {
                InputStream is = null;
                try {
                    is = uri.toURL().openStream();
                    Reader repoReader = new InputStreamReader(is);
                    Repository aPlatformRepo = helper.readRepository(repoReader);
                    Resource[] resources = aPlatformRepo.getResources();
                    for (Resource r : resources) {
                        Capability[] caps = r.getCapabilities();
                        for (Capability c : caps) {
                            obrResolver.addGlobalCapability(c);
                        }
                    }
                } catch (Exception e) {
                    // not a big problem
                    log.error(MessageUtil.getMessage("RESOLVER_UNABLE_TO_READ_REPOSITORY_EXCEPTION", new Object[] { appName, uri }));
                } finally {
                    IOUtils.close(is);
                }
            }
        }
    }
    log.debug(LOG_EXIT, "addPlatformRepositories");
}
Also used : PlatformRepository(org.apache.aries.application.management.spi.repository.PlatformRepository) Repository(org.apache.felix.bundlerepository.Repository) InputStreamReader(java.io.InputStreamReader) Capability(org.apache.felix.bundlerepository.Capability) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) DataModelHelper(org.apache.felix.bundlerepository.DataModelHelper) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) URI(java.net.URI) ResolverException(org.apache.aries.application.management.ResolverException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException)

Example 34 with Resource

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

the class OBRAriesResolver method refineUnsatisfiedRequirements.

/**
 * Refine the unsatisfied requirements ready for later human comsumption
 *
 * @param resolver The resolver to be used to refine the requirements
 * @param reasons The reasons
 * @return A map of the unsatifiedRequirement to the set of bundles that have that requirement unsatisfied (values associated with the keys can be null)
 */
private Map<String, Set<String>> refineUnsatisfiedRequirements(Resolver resolver, Reason[] reasons) {
    log.debug(LOG_ENTRY, "refineUnsatisfiedRequirements", new Object[] { resolver, Arrays.toString(reasons) });
    Map<Requirement, Set<String>> req_resources = new HashMap<Requirement, Set<String>>();
    // add the reasons to the map, use the requirement as the key, the resources required the requirement as the values
    Set<Resource> resources = new HashSet<Resource>();
    for (Reason reason : reasons) {
        resources.add(reason.getResource());
        Requirement key = reason.getRequirement();
        String value = reason.getResource().getSymbolicName() + "_" + reason.getResource().getVersion().toString();
        Set<String> values = req_resources.get(key);
        if (values == null) {
            values = new HashSet<String>();
        }
        values.add(value);
        req_resources.put(key, values);
    }
    // remove the requirements that can be satisifed by the resources. It is listed because the resources are not satisfied by other requirements.
    // For an instance, the unsatisfied reasons are [package a, required by bundle aa], [package b, required by bundle bb] and [package c, required by bundle cc],
    // If the bundle aa exports the package a and c. In our error message, we only want to display package a is needed by bundle aa.
    // Go through each requirement and find out whether the requirement can be satisfied by the reasons.
    Set<Capability> caps = new HashSet<Capability>();
    for (Resource res : resources) {
        if ((res != null) && (res.getCapabilities() != null)) {
            List<Capability> capList = Arrays.asList(res.getCapabilities());
            if (capList != null) {
                caps.addAll(capList);
            }
        }
    }
    Iterator<Map.Entry<Requirement, Set<String>>> iterator = req_resources.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<Requirement, Set<String>> entry = iterator.next();
        Requirement req = entry.getKey();
        for (Capability cap : caps) {
            if (req.isSatisfied(cap)) {
                // remove the key from the map
                iterator.remove();
                break;
            }
        }
    }
    // Now the map only contains the necessary missing requirements
    Map<String, Set<String>> result = new HashMap<String, Set<String>>();
    for (Map.Entry<Requirement, Set<String>> req_res : req_resources.entrySet()) {
        result.put(req_res.getKey().getFilter(), req_res.getValue());
    }
    log.debug(LOG_EXIT, "refineUnsatisfiedRequirements", new Object[] { result });
    return result;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Capability(org.apache.felix.bundlerepository.Capability) 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) Reason(org.apache.felix.bundlerepository.Reason) Requirement(org.apache.felix.bundlerepository.Requirement) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 35 with Resource

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

the class OBRAriesResolver method getLocalRepository.

private Repository getLocalRepository(RepositoryAdmin admin) {
    Repository localRepository = repositoryAdmin.getLocalRepository();
    Resource[] resources = localRepository.getResources();
    Resource[] newResources = new Resource[resources.length];
    for (int i = 0; i < resources.length; i++) {
        newResources[i] = new ResourceWrapper(resources[i]);
    }
    return repositoryAdmin.getHelper().repository(newResources);
}
Also used : ResourceWrapper(org.apache.aries.application.resolver.obr.impl.ResourceWrapper) PlatformRepository(org.apache.aries.application.management.spi.repository.PlatformRepository) Repository(org.apache.felix.bundlerepository.Repository) ModelledBundleResource(org.apache.aries.application.resolver.obr.ext.ModelledBundleResource) ModelledResource(org.apache.aries.application.modelling.ModelledResource) Resource(org.apache.felix.bundlerepository.Resource) ResolveConstraint(org.apache.aries.application.management.ResolveConstraint)

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