Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.repo.eclipse; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.Map.Entry; |
| 6 | import java.util.jar.*; |
| 7 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 8 | import aQute.bnd.header.*; |
| 9 | import aQute.bnd.osgi.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 10 | import aQute.bnd.service.*; |
Stuart McCulloch | cd1ddd7 | 2012-07-19 13:11:20 +0000 | [diff] [blame] | 11 | import aQute.bnd.version.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | import aQute.lib.io.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | import aQute.libg.generics.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 14 | import aQute.service.reporter.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | |
| 16 | public class EclipseRepo implements Plugin, RepositoryPlugin { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | File root; |
| 18 | Reporter reporter; |
| 19 | String name; |
| 20 | Parameters index; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 22 | public final static String LOCATION = "location"; |
| 23 | public final static String NAME = "name"; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 25 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | root = new File(location); |
| 31 | if (!root.isDirectory()) |
| 32 | throw new IllegalArgumentException("Repository is not a valid directory " + root); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 33 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 34 | if (!new File(root, "plugins").isDirectory()) |
| 35 | throw new IllegalArgumentException("Repository is not a valid directory (no plugins directory)" + root); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 37 | name = map.get(NAME); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 39 | try { |
| 40 | index = buildIndex(); |
| 41 | } |
| 42 | catch (Exception e) { |
| 43 | throw new RuntimeException("Could not build index for eclipse repo: " + root); |
| 44 | } |
| 45 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 47 | Parameters buildIndex() throws Exception { |
| 48 | File index = new File(root, "bnd.index").getAbsoluteFile(); |
| 49 | File[] plugins = new File(root, "plugins").listFiles(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 50 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 51 | for (File f : plugins) { |
| 52 | f = f.getAbsoluteFile(); |
| 53 | if (f.isFile()) { |
| 54 | if (f.lastModified() > index.lastModified()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 55 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 56 | Parameters map = buildIndex(plugins); |
| 57 | write(index, map); |
| 58 | return map; |
| 59 | } |
| 60 | } |
| 61 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 62 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 63 | String s = read(index); |
| 64 | return Processor.parseHeader(s, null); |
| 65 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 67 | private String read(File index) throws Exception { |
| 68 | if (index.isFile()) { |
| 69 | BufferedReader fr = IO.reader(index); |
| 70 | StringBuilder sb = new StringBuilder(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 72 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 85 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 86 | private void write(File index, Map<String, ? extends Map<String,String>> map) throws Exception { |
| 87 | String s = Processor.printClauses(map); |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 88 | File ip = index.getParentFile(); |
| 89 | if (!ip.exists() && !ip.mkdirs()) { |
| 90 | throw new IOException("Could not create directory " + ip); |
| 91 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | PrintWriter fw = IO.writer(index); |
| 93 | try { |
| 94 | fw.write(s); |
| 95 | } |
| 96 | finally { |
| 97 | fw.close(); |
| 98 | } |
| 99 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 100 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 101 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 109 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 110 | if (bsn != null) { |
| 111 | if (version == null) |
| 112 | version = "0"; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 113 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 114 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 127 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 128 | public void setReporter(Reporter reporter) { |
| 129 | this.reporter = reporter; |
| 130 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 131 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 132 | public boolean canWrite() { |
| 133 | return false; |
| 134 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 135 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 136 | private File[] get(String bsn, String range) throws Exception { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 137 | VersionRange r = new VersionRange(range); |
| 138 | Map<String,String> instances = index.get(bsn); |
| 139 | if (instances == null) |
| 140 | return null; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 141 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 142 | List<File> result = Create.list(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 143 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 144 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 154 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 155 | public String getName() { |
| 156 | return name; |
| 157 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 158 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 159 | public List<String> list(String regex) { |
| 160 | Instruction pattern = null; |
| 161 | if (regex != null) |
| 162 | pattern = new Instruction(regex); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 163 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 164 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 171 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 172 | public PutResult put(InputStream stream, PutOptions options) throws Exception { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 173 | return null; |
| 174 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 175 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 176 | public List<Version> versions(String bsn) { |
| 177 | Map<String,String> instances = index.get(bsn); |
| 178 | if (instances == null) |
| 179 | return null; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 180 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 181 | List<Version> versions = Create.list(); |
| 182 | for (String v : instances.keySet()) |
| 183 | versions.add(new Version(v)); |
| 184 | return versions; |
| 185 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 186 | |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 191 | case LOWEST : |
| 192 | return files[0]; |
| 193 | case HIGHEST : |
| 194 | return files[files.length - 1]; |
Stuart McCulloch | 1b98aa0 | 2012-06-18 11:15:15 +0000 | [diff] [blame] | 195 | case EXACT : |
| 196 | // TODO exact version handing |
| 197 | break; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | return null; |
| 201 | } |
| 202 | |
| 203 | public String getLocation() { |
| 204 | return root.toString(); |
| 205 | } |
| 206 | } |