Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.deployer; |
| 2 | |
| 3 | import java.io.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 4 | import java.util.*; |
| 5 | import java.util.jar.*; |
| 6 | import java.util.regex.*; |
| 7 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 8 | import aQute.bnd.header.*; |
| 9 | import aQute.bnd.osgi.*; |
Stuart McCulloch | cd1ddd7 | 2012-07-19 13:11:20 +0000 | [diff] [blame] | 10 | import aQute.bnd.version.*; |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 11 | import aQute.lib.io.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 12 | import aQute.service.reporter.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | |
| 14 | public class FileInstallRepo extends FileRepo { |
| 15 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 16 | String group; |
| 17 | boolean dirty; |
| 18 | Reporter reporter; |
| 19 | Pattern REPO_FILE = Pattern.compile("([-a-zA-z0-9_\\.]+)-([0-9\\.]+)\\.(jar|lib)"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 20 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 21 | @Override |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 22 | public void setProperties(Map<String,String> map) { |
| 23 | super.setProperties(map); |
| 24 | group = map.get("group"); |
| 25 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 27 | @Override |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | public void setReporter(Reporter reporter) { |
| 29 | super.setReporter(reporter); |
| 30 | this.reporter = reporter; |
| 31 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 33 | @Override |
| 34 | protected PutResult putArtifact(File tmpFile, PutOptions options) throws Exception { |
| 35 | assert (tmpFile != null); |
| 36 | assert (options != null); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 38 | Jar jar = null; |
| 39 | try { |
| 40 | init(); |
| 41 | dirty = true; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 43 | jar = new Jar(tmpFile); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 45 | Manifest manifest = jar.getManifest(); |
| 46 | if (manifest == null) |
| 47 | throw new IllegalArgumentException("No manifest in JAR: " + jar); |
| 48 | |
| 49 | String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME); |
| 50 | if (bsn == null) |
| 51 | throw new IllegalArgumentException("No Bundle SymbolicName set"); |
| 52 | |
| 53 | Parameters b = Processor.parseHeader(bsn, null); |
| 54 | if (b.size() != 1) |
| 55 | throw new IllegalArgumentException("Multiple bsn's specified " + b); |
| 56 | |
| 57 | for (String key : b.keySet()) { |
| 58 | bsn = key; |
| 59 | if (!Verifier.SYMBOLICNAME.matcher(bsn).matches()) |
| 60 | throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn); |
| 61 | } |
| 62 | |
| 63 | String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION); |
| 64 | Version version; |
| 65 | if (versionString == null) |
| 66 | version = new Version(); |
| 67 | else |
| 68 | version = new Version(versionString); |
| 69 | |
| 70 | if (reporter != null) |
| 71 | reporter.trace("bsn=%s version=%s", bsn, version); |
| 72 | |
| 73 | File dir; |
| 74 | if (group == null) { |
| 75 | dir = getRoot(); |
| 76 | } else { |
| 77 | dir = new File(getRoot(), group); |
| 78 | if (!dir.exists() && !dir.mkdirs()) { |
| 79 | throw new IOException("Could not create directory " + dir); |
| 80 | } |
| 81 | } |
| 82 | String fName = bsn + "-" + version.getWithoutQualifier() + ".jar"; |
| 83 | File file = new File(dir, fName); |
| 84 | |
| 85 | PutResult result = new PutResult(); |
| 86 | |
| 87 | if (reporter != null) |
| 88 | reporter.trace("updating %s ", file.getAbsolutePath()); |
| 89 | |
| 90 | if (file.exists()) { |
| 91 | IO.delete(file); |
| 92 | } |
| 93 | IO.rename(tmpFile, file); |
| 94 | result.artifact = file.toURI(); |
| 95 | |
| 96 | if (reporter != null) |
| 97 | reporter.progress(-1, "updated " + file.getAbsolutePath()); |
| 98 | |
| 99 | fireBundleAdded(jar, file); |
| 100 | |
| 101 | File latest = new File(dir, bsn + "-latest.jar"); |
| 102 | boolean latestExists = latest.exists() && latest.isFile(); |
| 103 | boolean latestIsOlder = latestExists && (latest.lastModified() < jar.lastModified()); |
| 104 | if ((options.createLatest && !latestExists) || latestIsOlder) { |
| 105 | if (latestExists) { |
| 106 | IO.delete(latest); |
| 107 | } |
| 108 | IO.copy(file, latest); |
| 109 | result.latest = latest.toURI(); |
| 110 | } |
| 111 | |
| 112 | return result; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 113 | } |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 114 | finally { |
| 115 | if (jar != null) { |
| 116 | jar.close(); |
| 117 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 118 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame^] | 121 | @Override |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 122 | public boolean refresh() { |
| 123 | if (dirty) { |
| 124 | dirty = false; |
| 125 | return true; |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 126 | } |
| 127 | return false; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 130 | @Override |
| 131 | public List<String> list(String regex) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 132 | Instruction pattern = null; |
| 133 | if (regex != null) |
| 134 | pattern = new Instruction(regex); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 135 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 136 | String list[] = getRoot().list(); |
| 137 | List<String> result = new ArrayList<String>(); |
| 138 | for (String f : list) { |
| 139 | Matcher m = REPO_FILE.matcher(f); |
| 140 | if (!m.matches()) { |
| 141 | continue; |
| 142 | } |
| 143 | String s = m.group(1); |
| 144 | if (pattern == null || pattern.matches(s)) |
| 145 | result.add(s); |
| 146 | } |
| 147 | return result; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 148 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 149 | } |