blob: 4be5ec8e56e330d4dbbd1a7bf6641fcbdf6e4503 [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
8import aQute.bnd.service.*;
9import aQute.lib.io.*;
10import aQute.lib.osgi.*;
11import aQute.libg.header.*;
12import aQute.libg.reporter.*;
13import aQute.libg.version.*;
14
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 McCulloch2286f232012-06-15 13:27:53 +0000150 reporter.trace("bsn=%s version=%s", bsn, version);
151
Stuart McCullochbb014372012-06-07 21:57:32 +0000152 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 McCulloch2286f232012-06-15 13:27:53 +0000179 List<RepositoryListenerPlugin> listeners = registry.getPlugins(RepositoryListenerPlugin.class);
Stuart McCullochbb014372012-06-07 21:57:32 +0000180 for (RepositoryListenerPlugin listener : listeners) {
181 try {
182 listener.bundleAdded(this, jar, file);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000183 }
184 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000185 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 McCullochd4826102012-06-26 16:34:24 +0000262 }
263 return false;
Stuart McCullochbb014372012-06-07 21:57:32 +0000264 }
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 McCulloch2286f232012-06-15 13:27:53 +0000276 File version = new File(bsns, bsn + "-" + v.getMajor() + "." + v.getMinor() + "." + v.getMicro() + ".jar");
277 if (version.exists())
Stuart McCullochbb014372012-06-07 21:57:32 +0000278 return new Jar(version);
Stuart McCullochd4826102012-06-26 16:34:24 +0000279 return null;
Stuart McCullochbb014372012-06-07 21:57:32 +0000280 }
281
Stuart McCulloch2286f232012-06-15 13:27:53 +0000282 public File get(String bsn, String version, Strategy strategy, Map<String,String> properties) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +0000283 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 McCullochd4826102012-06-26 16:34:24 +0000297 file = IO.getFile(root, bsn + "/" + bsn + "-" + version + ".lib");
298 if (file.isFile())
299 return file;
Stuart McCullochbb014372012-06-07 21:57:32 +0000300 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 McCulloch2286f232012-06-15 13:27:53 +0000309 case LOWEST :
310 return files[0];
311 case HIGHEST :
312 return files[files.length - 1];
Stuart McCulloch151384c2012-06-18 11:15:15 +0000313 case EXACT :
314 // TODO
315 break;
Stuart McCullochbb014372012-06-07 21:57:32 +0000316 }
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}