blob: 9b01ff77c2c8ca852b56fee5106b23348ca4608f [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.lib.deployer;
2
3import java.io.*;
Stuart McCullochf3173222012-06-07 21:57:32 +00004import java.util.*;
5import java.util.jar.*;
6import java.util.regex.*;
7
8import aQute.lib.osgi.*;
9import aQute.libg.header.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000010import aQute.libg.version.*;
Stuart McCulloch1a890552012-06-29 19:23:09 +000011import aQute.service.reporter.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000012
13public class FileInstallRepo extends FileRepo {
14
Stuart McCulloch4482c702012-06-15 13:27:53 +000015 String group;
16 boolean dirty;
17 Reporter reporter;
18 Pattern REPO_FILE = Pattern.compile("([-a-zA-z0-9_\\.]+)-([0-9\\.]+)\\.(jar|lib)");
Stuart McCullochf3173222012-06-07 21:57:32 +000019
Stuart McCulloch4482c702012-06-15 13:27:53 +000020 public void setProperties(Map<String,String> map) {
21 super.setProperties(map);
22 group = map.get("group");
23 }
Stuart McCullochf3173222012-06-07 21:57:32 +000024
Stuart McCulloch4482c702012-06-15 13:27:53 +000025 public void setReporter(Reporter reporter) {
26 super.setReporter(reporter);
27 this.reporter = reporter;
28 }
Stuart McCullochf3173222012-06-07 21:57:32 +000029
Stuart McCulloch4482c702012-06-15 13:27:53 +000030 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);
Stuart McCullochf3173222012-06-07 21:57:32 +000035
Stuart McCulloch4482c702012-06-15 13:27:53 +000036 String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME);
37 if (bsn == null)
38 throw new IllegalArgumentException("No Bundle SymbolicName set");
Stuart McCullochf3173222012-06-07 21:57:32 +000039
Stuart McCulloch4482c702012-06-15 13:27:53 +000040 Parameters b = Processor.parseHeader(bsn, null);
41 if (b.size() != 1)
42 throw new IllegalArgumentException("Multiple bsn's specified " + b);
Stuart McCullochf3173222012-06-07 21:57:32 +000043
Stuart McCulloch4482c702012-06-15 13:27:53 +000044 for (String key : b.keySet()) {
45 bsn = key;
46 if (!Verifier.SYMBOLICNAME.matcher(bsn).matches())
47 throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn);
48 }
Stuart McCullochf3173222012-06-07 21:57:32 +000049
Stuart McCulloch4482c702012-06-15 13:27:53 +000050 String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION);
51 Version version;
52 if (versionString == null)
53 version = new Version();
54 else
55 version = new Version(versionString);
Stuart McCullochf3173222012-06-07 21:57:32 +000056
Stuart McCulloch4482c702012-06-15 13:27:53 +000057 File dir;
58 if (group == null) {
59 dir = getRoot();
60 } else {
61 dir = new File(getRoot(), group);
62 dir.mkdirs();
63 }
64 String fName = bsn + "-" + version.getMajor() + "." + version.getMinor() + "." + version.getMicro() + ".jar";
65 File file = new File(dir, fName);
66
67 jar.write(file);
68 fireBundleAdded(jar, file);
69
70 file = new File(dir, bsn + "-latest.jar");
71 if (file.isFile() && file.lastModified() < jar.lastModified()) {
72 jar.write(file);
73 }
74 return file;
75 }
76
77 public boolean refresh() {
78 if (dirty) {
79 dirty = false;
80 return true;
Stuart McCulloch669423b2012-06-26 16:34:24 +000081 }
82 return false;
Stuart McCulloch4482c702012-06-15 13:27:53 +000083 }
84
Stuart McCullochf3173222012-06-07 21:57:32 +000085 @Override
86 public List<String> list(String regex) {
Stuart McCulloch4482c702012-06-15 13:27:53 +000087 Instruction pattern = null;
88 if (regex != null)
89 pattern = new Instruction(regex);
Stuart McCullochf3173222012-06-07 21:57:32 +000090
Stuart McCulloch4482c702012-06-15 13:27:53 +000091 String list[] = getRoot().list();
92 List<String> result = new ArrayList<String>();
93 for (String f : list) {
94 Matcher m = REPO_FILE.matcher(f);
95 if (!m.matches()) {
96 continue;
97 }
98 String s = m.group(1);
99 if (pattern == null || pattern.matches(s))
100 result.add(s);
101 }
102 return result;
Stuart McCullochf3173222012-06-07 21:57:32 +0000103 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000104}