Search in sources :

Example 1 with Packages

use of aQute.bnd.osgi.Packages in project felix by apache.

the class BundlePlugin method addLocalPackages.

private static void addLocalPackages(File outputDirectory, Analyzer analyzer) throws IOException {
    Packages packages = new Packages();
    if (outputDirectory != null && outputDirectory.isDirectory()) {
        // scan classes directory for potential packages
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(outputDirectory);
        scanner.setIncludes(new String[] { "**/*.class" });
        scanner.addDefaultExcludes();
        scanner.scan();
        String[] paths = scanner.getIncludedFiles();
        for (int i = 0; i < paths.length; i++) {
            packages.put(analyzer.getPackageRef(getPackageName(paths[i])));
        }
    }
    Packages exportedPkgs = new Packages();
    Packages privatePkgs = new Packages();
    boolean noprivatePackages = "!*".equals(analyzer.getProperty(Analyzer.PRIVATE_PACKAGE));
    for (PackageRef pkg : packages.keySet()) {
        // mark all source packages as private by default (can be overridden by export list)
        privatePkgs.put(pkg);
        // we can't export the default package (".") and we shouldn't export internal packages
        String fqn = pkg.getFQN();
        if (noprivatePackages || !(".".equals(fqn) || fqn.contains(".internal") || fqn.contains(".impl"))) {
            exportedPkgs.put(pkg);
        }
    }
    Properties properties = analyzer.getProperties();
    String exported = properties.getProperty(Analyzer.EXPORT_PACKAGE);
    if (exported == null) {
        if (!properties.containsKey(Analyzer.EXPORT_CONTENTS)) {
            // no -exportcontents overriding the exports, so use our computed list
            for (Attrs attrs : exportedPkgs.values()) {
                attrs.put(Constants.SPLIT_PACKAGE_DIRECTIVE, "merge-first");
            }
            properties.setProperty(Analyzer.EXPORT_PACKAGE, Processor.printClauses(exportedPkgs));
        } else {
            // leave Export-Package empty (but non-null) as we have -exportcontents
            properties.setProperty(Analyzer.EXPORT_PACKAGE, "");
        }
    } else if (exported.indexOf(LOCAL_PACKAGES) >= 0) {
        String newExported = StringUtils.replace(exported, LOCAL_PACKAGES, Processor.printClauses(exportedPkgs));
        properties.setProperty(Analyzer.EXPORT_PACKAGE, newExported);
    }
    String internal = properties.getProperty(Analyzer.PRIVATE_PACKAGE);
    if (internal == null) {
        if (!privatePkgs.isEmpty()) {
            for (Attrs attrs : privatePkgs.values()) {
                attrs.put(Constants.SPLIT_PACKAGE_DIRECTIVE, "merge-first");
            }
            properties.setProperty(Analyzer.PRIVATE_PACKAGE, Processor.printClauses(privatePkgs));
        } else {
            // if there are really no private packages then use "!*" as this will keep the Bnd Tool happy
            properties.setProperty(Analyzer.PRIVATE_PACKAGE, "!*");
        }
    } else if (internal.indexOf(LOCAL_PACKAGES) >= 0) {
        String newInternal = StringUtils.replace(internal, LOCAL_PACKAGES, Processor.printClauses(privatePkgs));
        properties.setProperty(Analyzer.PRIVATE_PACKAGE, newInternal);
    }
}
Also used : Packages(aQute.bnd.osgi.Packages) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Properties(java.util.Properties)

Example 2 with Packages

use of aQute.bnd.osgi.Packages in project bnd by bndtools.

the class bnd method doPrint.

private void doPrint(Jar jar, int options, printOptions po) throws ZipException, IOException, Exception {
    if ((options & VERIFY) != 0) {
        Verifier verifier = new Verifier(jar);
        verifier.setPedantic(isPedantic());
        verifier.verify();
        getInfo(verifier);
    }
    if ((options & MANIFEST) != 0) {
        Manifest manifest = jar.getManifest();
        if (manifest == null)
            warning("JAR has no manifest %s", jar);
        else {
            err.println("[MANIFEST " + jar.getName() + "]");
            printManifest(manifest);
        }
        out.println();
    }
    if ((options & IMPEXP) != 0) {
        out.println("[IMPEXP]");
        Manifest m = jar.getManifest();
        Domain domain = Domain.domain(m);
        if (m != null) {
            Parameters imports = domain.getImportPackage();
            Parameters exports = domain.getExportPackage();
            for (String p : exports.keySet()) {
                if (imports.containsKey(p)) {
                    Attrs attrs = imports.get(p);
                    if (attrs.containsKey(VERSION_ATTRIBUTE)) {
                        exports.get(p).put("imported-as", attrs.get(VERSION_ATTRIBUTE));
                    }
                }
            }
            print(Constants.IMPORT_PACKAGE, new TreeMap<String, Attrs>(imports));
            print(Constants.EXPORT_PACKAGE, new TreeMap<String, Attrs>(exports));
        } else
            warning("File has no manifest");
    }
    if ((options & CAPABILITIES) != 0) {
        out.println("[CAPABILITIES]");
        Manifest m = jar.getManifest();
        Domain domain = Domain.domain(m);
        if (m != null) {
            Parameters provide = domain.getProvideCapability();
            Parameters require = domain.getRequireCapability();
            print(Constants.PROVIDE_CAPABILITY, new TreeMap<String, Attrs>(provide));
            print(Constants.REQUIRE_CAPABILITY, new TreeMap<String, Attrs>(require));
        } else
            warning("File has no manifest");
    }
    if ((options & (USES | USEDBY | API)) != 0) {
        out.println();
        try (Analyzer analyzer = new Analyzer()) {
            analyzer.setPedantic(isPedantic());
            analyzer.setJar(jar);
            Manifest m = jar.getManifest();
            if (m != null) {
                String s = m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE);
                if (s != null)
                    analyzer.setExportPackage(s);
            }
            analyzer.analyze();
            boolean java = po.java();
            Packages exports = analyzer.getExports();
            if ((options & API) != 0) {
                Map<PackageRef, List<PackageRef>> apiUses = analyzer.cleanupUses(analyzer.getAPIUses(), !po.java());
                if (!po.xport()) {
                    if (exports.isEmpty())
                        warning("Not filtering on exported only since exports are empty");
                    else
                        apiUses.keySet().retainAll(analyzer.getExports().keySet());
                }
                out.println("[API USES]");
                printMultiMap(apiUses);
                Set<PackageRef> privates = analyzer.getPrivates();
                for (PackageRef export : exports.keySet()) {
                    Map<Def, List<TypeRef>> xRef = analyzer.getXRef(export, privates, Modifier.PROTECTED + Modifier.PUBLIC);
                    if (!xRef.isEmpty()) {
                        out.println();
                        out.printf("%s refers to private Packages (not good)\n\n", export);
                        for (Entry<Def, List<TypeRef>> e : xRef.entrySet()) {
                            TreeSet<PackageRef> refs = new TreeSet<Descriptors.PackageRef>();
                            for (TypeRef ref : e.getValue()) refs.add(ref.getPackageRef());
                            refs.retainAll(privates);
                            //
                            out.printf(//
                            "%60s %-40s %s\n", //
                            e.getKey().getOwnerType().getFQN(), e.getKey().getName(), refs);
                        }
                        out.println();
                    }
                }
                out.println();
            }
            Map<PackageRef, List<PackageRef>> uses = analyzer.cleanupUses(analyzer.getUses(), !po.java());
            if ((options & USES) != 0) {
                out.println("[USES]");
                printMultiMap(uses);
                out.println();
            }
            if ((options & USEDBY) != 0) {
                out.println("[USEDBY]");
                MultiMap<PackageRef, PackageRef> usedBy = new MultiMap<Descriptors.PackageRef, Descriptors.PackageRef>(uses).transpose();
                printMultiMap(usedBy);
            }
        }
    }
    if ((options & COMPONENT) != 0) {
        printComponents(out, jar);
    }
    if ((options & METATYPE) != 0) {
        printMetatype(out, jar);
    }
    if ((options & LIST) != 0) {
        out.println("[LIST]");
        for (Map.Entry<String, Map<String, Resource>> entry : jar.getDirectories().entrySet()) {
            String name = entry.getKey();
            Map<String, Resource> contents = entry.getValue();
            out.println(name);
            if (contents != null) {
                for (String element : contents.keySet()) {
                    int n = element.lastIndexOf('/');
                    if (n > 0)
                        element = element.substring(n + 1);
                    out.print("  ");
                    out.print(element);
                    String path = element;
                    if (name.length() != 0)
                        path = name + "/" + element;
                    Resource r = contents.get(path);
                    if (r != null) {
                        String extra = r.getExtra();
                        if (extra != null) {
                            out.print(" extra='" + escapeUnicode(extra) + "'");
                        }
                    }
                    out.println();
                }
            } else {
                out.println(name + " <no contents>");
            }
        }
        out.println();
    }
}
Also used : TypeRef(aQute.bnd.osgi.Descriptors.TypeRef) Attrs(aQute.bnd.header.Attrs) Verifier(aQute.bnd.osgi.Verifier) Analyzer(aQute.bnd.osgi.Analyzer) Packages(aQute.bnd.osgi.Packages) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ExtList(aQute.lib.collections.ExtList) List(java.util.List) SortedList(aQute.lib.collections.SortedList) Descriptors(aQute.bnd.osgi.Descriptors) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Parameters(aQute.bnd.header.Parameters) Def(aQute.bnd.osgi.Clazz.Def) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Domain(aQute.bnd.osgi.Domain) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 3 with Packages

use of aQute.bnd.osgi.Packages in project bnd by bndtools.

the class BuilderTest method testImportRangeCalculatedFromClasspath_1.

/**
	 * Check if we imported the package with the correct version range when
	 * there's an empty package in front of it in the classpath. First form.
	 */
public static void testImportRangeCalculatedFromClasspath_1() throws Exception {
    Properties base = new Properties();
    base.put(Analyzer.IMPORT_PACKAGE, "javax.servlet,javax.servlet.http");
    Builder analyzer = new Builder();
    try {
        analyzer.addClasspath(new File("bin"));
        analyzer.setPrivatePackage("test");
        analyzer.setClasspath(new File[] { IO.getFile("jar/jsp-api.jar"), IO.getFile("jar/servlet-api.jar") });
        analyzer.setProperties(base);
        analyzer.build();
        assertTrue(analyzer.check());
        Packages imports = analyzer.getImports();
        Attrs attrs = imports.getByFQN("javax.servlet.http");
        assertEquals("[3.0,4)", attrs.getVersion());
        attrs = imports.getByFQN("javax.servlet");
        assertEquals("[3.0,4)", attrs.getVersion());
    } finally {
        analyzer.close();
    }
}
Also used : Packages(aQute.bnd.osgi.Packages) Builder(aQute.bnd.osgi.Builder) Attrs(aQute.bnd.header.Attrs) Properties(java.util.Properties) File(java.io.File)

Example 4 with Packages

use of aQute.bnd.osgi.Packages in project bnd by bndtools.

the class ContractTest method testSimple.

public void testSimple() throws Exception {
    Jar bjar = getContractExporter("test", "2.5", "${exports}");
    Builder a = newBuilder();
    a.setTrace(true);
    a.addClasspath(bjar);
    a.setProperty(Constants.CONTRACT, "*");
    a.setImportPackage("org.osgi.service.cm,*");
    a.setProperty("Export-Package", "test.refer");
    Jar ajar = a.build();
    assertTrue(a.check());
    Domain domain = Domain.domain(ajar.getManifest());
    Parameters rc = domain.getRequireCapability();
    rc.remove("osgi.ee");
    System.out.println(rc);
    assertEquals(1, rc.size());
    Packages ps = a.getImports();
    assertTrue(ps.containsFQN("org.osgi.service.cm"));
    Attrs attrs = ps.getByFQN("org.osgi.service.cm");
    assertNotNull(attrs);
    assertNull(attrs.getVersion());
}
Also used : Parameters(aQute.bnd.header.Parameters) Packages(aQute.bnd.osgi.Packages) Builder(aQute.bnd.osgi.Builder) Attrs(aQute.bnd.header.Attrs) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain)

Example 5 with Packages

use of aQute.bnd.osgi.Packages in project bnd by bndtools.

the class BuilderTest method testImportRangeCalculatedFromClasspath_2.

/**
	 * Check if we imported the package with the correct version range when
	 * there's an empty package in front of it in the classpath. Second form.
	 */
public static void testImportRangeCalculatedFromClasspath_2() throws Exception {
    Properties base = new Properties();
    base.put(Analyzer.IMPORT_PACKAGE, "javax.servlet,javax.servlet.http");
    String pwd = System.getProperty("user.dir");
    base.put("pwd", new File(pwd).toURI().toString());
    base.put("-classpath", "${pwd}/jar/jsp-api.jar,${pwd}/jar/servlet-api.jar");
    Builder analyzer = new Builder();
    try {
        analyzer.addClasspath(new File("bin"));
        analyzer.setPrivatePackage("test");
        analyzer.setProperties(base);
        analyzer.build();
        assertTrue(analyzer.check());
        Packages imports = analyzer.getImports();
        Attrs attrs = imports.getByFQN("javax.servlet.http");
        assertEquals("[3.0,4)", attrs.getVersion());
        attrs = imports.getByFQN("javax.servlet");
        assertEquals("[3.0,4)", attrs.getVersion());
    } finally {
        analyzer.close();
    }
}
Also used : Packages(aQute.bnd.osgi.Packages) Builder(aQute.bnd.osgi.Builder) Attrs(aQute.bnd.header.Attrs) Properties(java.util.Properties) File(java.io.File)

Aggregations

Packages (aQute.bnd.osgi.Packages)12 Builder (aQute.bnd.osgi.Builder)9 Attrs (aQute.bnd.header.Attrs)7 File (java.io.File)7 Properties (java.util.Properties)7 Analyzer (aQute.bnd.osgi.Analyzer)4 Jar (aQute.bnd.osgi.Jar)4 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)3 Parameters (aQute.bnd.header.Parameters)2 Domain (aQute.bnd.osgi.Domain)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Manifest (java.util.jar.Manifest)2 PomFromManifest (aQute.bnd.maven.PomFromManifest)1 Def (aQute.bnd.osgi.Clazz.Def)1 Descriptors (aQute.bnd.osgi.Descriptors)1 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)1 FileResource (aQute.bnd.osgi.FileResource)1 Resource (aQute.bnd.osgi.Resource)1 Verifier (aQute.bnd.osgi.Verifier)1 AnalyzerPlugin (aQute.bnd.service.AnalyzerPlugin)1