Search in sources :

Example 1 with ImportedPackageImpl

use of org.apache.aries.application.modelling.impl.ImportedPackageImpl 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)

Aggregations

HashMap (java.util.HashMap)1 InvalidAttributeException (org.apache.aries.application.InvalidAttributeException)1 ImportedPackage (org.apache.aries.application.modelling.ImportedPackage)1 ImportedPackageImpl (org.apache.aries.application.modelling.impl.ImportedPackageImpl)1 VersionRange (org.apache.aries.util.VersionRange)1