Search in sources :

Example 1 with ImportedPackage

use of org.apache.aries.application.modelling.ImportedPackage in project aries by apache.

the class ModellingHelperImpl method intersectPackage_.

public static ImportedPackage intersectPackage_(ImportedPackage p1, ImportedPackage p2) {
    logger.debug(LOG_ENTRY, "intersectPackage_", new Object[] { p1, p2 });
    ImportedPackage result = null;
    if (p1.getPackageName().equals(p2.getPackageName())) {
        Map<String, String> att1 = new HashMap<String, String>(p1.getAttributes());
        Map<String, String> att2 = new HashMap<String, String>(p2.getAttributes());
        // Get the versions, we remove them so that the remaining attributes can be matched.
        String rangeStr1 = att1.remove(Constants.VERSION_ATTRIBUTE);
        String rangeStr2 = att2.remove(Constants.VERSION_ATTRIBUTE);
        //Also remove the optional directive as we don't care about that either
        att1.remove(OPTIONAL_KEY);
        att2.remove(OPTIONAL_KEY);
        //If identical take either, otherwise null!
        Map<String, String> mergedAttribs = (att1.equals(att2) ? att1 : null);
        if (mergedAttribs == null) {
            // Cannot intersect requirements if attributes are not identical.
            result = null;
        } else {
            boolean isIntersectSuccessful = true;
            if (rangeStr1 != null && rangeStr2 != null) {
                // Both requirements have a version constraint so check for an intersection between them.
                VersionRange range1 = ManifestHeaderProcessor.parseVersionRange(rangeStr1);
                VersionRange range2 = ManifestHeaderProcessor.parseVersionRange(rangeStr2);
                VersionRange intersectRange = range1.intersect(range2);
                if (intersectRange == null) {
                    // No intersection possible.
                    isIntersectSuccessful = false;
                } else {
                    // Use the intersected version range.
                    mergedAttribs.put(Constants.VERSION_ATTRIBUTE, intersectRange.toString());
                }
            } else if (rangeStr1 != null) {
                mergedAttribs.put(Constants.VERSION_ATTRIBUTE, rangeStr1);
            } else if (rangeStr2 != null) {
                mergedAttribs.put(Constants.VERSION_ATTRIBUTE, rangeStr2);
            }
            //If both optional, we are optional, otherwise use the default
            if (p1.isOptional() && p2.isOptional()) {
                mergedAttribs.put(OPTIONAL_KEY, Constants.RESOLUTION_OPTIONAL);
            }
            try {
                result = (isIntersectSuccessful ? new ImportedPackageImpl(p1.getPackageName(), mergedAttribs) : null);
            } catch (InvalidAttributeException iax) {
                logger.error(iax.getMessage());
            }
        }
    }
    logger.debug(LOG_EXIT, "intersectPackage_", result);
    return result;
}
Also used : ImportedPackageImpl(org.apache.aries.application.modelling.impl.ImportedPackageImpl) InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) HashMap(java.util.HashMap) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) VersionRange(org.apache.aries.util.VersionRange)

Example 2 with ImportedPackage

use of org.apache.aries.application.modelling.ImportedPackage in project aries by apache.

the class DeployedBundlesImpl method getRequiredUseBundle.

/**
   * Get the subset of bundles specified in use-bundle that are actually required to
   * satisfy direct requirements of deployed content.
   * @return a set of bundle metadata.
   * @throws ResolverException if the requirements could not be resolved.
   */
public Collection<ModelledResource> getRequiredUseBundle() throws ResolverException {
    logger.debug(LOG_ENTRY, "getRequiredUseBundle");
    Collection<ModelledResource> usedUseBundles = cachedRequiredUseBundle;
    if (usedUseBundles == null) {
        Collection<ImportedPackage> externalReqs = getExternalPackageRequirements();
        usedUseBundles = new HashSet<ModelledResource>();
        for (ImportedPackage req : externalReqs) {
            // Find a match from the supplied bundle capabilities.
            ExportedPackage match = getPackageMatch(req, deployedUseBundle);
            if (match != null) {
                usedUseBundles.add(match.getBundle());
            }
        }
        cachedRequiredUseBundle = usedUseBundles;
    }
    logger.debug(LOG_EXIT, "getRequiredUseBundle", usedUseBundles);
    return usedUseBundles;
}
Also used : ExportedPackage(org.apache.aries.application.modelling.ExportedPackage) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) ModelledResource(org.apache.aries.application.modelling.ModelledResource)

Example 3 with ImportedPackage

use of org.apache.aries.application.modelling.ImportedPackage in project aries by apache.

the class DeployedBundlesImpl method validateOtherImports.

/**
   * Create entries for the Import-Package header corresponding to the supplied
   * packages, referring to bundles not in Use-Bundle.
   * @param requirements packages for which entries should be created.
   * @return manifest header entries.
   * @throws ResolverException if the imports are invalid.
   */
private void validateOtherImports(Collection<ImportedPackage> requirements) throws ResolverException {
    logger.debug(LOG_ENTRY, "validateOtherImports", requirements);
    for (ImportedPackage req : requirements) {
        String pkgName = req.getPackageName();
        for (String name : req.getAttributes().keySet()) {
            if (Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE.equals(name) || Constants.BUNDLE_VERSION_ATTRIBUTE.equals(name)) {
                ResolverException re = new ResolverException(MessageUtil.getMessage("INVALID_PACKAGE_REQUIREMENT_ATTRIBUTES", new Object[] { assetName, name, pkgName }));
                re.setUnsatisfiedRequirements(Arrays.asList(pkgName));
                logger.debug(LOG_EXIT, "validateOtherImports", re);
                throw re;
            }
        }
    }
    logger.debug(LOG_EXIT, "validateOtherImports");
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage)

Example 4 with ImportedPackage

use of org.apache.aries.application.modelling.ImportedPackage in project aries by apache.

the class PackageRequirementMergerTest method testMergeInvalid.

@Test
public void testMergeInvalid() throws Exception {
    Collection<ImportedPackage> reqs = new ArrayList<ImportedPackage>();
    reqs.add(newImportedPackage("a", "[1.0.0,2.0.0]"));
    reqs.add(newImportedPackage("a", "[3.0.0,3.0.0]"));
    reqs.add(newImportedPackage("b", "1.0.0"));
    reqs.add(newImportedPackage("b", "2.0.0"));
    reqs.add(newImportedPackage("c", "[1.0.0,2.0.0)"));
    reqs.add(newImportedPackage("c", "2.0.0"));
    PackageRequirementMerger merger = new PackageRequirementMerger(reqs);
    Assert.assertFalse(merger.isMergeSuccessful());
    try {
        merger.getMergedRequirements();
        Assert.fail("getMergedRequirements should throw IllegalStateException.");
    } catch (IllegalStateException e) {
    }
    Set<String> result = merger.getInvalidRequirements();
    Set<String> expected = new HashSet<String>();
    expected.add("a");
    expected.add("c");
    Assert.assertEquals(expected, result);
}
Also used : ArrayList(java.util.ArrayList) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) PackageRequirementMerger(org.apache.aries.application.modelling.internal.PackageRequirementMerger) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with ImportedPackage

use of org.apache.aries.application.modelling.ImportedPackage in project aries by apache.

the class DeployedBundlesImpl method getExternalPackageRequirements.

/**
   * Get all the requirements of bundles in deployed content that are not satisfied
   * by other bundles in deployed content.
   * @return a collection of package requirements.
   * @throws ResolverException if the requirements could not be resolved.
   */
private Collection<ImportedPackage> getExternalPackageRequirements() throws ResolverException {
    logger.debug(LOG_ENTRY, "getExternalPackageRequirements");
    Collection<ImportedPackage> result = cachedExternalRequirements;
    if (result == null) {
        // Get all the internal requirements.
        Collection<ImportedPackage> requirements = new ArrayList<ImportedPackage>();
        Collection<ExportedPackage> internalExports = new ArrayList<ExportedPackage>();
        for (ModelledResource bundle : deployedContent) {
            requirements.addAll(bundle.getImportedPackages());
            internalExports.addAll(bundle.getExportedPackages());
        }
        // Filter out requirements satisfied by internal capabilities.
        result = new ArrayList<ImportedPackage>();
        for (ImportedPackage req : requirements) {
            boolean satisfied = false;
            for (ExportedPackage export : internalExports) {
                if (req.isSatisfied(export)) {
                    satisfied = true;
                    break;
                }
            }
            //If we didn't find a match then it must come from outside
            if (!satisfied)
                result.add(req);
        }
        PackageRequirementMerger merger = new PackageRequirementMerger(result);
        if (!merger.isMergeSuccessful()) {
            List<String> pkgNames = new ArrayList<String>(merger.getInvalidRequirements());
            StringBuilder buff = new StringBuilder();
            for (String pkgName : merger.getInvalidRequirements()) {
                buff.append(pkgName).append(", ");
            }
            int buffLen = buff.length();
            String pkgString = (buffLen > 0 ? buff.substring(0, buffLen - 2) : "");
            ResolverException re = new ResolverException(MessageUtil.getMessage("INCOMPATIBLE_PACKAGE_VERSION_REQUIREMENTS", new Object[] { assetName, pkgString }));
            re.setUnsatisfiedRequirements(pkgNames);
            logger.debug(LOG_EXIT, "getExternalPackageRequirements", re);
            throw re;
        }
        result = merger.getMergedRequirements();
        cachedExternalRequirements = result;
    }
    logger.debug(LOG_EXIT, "getExternalPackageRequirements", result);
    return result;
}
Also used : ResolverException(org.apache.aries.application.management.ResolverException) ArrayList(java.util.ArrayList) ImportedPackage(org.apache.aries.application.modelling.ImportedPackage) PackageRequirementMerger(org.apache.aries.application.modelling.internal.PackageRequirementMerger) ModelledResource(org.apache.aries.application.modelling.ModelledResource) ExportedPackage(org.apache.aries.application.modelling.ExportedPackage)

Aggregations

ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)23 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)9 PackageRequirementMerger (org.apache.aries.application.modelling.internal.PackageRequirementMerger)7 ExportedPackage (org.apache.aries.application.modelling.ExportedPackage)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 ResolverException (org.apache.aries.application.management.ResolverException)3 ModelledResource (org.apache.aries.application.modelling.ModelledResource)3 Map (java.util.Map)2 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)2 ImportedBundle (org.apache.aries.application.modelling.ImportedBundle)2 ImportedService (org.apache.aries.application.modelling.ImportedService)2 VersionRange (org.apache.aries.util.VersionRange)2 List (java.util.List)1 Content (org.apache.aries.application.Content)1 PreResolveHook (org.apache.aries.application.management.spi.resolve.PreResolveHook)1 DeployedBundles (org.apache.aries.application.modelling.DeployedBundles)1 ExportedService (org.apache.aries.application.modelling.ExportedService)1 ImportedPackageImpl (org.apache.aries.application.modelling.impl.ImportedPackageImpl)1