blob: 922b39c338c2e86570b1d680c7c99e6fa140a0f4 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.deployer;
2
3import java.io.*;
4import java.util.*;
5import java.util.jar.*;
6import java.util.regex.*;
7
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00008import aQute.bnd.header.*;
9import aQute.bnd.osgi.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000010import aQute.bnd.service.*;
Stuart McCulloch6a046662012-07-19 13:11:20 +000011import aQute.bnd.version.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000012import aQute.lib.io.*;
Stuart McCulloch81d48de2012-06-29 19:23:09 +000013import aQute.service.reporter.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000014
15public 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 McCulloch2286f232012-06-15 13:27:53 +000020 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 McCullochbb014372012-06-07 21:57:32 +000028
Stuart McCulloch2286f232012-06-15 13:27:53 +000029 public FileRepo() {}
Stuart McCullochbb014372012-06-07 21:57:32 +000030
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 McCulloch2286f232012-06-15 13:27:53 +000041 public void setProperties(Map<String,String> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +000042 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 McCullochbb014372012-06-07 21:57:32 +000047
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 McCullochd4826102012-06-26 16:34:24 +000059 private File[] get(String bsn, String versionRange) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +000060 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 McCulloch2286f232012-06-15 13:27:53 +000092 SortedMap<Version,File> versions = new TreeMap<Version,File>();
Stuart McCullochbb014372012-06-07 21:57:32 +000093 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 McCulloch2286f232012-06-15 13:27:53 +0000110 return new File[] {
111 files[files.length - 1]
112 };
Stuart McCullochbb014372012-06-07 21:57:32 +0000113 }
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 McCullocha21b9e82012-08-02 13:26:25 +0000150 if (reporter != null)
151 reporter.trace("bsn=%s version=%s", bsn, version);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000152
Stuart McCullochbb014372012-06-07 21:57:32 +0000153 File dir = new File(root, bsn);
154 dir.mkdirs();
155 String fName = bsn + "-" + version.getWithoutQualifier() + ".jar";
156 File file = new File(dir, fName);
157
Stuart McCullocha21b9e82012-08-02 13:26:25 +0000158 if (reporter != null)
159 reporter.trace("updating %s ", file.getAbsolutePath());
Stuart McCullochbb014372012-06-07 21:57:32 +0000160 if (!file.exists() || file.lastModified() < jar.lastModified()) {
161 jar.write(file);
Stuart McCullocha21b9e82012-08-02 13:26:25 +0000162 if (reporter != null)
163 reporter.progress(-1, "updated " + file.getAbsolutePath());
Stuart McCullochbb014372012-06-07 21:57:32 +0000164 fireBundleAdded(jar, file);
165 } else {
Stuart McCullocha21b9e82012-08-02 13:26:25 +0000166 if (reporter != null) {
167 reporter.progress(-1, "Did not update " + jar + " because repo has a newer version");
168 reporter.trace("NOT Updating " + fName + " (repo is newer)");
169 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000170 }
171
172 File latest = new File(dir, bsn + "-latest.jar");
173 if (latest.exists() && latest.lastModified() < jar.lastModified()) {
174 jar.write(latest);
175 file = latest;
176 }
177
178 return file;
179 }
180
181 protected void fireBundleAdded(Jar jar, File file) {
182 if (registry == null)
183 return;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000184 List<RepositoryListenerPlugin> listeners = registry.getPlugins(RepositoryListenerPlugin.class);
Stuart McCullochbb014372012-06-07 21:57:32 +0000185 for (RepositoryListenerPlugin listener : listeners) {
186 try {
187 listener.bundleAdded(this, jar, file);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000188 }
189 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000190 if (reporter != null)
191 reporter.warning("Repository listener threw an unexpected exception: %s", e);
192 }
193 }
194 }
195
196 public void setLocation(String string) {
197 root = new File(string);
198 if (!root.isDirectory())
199 throw new IllegalArgumentException("Invalid repository directory");
200 }
201
202 public void setReporter(Reporter reporter) {
203 this.reporter = reporter;
204 }
205
206 public List<String> list(String regex) throws Exception {
207 init();
208 Instruction pattern = null;
209 if (regex != null)
210 pattern = new Instruction(regex);
211
212 List<String> result = new ArrayList<String>();
213 if (root == null) {
214 if (reporter != null)
215 reporter.error("FileRepo root directory is not set.");
216 } else {
217 File[] list = root.listFiles();
218 if (list != null) {
219 for (File f : list) {
220 if (!f.isDirectory())
221 continue; // ignore non-directories
222 String fileName = f.getName();
223 if (fileName.charAt(0) == '.')
224 continue; // ignore hidden files
225 if (pattern == null || pattern.matches(fileName))
226 result.add(fileName);
227 }
228 } else if (reporter != null)
229 reporter.error("FileRepo root directory (%s) does not exist", root);
230 }
231
232 return result;
233 }
234
235 public List<Version> versions(String bsn) throws Exception {
236 init();
237 File dir = new File(root, bsn);
238 if (dir.isDirectory()) {
239 String versions[] = dir.list();
240 List<Version> list = new ArrayList<Version>();
241 for (String v : versions) {
242 Matcher m = REPO_FILE.matcher(v);
243 if (m.matches()) {
244 String version = m.group(2);
245 if (version.equals("latest"))
246 version = Integer.MAX_VALUE + "";
247 list.add(new Version(version));
248 }
249 }
250 return list;
251 }
252 return null;
253 }
254
255 public String toString() {
256 return String.format("%-40s r/w=%s", root.getAbsolutePath(), canWrite());
257 }
258
259 public File getRoot() {
260 return root;
261 }
262
263 public boolean refresh() {
264 if (dirty) {
265 dirty = false;
266 return true;
Stuart McCullochd4826102012-06-26 16:34:24 +0000267 }
268 return false;
Stuart McCullochbb014372012-06-07 21:57:32 +0000269 }
270
271 public String getName() {
272 if (name == null) {
273 return toString();
274 }
275 return name;
276 }
277
278 public Jar get(String bsn, Version v) throws Exception {
279 init();
280 File bsns = new File(root, bsn);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000281 File version = new File(bsns, bsn + "-" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro() + ".jar");
282 if (version.exists())
Stuart McCullochbb014372012-06-07 21:57:32 +0000283 return new Jar(version);
Stuart McCullochd4826102012-06-26 16:34:24 +0000284 return null;
Stuart McCullochbb014372012-06-07 21:57:32 +0000285 }
286
Stuart McCulloch2286f232012-06-15 13:27:53 +0000287 public File get(String bsn, String version, Strategy strategy, Map<String,String> properties) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +0000288 if (version == null)
289 version = "0.0.0";
290
291 if (strategy == Strategy.EXACT) {
292 VersionRange vr = new VersionRange(version);
293 if (vr.isRange())
294 return null;
295
296 if (vr.getHigh().getMajor() == Integer.MAX_VALUE)
297 version = "latest";
298
299 File file = IO.getFile(root, bsn + "/" + bsn + "-" + version + ".jar");
300 if (file.isFile())
301 return file;
Stuart McCullochd4826102012-06-26 16:34:24 +0000302 file = IO.getFile(root, bsn + "/" + bsn + "-" + version + ".lib");
303 if (file.isFile())
304 return file;
Stuart McCullochbb014372012-06-07 21:57:32 +0000305 return null;
306
307 }
308 File[] files = get(bsn, version);
309 if (files == null || files.length == 0)
310 return null;
311
312 if (files.length >= 0) {
313 switch (strategy) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000314 case LOWEST :
315 return files[0];
316 case HIGHEST :
317 return files[files.length - 1];
Stuart McCulloch151384c2012-06-18 11:15:15 +0000318 case EXACT :
319 // TODO
320 break;
Stuart McCullochbb014372012-06-07 21:57:32 +0000321 }
322 }
323 return null;
324 }
325
326 public void setRegistry(Registry registry) {
327 this.registry = registry;
328 }
329
330 public String getLocation() {
331 return root.toString();
332 }
333
334}