blob: cd980368db0ed2e0a09d9f98f22fa7f1d1450ac3 [file] [log] [blame]
Stuart McCullochf3173222012-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
Stuart McCulloch4482c702012-06-15 13:27:53 +000016 String group;
17 boolean dirty;
18 Reporter reporter;
19 Pattern REPO_FILE = Pattern.compile("([-a-zA-z0-9_\\.]+)-([0-9\\.]+)\\.(jar|lib)");
Stuart McCullochf3173222012-06-07 21:57:32 +000020
Stuart McCulloch4482c702012-06-15 13:27:53 +000021 public void setProperties(Map<String,String> map) {
22 super.setProperties(map);
23 group = map.get("group");
24 }
Stuart McCullochf3173222012-06-07 21:57:32 +000025
Stuart McCulloch4482c702012-06-15 13:27:53 +000026 public void setReporter(Reporter reporter) {
27 super.setReporter(reporter);
28 this.reporter = reporter;
29 }
Stuart McCullochf3173222012-06-07 21:57:32 +000030
Stuart McCulloch4482c702012-06-15 13:27:53 +000031 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);
Stuart McCullochf3173222012-06-07 21:57:32 +000036
Stuart McCulloch4482c702012-06-15 13:27:53 +000037 String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME);
38 if (bsn == null)
39 throw new IllegalArgumentException("No Bundle SymbolicName set");
Stuart McCullochf3173222012-06-07 21:57:32 +000040
Stuart McCulloch4482c702012-06-15 13:27:53 +000041 Parameters b = Processor.parseHeader(bsn, null);
42 if (b.size() != 1)
43 throw new IllegalArgumentException("Multiple bsn's specified " + b);
Stuart McCullochf3173222012-06-07 21:57:32 +000044
Stuart McCulloch4482c702012-06-15 13:27:53 +000045 for (String key : b.keySet()) {
46 bsn = key;
47 if (!Verifier.SYMBOLICNAME.matcher(bsn).matches())
48 throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn);
49 }
Stuart McCullochf3173222012-06-07 21:57:32 +000050
Stuart McCulloch4482c702012-06-15 13:27:53 +000051 String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION);
52 Version version;
53 if (versionString == null)
54 version = new Version();
55 else
56 version = new Version(versionString);
Stuart McCullochf3173222012-06-07 21:57:32 +000057
Stuart McCulloch4482c702012-06-15 13:27:53 +000058 File dir;
59 if (group == null) {
60 dir = getRoot();
61 } else {
62 dir = new File(getRoot(), group);
63 dir.mkdirs();
64 }
65 String fName = bsn + "-" + version.getMajor() + "." + version.getMinor() + "." + version.getMicro() + ".jar";
66 File file = new File(dir, fName);
67
68 jar.write(file);
69 fireBundleAdded(jar, file);
70
71 file = new File(dir, bsn + "-latest.jar");
72 if (file.isFile() && file.lastModified() < jar.lastModified()) {
73 jar.write(file);
74 }
75 return file;
76 }
77
78 public boolean refresh() {
79 if (dirty) {
80 dirty = false;
81 return true;
82 } else
83 return false;
84 }
85
Stuart McCullochf3173222012-06-07 21:57:32 +000086 @Override
87 public List<String> list(String regex) {
Stuart McCulloch4482c702012-06-15 13:27:53 +000088 Instruction pattern = null;
89 if (regex != null)
90 pattern = new Instruction(regex);
Stuart McCullochf3173222012-06-07 21:57:32 +000091
Stuart McCulloch4482c702012-06-15 13:27:53 +000092 String list[] = getRoot().list();
93 List<String> result = new ArrayList<String>();
94 for (String f : list) {
95 Matcher m = REPO_FILE.matcher(f);
96 if (!m.matches()) {
97 continue;
98 }
99 String s = m.group(1);
100 if (pattern == null || pattern.matches(s))
101 result.add(s);
102 }
103 return result;
Stuart McCullochf3173222012-06-07 21:57:32 +0000104 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000105
Stuart McCullochf3173222012-06-07 21:57:32 +0000106 @Override
107 public File[] get(String bsn, String versionRange) throws MalformedURLException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000108 // 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;
Stuart McCullochf3173222012-06-07 21:57:32 +0000112
Stuart McCulloch4482c702012-06-15 13:27:53 +0000113 //
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);
Stuart McCullochf3173222012-06-07 21:57:32 +0000122
Stuart McCulloch4482c702012-06-15 13:27:53 +0000123 //
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);
Stuart McCullochf3173222012-06-07 21:57:32 +0000141
Stuart McCulloch4482c702012-06-15 13:27:53 +0000142 if (range.includes(version))
143 versions.put(version, instances[i]);
144 }
145 }
146 return versions.values().toArray(new File[versions.size()]);
Stuart McCullochf3173222012-06-07 21:57:32 +0000147 }
148
149}