Search in sources :

Example 1 with LauncherConstants

use of aQute.launcher.constants.LauncherConstants 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 2 with LauncherConstants

use of aQute.launcher.constants.LauncherConstants in project bnd by bndtools.

the class ProjectLauncherImpl method invoke.

//
// Initialize the main class for a local launch start
//
protected int invoke(Class<?> main, String[] args) throws Exception {
    LauncherConstants lc = getConstants(getRunBundles(), false);
    Method mainMethod = main.getMethod("main", args.getClass(), Properties.class);
    Object o = mainMethod.invoke(null, args, lc.getProperties(new UTF8Properties()));
    if (o == null)
        return 0;
    return (Integer) o;
}
Also used : Method(java.lang.reflect.Method) LauncherConstants(aQute.launcher.constants.LauncherConstants) UTF8Properties(aQute.lib.utf8properties.UTF8Properties)

Example 3 with LauncherConstants

use of aQute.launcher.constants.LauncherConstants in project bnd by bndtools.

the class ProjectLauncherImpl method getConstants.

/**
	 * @throws Exception
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
private LauncherConstants getConstants(Collection<String> runbundles, boolean exported) throws Exception, FileNotFoundException, IOException {
    logger.debug("preparing the aQute launcher plugin");
    LauncherConstants lc = new LauncherConstants();
    lc.noreferences = getProject().is(Constants.RUNNOREFERENCES);
    lc.runProperties = getRunProperties();
    lc.storageDir = getStorageDir();
    lc.keep = isKeep();
    lc.runbundles.addAll(runbundles);
    lc.trace = getTrace();
    lc.timeout = getTimeout();
    lc.services = super.getRunFramework() == SERVICES ? true : false;
    lc.activators.addAll(getActivators());
    lc.name = getProject().getName();
    if (!exported && !getNotificationListeners().isEmpty()) {
        if (listenerComms == null) {
            listenerComms = new DatagramSocket(new InetSocketAddress(InetAddress.getByName(null), 0));
            new Thread(new Runnable() {

                public void run() {
                    DatagramSocket socket = listenerComms;
                    DatagramPacket packet = new DatagramPacket(new byte[65536], 65536);
                    while (!socket.isClosed()) {
                        try {
                            socket.receive(packet);
                            DataInputStream dai = new DataInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()));
                            NotificationType type = NotificationType.values()[dai.readInt()];
                            String message = dai.readUTF();
                            for (NotificationListener listener : getNotificationListeners()) {
                                listener.notify(type, message);
                            }
                        } catch (IOException e) {
                        }
                    }
                }
            }).start();
        }
        lc.notificationPort = listenerComms.getLocalPort();
    } else {
        lc.notificationPort = -1;
    }
    try {
        // If the workspace contains a newer version of biz.aQute.launcher
        // than the version of bnd(tools) used
        // then this could throw NoSuchMethodError. For now just ignore it.
        Map<String, ? extends Map<String, String>> systemPkgs = getSystemPackages();
        if (systemPkgs != null && !systemPkgs.isEmpty())
            lc.systemPackages = Processor.printClauses(systemPkgs);
    } catch (Throwable e) {
    }
    try {
        // If the workspace contains a newer version of biz.aQute.launcher
        // than the version of bnd(tools) used
        // then this could throw NoSuchMethodError. For now just ignore it.
        String systemCaps = getSystemCapabilities();
        if (systemCaps != null) {
            systemCaps = systemCaps.trim();
            if (systemCaps.length() > 0)
                lc.systemCapabilities = systemCaps;
        }
    } catch (Throwable e) {
    }
    return lc;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) DatagramSocket(java.net.DatagramSocket) ByteArrayInputStream(java.io.ByteArrayInputStream) DatagramPacket(java.net.DatagramPacket) LauncherConstants(aQute.launcher.constants.LauncherConstants)

Aggregations

LauncherConstants (aQute.launcher.constants.LauncherConstants)3 UTF8Properties (aQute.lib.utf8properties.UTF8Properties)2 Parameters (aQute.bnd.header.Parameters)1 Builder (aQute.bnd.osgi.Builder)1 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)1 FileResource (aQute.bnd.osgi.FileResource)1 Instructions (aQute.bnd.osgi.Instructions)1 Jar (aQute.bnd.osgi.Jar)1 URLResource (aQute.bnd.osgi.URLResource)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1