Search in sources :

Example 6 with URLResource

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

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

the class ProjectLauncherImpl method executable.

/**
	 * Create a standalone executable. All entries on the runpath are rolled out
	 * into the JAR and the runbundles are copied to a directory in the jar. The
	 * launcher will see that it starts in embedded mode and will automatically
	 * detect that it should load the bundles from inside. This is drive by the
	 * launcher.embedded flag.
	 * 
	 * @throws Exception
	 */
@Override
public Jar executable() throws Exception {
    // TODO use constants in the future
    Parameters packageHeader = OSGiHeader.parseHeader(getProject().getProperty("-package"));
    boolean useShas = packageHeader.containsKey("jpm");
    logger.debug("useShas {} {}", useShas, packageHeader);
    Jar jar = new Jar(getProject().getName());
    Builder b = new Builder();
    getProject().addClose(b);
    if (!getProject().getIncludeResource().isEmpty()) {
        b.setIncludeResource(getProject().getIncludeResource().toString());
        b.setProperty(Constants.RESOURCEONLY, "true");
        b.build();
        if (b.isOk()) {
            jar.addAll(b.getJar());
        }
        getProject().getInfo(b);
    }
    List<String> runpath = getRunpath();
    Set<String> runpathShas = new LinkedHashSet<String>();
    Set<String> runbundleShas = new LinkedHashSet<String>();
    List<String> classpath = new ArrayList<String>();
    for (String path : runpath) {
        logger.debug("embedding runpath {}", path);
        File file = new File(path);
        if (file.isFile()) {
            if (useShas) {
                String sha = SHA1.digest(file).asHex();
                runpathShas.add(sha + ";name=\"" + file.getName() + "\"");
            } else {
                String newPath = nonCollidingPath(file, jar);
                jar.putResource(newPath, new FileResource(file));
                classpath.add(newPath);
            }
        }
    }
    // Copy the bundles to the JAR
    List<String> runbundles = (List<String>) getRunBundles();
    List<String> actualPaths = new ArrayList<String>();
    for (String path : runbundles) {
        logger.debug("embedding run bundles {}", path);
        File file = new File(path);
        if (!file.isFile())
            getProject().error("Invalid entry in -runbundles %s", file);
        else {
            if (useShas) {
                String sha = SHA1.digest(file).asHex();
                runbundleShas.add(sha + ";name=\"" + file.getName() + "\"");
                actualPaths.add("${JPMREPO}/" + sha);
            } else {
                String newPath = nonCollidingPath(file, jar);
                jar.putResource(newPath, new FileResource(file));
                actualPaths.add(newPath);
            }
        }
    }
    LauncherConstants lc = getConstants(actualPaths, true);
    lc.embedded = !useShas;
    // cannot use local info
    lc.storageDir = null;
    final Properties p = lc.getProperties(new UTF8Properties());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    p.store(bout, "");
    jar.putResource(LauncherConstants.DEFAULT_LAUNCHER_PROPERTIES, new EmbeddedResource(bout.toByteArray(), 0L));
    Manifest m = new Manifest();
    Attributes main = m.getMainAttributes();
    for (Entry<Object, Object> e : getProject().getFlattenedProperties().entrySet()) {
        String key = (String) e.getKey();
        if (key.length() > 0 && Character.isUpperCase(key.charAt(0)))
            main.putValue(key, (String) e.getValue());
    }
    Instructions instructions = new Instructions(getProject().getProperty(Constants.REMOVEHEADERS));
    Collection<Object> result = instructions.select(main.keySet(), false);
    main.keySet().removeAll(result);
    if (useShas) {
        logger.debug("Use JPM launcher");
        m.getMainAttributes().putValue("Main-Class", JPM_LAUNCHER_FQN);
        m.getMainAttributes().putValue("JPM-Classpath", Processor.join(runpathShas));
        m.getMainAttributes().putValue("JPM-Runbundles", Processor.join(runbundleShas));
        URLResource jpmLauncher = new URLResource(this.getClass().getResource("/" + JPM_LAUNCHER));
        jar.putResource(JPM_LAUNCHER, jpmLauncher);
        doStart(jar, JPM_LAUNCHER_FQN);
    } else {
        logger.debug("Use Embedded launcher");
        m.getMainAttributes().putValue("Main-Class", EMBEDDED_LAUNCHER_FQN);
        m.getMainAttributes().putValue(EmbeddedLauncher.EMBEDDED_RUNPATH, Processor.join(classpath));
        URLResource embeddedLauncher = new URLResource(this.getClass().getResource("/" + EMBEDDED_LAUNCHER));
        jar.putResource(EMBEDDED_LAUNCHER, embeddedLauncher);
        doStart(jar, EMBEDDED_LAUNCHER_FQN);
    }
    if (getProject().getProperty(Constants.DIGESTS) != null)
        jar.setDigestAlgorithms(getProject().getProperty(Constants.DIGESTS).trim().split("\\s*,\\s*"));
    else
        jar.setDigestAlgorithms(new String[] { "SHA-1", "MD-5" });
    jar.setManifest(m);
    cleanup();
    return jar;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Parameters(aQute.bnd.header.Parameters) Builder(aQute.bnd.osgi.Builder) ArrayList(java.util.ArrayList) FileResource(aQute.bnd.osgi.FileResource) Attributes(java.util.jar.Attributes) Instructions(aQute.bnd.osgi.Instructions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UTF8Properties(aQute.lib.utf8properties.UTF8Properties) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) URLResource(aQute.bnd.osgi.URLResource) EmbeddedResource(aQute.bnd.osgi.EmbeddedResource) Jar(aQute.bnd.osgi.Jar) ArrayList(java.util.ArrayList) List(java.util.List) LauncherConstants(aQute.launcher.constants.LauncherConstants) File(java.io.File) UTF8Properties(aQute.lib.utf8properties.UTF8Properties)

Example 8 with URLResource

use of aQute.bnd.osgi.URLResource 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 9 with URLResource

use of aQute.bnd.osgi.URLResource in project felix by apache.

the class BndJarResourceStoreTestCase method testAnalysisWithExcludedEmbedComponents.

public void testAnalysisWithExcludedEmbedComponents() throws Exception {
    PojoizationPlugin plugin = new PojoizationPlugin();
    Map<String, String> props = new HashMap<String, String>();
    Resource resource = new URLResource(getClass().getResource("/EMBED-MANIFEST.MF"));
    doReturn(dot).when(analyzer).getJar();
    doReturn(resource).when(embed).getResource(eq("META-INF/MANIFEST.MF"));
    analyzer.setClasspath(new Jar[] { embed });
    plugin.setReporter(reporter);
    plugin.setProperties(props);
    plugin.analyzeJar(analyzer);
    assertNull(analyzer.getProperty("IPOJO-Components"));
}
Also used : URLResource(aQute.bnd.osgi.URLResource) HashMap(java.util.HashMap) Resource(aQute.bnd.osgi.Resource) URLResource(aQute.bnd.osgi.URLResource)

Example 10 with URLResource

use of aQute.bnd.osgi.URLResource in project felix by apache.

the class BndJarResourceStoreTestCase method testAnalysisWithLocallyDefinedComponentAndEmbedResource.

public void testAnalysisWithLocallyDefinedComponentAndEmbedResource() throws Exception {
    PojoizationPlugin plugin = new PojoizationPlugin();
    Map<String, String> props = new HashMap<String, String>();
    props.put("include-embed-bundles", "true");
    String path = EmptyComponent.class.getName().replace('.', '/').concat(".class");
    Resource resource2 = new URLResource(getClass().getResource("/metadata-test-component.xml"));
    doReturn(dot).when(analyzer).getJar();
    doReturn(resource2).when(dot).getResource(eq("META-INF/metadata.xml"));
    Collection<Clazz> classes = new ArrayList<Clazz>();
    Resource typeResource = new URLResource(getClass().getResource("EmptyComponent.class"));
    Clazz clazz = new Clazz(analyzer, path, typeResource);
    clazz.parseClassFile();
    classes.add(clazz);
    doReturn(classes).when(analyzer).getClasses(Matchers.<String[]>anyVararg());
    Resource resource = new URLResource(getClass().getResource("/EMBED-MANIFEST-EMPTY.MF"));
    doReturn(resource).when(embed).getResource(eq("META-INF/MANIFEST.MF"));
    doReturn(typeResource).when(embed).getResource(path);
    doReturn("aaa").when(embed).getBsn();
    analyzer.setClasspath(new Jar[] { embed });
    plugin.setReporter(reporter);
    plugin.setProperties(props);
    plugin.analyzeJar(analyzer);
    assertContains("component { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" manipulation { $classname=\"org.apache.felix.ipojo.bnd.EmptyComponent\" method { $name=\"$init\" }}}", analyzer.getProperty("IPOJO-Components"));
    verify(dot).putResource(eq(path), any(Resource.class));
}
Also used : URLResource(aQute.bnd.osgi.URLResource) HashMap(java.util.HashMap) Resource(aQute.bnd.osgi.Resource) URLResource(aQute.bnd.osgi.URLResource) ArrayList(java.util.ArrayList) Clazz(aQute.bnd.osgi.Clazz)

Aggregations

URLResource (aQute.bnd.osgi.URLResource)10 Resource (aQute.bnd.osgi.Resource)7 HashMap (java.util.HashMap)7 FileResource (aQute.bnd.osgi.FileResource)3 File (java.io.File)3 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)2 Jar (aQute.bnd.osgi.Jar)2 ArrayList (java.util.ArrayList)2 Manifest (java.util.jar.Manifest)2 Container (aQute.bnd.build.Container)1 Attrs (aQute.bnd.header.Attrs)1 Parameters (aQute.bnd.header.Parameters)1 Analyzer (aQute.bnd.osgi.Analyzer)1 Builder (aQute.bnd.osgi.Builder)1 Clazz (aQute.bnd.osgi.Clazz)1 Instructions (aQute.bnd.osgi.Instructions)1 LauncherConstants (aQute.launcher.constants.LauncherConstants)1 UTF8Properties (aQute.lib.utf8properties.UTF8Properties)1 EmbeddedActivator (aQute.remote.embedded.activator.EmbeddedActivator)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1