Search in sources :

Example 1 with Analyzer

use of aQute.bnd.osgi.Analyzer in project tdi-studio-se by Talend.

the class JobJavaScriptOSGIForESBManager method createAnalyzer.

protected Analyzer createAnalyzer(ExportFileResource libResource, ProcessItem processItem) throws IOException {
    Analyzer analyzer = new Analyzer();
    Jar bin = new Jar(classesLocation);
    analyzer.setJar(bin);
    final String bundleName = processItem.getProperty().getLabel();
    String symbolicName = bundleName;
    // http://jira.talendforge.org/browse/TESB-5382 LiXiaopeng
    Project project = ProjectManager.getInstance().getCurrentProject();
    if (project != null) {
        String proName = project.getLabel();
        if (proName != null) {
            symbolicName = proName.toLowerCase() + '.' + symbolicName;
        }
    }
    analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName);
    analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, symbolicName);
    analyzer.setProperty(Analyzer.BUNDLE_VERSION, getBundleVersion());
    IBrandingService brandingService = (IBrandingService) GlobalServiceRegister.getDefault().getService(IBrandingService.class);
    analyzer.setProperty(Analyzer.BUNDLE_VENDOR, //$NON-NLS-1$
    brandingService.getFullProductName() + " (" + brandingService.getAcronym() + '_' + RepositoryPlugin.getDefault().getBundle().getVersion().toString() + //$NON-NLS-1$
    ")");
    addOsgiDependencies(analyzer, libResource, processItem);
    //$NON-NLS-1$
    final StringBuilder bundleClasspath = new StringBuilder(".");
    final StringBuilder bundleNativeCode = new StringBuilder();
    Set<String> relativePathList = libResource.getRelativePathList();
    for (String path : relativePathList) {
        Set<URL> resources = libResource.getResourcesByRelativePath(path);
        for (URL url : resources) {
            File dependencyFile = new File(FilesUtils.getFileRealPath(url.getPath()));
            String relativePath = libResource.getDirectoryName() + PATH_SEPARATOR + dependencyFile.getName();
            bundleClasspath.append(MANIFEST_ITEM_SEPARATOR).append(relativePath);
            bin.putResource(relativePath, new FileResource(dependencyFile));
            // Add dynamic library declaration in manifest
            if (relativePath.toLowerCase().endsWith(DLL_FILE) || relativePath.toLowerCase().endsWith(SO_FILE)) {
                bundleNativeCode.append(libResource.getDirectoryName() + PATH_SEPARATOR + dependencyFile.getName()).append(OSGI_OS_CODE);
            }
        }
    }
    analyzer.setProperty(Analyzer.BUNDLE_CLASSPATH, bundleClasspath.toString());
    // TESB-15680: Add Bundle-NativeCode in manifest
    if (bundleNativeCode.length() > 0) {
        bundleNativeCode.setLength(bundleNativeCode.length() - 1);
        analyzer.setProperty(Analyzer.BUNDLE_NATIVECODE, bundleNativeCode.toString());
    }
    return analyzer;
}
Also used : Project(org.talend.core.model.general.Project) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) FileResource(aQute.bnd.osgi.FileResource) ExportFileResource(org.talend.repository.documentation.ExportFileResource) Jar(aQute.bnd.osgi.Jar) IBrandingService(org.talend.core.ui.branding.IBrandingService) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File) URL(java.net.URL)

Example 2 with Analyzer

use of aQute.bnd.osgi.Analyzer in project tdi-studio-se by Talend.

the class JobJavaScriptOSGIForESBManager method getManifest.

private Manifest getManifest(ExportFileResource libResource, ProcessItem processItem) throws IOException {
    Analyzer analyzer = createAnalyzer(libResource, processItem);
    // Calculate the manifest
    Manifest manifest = null;
    try {
        manifest = analyzer.calcManifest();
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        analyzer.close();
    }
    return manifest;
}
Also used : IOException(java.io.IOException) Analyzer(aQute.bnd.osgi.Analyzer) Manifest(java.util.jar.Manifest) ProcessorException(org.talend.designer.runprocess.ProcessorException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException)

Example 3 with Analyzer

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

the class RemoteProjectLauncherPlugin method executable.

/**
	 * Created a JAR that is a bundle and that contains its dependencies
	 */
@Override
public Jar executable() throws Exception {
    Collection<String> bsns = getProject().getBsns();
    if (bsns.size() != 1)
        throw new IllegalArgumentException("Can only handle a single bsn for a run configuration " + bsns);
    String bsn = bsns.iterator().next();
    Jar jar = new Jar(bsn);
    String path = "aQute/remote/embedded/activator/EmbeddedActivator.class";
    URLResource resource = new URLResource(getClass().getClassLoader().getResource(path));
    jar.putResource("aQute/remote/embedded/activator/EmbeddedActivator.class", resource);
    Collection<Container> rb = getProject().getRunbundles();
    rb = Container.flatten(rb);
    Attrs attrs = new Attrs();
    for (Container c : rb) {
        if (c.getError() != null) {
            getProject().error("invalid runbundle %s", c);
        } else {
            File f = c.getFile();
            String tbsn = c.getBundleSymbolicName();
            String version = c.getVersion();
            if (version == null || !Version.isVersion(version))
                getProject().warning("The version of embedded bundle %s does not have a proper version", c);
            jar.putResource("jar/" + c.getBundleSymbolicName() + ".jar", new FileResource(f));
            attrs.put(tbsn, version);
        }
    }
    Analyzer a = new Analyzer(getProject());
    a.setJar(jar);
    a.setBundleActivator(EmbeddedActivator.class.getName());
    a.setProperty("Bnd-Embedded", attrs.toString().replace(';', ','));
    Manifest manifest = a.calcManifest();
    jar.setManifest(manifest);
    getProject().getInfo(a);
    return jar;
}
Also used : Attrs(aQute.bnd.header.Attrs) FileResource(aQute.bnd.osgi.FileResource) EmbeddedActivator(aQute.remote.embedded.activator.EmbeddedActivator) Analyzer(aQute.bnd.osgi.Analyzer) Manifest(java.util.jar.Manifest) URLResource(aQute.bnd.osgi.URLResource) Container(aQute.bnd.build.Container) Jar(aQute.bnd.osgi.Jar) File(java.io.File)

Example 4 with Analyzer

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

the class Classpath method visit.

/**
	 * Visit each class on the class path.
	 * 
	 * @param visitor the visitor
	 */
public void visit(ClassVisitor visitor) throws Exception {
    try (Analyzer analyzer = new Analyzer()) {
        for (File f : entries) {
            try (Jar jar = new Jar(f)) {
                for (String path : jar.getResources().keySet()) {
                    if (path.endsWith(".class")) {
                        Resource r = jar.getResource(path);
                        Clazz c = new Clazz(analyzer, path, r);
                        c.parseClassFile();
                        visitor.visit(c);
                    }
                }
            }
        }
    }
}
Also used : Resource(aQute.bnd.osgi.Resource) Jar(aQute.bnd.osgi.Jar) Clazz(aQute.bnd.osgi.Clazz) Analyzer(aQute.bnd.osgi.Analyzer) File(java.io.File)

Example 5 with Analyzer

use of aQute.bnd.osgi.Analyzer 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)

Aggregations

Analyzer (aQute.bnd.osgi.Analyzer)55 File (java.io.File)21 Clazz (aQute.bnd.osgi.Clazz)14 Properties (java.util.Properties)14 Manifest (java.util.jar.Manifest)12 Jar (aQute.bnd.osgi.Jar)11 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)9 ClassDataCollector (aQute.bnd.osgi.ClassDataCollector)8 Parameters (aQute.bnd.header.Parameters)7 FileResource (aQute.bnd.osgi.FileResource)7 FileInputStream (java.io.FileInputStream)6 TypeRef (aQute.bnd.osgi.Descriptors.TypeRef)5 Description (aQute.lib.getopt.Description)5 Attrs (aQute.bnd.header.Attrs)4 Descriptors (aQute.bnd.osgi.Descriptors)4 Packages (aQute.bnd.osgi.Packages)4 Resource (aQute.bnd.osgi.Resource)4 Map (java.util.Map)4 PomFromManifest (aQute.bnd.maven.PomFromManifest)3 Domain (aQute.bnd.osgi.Domain)3