Search in sources :

Example 81 with Resource

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

the class Printer method printComponents.

/**
 * Print the components in this JAR.
 *
 * @param jar
 */
private void printComponents(Jar jar) throws Exception {
    out.println("[COMPONENTS]");
    Manifest manifest = jar.getManifest();
    if (manifest == null) {
        out.println("No manifest");
        return;
    }
    String componentHeader = manifest.getMainAttributes().getValue(Constants.SERVICE_COMPONENT);
    Parameters clauses = new Parameters(componentHeader);
    boolean printed = false;
    for (String path : clauses.keySet()) {
        printed = true;
        out.println(path);
        Resource r = jar.getResource(path);
        if (r != null) {
            IO.copy(IO.reader(r.openInputStream(), Constants.DEFAULT_CHARSET), or);
        } else {
            out.println("  - no resource");
            warning("No Resource found for service component: " + path);
        }
    }
    if (printed) {
        out.println();
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Resource(aQute.bnd.osgi.Resource) Manifest(java.util.jar.Manifest)

Example 82 with Resource

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

the class PackageListWizardPage method setVisible.

@Override
public void setVisible(boolean visible) {
    super.setVisible(visible);
    if (!loaded) {
        try {
            getContainer().run(false, false, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    availablePackages.clear();
                    selectedPackages.clear();
                    for (IPath path : paths) {
                        Jar jar = null;
                        try {
                            if (path.isAbsolute())
                                jar = new Jar(path.toFile());
                            else
                                jar = new Jar(ResourcesPlugin.getWorkspace().getRoot().getLocation().append(path).toFile());
                            Map<String, Map<String, Resource>> dirs = jar.getDirectories();
                            for (Entry<String, Map<String, Resource>> entry : dirs.entrySet()) {
                                if (entry.getValue() == null)
                                    continue;
                                IPath packagePath = new Path(entry.getKey());
                                if (packagePath.segmentCount() < 1)
                                    continue;
                                if ("META-INF".equalsIgnoreCase(packagePath.segment(0)))
                                    continue;
                                availablePackages.add(packagePath);
                            }
                        } catch (Exception e) {
                        } finally {
                            if (jar != null)
                                jar.close();
                        }
                    }
                }
            });
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            loaded = true;
        }
        availableViewer.setInput(availablePackages);
        selectedViewer.setInput(selectedPackages);
    }
    updateUI();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Resource(aQute.bnd.osgi.Resource) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Entry(java.util.Map.Entry) Jar(aQute.bnd.osgi.Jar) Map(java.util.Map)

Example 83 with Resource

use of aQute.bnd.osgi.Resource 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<File> files = new ArrayList<File>();
    for (Container c : project.getRunbundles()) {
        switch(c.getType()) {
            case ERROR:
                // skip, already reported
                break;
            case PROJECT:
            case EXTERNAL:
            case REPO:
                files.add(c.getFile());
                break;
            case LIBRARY:
                c.contributeFiles(files, project);
                break;
        }
    }
    for (File file : files) {
        Domain domain = Domain.domain(file);
        String bsn = domain.getBundleSymbolicName().getKey();
        String version = domain.getBundleVersion();
        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 : JarResource(aQute.bnd.osgi.JarResource) ArrayList(java.util.ArrayList) FileResource(aQute.bnd.osgi.FileResource) JarResource(aQute.bnd.osgi.JarResource) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Resource(aQute.bnd.osgi.Resource) FileResource(aQute.bnd.osgi.FileResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) Container(aQute.bnd.build.Container) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Jar(aQute.bnd.osgi.Jar) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Example 84 with Resource

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

the class BndMavenPlugin method expandJar.

private void expandJar(Jar jar, File dir) throws Exception {
    final long lastModified = jar.lastModified();
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Bundle lastModified: %tF %<tT.%<tL", lastModified));
    }
    dir = dir.getAbsoluteFile();
    Files.createDirectories(dir.toPath());
    for (Map.Entry<String, Resource> entry : jar.getResources().entrySet()) {
        File outFile = getFile(dir, entry.getKey());
        Resource resource = entry.getValue();
        // Skip the copy if the source and target are the same file
        if (resource instanceof FileResource) {
            @SuppressWarnings("resource") FileResource fr = (FileResource) resource;
            if (outFile.equals(fr.getFile())) {
                continue;
            }
        }
        if (!outFile.exists() || outFile.lastModified() < lastModified) {
            if (logger.isDebugEnabled()) {
                if (outFile.exists())
                    logger.debug(String.format("Updating lastModified: %tF %<tT.%<tL '%s'", outFile.lastModified(), outFile));
                else
                    logger.debug("Creating '{}'", outFile);
            }
            Files.createDirectories(outFile.toPath().getParent());
            try (OutputStream out = buildContext.newFileOutputStream(outFile)) {
                IO.copy(resource.openInputStream(), out);
            }
        }
    }
    if (manifestOutOfDate() || manifestPath.lastModified() < lastModified) {
        if (logger.isDebugEnabled()) {
            if (!manifestOutOfDate())
                logger.debug(String.format("Updating lastModified: %tF %<tT.%<tL '%s'", manifestPath.lastModified(), manifestPath));
            else
                logger.debug("Creating '{}'", manifestPath);
        }
        Files.createDirectories(manifestPath.toPath().getParent());
        try (OutputStream manifestOut = buildContext.newFileOutputStream(manifestPath)) {
            jar.writeManifest(manifestOut);
        }
        buildContext.setValue(MANIFEST_LAST_MODIFIED, manifestPath.lastModified());
    }
}
Also used : OutputStream(java.io.OutputStream) FileResource(aQute.bnd.osgi.FileResource) Resource(aQute.bnd.osgi.Resource) FileResource(aQute.bnd.osgi.FileResource) Map(java.util.Map) IO.getFile(aQute.lib.io.IO.getFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 85 with Resource

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

Aggregations

Resource (aQute.bnd.osgi.Resource)147 Jar (aQute.bnd.osgi.Jar)87 Builder (aQute.bnd.osgi.Builder)83 File (java.io.File)76 XmlTester (aQute.bnd.test.XmlTester)48 JarResource (aQute.bnd.osgi.JarResource)20 Attributes (java.util.jar.Attributes)20 Map (java.util.Map)19 Manifest (java.util.jar.Manifest)19 FileResource (aQute.bnd.osgi.FileResource)17 LogService (org.osgi.service.log.LogService)15 HashMap (java.util.HashMap)14 Document (org.w3c.dom.Document)14 Properties (java.util.Properties)12 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)11 InputStream (java.io.InputStream)9 Attrs (aQute.bnd.header.Attrs)8 ArrayList (java.util.ArrayList)8 TreeMap (java.util.TreeMap)8