Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame^] | 1 | package aQute.bnd.maven; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.regex.*; |
| 6 | |
| 7 | import aQute.bnd.service.*; |
| 8 | import aQute.lib.osgi.*; |
| 9 | import aQute.libg.reporter.*; |
| 10 | import aQute.libg.version.*; |
| 11 | |
| 12 | public class MavenRepository implements RepositoryPlugin, Plugin, BsnToMavenPath { |
| 13 | |
| 14 | public static String NAME = "name"; |
| 15 | |
| 16 | File root; |
| 17 | Reporter reporter; |
| 18 | String name; |
| 19 | |
| 20 | public String toString() { |
| 21 | return "maven:" + root; |
| 22 | } |
| 23 | |
| 24 | public boolean canWrite() { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | public File[] get(String bsn, String version) throws Exception { |
| 29 | VersionRange range = new VersionRange("0"); |
| 30 | if (version != null) |
| 31 | range = new VersionRange(version); |
| 32 | |
| 33 | List<BsnToMavenPath> plugins = ((Processor) reporter).getPlugins(BsnToMavenPath.class); |
| 34 | if ( plugins.isEmpty()) |
| 35 | plugins.add(this); |
| 36 | |
| 37 | for (BsnToMavenPath cvr : plugins) { |
| 38 | String[] paths = cvr.getGroupAndArtifact(bsn); |
| 39 | if (paths != null) { |
| 40 | File[] files = find(paths[0], paths[1], range); |
| 41 | if (files != null) |
| 42 | return files; |
| 43 | } |
| 44 | } |
| 45 | reporter.trace("Cannot find in maven: %s-%s", bsn, version); |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | File[] find(String groupId, String artifactId, VersionRange range) { |
| 50 | String path = groupId.replace(".", "/"); |
| 51 | File vsdir = Processor.getFile(root, path); |
| 52 | if (!vsdir.isDirectory()) |
| 53 | return null; |
| 54 | |
| 55 | vsdir = Processor.getFile(vsdir, artifactId); |
| 56 | |
| 57 | List<File> result = new ArrayList<File>(); |
| 58 | if (vsdir.isDirectory()) { |
| 59 | String versions[] = vsdir.list(); |
| 60 | for (String v : versions) { |
| 61 | String vv = Analyzer.cleanupVersion(v); |
| 62 | if (Verifier.isVersion(vv)) { |
| 63 | Version vvv = new Version(vv); |
| 64 | if (range.includes(vvv)) { |
| 65 | File file = Processor.getFile(vsdir, v + "/" + artifactId + "-" + v |
| 66 | + ".jar"); |
| 67 | if (file.isFile()) |
| 68 | result.add(file); |
| 69 | else |
| 70 | reporter.warning("Expected maven entry was not a valid file %s ", file); |
| 71 | } |
| 72 | } else { |
| 73 | reporter |
| 74 | .warning( |
| 75 | "Expected a version directory in maven: dir=%s raw-version=%s cleaned-up-version=%s", |
| 76 | vsdir, vv, v); |
| 77 | } |
| 78 | } |
| 79 | } else |
| 80 | return null; |
| 81 | |
| 82 | return result.toArray(new File[result.size()]); |
| 83 | } |
| 84 | |
| 85 | public List<String> list(String regex) { |
| 86 | List<String> bsns = new ArrayList<String>(); |
| 87 | Pattern match = Pattern.compile(".*"); |
| 88 | if (regex != null) |
| 89 | match = Pattern.compile(regex); |
| 90 | find(bsns, match, root, ""); |
| 91 | return bsns; |
| 92 | } |
| 93 | |
| 94 | void find(List<String> bsns, Pattern pattern, File base, String name) { |
| 95 | if (base.isDirectory()) { |
| 96 | String list[] = base.list(); |
| 97 | boolean found = false; |
| 98 | for (String entry : list) { |
| 99 | char c = entry.charAt(0); |
| 100 | if (c >= '0' && c <= '9') { |
| 101 | if (pattern.matcher(name).matches()) |
| 102 | found = true; |
| 103 | } else { |
| 104 | String nextName = entry; |
| 105 | if (name.length() != 0) |
| 106 | nextName = name + "." + entry; |
| 107 | |
| 108 | File next = Processor.getFile(base, entry); |
| 109 | find(bsns, pattern, next, nextName); |
| 110 | } |
| 111 | } |
| 112 | if (found) |
| 113 | bsns.add(name); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public File put(Jar jar) throws Exception { |
| 118 | throw new IllegalStateException("Maven does not support the put command"); |
| 119 | } |
| 120 | |
| 121 | public List<Version> versions(String bsn) throws Exception { |
| 122 | |
| 123 | File files[] = get( bsn, null); |
| 124 | List<Version> versions = new ArrayList<Version>(); |
| 125 | for ( File f : files ) { |
| 126 | Version v = new Version( f.getParentFile().getName()); |
| 127 | versions.add(v); |
| 128 | } |
| 129 | return versions; |
| 130 | } |
| 131 | |
| 132 | public void setProperties(Map<String, String> map) { |
| 133 | File home = new File(""); |
| 134 | String root = map.get("root"); |
| 135 | if (root == null) { |
| 136 | home = new File( System.getProperty("user.home") ); |
| 137 | this.root = Processor.getFile(home , ".m2/repository").getAbsoluteFile(); |
| 138 | } else |
| 139 | this.root = Processor.getFile(home, root).getAbsoluteFile(); |
| 140 | |
| 141 | if (!this.root.isDirectory()) { |
| 142 | reporter.error("Maven repository did not get a proper URL to the repository %s", root); |
| 143 | } |
| 144 | name = (String) map.get(NAME); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | public void setReporter(Reporter processor) { |
| 149 | this.reporter = processor; |
| 150 | } |
| 151 | |
| 152 | public String[] getGroupAndArtifact(String bsn) { |
| 153 | String groupId; |
| 154 | String artifactId; |
| 155 | int n = bsn.indexOf('.'); |
| 156 | |
| 157 | while ( n > 0 ) { |
| 158 | artifactId = bsn.substring(n+1); |
| 159 | groupId = bsn.substring(0,n); |
| 160 | |
| 161 | File gdir = new File(root, groupId.replace('.',File.separatorChar)).getAbsoluteFile(); |
| 162 | File adir = new File( gdir, artifactId).getAbsoluteFile(); |
| 163 | if ( adir.isDirectory() ) |
| 164 | return new String[] {groupId, artifactId}; |
| 165 | |
| 166 | n = bsn.indexOf('.',n+1); |
| 167 | } |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | public String getName() { |
| 172 | if (name == null) { |
| 173 | return toString(); |
| 174 | } |
| 175 | return name; |
| 176 | } |
| 177 | |
| 178 | public File get(String bsn, String range, Strategy strategy, Map<String,String> properties) throws Exception { |
| 179 | File[] files = get(bsn, range); |
| 180 | if (files.length >= 0) { |
| 181 | switch (strategy) { |
| 182 | case LOWEST: |
| 183 | return files[0]; |
| 184 | case HIGHEST: |
| 185 | return files[files.length - 1]; |
| 186 | } |
| 187 | } |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | public void setRoot( File f ) { |
| 192 | root = f; |
| 193 | } |
| 194 | } |