Search in sources :

Example 31 with PackageRef

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

the class Analyzer method doEEProfiles.

/**
	 * Added for 1.8 profiles. A 1.8 profile is a set of packages so the VM can
	 * be delivered in smaller versions. This method will look at the
	 * {@link Constants#EEPROFILE} option. If it is set, it can be "auto" or it
	 * can contain a list of profiles specified as name="a,b,c" values. If we
	 * find a package outside the profiles, no profile is set. Otherwise the
	 * highest found profile is added. This only works for java packages.
	 */
private String doEEProfiles(JAVA highest) throws IOException {
    String ee = getProperty(EEPROFILE);
    if (ee == null)
        return highest.getFilter();
    ee = ee.trim();
    Map<String, Set<String>> profiles;
    if (ee.equals(EEPROFILE_AUTO_ATTRIBUTE)) {
        profiles = highest.getProfiles();
        if (profiles == null)
            return highest.getFilter();
    } else {
        Attrs t = OSGiHeader.parseProperties(ee);
        profiles = new HashMap<String, Set<String>>();
        for (Map.Entry<String, String> e : t.entrySet()) {
            String profile = e.getKey();
            String l = e.getValue();
            SortedList<String> sl = new SortedList<String>(l.split("\\s*,\\s*"));
            profiles.put(profile, sl);
        }
    }
    SortedSet<String> found = new TreeSet<String>();
    nextPackage: for (PackageRef p : referred.keySet()) {
        if (p.isJava()) {
            String fqn = p.getFQN();
            for (Entry<String, Set<String>> entry : profiles.entrySet()) {
                if (entry.getValue().contains(fqn)) {
                    found.add(entry.getKey());
                    if (found.size() == profiles.size())
                        break nextPackage;
                    //
                    continue nextPackage;
                }
            }
            //
            return highest.getFilter();
        }
    }
    String filter = highest.getFilter();
    if (!found.isEmpty())
        filter = filter.replaceAll("JavaSE", "JavaSE/" + found.last());
    // TODO a more elegant way to build the filter, we now assume JavaSE
    return filter;
}
Also used : SortedSet(java.util.SortedSet) EnumSet(java.util.EnumSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Attrs(aQute.bnd.header.Attrs) SortedList(aQute.lib.collections.SortedList) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 32 with PackageRef

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

the class SpringComponent method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    Jar jar = analyzer.getJar();
    Map<String, Resource> dir = jar.getDirectories().get("META-INF/spring");
    if (dir == null || dir.isEmpty())
        return false;
    for (Iterator<Entry<String, Resource>> i = dir.entrySet().iterator(); i.hasNext(); ) {
        Entry<String, Resource> entry = i.next();
        String path = entry.getKey();
        Resource resource = entry.getValue();
        if (SPRING_SOURCE.matcher(path).matches()) {
            try {
                Set<CharSequence> set;
                try (InputStream in = resource.openInputStream()) {
                    set = analyze(in);
                }
                for (Iterator<CharSequence> r = set.iterator(); r.hasNext(); ) {
                    PackageRef pack = analyzer.getPackageRef((String) r.next());
                    if (!QN.matcher(pack.getFQN()).matches())
                        analyzer.warning("Package does not seem a package in spring resource (%s): %s", path, pack);
                    if (!analyzer.getReferred().containsKey(pack))
                        analyzer.getReferred().put(pack, new Attrs());
                }
            } catch (Exception e) {
                analyzer.error("Unexpected exception in processing spring resources(%s): %s", path, e);
            }
        }
    }
    return false;
}
Also used : InputStream(java.io.InputStream) Resource(aQute.bnd.osgi.Resource) Attrs(aQute.bnd.header.Attrs) Entry(java.util.Map.Entry) Jar(aQute.bnd.osgi.Jar) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef)

Example 33 with PackageRef

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

the class Verifier method verifyUnresolvedReferences.

/**
	 * Check for unresolved imports. These are referrals that are not imported
	 * by the manifest and that are not part of our bundle class path. The are
	 * calculated by removing all the imported packages and contained from the
	 * referred packages.
	 * 
	 * @throws Exception
	 */
private void verifyUnresolvedReferences() throws Exception {
    if (isFrombuilder())
        return;
    Manifest m = analyzer.getJar().getManifest();
    if (m == null) {
        error("No manifest");
        return;
    }
    Domain domain = Domain.domain(m);
    Set<PackageRef> unresolvedReferences = new TreeSet<PackageRef>(analyzer.getReferred().keySet());
    unresolvedReferences.removeAll(analyzer.getContained().keySet());
    for (String pname : domain.getImportPackage().keySet()) {
        PackageRef pref = analyzer.getPackageRef(pname);
        unresolvedReferences.remove(pref);
    }
    // Remove any java.** packages.
    for (Iterator<PackageRef> p = unresolvedReferences.iterator(); p.hasNext(); ) {
        PackageRef pack = p.next();
        if (pack.isJava())
            p.remove();
        else {
            // Remove any dynamic imports
            if (isDynamicImport(pack))
                p.remove();
        }
    }
    if (domain.getRequireBundle().isEmpty() && domain.get("ExtensionBundle-Activator") == null && (domain.getFragmentHost() == null || domain.getFragmentHost().getKey().equals("system.bundle"))) {
        if (!unresolvedReferences.isEmpty()) {
            // Now we want to know the
            // classes that are the culprits
            Set<String> culprits = new HashSet<String>();
            for (Clazz clazz : analyzer.getClassspace().values()) {
                if (hasOverlap(unresolvedReferences, clazz.getReferred()))
                    culprits.add(clazz.getAbsolutePath());
            }
            if (analyzer instanceof Builder)
                warning("Unresolved references to %s by class(es) %s on the " + Constants.BUNDLE_CLASSPATH + ": %s", unresolvedReferences, culprits, analyzer.getBundleClasspath().keySet());
            else
                error("Unresolved references to %s by class(es) %s on the " + Constants.BUNDLE_CLASSPATH + ": %s", unresolvedReferences, culprits, analyzer.getBundleClasspath().keySet());
            return;
        }
    } else if (isPedantic())
        warning("Use of " + Constants.REQUIRE_BUNDLE + ", ExtensionBundle-Activator, or a system bundle fragment makes it impossible to verify unresolved references");
}
Also used : TreeSet(java.util.TreeSet) Manifest(java.util.jar.Manifest) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) HashSet(java.util.HashSet)

Example 34 with PackageRef

use of aQute.bnd.osgi.Descriptors.PackageRef 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 35 with PackageRef

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

the class ClassParserTest method testSimple.

public void testSimple() throws Exception {
    InputStream in = getClass().getResourceAsStream("WithAnnotations.jclass");
    assertNotNull(in);
    Clazz clazz = new Clazz(a, "test", null);
    clazz.parseClassFile(in);
    Set<PackageRef> set = clazz.getReferred();
    PackageRef test = a.getPackageRef("test");
    PackageRef testAnnotations = a.getPackageRef("test/annotations");
    assertTrue(set.contains(test));
    assertTrue(set.contains(testAnnotations));
}
Also used : InputStream(java.io.InputStream) Clazz(aQute.bnd.osgi.Clazz) 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