Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.bnd.repo.eclipse; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.jar.*; |
| 6 | |
| 7 | import aQute.bnd.service.*; |
| 8 | import aQute.lib.osgi.*; |
| 9 | import aQute.libg.generics.*; |
| 10 | import aQute.libg.reporter.*; |
| 11 | import aQute.libg.version.*; |
| 12 | |
| 13 | public class EclipseRepo implements Plugin, RepositoryPlugin { |
| 14 | File root; |
| 15 | Reporter reporter; |
| 16 | String name; |
| 17 | Map<String, Map<String, String>> index; |
| 18 | |
| 19 | public static String LOCATION = "location"; |
| 20 | public static String NAME = "name"; |
| 21 | |
| 22 | public void setProperties(Map<String, String> map) { |
| 23 | String location = (String) map.get(LOCATION); |
| 24 | if (location == null) |
| 25 | throw new IllegalArgumentException( |
| 26 | "Location muse be set on a EclipseRepo plugin"); |
| 27 | |
| 28 | root = new File(location); |
| 29 | if (!root.isDirectory()) |
| 30 | throw new IllegalArgumentException( |
| 31 | "Repository is not a valid directory " + root); |
| 32 | |
| 33 | if (!new File(root, "plugins").isDirectory()) |
| 34 | throw new IllegalArgumentException( |
| 35 | "Repository is not a valid directory (no plugins directory)" |
| 36 | + root); |
| 37 | |
| 38 | name = (String) map.get(NAME); |
| 39 | |
| 40 | try { |
| 41 | index = buildIndex(); |
| 42 | } catch (Exception e) { |
| 43 | throw new RuntimeException( |
| 44 | "Could not build index for eclipse repo: " + root); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Map<String, Map<String, String>> buildIndex() throws Exception { |
| 49 | File index = new File(root, "bnd.index").getAbsoluteFile(); |
| 50 | File[] plugins = new File(root, "plugins").listFiles(); |
| 51 | |
| 52 | for (File f : plugins) { |
| 53 | f = f.getAbsoluteFile(); |
| 54 | if (f.isFile()) { |
| 55 | if (f.lastModified() > index.lastModified()) { |
| 56 | |
| 57 | Map<String, Map<String, String>> map = buildIndex(plugins); |
| 58 | write(index, map); |
| 59 | return map; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | String s = read(index); |
| 65 | return Processor.parseHeader(s, null); |
| 66 | } |
| 67 | |
| 68 | private String read(File index) throws Exception { |
| 69 | if (index.isFile()) { |
| 70 | BufferedReader fr = new BufferedReader(new FileReader(index)); |
| 71 | StringBuilder sb = new StringBuilder(); |
| 72 | |
| 73 | try { |
| 74 | String s = fr.readLine(); |
| 75 | while (s != null) { |
| 76 | sb.append(s); |
| 77 | s = fr.readLine(); |
| 78 | } |
| 79 | } finally { |
| 80 | fr.close(); |
| 81 | } |
| 82 | } |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | private void write(File index, Map<String, Map<String, String>> map) |
| 87 | throws Exception { |
| 88 | String s = Processor.printClauses(map); |
| 89 | index.getParentFile().mkdirs(); |
| 90 | FileWriter fw = new FileWriter(index); |
| 91 | try { |
| 92 | fw.write(s); |
| 93 | } finally { |
| 94 | fw.close(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private Map<String, Map<String, String>> buildIndex(File[] plugins) { |
| 99 | Map<String, Map<String, String>> map = Create.map(); |
| 100 | for (File plugin : plugins) { |
| 101 | try { |
| 102 | Jar jar = new Jar(plugin); |
| 103 | Manifest manifest = jar.getManifest(); |
| 104 | String bsn = manifest.getMainAttributes().getValue( |
| 105 | Constants.BUNDLE_SYMBOLICNAME); |
| 106 | String version = manifest.getMainAttributes().getValue( |
| 107 | Constants.BUNDLE_VERSION); |
| 108 | |
| 109 | if (bsn != null) { |
| 110 | if (version == null) |
| 111 | version = "0"; |
| 112 | |
| 113 | Map<String, String> instance = map.get(bsn); |
| 114 | if (instance == null) { |
| 115 | instance = Create.map(); |
| 116 | } |
| 117 | instance.put(version, plugin.getAbsolutePath()); |
| 118 | } |
| 119 | } catch (Exception e) { |
| 120 | // Ignore exceptions in the plugins dir. |
| 121 | } |
| 122 | } |
| 123 | return map; |
| 124 | } |
| 125 | |
| 126 | public void setReporter(Reporter reporter) { |
| 127 | this.reporter = reporter; |
| 128 | } |
| 129 | |
| 130 | public boolean canWrite() { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | public File[] get(String bsn, String range) throws Exception { |
| 135 | VersionRange r = new VersionRange(range); |
| 136 | Map<String, String> instances = index.get(bsn); |
| 137 | if (instances == null) |
| 138 | return null; |
| 139 | |
| 140 | List<File> result = Create.list(); |
| 141 | |
| 142 | for (String version : instances.keySet()) { |
| 143 | Version v = new Version(version); |
| 144 | if (r.includes(v)) { |
| 145 | File f = new File(instances.get(version)); |
| 146 | if (f.isFile()) { |
| 147 | result.add(f); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return result.toArray(new File[result.size()]); |
| 152 | } |
| 153 | |
| 154 | public String getName() { |
| 155 | return name; |
| 156 | } |
| 157 | |
| 158 | public List<String> list(String regex) { |
| 159 | Instruction pattern = null; |
| 160 | if (regex != null) |
| 161 | pattern = Instruction.getPattern(regex); |
| 162 | |
| 163 | List<String> result = new ArrayList<String>(); |
| 164 | for (String f : index.keySet()) { |
| 165 | if (pattern == null || pattern.matches(f)) |
| 166 | result.add(f); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | public File put(Jar jar) throws Exception { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | public List<Version> versions(String bsn) { |
| 176 | Map<String, String> instances = index.get(bsn); |
| 177 | if (instances == null) |
| 178 | return null; |
| 179 | |
| 180 | List<Version> versions = Create.list(); |
| 181 | for (String v : instances.keySet()) |
| 182 | versions.add(new Version(v)); |
| 183 | return versions; |
| 184 | } |
| 185 | |
| 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) { |
| 191 | case LOWEST: |
| 192 | return files[0]; |
| 193 | case HIGHEST: |
| 194 | return files[files.length - 1]; |
| 195 | } |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | } |