/** 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; }
/** * Verify that the Meta-Persistence header is correctly verified * * @throws Exception */ public void verifyMetaPersistence() throws Exception { Builder b = new Builder(); b.setIncludeResource("foo.xml;literal='I exist'"); Jar inner = b.build(); assertTrue(b.check()); Jar outer = new Jar("x"); outer.putResource("foo.jar", new JarResource(inner)); Manifest m = new Manifest(); m.getMainAttributes() .putValue(Constants.META_PERSISTENCE, "foo.jar, foo.jar!/foo.xml, absent.xml"); outer.setManifest(m); Verifier v = new Verifier(outer); v.verifyMetaPersistence(); assertTrue(v.check("Meta-Persistence refers to resources not in the bundle: \\[absent.xml\\]")); }
/* * Useful for when exported as folder or unzipped */ void doStart(Jar jar, String fqn) throws UnsupportedEncodingException { String nix = "#!/bin/sh\njava -cp . " + fqn + "\n"; String pc = "java -cp . " + fqn + "\r\n"; jar.putResource("start", new EmbeddedResource(nix, 0)); jar.putResource("start.bat", new EmbeddedResource(pc, 0)); }
/** * 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(project.getProperty("-package")); boolean useShas = packageHeader.containsKey("jpm"); project.trace("Useshas %s %s", useShas, packageHeader); Jar jar = new Jar(project.getName()); Builder b = new Builder(); project.addClose(b); if (!project.getIncludeResource().isEmpty()) { b.setIncludeResource(project.getIncludeResource().toString()); b.setProperty(Constants.RESOURCEONLY, "true"); b.build(); if (b.isOk()) { jar.addAll(b.getJar()); } project.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) { project.trace("embedding runpath %s", 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 = "jar/" + file.getName(); 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) { project.trace("embedding run bundles %s", path); File file = new File(path); if (!file.isFile()) project.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 = "jar/" + file.getName(); jar.putResource(newPath, new FileResource(file)); actualPaths.add(newPath); } } } LauncherConstants lc = getConstants(actualPaths, true); lc.embedded = !useShas; lc.storageDir = null; // cannot use local info 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 : project.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(project.getProperty(Constants.REMOVEHEADERS)); Collection<Object> result = instructions.select(main.keySet(), false); main.keySet().removeAll(result); if (useShas) { project.trace("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 { project.trace("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 (project.getProperty(Constants.DIGESTS) != null) jar.setDigestAlgorithms(project.getProperty(Constants.DIGESTS).trim().split("\\s*,\\s*")); else jar.setDigestAlgorithms(new String[] {"SHA-1", "MD-5"}); jar.setManifest(m); return jar; }
public void sign(Builder builder, String alias) throws Exception { File f = builder.getFile(keystore); if (!f.isFile()) { builder.error("Invalid keystore %s", f.getAbsolutePath()); return; } Jar jar = builder.getJar(); File tmp = File.createTempFile("signdjar", ".jar"); tmp.deleteOnExit(); jar.write(tmp); Command command = new Command(); command.add(path); if (keystore != null) { command.add("-keystore"); command.add(f.getAbsolutePath()); } if (storetype != null) { command.add("-storetype"); command.add(storetype); } if (keypass != null) { command.add("-keypass"); command.add(keypass); } if (storepass != null) { command.add("-storepass"); command.add(storepass); } if (sigFile != null) { command.add("-sigFile"); command.add(sigFile); } if (digestalg != null) { command.add("-digestalg"); command.add(digestalg); } command.add(tmp.getAbsolutePath()); command.add(alias); logger.debug("Jarsigner command: {}", command); command.setTimeout(20, TimeUnit.SECONDS); StringBuilder out = new StringBuilder(); StringBuilder err = new StringBuilder(); int exitValue = command.execute(out, err); if (exitValue != 0) { builder.error("Signing Jar out: %s%nerr: %s", out, err); } else { logger.debug("Signing Jar out: {}\nerr: {}", out, err); } Jar signed = new Jar(tmp); builder.addClose(signed); Map<String, Resource> dir = signed.getDirectories().get("META-INF"); for (Entry<String, Resource> entry : dir.entrySet()) { String path = entry.getKey(); if (path.matches(".*\\.(DSA|RSA|SF|MF)$")) { jar.putResource(path, entry.getValue()); } } jar.setDoNotTouchManifest(); }
public void signJar(Jar jar) { if (digestNames == null || digestNames.length == 0) error("Need at least one digest algorithm name, none are specified"); if (keystoreFile == null || !keystoreFile.getAbsoluteFile().exists()) { error("No such keystore file: " + keystoreFile); return; } if (alias == null) { error("Private key alias not set for signing"); return; } MessageDigest digestAlgorithms[] = new MessageDigest[digestNames.length]; getAlgorithms(digestNames, digestAlgorithms); try { Manifest manifest = jar.getManifest(); manifest.getMainAttributes().putValue("Signed-By", "Bnd"); // Create a new manifest that contains the // Name parts with the specified digests ByteArrayOutputStream o = new ByteArrayOutputStream(); manifest.write(o); doManifest(jar, digestNames, digestAlgorithms, o); o.flush(); byte newManifestBytes[] = o.toByteArray(); jar.putResource("META-INF/MANIFEST.MF", new EmbeddedResource(newManifestBytes, 0)); // Use the bytes from the new manifest to create // a signature file byte[] signatureFileBytes = doSignatureFile(digestNames, digestAlgorithms, newManifestBytes); jar.putResource("META-INF/BND.SF", new EmbeddedResource(signatureFileBytes, 0)); // Now we must create an RSA signature // this requires the private key from the keystore KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore.PrivateKeyEntry privateKeyEntry = null; java.io.FileInputStream keystoreInputStream = null; try { keystoreInputStream = new java.io.FileInputStream(keystoreFile); char[] pw = password == null ? new char[0] : password.toCharArray(); keystore.load(keystoreInputStream, pw); keystoreInputStream.close(); privateKeyEntry = (PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(pw)); } catch (Exception e) { error( "No able to load the private key from the give keystore(" + keystoreFile.getAbsolutePath() + ") with alias " + alias + " : " + e); return; } finally { IO.close(keystoreInputStream); } PrivateKey privateKey = privateKeyEntry.getPrivateKey(); Signature signature = Signature.getInstance("MD5withRSA"); signature.initSign(privateKey); signature.update(signatureFileBytes); signature.sign(); // TODO, place the SF in a PCKS#7 structure ... // no standard class for this? The following // is an idea but we will to have do ASN.1 BER // encoding ... ByteArrayOutputStream tmpStream = new ByteArrayOutputStream(); jar.putResource("META-INF/BND.RSA", new EmbeddedResource(tmpStream.toByteArray(), 0)); } catch (Exception e) { error("During signing: " + e); } }