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 | |
| 8 | import aQute.lib.osgi.*; |
| 9 | import aQute.libg.header.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | import aQute.libg.version.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 11 | import aQute.service.reporter.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | |
| 13 | public class FileInstallRepo extends FileRepo { |
| 14 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | String group; |
| 16 | boolean dirty; |
| 17 | Reporter reporter; |
| 18 | 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] | 19 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | public void setProperties(Map<String,String> map) { |
| 21 | super.setProperties(map); |
| 22 | group = map.get("group"); |
| 23 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 25 | public void setReporter(Reporter reporter) { |
| 26 | super.setReporter(reporter); |
| 27 | this.reporter = reporter; |
| 28 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | public File put(Jar jar) throws Exception { |
| 31 | dirty = true; |
| 32 | Manifest manifest = jar.getManifest(); |
| 33 | if (manifest == null) |
| 34 | throw new IllegalArgumentException("No manifest in JAR: " + jar); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME); |
| 37 | if (bsn == null) |
| 38 | throw new IllegalArgumentException("No Bundle SymbolicName set"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 39 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 40 | Parameters b = Processor.parseHeader(bsn, null); |
| 41 | if (b.size() != 1) |
| 42 | throw new IllegalArgumentException("Multiple bsn's specified " + b); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 44 | for (String key : b.keySet()) { |
| 45 | bsn = key; |
| 46 | if (!Verifier.SYMBOLICNAME.matcher(bsn).matches()) |
| 47 | throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn); |
| 48 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 49 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 50 | String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION); |
| 51 | Version version; |
| 52 | if (versionString == null) |
| 53 | version = new Version(); |
| 54 | else |
| 55 | version = new Version(versionString); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 56 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 57 | File dir; |
| 58 | if (group == null) { |
| 59 | dir = getRoot(); |
| 60 | } else { |
| 61 | dir = new File(getRoot(), group); |
| 62 | dir.mkdirs(); |
| 63 | } |
| 64 | String fName = bsn + "-" + version.getMajor() + "." + version.getMinor() + "." + version.getMicro() + ".jar"; |
| 65 | File file = new File(dir, fName); |
| 66 | |
| 67 | jar.write(file); |
| 68 | fireBundleAdded(jar, file); |
| 69 | |
| 70 | file = new File(dir, bsn + "-latest.jar"); |
| 71 | if (file.isFile() && file.lastModified() < jar.lastModified()) { |
| 72 | jar.write(file); |
| 73 | } |
| 74 | return file; |
| 75 | } |
| 76 | |
| 77 | public boolean refresh() { |
| 78 | if (dirty) { |
| 79 | dirty = false; |
| 80 | return true; |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 81 | } |
| 82 | return false; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 85 | @Override |
| 86 | public List<String> list(String regex) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 87 | Instruction pattern = null; |
| 88 | if (regex != null) |
| 89 | pattern = new Instruction(regex); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 90 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 91 | String list[] = getRoot().list(); |
| 92 | List<String> result = new ArrayList<String>(); |
| 93 | for (String f : list) { |
| 94 | Matcher m = REPO_FILE.matcher(f); |
| 95 | if (!m.matches()) { |
| 96 | continue; |
| 97 | } |
| 98 | String s = m.group(1); |
| 99 | if (pattern == null || pattern.matches(s)) |
| 100 | result.add(s); |
| 101 | } |
| 102 | return result; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 103 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 104 | } |