Search in sources :

Example 26 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Analyzer method getManifestInfoFromClasspath.

/**
	 * @param jar
	 * @param contracts
	 * @param contracted
	 */
private void getManifestInfoFromClasspath(Jar jar, Packages classpathExports, Contracts contracts) {
    try {
        Manifest m = jar.getManifest();
        if (m != null) {
            Domain domain = Domain.domain(m);
            Parameters exported = domain.getExportPackage();
            for (Entry<String, Attrs> e : exported.entrySet()) {
                PackageRef ref = getPackageRef(e.getKey());
                if (!classpathExports.containsKey(ref)) {
                    e.getValue().put(Constants.INTERNAL_EXPORTED_DIRECTIVE, jar.getBsn() + "-" + jar.getVersion());
                    Attrs attrs = e.getValue();
                    //
                    // Fixup any old style specification versions
                    //
                    fixupOldStyleVersions(attrs);
                    classpathExports.put(ref, attrs);
                }
            }
            //
            // Collect any declared contracts
            //
            Parameters pcs = domain.getProvideCapability();
            contracts.collectContracts(jar.getName(), pcs);
        }
    } catch (Exception e) {
        warning("Erroneous Manifest for %s %s", jar, e);
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) Manifest(java.util.jar.Manifest) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 27 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Analyzer method calcManifest.

/**
	 * One of the main workhorses of this class. This will analyze the current
	 * setup and calculate a new manifest according to this setup.
	 * 
	 * @throws IOException
	 */
public Manifest calcManifest() throws Exception {
    try {
        analyze();
        Manifest manifest = new Manifest();
        Attributes main = manifest.getMainAttributes();
        main.put(Attributes.Name.MANIFEST_VERSION, "1.0");
        main.putValue(BUNDLE_MANIFESTVERSION, "2");
        boolean noExtraHeaders = "true".equalsIgnoreCase(getProperty(NOEXTRAHEADERS));
        if (!noExtraHeaders) {
            main.putValue(CREATED_BY, System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")");
            main.putValue(TOOL, "Bnd-" + getBndVersion());
            main.putValue(BND_LASTMODIFIED, "" + System.currentTimeMillis());
        }
        String exportHeader = printClauses(exports, true);
        if (exportHeader.length() > 0)
            main.putValue(EXPORT_PACKAGE, exportHeader);
        else
            main.remove(EXPORT_PACKAGE);
        // Divide imports with resolution:=dynamic to DynamicImport-Package
        // and add them to the existing DynamicImport-Package instruction
        Pair<Packages, Parameters> regularAndDynamicImports = divideRegularAndDynamicImports();
        Packages regularImports = regularAndDynamicImports.getFirst();
        if (!regularImports.isEmpty()) {
            main.putValue(IMPORT_PACKAGE, printClauses(regularImports));
        } else {
            main.remove(IMPORT_PACKAGE);
        }
        Parameters dynamicImports = regularAndDynamicImports.getSecond();
        if (!dynamicImports.isEmpty()) {
            main.putValue(DYNAMICIMPORT_PACKAGE, printClauses(dynamicImports));
        } else {
            main.remove(DYNAMICIMPORT_PACKAGE);
        }
        Packages temp = new Packages(contained);
        temp.keySet().removeAll(exports.keySet());
        for (Iterator<PackageRef> i = temp.keySet().iterator(); i.hasNext(); ) {
            String binary = i.next().getBinary();
            Resource r = dot.getResource(binary);
            if (r != null)
                i.remove();
        }
        if (!temp.isEmpty())
            main.putValue(PRIVATE_PACKAGE, printClauses(temp));
        else
            main.remove(PRIVATE_PACKAGE);
        Parameters bcp = getBundleClasspath();
        if (bcp.isEmpty() || (bcp.containsKey(".") && bcp.size() == 1))
            main.remove(BUNDLE_CLASSPATH);
        else
            main.putValue(BUNDLE_CLASSPATH, printClauses(bcp));
        // ----- Require/Capabilities section
        Parameters requirements = new Parameters(annotationHeaders.getHeader(REQUIRE_CAPABILITY), this);
        Parameters capabilities = new Parameters(annotationHeaders.getHeader(PROVIDE_CAPABILITY), this);
        //
        // Do any contracts contracts
        //
        contracts.addToRequirements(requirements);
        if (//
        !isTrue(getProperty(NOEE)) && // no use otherwise
        !ees.isEmpty() && // we want people to not have to
        since(About._2_3) && // automatically add it
        !// and
        requirements.containsKey(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE)) // it
        // should
        // not
        // be
        // there
        // already
        {
            JAVA highest = ees.last();
            Attrs attrs = new Attrs();
            String filter = doEEProfiles(highest);
            attrs.put(Constants.FILTER_DIRECTIVE, filter);
            //
            // Java 1.8 introduced profiles.
            // If -eeprofile= auto | (<profile>="...")+ is set then
            // we add a
            requirements.add(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE, attrs);
        }
        if (!requirements.isEmpty())
            main.putValue(REQUIRE_CAPABILITY, requirements.toString());
        if (!capabilities.isEmpty())
            main.putValue(PROVIDE_CAPABILITY, capabilities.toString());
        // -----
        doNamesection(dot, manifest);
        for (Enumeration<?> h = getProperties().propertyNames(); h.hasMoreElements(); ) {
            String header = (String) h.nextElement();
            if (header.trim().length() == 0) {
                warning("Empty property set with value: %s", getProperties().getProperty(header));
                continue;
            }
            if (isMissingPlugin(header.trim())) {
                error("Missing plugin for command %s", header);
            }
            if (!Character.isUpperCase(header.charAt(0))) {
                if (header.charAt(0) == '@')
                    doNameSection(manifest, header);
                continue;
            }
            if (header.equals(BUNDLE_CLASSPATH) || header.equals(EXPORT_PACKAGE) || header.equals(IMPORT_PACKAGE) || header.equals(DYNAMICIMPORT_PACKAGE) || header.equals(REQUIRE_CAPABILITY) || header.equals(PROVIDE_CAPABILITY))
                continue;
            if (header.equalsIgnoreCase("Name")) {
                error("Your bnd file contains a header called 'Name'. This interferes with the manifest name section.");
                continue;
            }
            if (Verifier.HEADER_PATTERN.matcher(header).matches()) {
                doHeader(main, header);
            } else {
            // TODO should we report?
            }
        }
        //
        // Ensure we pick up any headers only set by the
        // annotations (see AnnotationHeaders)
        //
        doHeader(main, BUNDLE_LICENSE);
        doHeader(main, BUNDLE_DEVELOPERS);
        doHeader(main, BUNDLE_CONTRIBUTORS);
        doHeader(main, BUNDLE_COPYRIGHT);
        doHeader(main, BUNDLE_DOCURL);
        doHeader(main, BUNDLE_LICENSE);
        doHeader(main, BUNDLE_CATEGORY);
        // Copy old values into new manifest, when they
        // exist in the old one, but not in the new one
        merge(manifest, dot.getManifest());
        //
        // Calculate the bundle symbolic name if it is
        // not set.
        // 1. set
        // 2. name of properties file (must be != bnd.bnd)
        // 3. name of directory, which is usualy project name
        //
        String bsn = getBsn();
        if (main.getValue(BUNDLE_SYMBOLICNAME) == null) {
            main.putValue(BUNDLE_SYMBOLICNAME, bsn);
        }
        //
        if (main.getValue(BUNDLE_NAME) == null) {
            main.putValue(BUNDLE_NAME, bsn);
        }
        if (main.getValue(BUNDLE_VERSION) == null)
            main.putValue(BUNDLE_VERSION, "0");
        // Remove all the headers mentioned in -removeheaders
        Instructions instructions = new Instructions(mergeProperties(REMOVEHEADERS));
        Collection<Object> result = instructions.select(main.keySet(), false);
        main.keySet().removeAll(result);
        // dot.setManifest(manifest);
        return manifest;
    } catch (Exception e) {
        // information. So to help diagnostics. We do a full property dump
        throw new IllegalStateException("Calc manifest failed, state=\n" + getFlattenedProperties(), e);
    }
}
Also used : JAVA(aQute.bnd.osgi.Clazz.JAVA) Parameters(aQute.bnd.header.Parameters) Attributes(java.util.jar.Attributes) Attrs(aQute.bnd.header.Attrs) Manifest(java.util.jar.Manifest) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 28 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Analyzer method getUnreachable.

/**
	 * Return the set of unreachable code depending on exports and the bundle
	 * activator.
	 * 
	 */
public Set<PackageRef> getUnreachable() {
    // all
    Set<PackageRef> unreachable = new HashSet<PackageRef>(uses.keySet());
    for (Iterator<PackageRef> r = exports.keySet().iterator(); r.hasNext(); ) {
        PackageRef packageRef = r.next();
        removeTransitive(packageRef, unreachable);
    }
    if (activator != null) {
        removeTransitive(activator.getPackageRef(), unreachable);
    }
    return unreachable;
}
Also used : PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) HashSet(java.util.HashSet)

Example 29 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class AnnotationHeaders method add.

/*
	 * Adds a header. Will preprocess the text.
	 */
private void add(String name, String value) throws IOException {
    if (value == null)
        return;
    Processor next = new Processor(analyzer);
    next.setProperty("@class", current.getFQN());
    next.setProperty("@class-short", current.getClassName().getShortName());
    PackageRef pref = current.getClassName().getPackageRef();
    next.setProperty("@package", pref.getFQN());
    Attrs info = analyzer.getClasspathExports().get(pref);
    if (info == null)
        info = analyzer.getContained().get(pref);
    if (info != null && info.containsKey("version")) {
        next.setProperty("@version", info.get("version"));
    }
    Macro macro = next.getReplacer();
    /*
		 * These strings come from code, which might also be included from
		 * external parties. So we just do not want to call any system commands
		 * from these sources
		 */
    boolean prev = macro.setNosystem(true);
    try {
        value = macro.process(value);
        headers.add(name, value);
        next.close();
    } finally {
        macro.setNosystem(prev);
    }
}
Also used : Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 30 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef in project bnd by bndtools.

the class Builder method addSources.

/**
	 * @throws IOException
	 */
private void addSources(Jar dot) throws Exception {
    if (!hasSources())
        return;
    Set<PackageRef> packages = Create.set();
    for (TypeRef typeRef : getClassspace().keySet()) {
        PackageRef packageRef = typeRef.getPackageRef();
        String sourcePath = typeRef.getSourcePath();
        String packagePath = packageRef.getPath();
        boolean found = false;
        String[] fixed = { "packageinfo", "package.html", "module-info.java", "package-info.java" };
        for (File root : getSourcePath()) {
            File f = getFile(root, sourcePath);
            if (f.exists()) {
                found = true;
                if (!packages.contains(packageRef)) {
                    packages.add(packageRef);
                    for (int j = 0; j < fixed.length; j++) {
                        for (File sp : getSourcePath()) {
                            File bdir = getFile(sp, packagePath);
                            File ff = getFile(bdir, fixed[j]);
                            if (ff.isFile()) {
                                String name = "OSGI-OPT/src/" + packagePath + "/" + fixed[j];
                                dot.putResource(name, new FileResource(ff));
                                break;
                            }
                        }
                    }
                }
                if (packageRef.isDefaultPackage())
                    logger.debug("Package reference is default package");
                dot.putResource("OSGI-OPT/src/" + sourcePath, new FileResource(f));
            }
        }
        if (getSourcePath().isEmpty())
            warning("Including sources but " + SOURCEPATH + " does not contain any source directories ");
    // TODO copy from the jars where they came from
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) File(java.io.File)

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