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