Search in sources :

Example 6 with PackageRef

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);
    }
}
Also used : Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 7 with PackageRef

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);
}
Also used : Entry(java.util.Map.Entry) Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Pair(aQute.libg.tuple.Pair)

Example 8 with PackageRef

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;
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) LinkedList(java.util.LinkedList)

Example 9 with PackageRef

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;
}
Also used : Attrs(aQute.bnd.header.Attrs) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) List(java.util.List) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 10 with PackageRef

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;
}
Also used : Parameters(aQute.bnd.header.Parameters) PomPropertiesResource(aQute.bnd.maven.PomPropertiesResource) PomResource(aQute.bnd.maven.PomResource) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Aggregations

PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)40 Attrs (aQute.bnd.header.Attrs)18 Analyzer (aQute.bnd.osgi.Analyzer)9 Clazz (aQute.bnd.osgi.Clazz)8 Map (java.util.Map)8 Parameters (aQute.bnd.header.Parameters)7 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)7 MultiMap (aQute.lib.collections.MultiMap)7 HashSet (java.util.HashSet)7 TreeSet (java.util.TreeSet)7 Manifest (java.util.jar.Manifest)7 Jar (aQute.bnd.osgi.Jar)6 Resource (aQute.bnd.osgi.Resource)6 Descriptors (aQute.bnd.osgi.Descriptors)5 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)4 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 Entry (java.util.Map.Entry)4