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.*; |
Stuart McCulloch | cd1ddd7 | 2012-07-19 13:11:20 +0000 | [diff] [blame] | 8 | import aQute.bnd.version.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | import aQute.lib.io.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 10 | import aQute.service.reporter.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | |
| 12 | public class MavenRemoteRepository implements RepositoryPlugin, RegistryPlugin, Plugin { |
| 13 | Reporter reporter; |
| 14 | URI[] repositories; |
| 15 | Registry registry; |
| 16 | Maven maven; |
| 17 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 18 | 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] | 19 | String groupId = null; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | if (properties != null) |
| 22 | groupId = properties.get("groupId"); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 24 | if (groupId == null) { |
| 25 | int n = bsn.indexOf('+'); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 26 | if (n < 0) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 27 | return null; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | |
| 29 | groupId = bsn.substring(0, n); |
| 30 | bsn = bsn.substring(n + 1); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | String artifactId = bsn; |
| 34 | |
| 35 | if (version == null) { |
| 36 | if (reporter != null) |
| 37 | reporter.error("Maven dependency version not set for %s - %s", groupId, artifactId); |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | CachedPom pom = getMaven().getPom(groupId, artifactId, version, repositories); |
| 42 | |
| 43 | String value = properties == null ? null : properties.get("scope"); |
| 44 | if (value == null) |
| 45 | return pom.getArtifact(); |
| 46 | |
| 47 | Pom.Scope action = null; |
| 48 | |
| 49 | try { |
| 50 | action = Pom.Scope.valueOf(value); |
| 51 | return pom.getLibrary(action, repositories); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | } |
| 53 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 54 | return pom.getArtifact(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public Maven getMaven() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 59 | if (maven != null) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | return maven; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 61 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 62 | maven = registry.getPlugin(Maven.class); |
| 63 | return maven; |
| 64 | } |
| 65 | |
| 66 | public boolean canWrite() { |
| 67 | return false; |
| 68 | } |
| 69 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 70 | public PutResult put(InputStream stream, PutOptions options) throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | throw new UnsupportedOperationException("cannot do put"); |
| 72 | } |
| 73 | |
| 74 | public List<String> list(String regex) throws Exception { |
| 75 | throw new UnsupportedOperationException("cannot do list"); |
| 76 | } |
| 77 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 78 | public SortedSet<Version> versions(String bsn) throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | throw new UnsupportedOperationException("cannot do versions"); |
| 80 | } |
| 81 | |
| 82 | public String getName() { |
| 83 | return "maven"; |
| 84 | } |
| 85 | |
| 86 | public void setRepositories(URI... urls) { |
| 87 | repositories = urls; |
| 88 | } |
| 89 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 90 | public void setProperties(Map<String,String> map) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 91 | String repoString = map.get("repositories"); |
| 92 | if (repoString != null) { |
| 93 | String[] repos = repoString.split("\\s*,\\s*"); |
| 94 | repositories = new URI[repos.length]; |
| 95 | int n = 0; |
| 96 | for (String repo : repos) { |
| 97 | try { |
| 98 | URI uri = new URI(repo); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 99 | if (!uri.isAbsolute()) |
| 100 | uri = IO.getFile(new File(""), repo).toURI(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 101 | repositories[n++] = uri; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 102 | } |
| 103 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 104 | if (reporter != null) |
| 105 | reporter.error("Invalid repository %s for maven plugin, %s", repo, e); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public void setReporter(Reporter reporter) { |
| 112 | this.reporter = reporter; |
| 113 | } |
| 114 | |
| 115 | public void setRegistry(Registry registry) { |
| 116 | this.registry = registry; |
| 117 | } |
| 118 | |
| 119 | public void setMaven(Maven maven) { |
| 120 | this.maven = maven; |
| 121 | } |
| 122 | |
| 123 | public String getLocation() { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 124 | if (repositories == null || repositories.length == 0) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 125 | return "maven central"; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 126 | |
| 127 | return Arrays.toString(repositories); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 128 | } |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 129 | |
| 130 | public File get(String bsn, Version version, Map<String,String> properties, DownloadListener ... listeners) throws Exception { |
| 131 | File f= get(bsn, version.toString(), Strategy.EXACT, properties); |
| 132 | if ( f == null) |
| 133 | return null; |
| 134 | |
| 135 | for (DownloadListener l : listeners) { |
| 136 | try { |
| 137 | l.success(f); |
| 138 | } |
| 139 | catch (Exception e) { |
| 140 | reporter.exception(e, "Download listener for %s", f); |
| 141 | } |
| 142 | } |
| 143 | return f; |
| 144 | } |
| 145 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 146 | } |