2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.plugins.resolver;
21
import java.net.MalformedURLException;
23
import java.text.ParseException;
24
import java.util.ArrayList;
25
import java.util.Collection;
26
import java.util.Collections;
27
import java.util.Date;
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
33
import org.apache.ivy.core.IvyPatternHelper;
34
import org.apache.ivy.core.cache.ArtifactOrigin;
35
import org.apache.ivy.core.module.descriptor.Artifact;
36
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
37
import org.apache.ivy.core.report.DownloadReport;
38
import org.apache.ivy.core.resolve.DownloadOptions;
39
import org.apache.ivy.core.resolve.ResolveData;
40
import org.apache.ivy.core.resolve.ResolvedModuleRevision;
41
import org.apache.ivy.core.search.ModuleEntry;
42
import org.apache.ivy.core.search.OrganisationEntry;
43
import org.apache.ivy.core.search.RevisionEntry;
44
import org.apache.ivy.plugins.resolver.util.ResolvedResource;
45
import org.apache.ivy.util.Message;
46
import org.apache.ivy.util.XMLHelper;
47
import org.xml.sax.SAXException;
48
import org.xml.sax.helpers.DefaultHandler;
51
* IvyRepResolver is a resolver which can be used to resolve dependencies found in the ivy official
52
* repository for ivy files and ibiblio maven repository for the artifacts, or similar repositories.
53
* For more flexibility with url and patterns, see
54
* {@link org.apache.ivy.plugins.resolver.URLResolver}.
56
public class IvyRepResolver extends URLResolver {
57
public static final String DEFAULT_IVYPATTERN = "[organisation]/[module]/ivy-[revision].xml";
59
public static final String DEFAULT_IVYROOT = "http://ivyrep.jayasoft.org/";
61
private String ivyroot = null;
63
private String ivypattern = null;
65
private String artroot = null;
67
private String artpattern = null;
69
public IvyRepResolver() {
72
private void ensureArtifactConfigured(ResolverSettings settings) {
73
if (settings != null && (artroot == null || artpattern == null)) {
74
if (artroot == null) {
75
String root = settings.getVariable("ivy.ivyrep.default.artifact.root");
79
settings.configureRepositories(true);
80
artroot = settings.getVariable("ivy.ivyrep.default.artifact.root");
83
if (artpattern == null) {
84
String pattern = settings.getVariable("ivy.ivyrep.default.artifact.pattern");
85
if (pattern != null) {
88
settings.configureRepositories(false);
89
artpattern = settings.getVariable("ivy.ivyrep.default.artifact.pattern");
92
updateWholeArtPattern();
96
private void ensureIvyConfigured(ResolverSettings settings) {
97
if (settings != null && (ivyroot == null || ivypattern == null)) {
98
if (ivyroot == null) {
99
String root = settings.getVariable("ivy.ivyrep.default.ivy.root");
103
throw new IllegalStateException(
104
"ivyroot is mandatory on IvyRepResolver. "
105
+ "Make sure to set it in your settings, before setting ivypattern "
106
+ "if you wish to set ivypattern too.");
109
if (ivypattern == null) {
110
String pattern = settings.getVariable("ivy.ivyrep.default.ivy.pattern");
111
if (pattern != null) {
112
ivypattern = pattern;
114
settings.configureRepositories(false);
115
ivypattern = settings.getVariable("ivy.ivyrep.default.ivy.pattern");
118
updateWholeIvyPattern();
122
private String getWholeIvyPattern() {
123
if (ivyroot == null || ivypattern == null) {
126
return ivyroot + ivypattern;
129
private String getWholeArtPattern() {
130
return artroot + artpattern;
133
public String getIvypattern() {
137
public void setIvypattern(String pattern) {
138
if (pattern == null) {
139
throw new NullPointerException("pattern must not be null");
141
ivypattern = pattern;
142
ensureIvyConfigured(getSettings());
143
updateWholeIvyPattern();
146
public String getIvyroot() {
151
* Sets the root of the maven like repository. The maven like repository is necessarily an http
155
* the root of the maven like repository
156
* @throws IllegalArgumentException
157
* if root does not start with "http://"
159
public void setIvyroot(String root) {
161
throw new NullPointerException("root must not be null");
163
if (!root.endsWith("/")) {
164
ivyroot = root + "/";
168
ensureIvyConfigured(getSettings());
169
updateWholeIvyPattern();
172
public void setM2compatible(boolean m2compatible) {
174
throw new IllegalArgumentException("ivyrep does not support maven2 compatibility. "
175
+ "Please use ibiblio resolver instead, or even url or filesystem resolvers for"
176
+ " more specific needs.");
180
private void updateWholeIvyPattern() {
181
setIvyPatterns(Collections.singletonList(getWholeIvyPattern()));
184
private void updateWholeArtPattern() {
185
setArtifactPatterns(Collections.singletonList(getWholeArtPattern()));
188
public void publish(Artifact artifact, File src) {
189
throw new UnsupportedOperationException("publish not supported by IBiblioResolver");
192
public String getArtroot() {
196
public String getArtpattern() {
200
public void setArtpattern(String pattern) {
201
if (pattern == null) {
202
throw new NullPointerException("pattern must not be null");
204
artpattern = pattern;
205
ensureArtifactConfigured(getSettings());
206
updateWholeArtPattern();
209
public void setArtroot(String root) {
211
throw new NullPointerException("root must not be null");
213
if (!root.endsWith("/")) {
214
artroot = root + "/";
218
ensureArtifactConfigured(getSettings());
219
updateWholeArtPattern();
222
public OrganisationEntry[] listOrganisations() {
223
ensureIvyConfigured(getSettings());
225
URL content = new URL(ivyroot + "content.xml");
226
final List ret = new ArrayList();
227
XMLHelper.parse(content, null, new DefaultHandler() {
228
public void startElement(String uri, String localName, String qName,
229
org.xml.sax.Attributes attributes) throws SAXException {
230
if ("organisation".equals(qName)) {
231
String org = attributes.getValue("name");
233
ret.add(new OrganisationEntry(IvyRepResolver.this, org));
238
return (OrganisationEntry[]) ret.toArray(new OrganisationEntry[ret.size()]);
239
} catch (MalformedURLException e) {
241
} catch (Exception e) {
242
Message.warn("unable to parse content.xml file on ivyrep: " + e.getMessage());
244
return super.listOrganisations();
247
// overwrite parent to use only ivy patterns (and not artifact ones, cause ibiblio is too slow
249
public ModuleEntry[] listModules(OrganisationEntry org) {
250
ensureIvyConfigured(getSettings());
251
Map tokenValues = new HashMap();
252
tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org.getOrganisation());
253
Collection names = findIvyNames(tokenValues, IvyPatternHelper.MODULE_KEY);
254
ModuleEntry[] ret = new ModuleEntry[names.size()];
256
for (Iterator iter = names.iterator(); iter.hasNext(); i++) {
257
String name = (String) iter.next();
258
ret[i] = new ModuleEntry(org, name);
263
public RevisionEntry[] listRevisions(ModuleEntry mod) {
264
ensureIvyConfigured(getSettings());
265
ensureArtifactConfigured(getSettings());
266
return super.listRevisions(mod);
269
public String getTypeName() {
273
// override some methods to ensure configuration
274
public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data)
275
throws ParseException {
276
ensureIvyConfigured(data.getSettings());
277
return super.getDependency(dd, data);
280
protected ResolvedResource findArtifactRef(Artifact artifact, Date date) {
281
ensureArtifactConfigured(getSettings());
282
return super.findArtifactRef(artifact, date);
285
public DownloadReport download(Artifact[] artifacts, DownloadOptions options) {
286
ensureArtifactConfigured(getSettings());
287
return super.download(artifacts, options);
290
public boolean exists(Artifact artifact) {
291
ensureArtifactConfigured(getSettings());
292
return super.exists(artifact);
295
public ArtifactOrigin locate(Artifact artifact) {
296
ensureArtifactConfigured(getSettings());
297
return super.locate(artifact);
300
public List getIvyPatterns() {
301
ensureIvyConfigured(getSettings());
302
return super.getIvyPatterns();
305
public List getArtifactPatterns() {
306
ensureArtifactConfigured(getSettings());
307
return super.getArtifactPatterns();