Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.deployer; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.net.*; |
| 5 | import java.util.*; |
| 6 | import java.util.jar.*; |
| 7 | import java.util.regex.*; |
| 8 | |
| 9 | import aQute.lib.osgi.*; |
| 10 | import aQute.libg.header.*; |
| 11 | import aQute.libg.reporter.*; |
| 12 | import aQute.libg.version.*; |
| 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 | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 21 | public void setProperties(Map<String,String> map) { |
| 22 | super.setProperties(map); |
| 23 | group = map.get("group"); |
| 24 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 25 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 26 | public void setReporter(Reporter reporter) { |
| 27 | super.setReporter(reporter); |
| 28 | this.reporter = reporter; |
| 29 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 31 | public File put(Jar jar) throws Exception { |
| 32 | dirty = true; |
| 33 | Manifest manifest = jar.getManifest(); |
| 34 | if (manifest == null) |
| 35 | throw new IllegalArgumentException("No manifest in JAR: " + jar); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 37 | String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME); |
| 38 | if (bsn == null) |
| 39 | throw new IllegalArgumentException("No Bundle SymbolicName set"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | Parameters b = Processor.parseHeader(bsn, null); |
| 42 | if (b.size() != 1) |
| 43 | throw new IllegalArgumentException("Multiple bsn's specified " + b); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 45 | for (String key : b.keySet()) { |
| 46 | bsn = key; |
| 47 | if (!Verifier.SYMBOLICNAME.matcher(bsn).matches()) |
| 48 | throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn); |
| 49 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 51 | String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION); |
| 52 | Version version; |
| 53 | if (versionString == null) |
| 54 | version = new Version(); |
| 55 | else |
| 56 | version = new Version(versionString); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 57 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 58 | File dir; |
| 59 | if (group == null) { |
| 60 | dir = getRoot(); |
| 61 | } else { |
| 62 | dir = new File(getRoot(), group); |
| 63 | dir.mkdirs(); |
| 64 | } |
| 65 | String fName = bsn + "-" + version.getMajor() + "." + version.getMinor() + "." + version.getMicro() + ".jar"; |
| 66 | File file = new File(dir, fName); |
| 67 | |
| 68 | jar.write(file); |
| 69 | fireBundleAdded(jar, file); |
| 70 | |
| 71 | file = new File(dir, bsn + "-latest.jar"); |
| 72 | if (file.isFile() && file.lastModified() < jar.lastModified()) { |
| 73 | jar.write(file); |
| 74 | } |
| 75 | return file; |
| 76 | } |
| 77 | |
| 78 | public boolean refresh() { |
| 79 | if (dirty) { |
| 80 | dirty = false; |
| 81 | return true; |
| 82 | } else |
| 83 | return false; |
| 84 | } |
| 85 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 86 | @Override |
| 87 | public List<String> list(String regex) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 88 | Instruction pattern = null; |
| 89 | if (regex != null) |
| 90 | pattern = new Instruction(regex); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 91 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | String list[] = getRoot().list(); |
| 93 | List<String> result = new ArrayList<String>(); |
| 94 | for (String f : list) { |
| 95 | Matcher m = REPO_FILE.matcher(f); |
| 96 | if (!m.matches()) { |
| 97 | continue; |
| 98 | } |
| 99 | String s = m.group(1); |
| 100 | if (pattern == null || pattern.matches(s)) |
| 101 | result.add(s); |
| 102 | } |
| 103 | return result; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 104 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 105 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 106 | @Override |
| 107 | public File[] get(String bsn, String versionRange) throws MalformedURLException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 108 | // If the version is set to project, we assume it is not |
| 109 | // for us. A project repo will then get it. |
| 110 | if (versionRange != null && versionRange.equals("project")) |
| 111 | return null; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 112 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 113 | // |
| 114 | // The version range we are looking for can |
| 115 | // be null (for all) or a version range. |
| 116 | // |
| 117 | VersionRange range; |
| 118 | if (versionRange == null || versionRange.equals("latest")) { |
| 119 | range = new VersionRange("0"); |
| 120 | } else |
| 121 | range = new VersionRange(versionRange); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 122 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 123 | // |
| 124 | // Iterator over all the versions for this BSN. |
| 125 | // Create a sorted map over the version as key |
| 126 | // and the file as URL as value. Only versions |
| 127 | // that match the desired range are included in |
| 128 | // this list. |
| 129 | // |
| 130 | File instances[] = getRoot().listFiles(); |
| 131 | SortedMap<Version,File> versions = new TreeMap<Version,File>(); |
| 132 | for (int i = 0; i < instances.length; i++) { |
| 133 | Matcher m = REPO_FILE.matcher(instances[i].getName()); |
| 134 | if (m.matches() && m.group(1).equals(bsn)) { |
| 135 | String versionString = m.group(2); |
| 136 | Version version; |
| 137 | if (versionString.equals("latest")) |
| 138 | version = new Version(Integer.MAX_VALUE); |
| 139 | else |
| 140 | version = new Version(versionString); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 141 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 142 | if (range.includes(version)) |
| 143 | versions.put(version, instances[i]); |
| 144 | } |
| 145 | } |
| 146 | return versions.values().toArray(new File[versions.size()]); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | } |