Search in sources :

Example 11 with MultiMap

use of aQute.lib.collections.MultiMap in project bnd by bndtools.

the class BaselineCommands method _schema.

/**
	 * Create a schema of a set of jars outling the packages and their versions.
	 * This will create a list of packages with multiple versions, link to their
	 * specifications, and the deltas between versions.
	 * 
	 * <pre>
	 *  bnd package schema
	 * <file.jar>*
	 * </pre>
	 * 
	 * @param opts
	 * @throws Exception
	 */
public void _schema(schemaOptions opts) throws Exception {
    MultiMap<String, PSpec> map = new MultiMap<String, PSpec>();
    Tag top = new Tag("jschema");
    int n = 1000;
    for (String spec : opts._arguments()) {
        File f = bnd.getFile(spec);
        if (!f.isFile()) {
            bnd.messages.NoSuchFile_(f);
        } else {
            // For each specification jar we found
            logger.debug("spec {}", f);
            // spec
            Jar jar = new Jar(f);
            Manifest m = jar.getManifest();
            Attributes main = m.getMainAttributes();
            Tag specTag = new Tag(top, "specification");
            specTag.addAttribute("jar", spec);
            specTag.addAttribute("name", main.getValue("Specification-Name"));
            specTag.addAttribute("title", main.getValue("Specification-Title"));
            specTag.addAttribute("jsr", main.getValue("Specification-JSR"));
            specTag.addAttribute("url", main.getValue("Specification-URL"));
            specTag.addAttribute("version", main.getValue("Specification-Version"));
            specTag.addAttribute("vendor", main.getValue("Specification-Vendor"));
            specTag.addAttribute("id", n);
            specTag.addContent(main.getValue(Constants.BUNDLE_DESCRIPTION));
            Parameters exports = OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.EXPORT_PACKAGE));
            // Create a map with versions. Ensure import ranges overwrite
            // the
            // exported versions
            Parameters versions = new Parameters();
            versions.putAll(exports);
            versions.putAll(OSGiHeader.parseHeader(m.getMainAttributes().getValue(Constants.IMPORT_PACKAGE)));
            Analyzer analyzer = new Analyzer();
            analyzer.setJar(jar);
            analyzer.analyze();
            Tree tree = differ.tree(analyzer);
            for (Entry<String, Attrs> entry : exports.entrySet()) {
                // For each exported package in the specification JAR
                Attrs attrs = entry.getValue();
                String packageName = entry.getKey();
                PackageRef packageRef = analyzer.getPackageRef(packageName);
                String version = attrs.get(Constants.VERSION_ATTRIBUTE);
                PSpec pspec = new PSpec();
                pspec.packageName = packageName;
                pspec.version = new Version(version);
                pspec.id = n;
                pspec.attrs = attrs;
                pspec.tree = tree;
                Collection<PackageRef> uses = analyzer.getUses().get(packageRef);
                if (uses != null) {
                    for (PackageRef x : uses) {
                        if (x.isJava())
                            continue;
                        String imp = x.getFQN();
                        if (imp.equals(packageName))
                            continue;
                        String v = null;
                        if (versions.containsKey(imp))
                            v = versions.get(imp).get(Constants.VERSION_ATTRIBUTE);
                        pspec.uses.put(imp, v);
                    }
                }
                map.add(packageName, pspec);
            }
            jar.close();
            n++;
        }
    }
    // We now gather all the information about all packages in the map.
    // Next phase is generating the XML. Sorting the packages is
    // important because XSLT is brain dead.
    SortedList<String> names = new SortedList<String>(map.keySet());
    Tag packagesTag = new Tag(top, "packages");
    Tag baselineTag = new Tag(top, "baseline");
    for (String pname : names) {
        // For each distinct package name
        SortedList<PSpec> specs = new SortedList<PSpec>(map.get(pname));
        PSpec older = null;
        Parameters olderExport = null;
        for (PSpec newer : specs) {
            // For each package in the total set
            Tag pack = new Tag(packagesTag, "package");
            pack.addAttribute("name", newer.packageName);
            pack.addAttribute("version", newer.version);
            pack.addAttribute("spec", newer.id);
            Parameters newerExport = new Parameters();
            newerExport.put(pname, newer.attrs);
            if (older != null) {
                String compareId = newer.packageName + "-" + newer.id + "-" + older.id;
                pack.addAttribute("delta", compareId);
                logger.debug(" newer={} older={}", newerExport, olderExport);
                Set<Info> infos = baseline.baseline(newer.tree, newerExport, older.tree, olderExport, new Instructions(pname));
                for (Info info : infos) {
                    Tag tag = getTag(info);
                    tag.addAttribute("id", compareId);
                    tag.addAttribute("newerSpec", newer.id);
                    tag.addAttribute("olderSpec", older.id);
                    baselineTag.addContent(tag);
                }
                older.tree = null;
                older.attrs = null;
                older = newer;
            }
            for (Entry<String, String> uses : newer.uses.entrySet()) {
                Tag reference = new Tag(pack, "import");
                reference.addAttribute("name", uses.getKey());
                reference.addAttribute("version", uses.getValue());
            }
            older = newer;
            olderExport = newerExport;
        }
    }
    String o = opts.output("schema.xml");
    File of = bnd.getFile(o);
    File pof = of.getParentFile();
    IO.mkdirs(pof);
    try (PrintWriter pw = IO.writer(of, UTF_8)) {
        pw.print("<?xml version='1.0' encoding='UTF-8'?>\n");
        top.print(0, pw);
    }
    if (opts.xsl() != null) {
        URL home = bnd.getBase().toURI().toURL();
        URL xslt = new URL(home, opts.xsl());
        String path = of.getAbsolutePath();
        if (path.endsWith(".xml"))
            path = path.substring(0, path.length() - 4);
        path = path + ".html";
        File html = new File(path);
        logger.debug("xslt {} {} {} {}", xslt, of, html, html.exists());
        try (OutputStream out = IO.outputStream(html);
            InputStream in = xslt.openStream()) {
            Transformer transformer = transformerFactory.newTransformer(new StreamSource(in));
            transformer.transform(new StreamSource(of), new StreamResult(out));
        }
    }
}
Also used : Transformer(javax.xml.transform.Transformer) OutputStream(java.io.OutputStream) Attributes(java.util.jar.Attributes) Attrs(aQute.bnd.header.Attrs) SortedList(aQute.lib.collections.SortedList) Analyzer(aQute.bnd.osgi.Analyzer) URL(java.net.URL) MultiMap(aQute.lib.collections.MultiMap) Version(aQute.bnd.version.Version) Tree(aQute.bnd.service.diff.Tree) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) PrintWriter(java.io.PrintWriter) Parameters(aQute.bnd.header.Parameters) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Instructions(aQute.bnd.osgi.Instructions) Manifest(java.util.jar.Manifest) Info(aQute.bnd.differ.Baseline.Info) BundleInfo(aQute.bnd.differ.Baseline.BundleInfo) Jar(aQute.bnd.osgi.Jar) Tag(aQute.lib.tag.Tag) File(java.io.File)

Example 12 with MultiMap

use of aQute.lib.collections.MultiMap in project bnd by bndtools.

the class RepoCommand method _diff.

@Description("Diff jars (or show tree)")
public void _diff(diffOptions options) throws UnsupportedEncodingException, IOException, Exception {
    List<String> args = options._arguments();
    String newer = args.remove(0);
    String older = args.size() > 0 ? args.remove(0) : null;
    RepositoryPlugin rnewer = findRepo(newer);
    RepositoryPlugin rolder = older == null ? null : findRepo(older);
    if (rnewer == null) {
        bnd.messages.NoSuchRepository_(newer);
        return;
    }
    if (older != null && rolder == null) {
        bnd.messages.NoSuchRepository_(newer);
        return;
    }
    PrintWriter pw = IO.writer(bnd.out, UTF_8);
    Tree tNewer = RepositoryElement.getTree(rnewer);
    if (rolder == null) {
        if (options.json())
            codec.enc().to(pw).put(tNewer.serialize()).flush();
        else
            DiffCommand.show(pw, tNewer, 0);
    } else {
        Tree tOlder = RepositoryElement.getTree(rolder);
        Diff diff = new DiffImpl(tNewer, tOlder);
        MultiMap<String, String> map = new MultiMap<String, String>();
        for (Diff bsn : diff.getChildren()) {
            for (Diff version : bsn.getChildren()) {
                if (version.getDelta() == Delta.UNCHANGED)
                    continue;
                if (options.remove() == false && options.added() == false || (//
                options.remove() && version.getDelta() == Delta.REMOVED) || (options.added() && version.getDelta() == Delta.ADDED)) {
                    map.add(bsn.getName(), version.getName());
                }
            }
        }
        if (options.json())
            codec.enc().to(pw).put(map).flush();
        else if (!options.diff())
            bnd.printMultiMap(map);
        else
            DiffCommand.show(pw, diff, 0, !options.full());
    }
    pw.flush();
}
Also used : MultiMap(aQute.lib.collections.MultiMap) Diff(aQute.bnd.service.diff.Diff) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Tree(aQute.bnd.service.diff.Tree) DiffImpl(aQute.bnd.differ.DiffImpl) PrintWriter(java.io.PrintWriter) Description(aQute.lib.getopt.Description)

Example 13 with MultiMap

use of aQute.lib.collections.MultiMap in project bnd by bndtools.

the class SubsystemExporter method export.

@Override
public Map.Entry<String, Resource> export(String type, final Project project, Map<String, String> options) throws Exception {
    Jar jar = new Jar(".");
    project.addClose(jar);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue("Subsystem-ManifestVersion", "1");
    List<Container> distro = project.getBundles(Strategy.LOWEST, project.getProperty(Constants.DISTRO), Constants.DISTRO);
    List<File> distroFiles = getBundles(distro, project);
    List<File> files = getBundles(project.getRunbundles(), project);
    MultiMap<String, Attrs> imports = new MultiMap<String, Attrs>();
    MultiMap<String, Attrs> exports = new MultiMap<String, Attrs>();
    Parameters requirements = new Parameters();
    Parameters capabilities = new Parameters();
    for (File file : files) {
        Domain domain = Domain.domain(file);
        String bsn = domain.getBundleSymbolicName().getKey();
        String version = domain.getBundleVersion();
        for (Entry<String, Attrs> e : domain.getImportPackage().entrySet()) {
            imports.add(e.getKey(), e.getValue());
        }
        for (Entry<String, Attrs> e : domain.getExportPackage().entrySet()) {
            exports.add(e.getKey(), e.getValue());
        }
        String path = bsn + "-" + version + ".jar";
        jar.putResource(path, new FileResource(file));
    }
    headers(project, manifest.getMainAttributes());
    set(manifest.getMainAttributes(), SUBSYSTEM_TYPE, OSGI_SUBSYSTEM_FEATURE);
    String ssn = project.getName();
    Collection<String> bsns = project.getBsns();
    if (bsns.size() > 0) {
        ssn = bsns.iterator().next();
    }
    set(manifest.getMainAttributes(), SUBSYSTEM_SYMBOLIC_NAME, ssn);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    manifest.write(bout);
    jar.putResource(OSGI_INF_SUBSYSTEM_MF, new EmbeddedResource(bout.toByteArray(), 0));
    final JarResource jarResource = new JarResource(jar);
    final String name = ssn + ".esa";
    return new Map.Entry<String, Resource>() {

        @Override
        public String getKey() {
            return name;
        }

        @Override
        public Resource getValue() {
            return jarResource;
        }

        @Override
        public Resource setValue(Resource arg0) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : Parameters(aQute.bnd.header.Parameters) JarResource(aQute.bnd.osgi.JarResource) Attrs(aQute.bnd.header.Attrs) FileResource(aQute.bnd.osgi.FileResource) JarResource(aQute.bnd.osgi.JarResource) FileResource(aQute.bnd.osgi.FileResource) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Resource(aQute.bnd.osgi.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) MultiMap(aQute.lib.collections.MultiMap) Container(aQute.bnd.build.Container) Entry(java.util.Map.Entry) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Aggregations

MultiMap (aQute.lib.collections.MultiMap)13 Map (java.util.Map)6 Parameters (aQute.bnd.header.Parameters)5 Attrs (aQute.bnd.header.Attrs)4 Analyzer (aQute.bnd.osgi.Analyzer)3 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)3 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)3 FileResource (aQute.bnd.osgi.FileResource)3 Instructions (aQute.bnd.osgi.Instructions)3 Jar (aQute.bnd.osgi.Jar)3 Resource (aQute.bnd.osgi.Resource)3 SortedList (aQute.lib.collections.SortedList)3 Description (aQute.lib.getopt.Description)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Project (aQute.bnd.build.Project)2 Clazz (aQute.bnd.osgi.Clazz)2 Domain (aQute.bnd.osgi.Domain)2 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)2