Search in sources :

Example 16 with FileResource

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

the class JarTest method testManualManifest.

public static void testManualManifest() throws Exception {
    Jar jar = new Jar("dot");
    jar.setManifest(new Manifest());
    jar.setDoNotTouchManifest();
    jar.putResource("a/b", new FileResource(IO.getFile("testresources/bnd.jar")));
    jar.putResource("META-INF/MANIFEST.MF", new EmbeddedResource("Manifest-Version: 1\r\nX: 1\r\n\r\n".getBytes(), 0));
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    jar.write(bout);
    JarInputStream jin = new JarInputStream(new ByteArrayInputStream(bout.toByteArray()));
    Manifest m = jin.getManifest();
    assertNotNull(m);
    assertEquals("1", m.getMainAttributes().getValue("X"));
    jin.close();
}
Also used : EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileResource(aQute.bnd.osgi.FileResource) Jar(aQute.bnd.osgi.Jar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest)

Example 17 with FileResource

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

the class MakeCopy method make.

public Resource make(Builder builder, String destination, Map<String, String> argumentsOnMake) throws Exception {
    String type = argumentsOnMake.get("type");
    if (!type.equals("copy"))
        return null;
    String from = argumentsOnMake.get("from");
    if (from == null) {
        String content = argumentsOnMake.get("content");
        if (content == null)
            throw new IllegalArgumentException("No 'from' or 'content' field in copy " + argumentsOnMake);
        return new EmbeddedResource(content.getBytes(UTF_8), 0);
    }
    File f = builder.getFile(from);
    if (f.isFile())
        return new FileResource(f);
    try {
        URL url = new URL(from);
        return new URLResource(url);
    } catch (MalformedURLException mfue) {
    // We ignore this
    }
    throw new IllegalArgumentException("Copy source does not exist " + from + " for destination " + destination);
}
Also used : URLResource(aQute.bnd.osgi.URLResource) MalformedURLException(java.net.MalformedURLException) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) FileResource(aQute.bnd.osgi.FileResource) File(java.io.File) URL(java.net.URL)

Example 18 with FileResource

use of aQute.bnd.osgi.FileResource 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 19 with FileResource

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

Aggregations

FileResource (aQute.bnd.osgi.FileResource)19 File (java.io.File)13 Jar (aQute.bnd.osgi.Jar)10 Manifest (java.util.jar.Manifest)7 Clazz (aQute.bnd.osgi.Clazz)6 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)6 Analyzer (aQute.bnd.osgi.Analyzer)5 Resource (aQute.bnd.osgi.Resource)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Container (aQute.bnd.build.Container)3 Parameters (aQute.bnd.header.Parameters)3 URLResource (aQute.bnd.osgi.URLResource)3 Attrs (aQute.bnd.header.Attrs)2 Domain (aQute.bnd.osgi.Domain)2 JarResource (aQute.bnd.osgi.JarResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Workspace (aQute.bnd.build.Workspace)1 PomResource (aQute.bnd.maven.PomResource)1