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