blob: 6bb33b6b9d24b8a1d307dfb9d945beb468b9bdfd [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
Stuart McCulloch42151ee2012-07-16 13:43:38 +00008import aQute.bnd.header.*;
9import aQute.bnd.osgi.*;
Stuart McCullochcd1ddd72012-07-19 13:11:20 +000010import aQute.bnd.version.*;
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000011import aQute.lib.io.*;
Stuart McCulloch1a890552012-06-29 19:23:09 +000012import aQute.service.reporter.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000013
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 McCulloch2929e2d2012-08-07 10:57:21 +000021 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000022 public void setProperties(Map<String,String> map) {
23 super.setProperties(map);
24 group = map.get("group");
25 }
Stuart McCullochf3173222012-06-07 21:57:32 +000026
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000027 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +000028 public void setReporter(Reporter reporter) {
29 super.setReporter(reporter);
30 this.reporter = reporter;
31 }
Stuart McCullochf3173222012-06-07 21:57:32 +000032
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000033 @Override
34 protected PutResult putArtifact(File tmpFile, PutOptions options) throws Exception {
35 assert (tmpFile != null);
36 assert (options != null);
Stuart McCullochf3173222012-06-07 21:57:32 +000037
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000038 Jar jar = null;
39 try {
40 init();
41 dirty = true;
Stuart McCullochf3173222012-06-07 21:57:32 +000042
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000043 jar = new Jar(tmpFile);
Stuart McCullochf3173222012-06-07 21:57:32 +000044
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000045 Manifest manifest = jar.getManifest();
46 if (manifest == null)
47 throw new IllegalArgumentException("No manifest in JAR: " + jar);
48
49 String bsn = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_SYMBOLICNAME);
50 if (bsn == null)
51 throw new IllegalArgumentException("No Bundle SymbolicName set");
52
53 Parameters b = Processor.parseHeader(bsn, null);
54 if (b.size() != 1)
55 throw new IllegalArgumentException("Multiple bsn's specified " + b);
56
57 for (String key : b.keySet()) {
58 bsn = key;
59 if (!Verifier.SYMBOLICNAME.matcher(bsn).matches())
60 throw new IllegalArgumentException("Bundle SymbolicName has wrong format: " + bsn);
61 }
62
63 String versionString = manifest.getMainAttributes().getValue(Analyzer.BUNDLE_VERSION);
64 Version version;
65 if (versionString == null)
66 version = new Version();
67 else
68 version = new Version(versionString);
69
70 if (reporter != null)
71 reporter.trace("bsn=%s version=%s", bsn, version);
72
73 File dir;
74 if (group == null) {
75 dir = getRoot();
76 } else {
77 dir = new File(getRoot(), group);
78 if (!dir.exists() && !dir.mkdirs()) {
79 throw new IOException("Could not create directory " + dir);
80 }
81 }
82 String fName = bsn + "-" + version.getWithoutQualifier() + ".jar";
83 File file = new File(dir, fName);
84
85 PutResult result = new PutResult();
86
87 if (reporter != null)
88 reporter.trace("updating %s ", file.getAbsolutePath());
89
90 if (file.exists()) {
91 IO.delete(file);
92 }
93 IO.rename(tmpFile, file);
94 result.artifact = file.toURI();
95
96 if (reporter != null)
97 reporter.progress(-1, "updated " + file.getAbsolutePath());
98
99 fireBundleAdded(jar, file);
100
101 File latest = new File(dir, bsn + "-latest.jar");
102 boolean latestExists = latest.exists() && latest.isFile();
103 boolean latestIsOlder = latestExists && (latest.lastModified() < jar.lastModified());
104 if ((options.createLatest && !latestExists) || latestIsOlder) {
105 if (latestExists) {
106 IO.delete(latest);
107 }
108 IO.copy(file, latest);
109 result.latest = latest.toURI();
110 }
111
112 return result;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000113 }
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000114 finally {
115 if (jar != null) {
116 jar.close();
117 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000118 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000119 }
120
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000121 @Override
Stuart McCulloch4482c702012-06-15 13:27:53 +0000122 public boolean refresh() {
123 if (dirty) {
124 dirty = false;
125 return true;
Stuart McCulloch669423b2012-06-26 16:34:24 +0000126 }
127 return false;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000128 }
129
Stuart McCullochf3173222012-06-07 21:57:32 +0000130 @Override
131 public List<String> list(String regex) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000132 Instruction pattern = null;
133 if (regex != null)
134 pattern = new Instruction(regex);
Stuart McCullochf3173222012-06-07 21:57:32 +0000135
Stuart McCulloch4482c702012-06-15 13:27:53 +0000136 String list[] = getRoot().list();
137 List<String> result = new ArrayList<String>();
138 for (String f : list) {
139 Matcher m = REPO_FILE.matcher(f);
140 if (!m.matches()) {
141 continue;
142 }
143 String s = m.group(1);
144 if (pattern == null || pattern.matches(s))
145 result.add(s);
146 }
147 return result;
Stuart McCullochf3173222012-06-07 21:57:32 +0000148 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000149}