blob: d7817ad9951166ab9439f56213513e6fd8368dd8 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.repo.eclipse;
2
3import java.io.*;
4import java.util.*;
5import java.util.Map.Entry;
6import java.util.jar.*;
7
Stuart McCulloch42151ee2012-07-16 13:43:38 +00008import aQute.bnd.header.*;
9import aQute.bnd.osgi.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000010import aQute.bnd.service.*;
Stuart McCullochcd1ddd72012-07-19 13:11:20 +000011import aQute.bnd.version.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000012import aQute.lib.io.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000013import aQute.libg.generics.*;
Stuart McCulloch1a890552012-06-29 19:23:09 +000014import aQute.service.reporter.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000015
16public class EclipseRepo implements Plugin, RepositoryPlugin {
Stuart McCulloch4482c702012-06-15 13:27:53 +000017 File root;
18 Reporter reporter;
19 String name;
20 Parameters index;
Stuart McCullochf3173222012-06-07 21:57:32 +000021
Stuart McCulloch4482c702012-06-15 13:27:53 +000022 public final static String LOCATION = "location";
23 public final static String NAME = "name";
Stuart McCullochf3173222012-06-07 21:57:32 +000024
Stuart McCulloch4482c702012-06-15 13:27:53 +000025 public void setProperties(Map<String,String> map) {
26 String location = map.get(LOCATION);
27 if (location == null)
28 throw new IllegalArgumentException("Location muse be set on a EclipseRepo plugin");
Stuart McCullochf3173222012-06-07 21:57:32 +000029
Stuart McCulloch4482c702012-06-15 13:27:53 +000030 root = new File(location);
31 if (!root.isDirectory())
32 throw new IllegalArgumentException("Repository is not a valid directory " + root);
Stuart McCullochf3173222012-06-07 21:57:32 +000033
Stuart McCulloch4482c702012-06-15 13:27:53 +000034 if (!new File(root, "plugins").isDirectory())
35 throw new IllegalArgumentException("Repository is not a valid directory (no plugins directory)" + root);
Stuart McCullochf3173222012-06-07 21:57:32 +000036
Stuart McCulloch4482c702012-06-15 13:27:53 +000037 name = map.get(NAME);
Stuart McCullochf3173222012-06-07 21:57:32 +000038
Stuart McCulloch4482c702012-06-15 13:27:53 +000039 try {
40 index = buildIndex();
41 }
42 catch (Exception e) {
43 throw new RuntimeException("Could not build index for eclipse repo: " + root);
44 }
45 }
Stuart McCullochf3173222012-06-07 21:57:32 +000046
Stuart McCulloch4482c702012-06-15 13:27:53 +000047 Parameters buildIndex() throws Exception {
48 File index = new File(root, "bnd.index").getAbsoluteFile();
49 File[] plugins = new File(root, "plugins").listFiles();
Stuart McCullochf3173222012-06-07 21:57:32 +000050
Stuart McCulloch4482c702012-06-15 13:27:53 +000051 for (File f : plugins) {
52 f = f.getAbsoluteFile();
53 if (f.isFile()) {
54 if (f.lastModified() > index.lastModified()) {
Stuart McCullochf3173222012-06-07 21:57:32 +000055
Stuart McCulloch4482c702012-06-15 13:27:53 +000056 Parameters map = buildIndex(plugins);
57 write(index, map);
58 return map;
59 }
60 }
61 }
Stuart McCullochf3173222012-06-07 21:57:32 +000062
Stuart McCulloch4482c702012-06-15 13:27:53 +000063 String s = read(index);
64 return Processor.parseHeader(s, null);
65 }
Stuart McCullochf3173222012-06-07 21:57:32 +000066
Stuart McCulloch4482c702012-06-15 13:27:53 +000067 private String read(File index) throws Exception {
68 if (index.isFile()) {
69 BufferedReader fr = IO.reader(index);
70 StringBuilder sb = new StringBuilder();
Stuart McCullochf3173222012-06-07 21:57:32 +000071
Stuart McCulloch4482c702012-06-15 13:27:53 +000072 try {
73 String s = fr.readLine();
74 while (s != null) {
75 sb.append(s);
76 s = fr.readLine();
77 }
78 }
79 finally {
80 fr.close();
81 }
82 }
83 return null;
84 }
Stuart McCullochf3173222012-06-07 21:57:32 +000085
Stuart McCulloch4482c702012-06-15 13:27:53 +000086 private void write(File index, Map<String, ? extends Map<String,String>> map) throws Exception {
87 String s = Processor.printClauses(map);
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000088 File ip = index.getParentFile();
89 if (!ip.exists() && !ip.mkdirs()) {
90 throw new IOException("Could not create directory " + ip);
91 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000092 PrintWriter fw = IO.writer(index);
93 try {
94 fw.write(s);
95 }
96 finally {
97 fw.close();
98 }
99 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000100
Stuart McCulloch4482c702012-06-15 13:27:53 +0000101 private Parameters buildIndex(File[] plugins) {
102 Parameters map = new Parameters();
103 for (File plugin : plugins) {
104 try {
105 Jar jar = new Jar(plugin);
106 Manifest manifest = jar.getManifest();
107 String bsn = manifest.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
108 String version = manifest.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
Stuart McCullochf3173222012-06-07 21:57:32 +0000109
Stuart McCulloch4482c702012-06-15 13:27:53 +0000110 if (bsn != null) {
111 if (version == null)
112 version = "0";
Stuart McCullochf3173222012-06-07 21:57:32 +0000113
Stuart McCulloch4482c702012-06-15 13:27:53 +0000114 Map<String,String> instance = map.get(bsn);
115 if (instance == null) {
116 instance = Create.map();
117 }
118 instance.put(version, plugin.getAbsolutePath());
119 }
120 }
121 catch (Exception e) {
122 // Ignore exceptions in the plugins dir.
123 }
124 }
125 return map;
126 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000127
Stuart McCulloch4482c702012-06-15 13:27:53 +0000128 public void setReporter(Reporter reporter) {
129 this.reporter = reporter;
130 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000131
Stuart McCulloch4482c702012-06-15 13:27:53 +0000132 public boolean canWrite() {
133 return false;
134 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000135
Stuart McCulloch669423b2012-06-26 16:34:24 +0000136 private File[] get(String bsn, String range) throws Exception {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000137 VersionRange r = new VersionRange(range);
138 Map<String,String> instances = index.get(bsn);
139 if (instances == null)
140 return null;
Stuart McCullochf3173222012-06-07 21:57:32 +0000141
Stuart McCulloch4482c702012-06-15 13:27:53 +0000142 List<File> result = Create.list();
Stuart McCullochf3173222012-06-07 21:57:32 +0000143
Stuart McCulloch4482c702012-06-15 13:27:53 +0000144 for (Entry<String,String> entry : instances.entrySet()) {
145 if (r.includes(new Version(entry.getKey()))) {
146 File f = new File(entry.getValue());
147 if (f.isFile()) {
148 result.add(f);
149 }
150 }
151 }
152 return result.toArray(new File[result.size()]);
153 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000154
Stuart McCulloch4482c702012-06-15 13:27:53 +0000155 public String getName() {
156 return name;
157 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000158
Stuart McCulloch4482c702012-06-15 13:27:53 +0000159 public List<String> list(String regex) {
160 Instruction pattern = null;
161 if (regex != null)
162 pattern = new Instruction(regex);
Stuart McCullochf3173222012-06-07 21:57:32 +0000163
Stuart McCulloch4482c702012-06-15 13:27:53 +0000164 List<String> result = new ArrayList<String>();
165 for (String f : index.keySet()) {
166 if (pattern == null || pattern.matches(f))
167 result.add(f);
168 }
169 return result;
170 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000171
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000172 public PutResult put(InputStream stream, PutOptions options) throws Exception {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000173 return null;
174 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000175
Stuart McCulloch4482c702012-06-15 13:27:53 +0000176 public List<Version> versions(String bsn) {
177 Map<String,String> instances = index.get(bsn);
178 if (instances == null)
179 return null;
Stuart McCullochf3173222012-06-07 21:57:32 +0000180
Stuart McCulloch4482c702012-06-15 13:27:53 +0000181 List<Version> versions = Create.list();
182 for (String v : instances.keySet())
183 versions.add(new Version(v));
184 return versions;
185 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000186
187 public File get(String bsn, String range, Strategy strategy, Map<String,String> properties) throws Exception {
188 File[] files = get(bsn, range);
189 if (files.length >= 0) {
190 switch (strategy) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000191 case LOWEST :
192 return files[0];
193 case HIGHEST :
194 return files[files.length - 1];
Stuart McCulloch1b98aa02012-06-18 11:15:15 +0000195 case EXACT :
196 // TODO exact version handing
197 break;
Stuart McCullochf3173222012-06-07 21:57:32 +0000198 }
199 }
200 return null;
201 }
202
203 public String getLocation() {
204 return root.toString();
205 }
206}