Package aQute.bnd.osgi

Examples of aQute.bnd.osgi.Jar


     * from our target bundle.
     */
    public void execute() throws MojoExecutionException
    {
        Analyzer analyzer = null;
        Jar jar = null;

        try
        {
            // Get the name of our target bundle we are parsing for annotations.
            File target = getBundleName();
            getLog().info("Generating DM component descriptors for bundle " + target);

            // Create a bnd analyzer and analyze our target bundle classes.
            analyzer = new Analyzer();
            analyzer.setJar(target);
            analyzer.analyze();

            // This helper class will parse classes using the analyzer we just created.
            DescriptorGenerator generator = new DescriptorGenerator(analyzer, new MvnLogger(getLog(), m_log));

            // Start scanning
            if (generator.execute())
            {
                // Some annotations have been parsed.
                // Add the list of generated component descriptors in our
                // special header.
                jar = analyzer.getJar();
                jar.getManifest().getMainAttributes()
                    .putValue( "DependencyManager-Component", generator.getDescriptorPaths() );

                // Add generated descriptors into the target bundle (we'll use a
                // temp file).
                Map<String, Resource> resources = generator.getDescriptors();
                for (Map.Entry<String, Resource> entry : resources.entrySet())
                {
                    addResource(entry.getKey(), entry.getValue().openInputStream());
                    jar.putResource(entry.getKey(), entry.getValue());
                }

                Resource metaType = generator.getMetaTypeResource();
                if (metaType != null)
                {
                    addResource("OSGI-INF/metatype/metatype.xml", metaType.openInputStream());
                    jar.putResource("OSGI-INF/metatype/metatype.xml", metaType);
                }

                // Possibly set the Import-Service/Export-Service header
                if (m_buildImportExportService)
                {
                    // Don't override Import-Service header, if it is found from
                    // the bnd directives.
                    if (jar.getManifest().getMainAttributes().getValue(IMPORT_SERVICE) == null)
                    {
                        buildImportExportService(jar, IMPORT_SERVICE, generator.getImportService());
                    }

                    // Don't override Export-Service header, if already defined
                    if (jar.getManifest().getMainAttributes().getValue(EXPORT_SERVICE) == null)
                    {
                        buildImportExportService(jar, EXPORT_SERVICE, generator.getExportService());
                    }
                }

                copy(jar, target);
            }
        }

        catch (MojoExecutionException e)
        {
            throw e;
        }

        catch (Throwable t)
        {
            getLog().error("Exception while scanning annotation", t);
            throw new MojoExecutionException(t.getMessage(), t.getCause());
        }

        finally
        {
            if (jar != null)
            {
                jar.close();
            }
        }
    }
View Full Code Here


        return createStatus("Problem(s) preparing the runtime environment.", errors, warnings);
    }

    private static String validateClasspath(Collection<String> classpath) {
        for (String fileName : classpath) {
            Jar jar = null;
            try {
                jar = new Jar(new File(fileName));
                boolean frameworkExists = jar.exists("META-INF/services/" + FrameworkFactory.class.getName());
                if (frameworkExists)
                    return fileName;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (jar != null) {
                    jar.close();
                }
            }
        }
        return null;
    }
View Full Code Here

            doPrint(file, optionsi);
        }
    }

    private void doPrint(File file, int options) throws ZipException, IOException, Exception {
        Jar jar = new Jar(file.getName(), file);
        try {
            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 " + file);
                else {
                    out.println("[MANIFEST " + jar.getName() + "]");
                    printManifest(manifest);
                }
                out.println();
            }
            if ((options & IMPEXP) != 0) {
                out.println("[IMPEXP]");
                Manifest m = jar.getManifest();

                if (m != null) {
                    Domain domain = Domain.domain(m);
                    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("Import-Package", new TreeMap<String,Attrs>(imports));
                    print("Export-Package", new TreeMap<String,Attrs>(exports));
                } else
                    warning("File has no manifest");
            }

            if ((options & (USES | USEDBY)) != 0) {
                out.println();
                Analyzer analyzer = new Analyzer();
                analyzer.setPedantic(isPedantic());
                analyzer.setJar(jar);
                analyzer.analyze();
                if ((options & USES) != 0) {
                    out.println("[USES]");
                    printMultiMap(analyzer.getUses());
                    out.println();
                }
                if ((options & USEDBY) != 0) {
                    out.println("[USEDBY]");
                    Map<PackageRef,Set<PackageRef>> usedBy = CollectionUtil.invertMapOfCollection(analyzer.getUses());
                    printMultiMap(usedBy);
                }
                out.println();
            }

            if ((options & COMPONENT) != 0) {
                printComponents(jar);
                out.println();
            }

            if ((options & METATYPE) != 0) {
                printMetatype(jar);
                out.println();
            }

            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();
                        }
                    }
                }
                out.println();
            }
        } finally {
            jar.close();
        }
    }
View Full Code Here

            doPrint(file, optionsi);
        }
    }

    private void doPrint(File file, int options) throws ZipException, IOException, Exception {
        Jar jar = new Jar(file.getName(), file);
        try {
            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 " + file);
                else {
                    out.println("[MANIFEST " + jar.getName() + "]");
                    printManifest(manifest);
                }
                out.println();
            }
            if ((options & IMPEXP) != 0) {
                out.println("[IMPEXP]");
                Manifest m = jar.getManifest();

                if (m != null) {
                    Domain domain = Domain.domain(m);
                    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("Import-Package", new TreeMap<String,Attrs>(imports));
                    print("Export-Package", new TreeMap<String,Attrs>(exports));
                } else
                    warning("File has no manifest");
            }

            if ((options & (USES | USEDBY)) != 0) {
                out.println();
                Analyzer analyzer = new Analyzer();
                try {
                    analyzer.setPedantic(isPedantic());
                    analyzer.setJar(jar);
                    analyzer.analyze();
                    if ((options & USES) != 0) {
                        out.println("[USES]");
                        printMultiMap(analyzer.getUses());
                        out.println();
                    }
                    if ((options & USEDBY) != 0) {
                        out.println("[USEDBY]");
                        Map<PackageRef,Set<PackageRef>> usedBy = CollectionUtil.invertMapOfCollection(analyzer.getUses());
                        printMultiMap(usedBy);
                    }
                    analyzer.setJar((Jar) null);
                } finally {
                    analyzer.close();
                }
                out.println();
            }

            if ((options & COMPONENT) != 0) {
                printComponents(jar);
                out.println();
            }

            if ((options & METATYPE) != 0) {
                printMetatype(jar);
                out.println();
            }

            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();
                        }
                    }
                }
                out.println();
            }
        } finally {
            jar.close();
        }
    }
View Full Code Here

        MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Failed to install one or more bundles", null);

        List<File> files = fileSelectionPage.getFiles();
        selectedBundles = new LinkedList<Pair<String,String>>();
        for (File file : files) {
            Jar jar = null;
            try {
                jar = new Jar(file);
                jar.setDoNotTouchManifest();

                Attributes mainAttribs = jar.getManifest().getMainAttributes();
                String bsn = BundleUtils.getBundleSymbolicName(mainAttribs);
                String version = mainAttribs.getValue(Constants.BUNDLE_VERSION);
                if (version == null)
                    version = "0";
                selectedBundles.add(Pair.newInstance(bsn, version));
            } catch (Exception e) {
                status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format("Failed to analyse JAR: {0}", file.getPath()), e));
                continue;
            } finally {
                if (jar != null)
                    jar.close();
            }

            try {
                RepositoryPlugin.PutResult result = repository.put(new BufferedInputStream(new FileInputStream(file)), new RepositoryPlugin.PutOptions());
                if (result.artifact != null && result.artifact.getScheme().equals("file")) {
View Full Code Here

        return Status.OK_STATUS;
    }

    static Builder setupBuilderForJarFile(File file) throws IOException, CoreException {
        Builder builder = new Builder();
        Jar jar = new Jar(file);
        builder.setJar(jar);
        try {
            builder.analyze();
        } catch (Exception e) {
            throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Bnd analysis failed", e));
View Full Code Here

            throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Bnd analysis failed", e));
        }
    }

    static void mergeCapabilities(Map<String,List<ExportPackage>> exports, MultiMap<String,String> usedBy, Map<String,Set<Version>> bundleVersions, Builder builder) throws Exception {
        Jar jar = builder.getJar();
        if (jar == null)
            return;
        Manifest manifest = jar.getManifest();
        if (manifest == null)
            return;

        Attributes attribs = manifest.getMainAttributes();
        String exportPkgStr = attribs.getValue(Constants.EXPORT_PACKAGE);
View Full Code Here

        return result;
    }

    static void mergeRequirements(Map<String,List<ImportPackage>> imports, Map<String,List<ExportPackage>> exports, MultiMap<String,String> usedBy, Map<String,List<RequiredBundle>> requiredBundles, Map<String,Set<Version>> bundleVersions,
            Builder builder) throws Exception {
        Jar jar = builder.getJar();
        if (jar == null)
            return;
        Manifest manifest = jar.getManifest();
        if (manifest == null)
            return;
        Attributes attribs = manifest.getMainAttributes();

        // Process imports
View Full Code Here

        selectedBundles = new LinkedList<Pair<String,String>>();
        monitor.beginTask("Processing files", files.size());
        for (File file : files) {
            monitor.subTask(file.getName());

            Jar jar = null;
            try {
                jar = new Jar(file);
                jar.setDoNotTouchManifest();

                Attributes mainAttribs = jar.getManifest().getMainAttributes();
                String bsn = BundleUtils.getBundleSymbolicName(mainAttribs);
                String version = mainAttribs.getValue(Constants.BUNDLE_VERSION);
                if (version == null)
                    version = "0";
                selectedBundles.add(Pair.newInstance(bsn, version));
            } catch (Exception e) {
                status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, MessageFormat.format("Failed to analyse JAR: {0}", file.getPath()), e));
                continue;
            } finally {
                if (jar != null)
                    jar.close();
            }

            try {
                RepositoryPlugin.PutResult result = repository.put(new BufferedInputStream(new FileInputStream(file)), new RepositoryPlugin.PutOptions());
                if (result.artifact != null && result.artifact.getScheme().equals("file")) {
View Full Code Here

    private IStatus generateJar(String jarPath) {
        MultiStatus status = new MultiStatus(Plugin.PLUGIN_ID, 0, "Errors occurred during exporting.", null);
        try {
            ProjectLauncher launcher = bndProject.getProjectLauncher();
            Jar jar = launcher.executable();
            jar.write(jarPath);
        } catch (Exception e) {
            status.add(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error generating executable JAR.", e));
        }
        return status;
    }
View Full Code Here

TOP

Related Classes of aQute.bnd.osgi.Jar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.