use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Analyzer method augmentImports.
/**
* Find some more information about imports in manifest and other places. It
* is assumed that the augmentsExports has already copied external attrs
* from the classpathExports.
*
* @throws Exception
*/
void augmentImports(Packages imports, Packages exports) throws Exception {
List<PackageRef> noimports = Create.list();
Set<PackageRef> provided = findProvidedPackages();
for (PackageRef packageRef : imports.keySet()) {
String packageName = packageRef.getFQN();
setProperty(CURRENT_PACKAGE, packageName);
try {
Attrs defaultAttrs = new Attrs();
Attrs importAttributes = imports.get(packageRef);
Attrs exportAttributes = exports.get(packageRef, classpathExports.get(packageRef, defaultAttrs));
String exportVersion = exportAttributes.getVersion();
String importRange = importAttributes.getVersion();
if (check(Check.IMPORTS)) {
if (exportAttributes == defaultAttrs) {
warning("Import package %s not found in any bundle on the -buildpath. List explicitly in Import-Package: p,* to get rid of this warning if false", packageRef);
continue;
}
if (!exportAttributes.containsKey(INTERNAL_EXPORTED_DIRECTIVE) && !exports.containsKey(packageRef)) {
warning("'%s' is a private package import from %s", packageRef, exportAttributes.get(INTERNAL_SOURCE_DIRECTIVE));
continue;
}
}
//
if (contracts.isContracted(packageRef)) {
// yes, contract based, so remove
// the version and don't do the
// version policy stuff
importAttributes.remove(VERSION_ATTRIBUTE);
continue;
}
if (exportVersion == null) {
// TODO Should check if the source is from a bundle.
} else {
//
// Version Policy - Import version substitution. We
// calculate the export version and then allow the
// import version attribute to use it in a substitution
// by using a ${@} macro. The export version can
// be defined externally or locally
//
boolean provider = isTrue(importAttributes.get(PROVIDE_DIRECTIVE)) || isTrue(exportAttributes.get(PROVIDE_DIRECTIVE)) || provided.contains(packageRef);
exportVersion = cleanupVersion(exportVersion);
importRange = applyVersionPolicy(exportVersion, importRange, provider);
if (!importRange.trim().isEmpty()) {
importAttributes.put(VERSION_ATTRIBUTE, importRange);
}
}
//
// Check if exporter has mandatory attributes
//
String mandatory = exportAttributes.get(MANDATORY_DIRECTIVE);
if (mandatory != null) {
String[] attrs = mandatory.split("\\s*,\\s*");
for (int i = 0; i < attrs.length; i++) {
if (!importAttributes.containsKey(attrs[i]))
importAttributes.put(attrs[i], exportAttributes.get(attrs[i]));
}
}
if (exportAttributes.containsKey(IMPORT_DIRECTIVE))
importAttributes.put(IMPORT_DIRECTIVE, exportAttributes.get(IMPORT_DIRECTIVE));
fixupAttributes(packageRef, importAttributes);
removeAttributes(importAttributes);
String result = importAttributes.get(Constants.VERSION_ATTRIBUTE);
if (result == null || !Verifier.isVersionRange(result))
noimports.add(packageRef);
} finally {
unsetProperty(CURRENT_PACKAGE);
}
}
if (isPedantic() && noimports.size() != 0) {
warning("Imports that lack version ranges: %s", noimports);
}
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Analyzer method divideRegularAndDynamicImports.
Pair<Packages, Parameters> divideRegularAndDynamicImports() {
Packages regularImports = new Packages(imports);
Parameters dynamicImports = getDynamicImportPackage();
Iterator<Entry<PackageRef, Attrs>> regularImportsIterator = regularImports.entrySet().iterator();
while (regularImportsIterator.hasNext()) {
Entry<PackageRef, Attrs> packageEntry = regularImportsIterator.next();
PackageRef packageRef = packageEntry.getKey();
Attrs attrs = packageEntry.getValue();
String resolution = attrs.get(Constants.RESOLUTION_DIRECTIVE);
if (Constants.RESOLUTION_DYNAMIC.equals(resolution)) {
attrs.remove(Constants.RESOLUTION_DIRECTIVE);
dynamicImports.put(packageRef.fqn, attrs);
regularImportsIterator.remove();
}
}
return new Pair<>(regularImports, dynamicImports);
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Analyzer method getPackages.
public Collection<PackageRef> getPackages(Packages scope, String... args) throws Exception {
List<PackageRef> pkgs = new LinkedList<PackageRef>();
Packages.QUERY queryType;
Instruction instr;
if (args.length == 1) {
queryType = null;
instr = null;
} else if (args.length >= 2) {
queryType = Packages.QUERY.valueOf(args[1].toUpperCase());
if (args.length > 2)
instr = new Instruction(args[2]);
else
instr = null;
} else {
throw new IllegalArgumentException("${packages} macro: invalid argument count");
}
for (Entry<PackageRef, Attrs> entry : scope.entrySet()) {
PackageRef pkg = entry.getKey();
TypeRef pkgInfoTypeRef = getTypeRefFromFQN(pkg.getFQN() + ".package-info");
Clazz pkgInfo = classspace.get(pkgInfoTypeRef);
boolean accept = false;
if (queryType != null) {
switch(queryType) {
case ANY:
accept = true;
break;
case NAMED:
if (instr == null)
throw new IllegalArgumentException("Not enough arguments in ${packages} macro");
accept = instr.matches(pkg.getFQN()) ^ instr.isNegated();
break;
case ANNOTATED:
if (instr == null)
throw new IllegalArgumentException("Not enough arguments in ${packages} macro");
accept = pkgInfo != null && pkgInfo.is(Clazz.QUERY.ANNOTATED, instr, this);
break;
case VERSIONED:
accept = entry.getValue().getVersion() != null;
break;
}
} else {
accept = true;
}
if (accept)
pkgs.add(pkg);
}
return pkgs;
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Builder method doExpand.
/**
* Destructively filter the packages from the build up index. This index is
* used by the Export Package as well as the Private Package
*
* @param jar
* @param name
* @param instructions
* @throws Exception
*/
private Set<Instruction> doExpand(Jar jar, MultiMap<String, Jar> index, Instructions filter) throws Exception {
Set<Instruction> unused = Create.set();
for (Entry<Instruction, Attrs> e : filter.entrySet()) {
Instruction instruction = e.getKey();
if (instruction.isDuplicate())
continue;
Attrs directives = e.getValue();
// We can optionally filter on the
// source of the package. We assume
// they all match but this can be overridden
// on the instruction
Instruction from = new Instruction(directives.get(FROM_DIRECTIVE, "*"));
boolean used = false;
for (Iterator<Entry<String, List<Jar>>> entry = index.entrySet().iterator(); entry.hasNext(); ) {
Entry<String, List<Jar>> p = entry.next();
String directory = p.getKey();
PackageRef packageRef = getPackageRef(directory);
// Skip * and meta data, we're talking packages!
if (packageRef.isMetaData() && instruction.isAny())
continue;
if (!instruction.matches(packageRef.getFQN()))
continue;
// Ensure it is never matched again
entry.remove();
// includes exports)
if (instruction.isNegated()) {
used = true;
continue;
}
// Do the from: directive, filters on the JAR type
List<Jar> providers = filterFrom(from, p.getValue());
if (providers.isEmpty())
continue;
int splitStrategy = getSplitStrategy(directives.get(SPLIT_PACKAGE_DIRECTIVE));
copyPackage(jar, providers, directory, splitStrategy);
Attrs contained = getContained().put(packageRef);
contained.put(INTERNAL_SOURCE_DIRECTIVE, getName(providers.get(0)));
used = true;
}
if (!used && !isTrue(directives.get("optional:")))
unused.add(instruction);
}
return unused;
}
use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.
the class Builder method getExtra.
/**
* Answer extra packages. In this case we implement conditional package. Any
*/
@Override
protected Jar getExtra() throws Exception {
Parameters conditionals = getMergedParameters(CONDITIONAL_PACKAGE);
conditionals.putAll(getMergedParameters(CONDITIONALPACKAGE));
if (conditionals.isEmpty())
return null;
logger.debug("do Conditional Package {}", conditionals);
Instructions instructions = new Instructions(conditionals);
Collection<PackageRef> referred = instructions.select(getReferred().keySet(), false);
referred.removeAll(getContained().keySet());
if (referred.isEmpty()) {
logger.debug("no additional conditional packages to add");
return null;
}
Jar jar = new Jar("conditional-import");
addClose(jar);
for (PackageRef pref : referred) {
for (Jar cpe : getClasspath()) {
Map<String, Resource> map = cpe.getDirectories().get(pref.getPath());
if (map != null) {
copy(jar, cpe, pref.getPath(), false);
break;
}
}
}
if (jar.getDirectories().size() == 0) {
logger.debug("extra dirs {}", jar.getDirectories());
return null;
}
return jar;
}
Aggregations