blob: cf05f9861d580fc6a2843b3f38e6ac514fd5c90c [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +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.reporter.*;
11import aQute.libg.version.*;
12
13public class FileInstallRepo extends FileRepo {
14
15 String group;
16 boolean dirty;
17 Reporter reporter;
18 Pattern REPO_FILE = Pattern
19 .compile("([-a-zA-z0-9_\\.]+)-([0-9\\.]+)\\.(jar|lib)");
20
21 public void setProperties(Map<String, String> map) {
22 super.setProperties(map);
23 group = map.get("group");
24 }
25 public void setReporter(Reporter reporter) {
26 super.setReporter(reporter);
27 this.reporter = reporter;
28 }
29
30 public File put(Jar jar) throws Exception {
31 dirty = true;
32 Manifest manifest = jar.getManifest();
33 if (manifest == null)
34 throw new IllegalArgumentException("No manifest in JAR: " + jar);
35
36 String bsn = manifest.getMainAttributes().getValue(
37 Analyzer.BUNDLE_SYMBOLICNAME);
38 if (bsn == null)
39 throw new IllegalArgumentException("No Bundle SymbolicName set");
40
41 Map<String, Map<String, String>> b = Processor.parseHeader(bsn, null);
42 if (b.size() != 1)
43 throw new IllegalArgumentException("Multiple bsn's specified " + b);
44
45 for (String key : b.keySet()) {
46 bsn = key;
47 if (!Verifier.SYMBOLICNAME.matcher(bsn).matches())
48 throw new IllegalArgumentException(
49 "Bundle SymbolicName has wrong format: " + bsn);
50 }
51
52 String versionString = manifest.getMainAttributes().getValue(
53 Analyzer.BUNDLE_VERSION);
54 Version version;
55 if (versionString == null)
56 version = new Version();
57 else
58 version = new Version(versionString);
59
60 File dir;
61 if (group == null) {
62 dir = getRoot();
63 } else {
64 dir= new File(getRoot(), group);
65 dir.mkdirs();
66 }
67 String fName = bsn + "-" + version.getMajor() + "."
68 + version.getMinor() + "." + version.getMicro() + ".jar";
69 File file = new File(dir, fName);
70
71 jar.write(file);
72 fireBundleAdded(jar, file);
73
74 file = new File(dir, bsn + "-latest.jar");
75 if (file.isFile() && file.lastModified() < jar.lastModified()) {
76 jar.write(file);
77 }
78 return file;
79 }
80 public boolean refresh() {
81 if ( dirty ) {
82 dirty = false;
83 return true;
84 } else
85 return false;
86 }
87 @Override
88 public List<String> list(String regex) {
89 Instruction pattern = null;
90 if (regex != null)
91 pattern = Instruction.getPattern(regex);
92
93 String list[] = getRoot().list();
94 List<String> result = new ArrayList<String>();
95 for (String f : list) {
96 Matcher m = REPO_FILE.matcher(f);
97 if (!m.matches()) {
98 continue;
99 }
100 String s = m.group(1);
101 if (pattern == null || pattern.matches(s))
102 result.add(s);
103 }
104 return result;
105 }
106 @Override
107 public File[] get(String bsn, String versionRange) throws MalformedURLException {
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;
112
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);
122
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);
141
142 if (range.includes(version))
143 versions.put(version, instances[i]);
144 }
145 }
146 return (File[]) versions.values().toArray(new File[versions.size()]);
147 }
148
149}