Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.deployer; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.jar.*; |
| 6 | import java.util.regex.*; |
| 7 | |
| 8 | import aQute.bnd.service.*; |
| 9 | import aQute.lib.io.*; |
| 10 | import aQute.lib.osgi.*; |
| 11 | import aQute.libg.header.*; |
| 12 | import aQute.libg.reporter.*; |
| 13 | import aQute.libg.version.*; |
| 14 | |
| 15 | public class FileRepo implements Plugin, RepositoryPlugin, Refreshable, RegistryPlugin { |
| 16 | public final static String LOCATION = "location"; |
| 17 | public final static String READONLY = "readonly"; |
| 18 | public final static String NAME = "name"; |
| 19 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | File[] EMPTY_FILES = new File[0]; |
| 21 | protected File root; |
| 22 | Registry registry; |
| 23 | boolean canWrite = true; |
| 24 | Pattern REPO_FILE = Pattern.compile("([-a-zA-z0-9_\\.]+)-([0-9\\.]+|latest)\\.(jar|lib)"); |
| 25 | Reporter reporter; |
| 26 | boolean dirty; |
| 27 | String name; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 29 | public FileRepo() {} |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
| 31 | public FileRepo(String name, File location, boolean canWrite) { |
| 32 | this.name = name; |
| 33 | this.root = location; |
| 34 | this.canWrite = canWrite; |
| 35 | } |
| 36 | |
| 37 | protected void init() throws Exception { |
| 38 | // for extensions |
| 39 | } |
| 40 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | public void setProperties(Map<String,String> map) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | String location = map.get(LOCATION); |
| 43 | if (location == null) |
| 44 | throw new IllegalArgumentException("Location must be set on a FileRepo plugin"); |
| 45 | |
| 46 | root = new File(location); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 47 | |
| 48 | String readonly = map.get(READONLY); |
| 49 | if (readonly != null && Boolean.valueOf(readonly).booleanValue()) |
| 50 | canWrite = false; |
| 51 | |
| 52 | name = map.get(NAME); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get a list of URLs to bundles that are constrained by the bsn and |
| 57 | * versionRange. |
| 58 | */ |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 59 | private File[] get(String bsn, String versionRange) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | init(); |
| 61 | |
| 62 | // If the version is set to project, we assume it is not |
| 63 | // for us. A project repo will then get it. |
| 64 | if (versionRange != null && versionRange.equals("project")) |
| 65 | return null; |
| 66 | |
| 67 | // |
| 68 | // Check if the entry exists |
| 69 | // |
| 70 | File f = new File(root, bsn); |
| 71 | if (!f.isDirectory()) |
| 72 | return null; |
| 73 | |
| 74 | // |
| 75 | // The version range we are looking for can |
| 76 | // be null (for all) or a version range. |
| 77 | // |
| 78 | VersionRange range; |
| 79 | if (versionRange == null || versionRange.equals("latest")) { |
| 80 | range = new VersionRange("0"); |
| 81 | } else |
| 82 | range = new VersionRange(versionRange); |
| 83 | |
| 84 | // |
| 85 | // Iterator over all the versions for this BSN. |
| 86 | // Create a sorted map over the version as key |
| 87 | // and the file as URL as value. Only versions |
| 88 | // that match the desired range are included in |
| 89 | // this list. |
| 90 | // |
| 91 | File instances[] = f.listFiles(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | SortedMap<Version,File> versions = new TreeMap<Version,File>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 93 | for (int i = 0; i < instances.length; i++) { |
| 94 | Matcher m = REPO_FILE.matcher(instances[i].getName()); |
| 95 | if (m.matches() && m.group(1).equals(bsn)) { |
| 96 | String versionString = m.group(2); |
| 97 | Version version; |
| 98 | if (versionString.equals("latest")) |
| 99 | version = new Version(Integer.MAX_VALUE); |
| 100 | else |
| 101 | version = new Version(versionString); |
| 102 | |
| 103 | if (range.includes(version) || versionString.equals(versionRange)) |
| 104 | versions.put(version, instances[i]); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | File[] files = versions.values().toArray(EMPTY_FILES); |
| 109 | if ("latest".equals(versionRange) && files.length > 0) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 110 | return new File[] { |
| 111 | files[files.length - 1] |
| 112 | }; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 113 | } |
| 114 | return files; |
| 115 | } |
| 116 | |
| 117 | public boolean canWrite() { |
| 118 | return canWrite; |
| 119 | } |
| 120 | |
| 121 | public File put(Jar jar) throws Exception { |
| 122 | init(); |
| 123 | dirty = true; |
| 124 | |
| 125 | Manifest manifest = jar.getManifest(); |
| 126 | if (manifest == null) |
| 127 | throw new IllegalArgumentException("No manifest in JAR: " + jar); |
| 128 | |
| 129 | String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME); |
| 130 | if (bsn == null) |
| 131 | throw new IllegalArgumentException("No Bundle SymbolicName set"); |
| 132 | |
| 133 | Parameters b = Processor.parseHeader(bsn, null); |
| 134 | if (b.size() != 1) |
| 135 | throw new IllegalArgumentException("Multiple bsn's specified " + b); |
| 136 | |
| 137 | for (String key : b.keySet()) { |
| 138 | bsn = key; |
| 139 | if (!Verifier.SYMBOLICNAME.matcher(bsn).matches()) |
| 140 | throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn); |
| 141 | } |
| 142 | |
| 143 | String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION); |
| 144 | Version version; |
| 145 | if (versionString == null) |
| 146 | version = new Version(); |
| 147 | else |
| 148 | version = new Version(versionString); |
| 149 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 150 | reporter.trace("bsn=%s version=%s", bsn, version); |
| 151 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 152 | File dir = new File(root, bsn); |
| 153 | dir.mkdirs(); |
| 154 | String fName = bsn + "-" + version.getWithoutQualifier() + ".jar"; |
| 155 | File file = new File(dir, fName); |
| 156 | |
| 157 | reporter.trace("updating %s ", file.getAbsolutePath()); |
| 158 | if (!file.exists() || file.lastModified() < jar.lastModified()) { |
| 159 | jar.write(file); |
| 160 | reporter.progress("updated " + file.getAbsolutePath()); |
| 161 | fireBundleAdded(jar, file); |
| 162 | } else { |
| 163 | reporter.progress("Did not update " + jar + " because repo has a newer version"); |
| 164 | reporter.trace("NOT Updating " + fName + " (repo is newer)"); |
| 165 | } |
| 166 | |
| 167 | File latest = new File(dir, bsn + "-latest.jar"); |
| 168 | if (latest.exists() && latest.lastModified() < jar.lastModified()) { |
| 169 | jar.write(latest); |
| 170 | file = latest; |
| 171 | } |
| 172 | |
| 173 | return file; |
| 174 | } |
| 175 | |
| 176 | protected void fireBundleAdded(Jar jar, File file) { |
| 177 | if (registry == null) |
| 178 | return; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 179 | List<RepositoryListenerPlugin> listeners = registry.getPlugins(RepositoryListenerPlugin.class); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 180 | for (RepositoryListenerPlugin listener : listeners) { |
| 181 | try { |
| 182 | listener.bundleAdded(this, jar, file); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 183 | } |
| 184 | catch (Exception e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 185 | if (reporter != null) |
| 186 | reporter.warning("Repository listener threw an unexpected exception: %s", e); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public void setLocation(String string) { |
| 192 | root = new File(string); |
| 193 | if (!root.isDirectory()) |
| 194 | throw new IllegalArgumentException("Invalid repository directory"); |
| 195 | } |
| 196 | |
| 197 | public void setReporter(Reporter reporter) { |
| 198 | this.reporter = reporter; |
| 199 | } |
| 200 | |
| 201 | public List<String> list(String regex) throws Exception { |
| 202 | init(); |
| 203 | Instruction pattern = null; |
| 204 | if (regex != null) |
| 205 | pattern = new Instruction(regex); |
| 206 | |
| 207 | List<String> result = new ArrayList<String>(); |
| 208 | if (root == null) { |
| 209 | if (reporter != null) |
| 210 | reporter.error("FileRepo root directory is not set."); |
| 211 | } else { |
| 212 | File[] list = root.listFiles(); |
| 213 | if (list != null) { |
| 214 | for (File f : list) { |
| 215 | if (!f.isDirectory()) |
| 216 | continue; // ignore non-directories |
| 217 | String fileName = f.getName(); |
| 218 | if (fileName.charAt(0) == '.') |
| 219 | continue; // ignore hidden files |
| 220 | if (pattern == null || pattern.matches(fileName)) |
| 221 | result.add(fileName); |
| 222 | } |
| 223 | } else if (reporter != null) |
| 224 | reporter.error("FileRepo root directory (%s) does not exist", root); |
| 225 | } |
| 226 | |
| 227 | return result; |
| 228 | } |
| 229 | |
| 230 | public List<Version> versions(String bsn) throws Exception { |
| 231 | init(); |
| 232 | File dir = new File(root, bsn); |
| 233 | if (dir.isDirectory()) { |
| 234 | String versions[] = dir.list(); |
| 235 | List<Version> list = new ArrayList<Version>(); |
| 236 | for (String v : versions) { |
| 237 | Matcher m = REPO_FILE.matcher(v); |
| 238 | if (m.matches()) { |
| 239 | String version = m.group(2); |
| 240 | if (version.equals("latest")) |
| 241 | version = Integer.MAX_VALUE + ""; |
| 242 | list.add(new Version(version)); |
| 243 | } |
| 244 | } |
| 245 | return list; |
| 246 | } |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | public String toString() { |
| 251 | return String.format("%-40s r/w=%s", root.getAbsolutePath(), canWrite()); |
| 252 | } |
| 253 | |
| 254 | public File getRoot() { |
| 255 | return root; |
| 256 | } |
| 257 | |
| 258 | public boolean refresh() { |
| 259 | if (dirty) { |
| 260 | dirty = false; |
| 261 | return true; |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 262 | } |
| 263 | return false; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | public String getName() { |
| 267 | if (name == null) { |
| 268 | return toString(); |
| 269 | } |
| 270 | return name; |
| 271 | } |
| 272 | |
| 273 | public Jar get(String bsn, Version v) throws Exception { |
| 274 | init(); |
| 275 | File bsns = new File(root, bsn); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 276 | File version = new File(bsns, bsn + "-" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro() + ".jar"); |
| 277 | if (version.exists()) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 278 | return new Jar(version); |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 279 | return null; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 282 | public File get(String bsn, String version, Strategy strategy, Map<String,String> properties) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 283 | if (version == null) |
| 284 | version = "0.0.0"; |
| 285 | |
| 286 | if (strategy == Strategy.EXACT) { |
| 287 | VersionRange vr = new VersionRange(version); |
| 288 | if (vr.isRange()) |
| 289 | return null; |
| 290 | |
| 291 | if (vr.getHigh().getMajor() == Integer.MAX_VALUE) |
| 292 | version = "latest"; |
| 293 | |
| 294 | File file = IO.getFile(root, bsn + "/" + bsn + "-" + version + ".jar"); |
| 295 | if (file.isFile()) |
| 296 | return file; |
Stuart McCulloch | d482610 | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 297 | file = IO.getFile(root, bsn + "/" + bsn + "-" + version + ".lib"); |
| 298 | if (file.isFile()) |
| 299 | return file; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 300 | return null; |
| 301 | |
| 302 | } |
| 303 | File[] files = get(bsn, version); |
| 304 | if (files == null || files.length == 0) |
| 305 | return null; |
| 306 | |
| 307 | if (files.length >= 0) { |
| 308 | switch (strategy) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 309 | case LOWEST : |
| 310 | return files[0]; |
| 311 | case HIGHEST : |
| 312 | return files[files.length - 1]; |
Stuart McCulloch | 151384c | 2012-06-18 11:15:15 +0000 | [diff] [blame] | 313 | case EXACT : |
| 314 | // TODO |
| 315 | break; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | return null; |
| 319 | } |
| 320 | |
| 321 | public void setRegistry(Registry registry) { |
| 322 | this.registry = registry; |
| 323 | } |
| 324 | |
| 325 | public String getLocation() { |
| 326 | return root.toString(); |
| 327 | } |
| 328 | |
| 329 | } |