Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.bnd.signing; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.concurrent.*; |
| 6 | |
| 7 | import aQute.bnd.service.*; |
| 8 | import aQute.lib.osgi.*; |
| 9 | import aQute.libg.command.*; |
| 10 | import aQute.libg.reporter.*; |
| 11 | |
| 12 | /** |
| 13 | * Sign the jar file. |
| 14 | * |
| 15 | * -sign : <alias> [ ';' 'password:=' <password> ] [ ';' 'keystore:=' <keystore> ] [ |
| 16 | * ';' 'sign-password:=' <pw> ] ( ',' ... )* |
| 17 | * |
| 18 | * @author aqute |
| 19 | * |
| 20 | */ |
| 21 | public class JartoolSigner implements Plugin, SignerPlugin { |
| 22 | String keystore; |
| 23 | String storetype; |
| 24 | String path = "jarsigner"; |
| 25 | String storepass; |
| 26 | String keypass; |
| 27 | String sigFile; |
| 28 | |
| 29 | public void setProperties(Map<String, String> map) { |
| 30 | if (map.containsKey("keystore")) |
| 31 | this.keystore = map.get("keystore"); |
| 32 | if (map.containsKey("storetype")) |
| 33 | this.storetype = map.get("storetype"); |
| 34 | if (map.containsKey("storepass")) |
| 35 | this.storepass = map.get("storepass"); |
| 36 | if (map.containsKey("keypass")) |
| 37 | this.keypass = map.get("keypass"); |
| 38 | if (map.containsKey("path")) |
| 39 | this.path = map.get("path"); |
| 40 | if (map.containsKey("sigFile")) |
| 41 | this.sigFile = map.get("sigFile"); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | public void setReporter(Reporter processor) { |
| 46 | } |
| 47 | |
| 48 | public void sign(Builder builder, String alias) throws Exception { |
| 49 | File f = builder.getFile(keystore); |
| 50 | if ( !f.isFile()) { |
| 51 | builder.error("Invalid keystore %s", f.getAbsolutePath() ); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | Jar jar = builder.getJar(); |
| 56 | File tmp = File.createTempFile("signdjar", ".jar"); |
| 57 | tmp.deleteOnExit(); |
| 58 | |
| 59 | jar.write(tmp); |
| 60 | |
| 61 | Command command = new Command(); |
| 62 | command.add(path); |
| 63 | if (keystore != null) { |
| 64 | command.add("-keystore"); |
| 65 | command.add(f.getAbsolutePath()); |
| 66 | } |
| 67 | |
| 68 | if (storetype != null) { |
| 69 | command.add("-storetype"); |
| 70 | command.add(storetype); |
| 71 | } |
| 72 | |
| 73 | if (keypass != null) { |
| 74 | command.add("-keypass"); |
| 75 | command.add(keypass); |
| 76 | } |
| 77 | |
| 78 | if (storepass != null) { |
| 79 | command.add("-storepass"); |
| 80 | command.add(storepass); |
| 81 | } |
| 82 | |
| 83 | if (sigFile != null) { |
| 84 | command.add("-sigFile"); |
| 85 | command.add(sigFile); |
| 86 | } |
| 87 | |
| 88 | command.add(tmp.getAbsolutePath()); |
| 89 | command.add(alias); |
| 90 | builder.trace("Jarsigner command: %s", command); |
| 91 | command.setTimeout(20, TimeUnit.SECONDS); |
| 92 | StringBuffer out = new StringBuffer(); |
| 93 | StringBuffer err = new StringBuffer(); |
| 94 | int exitValue = command.execute(System.in, out, err); |
| 95 | if (exitValue != 0) { |
| 96 | builder.error("Signing Jar out: %s\nerr: %s", out, err); |
| 97 | } else { |
| 98 | builder.trace("Signing Jar out: %s \nerr: %s", out, err); |
| 99 | } |
| 100 | |
| 101 | Jar signed = new Jar(tmp); |
| 102 | builder.addClose(signed); |
| 103 | |
| 104 | Map<String, Resource> dir = signed.getDirectories().get("META-INF"); |
| 105 | for (String path : dir.keySet()) { |
| 106 | if (path.matches(".*\\.(DSA|RSA|SF|MF)$")) { |
| 107 | jar.putResource(path, dir.get(path)); |
| 108 | } |
| 109 | } |
| 110 | jar.setDoNotTouchManifest(); |
| 111 | } |
| 112 | |
| 113 | StringBuffer collect(final InputStream in) throws Exception { |
| 114 | final StringBuffer sb = new StringBuffer(); |
| 115 | |
| 116 | Thread tin = new Thread() { |
| 117 | public void run() { |
| 118 | try { |
| 119 | BufferedReader rdr = new BufferedReader(new InputStreamReader(in, Constants.DEFAULT_CHARSET)); |
| 120 | String line = rdr.readLine(); |
| 121 | while (line != null) { |
| 122 | sb.append(line); |
| 123 | line = rdr.readLine(); |
| 124 | } |
| 125 | rdr.close(); |
| 126 | in.close(); |
| 127 | } catch (Exception e) { |
| 128 | // Ignore any exceptions |
| 129 | } |
| 130 | } |
| 131 | }; |
| 132 | tin.start(); |
| 133 | return sb; |
| 134 | } |
| 135 | } |