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;
}
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;
}
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");
}
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);
}
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;
}
Aggregations