blob: 345d3eb05eac91bf095a873c0a19e992ac80d4a4 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.maven.support;
2
3import java.io.*;
4import java.net.*;
5import java.util.*;
6
7import aQute.bnd.service.*;
Stuart McCullochcd1ddd72012-07-19 13:11:20 +00008import aQute.bnd.version.*;
Stuart McCullochf3173222012-06-07 21:57:32 +00009import aQute.lib.io.*;
Stuart McCulloch1a890552012-06-29 19:23:09 +000010import aQute.service.reporter.*;
Stuart McCullochf3173222012-06-07 21:57:32 +000011
12public class MavenRemoteRepository implements RepositoryPlugin, RegistryPlugin, Plugin {
13 Reporter reporter;
14 URI[] repositories;
15 Registry registry;
16 Maven maven;
17
Stuart McCulloch4482c702012-06-15 13:27:53 +000018 public File get(String bsn, String version, Strategy strategy, Map<String,String> properties) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000019 String groupId = null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000020
Stuart McCullochf3173222012-06-07 21:57:32 +000021 if (properties != null)
22 groupId = properties.get("groupId");
Stuart McCulloch4482c702012-06-15 13:27:53 +000023
Stuart McCullochf3173222012-06-07 21:57:32 +000024 if (groupId == null) {
25 int n = bsn.indexOf('+');
Stuart McCulloch4482c702012-06-15 13:27:53 +000026 if (n < 0)
Stuart McCullochf3173222012-06-07 21:57:32 +000027 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +000028
29 groupId = bsn.substring(0, n);
30 bsn = bsn.substring(n + 1);
Stuart McCullochf3173222012-06-07 21:57:32 +000031 }
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 McCulloch4482c702012-06-15 13:27:53 +000052 }
53 catch (Exception e) {
Stuart McCullochf3173222012-06-07 21:57:32 +000054 return pom.getArtifact();
55 }
56 }
57
58 public Maven getMaven() {
Stuart McCulloch4482c702012-06-15 13:27:53 +000059 if (maven != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000060 return maven;
Stuart McCulloch4482c702012-06-15 13:27:53 +000061
Stuart McCullochf3173222012-06-07 21:57:32 +000062 maven = registry.getPlugin(Maven.class);
63 return maven;
64 }
65
66 public boolean canWrite() {
67 return false;
68 }
69
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000070 public PutResult put(InputStream stream, PutOptions options) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000071 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 McCulloch2a0afd62012-09-06 18:28:06 +000078 public SortedSet<Version> versions(String bsn) throws Exception {
Stuart McCullochf3173222012-06-07 21:57:32 +000079 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 McCulloch4482c702012-06-15 13:27:53 +000090 public void setProperties(Map<String,String> map) {
Stuart McCullochf3173222012-06-07 21:57:32 +000091 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 McCulloch4482c702012-06-15 13:27:53 +000099 if (!uri.isAbsolute())
100 uri = IO.getFile(new File(""), repo).toURI();
Stuart McCullochf3173222012-06-07 21:57:32 +0000101 repositories[n++] = uri;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000102 }
103 catch (Exception e) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000104 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 McCulloch4482c702012-06-15 13:27:53 +0000124 if (repositories == null || repositories.length == 0)
Stuart McCullochf3173222012-06-07 21:57:32 +0000125 return "maven central";
Stuart McCulloch4482c702012-06-15 13:27:53 +0000126
127 return Arrays.toString(repositories);
Stuart McCullochf3173222012-06-07 21:57:32 +0000128 }
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000129
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 McCullochf3173222012-06-07 21:57:32 +0000146}