Search in sources :

Example 1 with Verifier

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

the class RemoteCommand method _distro.

public void _distro(DistroOptions opts) throws Exception {
    List<String> args = opts._arguments();
    String bsn;
    String version;
    bsn = args.remove(0);
    if (!Verifier.isBsn(bsn)) {
        error("Not a bundle symbolic name %s", bsn);
    }
    if (args.isEmpty())
        version = "0";
    else {
        version = args.remove(0);
        if (!Version.isVersion(version)) {
            error("Invalid version %s", version);
        }
    }
    File output = getFile(opts.output("distro.jar"));
    if (output.getParentFile() == null || !output.getParentFile().isDirectory()) {
        error("Cannot write to %s because parent not a directory", output);
    }
    if (output.isFile() && !output.canWrite()) {
        error("Cannot write to %s", output);
    }
    logger.debug("Starting distro {};{}", bsn, version);
    List<BundleRevisionDTO> bundleRevisons = agent.getBundleRevisons();
    logger.debug("Found {} bundle revisions", bundleRevisons.size());
    Parameters packages = new Parameters();
    List<Parameters> provided = new ArrayList<>();
    for (BundleRevisionDTO brd : bundleRevisons) {
        for (CapabilityDTO c : brd.capabilities) {
            CapabilityBuilder cb = new CapabilityBuilder(c.namespace);
            for (Entry<String, Object> e : c.attributes.entrySet()) {
                String key = e.getKey();
                Object value = e.getValue();
                if (key.equals("version")) {
                    if (value instanceof Collection || value.getClass().isArray())
                        value = Converter.cnv(tref, value);
                    else
                        value = new Version((String) value);
                }
                cb.addAttribute(key, value);
            }
            cb.addDirectives(c.directives);
            Attrs attrs = cb.toAttrs();
            if (cb.isPackage()) {
                attrs.remove(Constants.BUNDLE_SYMBOLIC_NAME_ATTRIBUTE);
                attrs.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
                String pname = attrs.remove(PackageNamespace.PACKAGE_NAMESPACE);
                if (pname == null) {
                    warning("Invalid package capability found %s", c);
                } else
                    packages.put(pname, attrs);
                logger.debug("P: {};{}", pname, attrs);
            } else if (NativeNamespace.NATIVE_NAMESPACE.equals(c.namespace)) {
                Attrs newAttrs = new Attrs();
                for (Entry<String, String> entry : attrs.entrySet()) {
                    if (entry.getKey().startsWith(NativeNamespace.NATIVE_NAMESPACE)) {
                        newAttrs.put(entry.getKey(), entry.getValue());
                    }
                }
                Parameters p = new Parameters();
                p.put(c.namespace, newAttrs);
                provided.add(p);
            } else if (!IGNORED_NAMESPACES.contains(c.namespace)) {
                logger.debug("C {};{}", c.namespace, attrs);
                Parameters p = new Parameters();
                p.put(c.namespace, attrs);
                provided.add(p);
            }
        }
    }
    if (isOk()) {
        Manifest m = new Manifest();
        Attributes main = m.getMainAttributes();
        main.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
        main.putValue(Constants.BUNDLE_SYMBOLICNAME, bsn);
        main.putValue(Constants.BUNDLE_VERSION, version);
        main.putValue(Constants.EXPORT_PACKAGE, packages.toString());
        // Make distro unresolvable
        Parameters unresolveable = new Parameters("osgi.unresolvable; filter:='(&(must.not.resolve=*)(!(must.not.resolve=*)))'");
        main.putValue(Constants.REQUIRE_CAPABILITY, unresolveable.toString());
        provided.add(new Parameters("osgi.unresolvable"));
        StringBuilder sb = new StringBuilder();
        for (Parameters parameter : provided) {
            sb.append(parameter.toString());
            sb.append(",");
        }
        String capabilities = sb.toString().substring(0, sb.length() - 1);
        main.putValue(Constants.PROVIDE_CAPABILITY, capabilities);
        if (opts.description() != null)
            main.putValue(Constants.BUNDLE_DESCRIPTION, opts.description());
        if (opts.license() != null)
            main.putValue(Constants.BUNDLE_LICENSE, opts.license());
        if (opts.copyright() != null)
            main.putValue(Constants.BUNDLE_COPYRIGHT, opts.copyright());
        if (opts.vendor() != null)
            main.putValue(Constants.BUNDLE_VENDOR, opts.vendor());
        Jar jar = new Jar("distro");
        jar.setManifest(m);
        Verifier v = new Verifier(jar);
        v.setProperty(Constants.FIXUPMESSAGES, "osgi.* namespace must not be specified with generic capabilities");
        v.verify();
        v.getErrors();
        if (isFailOk() || v.isOk()) {
            jar.updateModified(System.currentTimeMillis(), "Writing distro jar");
            jar.write(output);
        } else
            getInfo(v);
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) BundleRevisionDTO(org.osgi.framework.wiring.dto.BundleRevisionDTO) ArrayList(java.util.ArrayList) Attrs(aQute.bnd.header.Attrs) Attributes(java.util.jar.Attributes) CapabilityBuilder(aQute.bnd.osgi.resource.CapabilityBuilder) Manifest(java.util.jar.Manifest) Verifier(aQute.bnd.osgi.Verifier) Entry(java.util.Map.Entry) CapabilityDTO(org.osgi.resource.dto.CapabilityDTO) Version(aQute.bnd.version.Version) Collection(java.util.Collection) Jar(aQute.bnd.osgi.Jar) File(java.io.File)

Example 2 with Verifier

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

the class NativeHeader method testSimple.

public static void testSimple() throws Exception {
    Verifier v = new Verifier(b);
    v.doNative("\rnative/libclib_jiio.so ;\r" + "native/libmlib_jai.so;\r" + "osname=Linux ;\r" + "processor=amd64\r");
    assertOk(v);
}
Also used : Verifier(aQute.bnd.osgi.Verifier)

Example 3 with Verifier

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

the class NativeHeader method testFunnyHeader.

public static void testFunnyHeader() throws Exception {
    Verifier v = new Verifier(b);
    v.doNative("org/osgi/test/cases/framework/fragments/tb8/linux_x86/libNative.so; osname=Linux; processor=x86; osversion=\"(1000,10000]\",");
    assertBad(v, "name");
}
Also used : Verifier(aQute.bnd.osgi.Verifier)

Example 4 with Verifier

use of aQute.bnd.osgi.Verifier 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 5 with Verifier

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

the class BuilderTest method testVerify.

public void testVerify() throws Exception {
    System.err.println("Erroneous bundle: tb1.jar");
    Jar jar = new Jar("test", getClass().getResourceAsStream("tb1.jar"));
    Verifier verifier = new Verifier(jar);
    verifier.verify();
    assertTrue(verifier.check());
    jar.close();
    verifier.close();
}
Also used : Jar(aQute.bnd.osgi.Jar) Verifier(aQute.bnd.osgi.Verifier)

Aggregations

Verifier (aQute.bnd.osgi.Verifier)15 Jar (aQute.bnd.osgi.Jar)6 File (java.io.File)6 Builder (aQute.bnd.osgi.Builder)5 Manifest (java.util.jar.Manifest)4 Attrs (aQute.bnd.header.Attrs)3 Parameters (aQute.bnd.header.Parameters)3 Analyzer (aQute.bnd.osgi.Analyzer)2 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)2 Domain (aQute.bnd.osgi.Domain)2 Resource (aQute.bnd.osgi.Resource)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)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