Stuart McCulloch | 26e7a5a | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.bnd.maven.support; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.net.*; |
| 5 | import java.util.*; |
| 6 | |
| 7 | import aQute.bnd.service.*; |
| 8 | import aQute.lib.io.*; |
| 9 | import aQute.lib.osgi.*; |
| 10 | import aQute.libg.reporter.*; |
| 11 | import aQute.libg.version.*; |
| 12 | |
| 13 | public class MavenRemoteRepository implements RepositoryPlugin, RegistryPlugin, Plugin { |
| 14 | Reporter reporter; |
| 15 | URI[] repositories; |
| 16 | Registry registry; |
| 17 | Maven maven; |
| 18 | |
| 19 | public File[] get(String bsn, String range) throws Exception { |
| 20 | File f = get(bsn, range, Strategy.HIGHEST, null); |
| 21 | if (f == null) |
| 22 | return null; |
| 23 | |
| 24 | return new File[] { f }; |
| 25 | } |
| 26 | |
| 27 | public File get(String bsn, String version, Strategy strategy, Map<String, String> properties) |
| 28 | throws Exception { |
| 29 | String groupId = null; |
| 30 | |
| 31 | if (properties != null) |
| 32 | groupId = properties.get("groupId"); |
| 33 | |
| 34 | if (groupId == null) { |
| 35 | int n = bsn.indexOf('+'); |
| 36 | if ( n < 0) |
| 37 | return null; |
| 38 | |
| 39 | groupId = bsn.substring(0,n); |
| 40 | bsn = bsn.substring(n+1); |
| 41 | } |
| 42 | |
| 43 | String artifactId = bsn; |
| 44 | |
| 45 | if (version == null) { |
| 46 | if (reporter != null) |
| 47 | reporter.error("Maven dependency version not set for %s - %s", groupId, artifactId); |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | CachedPom pom = getMaven().getPom(groupId, artifactId, version, repositories); |
| 52 | |
| 53 | String value = properties == null ? null : properties.get("scope"); |
| 54 | if (value == null) |
| 55 | return pom.getArtifact(); |
| 56 | |
| 57 | Pom.Scope action = null; |
| 58 | |
| 59 | try { |
| 60 | action = Pom.Scope.valueOf(value); |
| 61 | return pom.getLibrary(action, repositories); |
| 62 | } catch (Exception e) { |
| 63 | return pom.getArtifact(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public Maven getMaven() { |
| 68 | if ( maven != null) |
| 69 | return maven; |
| 70 | |
| 71 | maven = registry.getPlugin(Maven.class); |
| 72 | return maven; |
| 73 | } |
| 74 | |
| 75 | public boolean canWrite() { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | public File put(Jar jar) throws Exception { |
| 80 | throw new UnsupportedOperationException("cannot do put"); |
| 81 | } |
| 82 | |
| 83 | public List<String> list(String regex) throws Exception { |
| 84 | throw new UnsupportedOperationException("cannot do list"); |
| 85 | } |
| 86 | |
| 87 | public List<Version> versions(String bsn) throws Exception { |
| 88 | throw new UnsupportedOperationException("cannot do versions"); |
| 89 | } |
| 90 | |
| 91 | public String getName() { |
| 92 | return "maven"; |
| 93 | } |
| 94 | |
| 95 | public void setRepositories(URI... urls) { |
| 96 | repositories = urls; |
| 97 | } |
| 98 | |
| 99 | public void setProperties(Map<String, String> map) { |
| 100 | String repoString = map.get("repositories"); |
| 101 | if (repoString != null) { |
| 102 | String[] repos = repoString.split("\\s*,\\s*"); |
| 103 | repositories = new URI[repos.length]; |
| 104 | int n = 0; |
| 105 | for (String repo : repos) { |
| 106 | try { |
| 107 | URI uri = new URI(repo); |
| 108 | if ( !uri.isAbsolute()) |
| 109 | uri = IO.getFile( new File(""),repo).toURI(); |
| 110 | repositories[n++] = uri; |
| 111 | } catch (Exception e) { |
| 112 | if (reporter != null) |
| 113 | reporter.error("Invalid repository %s for maven plugin, %s", repo, e); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public void setReporter(Reporter reporter) { |
| 120 | this.reporter = reporter; |
| 121 | } |
| 122 | |
| 123 | public void setRegistry(Registry registry) { |
| 124 | this.registry = registry; |
| 125 | } |
| 126 | |
| 127 | public void setMaven(Maven maven) { |
| 128 | this.maven = maven; |
| 129 | } |
| 130 | } |