001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.resolver;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.text.ParseException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Collections;
027: import java.util.Date;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.apache.ivy.core.IvyPatternHelper;
034: import org.apache.ivy.core.module.descriptor.Artifact;
035: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
036: import org.apache.ivy.core.report.DownloadReport;
037: import org.apache.ivy.core.resolve.DownloadOptions;
038: import org.apache.ivy.core.resolve.ResolveData;
039: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
040: import org.apache.ivy.core.search.ModuleEntry;
041: import org.apache.ivy.core.search.OrganisationEntry;
042: import org.apache.ivy.core.search.RevisionEntry;
043: import org.apache.ivy.plugins.resolver.util.ResolvedResource;
044: import org.apache.ivy.util.Message;
045: import org.apache.ivy.util.XMLHelper;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.helpers.DefaultHandler;
048:
049: /**
050: * IvyRepResolver is a resolver which can be used to resolve dependencies found in the ivy official
051: * repository for ivy files and ibiblio maven repository for the artifacts, or similar repositories.
052: * For more flexibility with url and patterns, see
053: * {@link org.apache.ivy.plugins.resolver.URLResolver}.
054: */
055: public class IvyRepResolver extends URLResolver {
056: public static final String DEFAULT_IVYPATTERN = "[organisation]/[module]/ivy-[revision].xml";
057:
058: public static final String DEFAULT_IVYROOT = "http://ivyrep.jayasoft.org/";
059:
060: private String ivyroot = null;
061:
062: private String ivypattern = null;
063:
064: private String artroot = null;
065:
066: private String artpattern = null;
067:
068: public IvyRepResolver() {
069: }
070:
071: private void ensureArtifactConfigured(ResolverSettings settings) {
072: if (settings != null && (artroot == null || artpattern == null)) {
073: if (artroot == null) {
074: String root = settings
075: .getVariable("ivy.ivyrep.default.artifact.root");
076: if (root != null) {
077: artroot = root;
078: } else {
079: settings.configureRepositories(true);
080: artroot = settings
081: .getVariable("ivy.ivyrep.default.artifact.root");
082: }
083: }
084: if (artpattern == null) {
085: String pattern = settings
086: .getVariable("ivy.ivyrep.default.artifact.pattern");
087: if (pattern != null) {
088: artpattern = pattern;
089: } else {
090: settings.configureRepositories(false);
091: artpattern = settings
092: .getVariable("ivy.ivyrep.default.artifact.pattern");
093: }
094: }
095: updateWholeArtPattern();
096: }
097: }
098:
099: private void ensureIvyConfigured(ResolverSettings settings) {
100: if (settings != null && (ivyroot == null || ivypattern == null)) {
101: if (ivyroot == null) {
102: String root = settings
103: .getVariable("ivy.ivyrep.default.ivy.root");
104: if (root != null) {
105: ivyroot = root;
106: } else {
107: throw new IllegalStateException(
108: "ivyroot is mandatory on IvyRepResolver. "
109: + "Make sure to set it in your settings, before setting ivypattern "
110: + "if you wish to set ivypattern too.");
111: }
112: }
113: if (ivypattern == null) {
114: String pattern = settings
115: .getVariable("ivy.ivyrep.default.ivy.pattern");
116: if (pattern != null) {
117: ivypattern = pattern;
118: } else {
119: settings.configureRepositories(false);
120: ivypattern = settings
121: .getVariable("ivy.ivyrep.default.ivy.pattern");
122: }
123: }
124: updateWholeIvyPattern();
125: }
126: }
127:
128: private String getWholeIvyPattern() {
129: if (ivyroot == null || ivypattern == null) {
130: return null;
131: }
132: return ivyroot + ivypattern;
133: }
134:
135: private String getWholeArtPattern() {
136: return artroot + artpattern;
137: }
138:
139: public String getIvypattern() {
140: return ivypattern;
141: }
142:
143: public void setIvypattern(String pattern) {
144: if (pattern == null) {
145: throw new NullPointerException("pattern must not be null");
146: }
147: ivypattern = pattern;
148: ensureIvyConfigured(getSettings());
149: updateWholeIvyPattern();
150: }
151:
152: public String getIvyroot() {
153: return ivyroot;
154: }
155:
156: /**
157: * Sets the root of the maven like repository. The maven like repository is necessarily an http
158: * repository.
159: *
160: * @param root
161: * the root of the maven like repository
162: * @throws IllegalArgumentException
163: * if root does not start with "http://"
164: */
165: public void setIvyroot(String root) {
166: if (root == null) {
167: throw new NullPointerException("root must not be null");
168: }
169: if (!root.endsWith("/")) {
170: ivyroot = root + "/";
171: } else {
172: ivyroot = root;
173: }
174: ensureIvyConfigured(getSettings());
175: updateWholeIvyPattern();
176: }
177:
178: public void setM2compatible(boolean m2compatible) {
179: if (m2compatible) {
180: throw new IllegalArgumentException(
181: "ivyrep does not support maven2 compatibility. "
182: + "Please use ibiblio resolver instead, or even url or filesystem resolvers for"
183: + " more specific needs.");
184: }
185: }
186:
187: private void updateWholeIvyPattern() {
188: setIvyPatterns(Collections.singletonList(getWholeIvyPattern()));
189: }
190:
191: private void updateWholeArtPattern() {
192: setArtifactPatterns(Collections
193: .singletonList(getWholeArtPattern()));
194: }
195:
196: public void publish(Artifact artifact, File src) {
197: throw new UnsupportedOperationException(
198: "publish not supported by IBiblioResolver");
199: }
200:
201: public String getArtroot() {
202: return artroot;
203: }
204:
205: public String getArtpattern() {
206: return artpattern;
207: }
208:
209: public void setArtpattern(String pattern) {
210: if (pattern == null) {
211: throw new NullPointerException("pattern must not be null");
212: }
213: artpattern = pattern;
214: ensureArtifactConfigured(getSettings());
215: updateWholeArtPattern();
216: }
217:
218: public void setArtroot(String root) {
219: if (root == null) {
220: throw new NullPointerException("root must not be null");
221: }
222: if (!root.endsWith("/")) {
223: artroot = root + "/";
224: } else {
225: artroot = root;
226: }
227: ensureArtifactConfigured(getSettings());
228: updateWholeArtPattern();
229: }
230:
231: public OrganisationEntry[] listOrganisations() {
232: ensureIvyConfigured(getSettings());
233: try {
234: URL content = new URL(ivyroot + "content.xml");
235: final List ret = new ArrayList();
236: XMLHelper.parse(content, null, new DefaultHandler() {
237: public void startElement(String uri, String localName,
238: String qName, org.xml.sax.Attributes attributes)
239: throws SAXException {
240: if ("organisation".equals(qName)) {
241: String org = attributes.getValue("name");
242: if (org != null) {
243: ret.add(new OrganisationEntry(
244: IvyRepResolver.this , org));
245: }
246: }
247: }
248: });
249: return (OrganisationEntry[]) ret
250: .toArray(new OrganisationEntry[ret.size()]);
251: } catch (MalformedURLException e) {
252: //???
253: } catch (Exception e) {
254: Message.warn("unable to parse content.xml file on ivyrep: "
255: + e.getMessage());
256: }
257: return super .listOrganisations();
258: }
259:
260: // overwrite parent to use only ivy patterns (and not artifact ones, cause ibiblio is too slow
261: // to answer)
262: public ModuleEntry[] listModules(OrganisationEntry org) {
263: ensureIvyConfigured(getSettings());
264: Map tokenValues = new HashMap();
265: tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org
266: .getOrganisation());
267: Collection names = findIvyNames(tokenValues,
268: IvyPatternHelper.MODULE_KEY);
269: ModuleEntry[] ret = new ModuleEntry[names.size()];
270: int i = 0;
271: for (Iterator iter = names.iterator(); iter.hasNext(); i++) {
272: String name = (String) iter.next();
273: ret[i] = new ModuleEntry(org, name);
274: }
275: return ret;
276: }
277:
278: public RevisionEntry[] listRevisions(ModuleEntry mod) {
279: ensureIvyConfigured(getSettings());
280: ensureArtifactConfigured(getSettings());
281: return super .listRevisions(mod);
282: }
283:
284: public String getTypeName() {
285: return "ivyrep";
286: }
287:
288: // override some methods to ensure configuration
289: public ResolvedModuleRevision getDependency(
290: DependencyDescriptor dd, ResolveData data)
291: throws ParseException {
292: ensureIvyConfigured(data.getSettings());
293: return super .getDependency(dd, data);
294: }
295:
296: protected ResolvedResource findArtifactRef(Artifact artifact,
297: Date date) {
298: ensureArtifactConfigured(getSettings());
299: return super .findArtifactRef(artifact, date);
300: }
301:
302: public DownloadReport download(Artifact[] artifacts,
303: DownloadOptions options) {
304: ensureArtifactConfigured(getSettings());
305: return super .download(artifacts, options);
306: }
307:
308: public boolean exists(Artifact artifact) {
309: ensureArtifactConfigured(getSettings());
310: return super .exists(artifact);
311: }
312:
313: public List getIvyPatterns() {
314: ensureIvyConfigured(getSettings());
315: return super .getIvyPatterns();
316: }
317:
318: public List getArtifactPatterns() {
319: ensureArtifactConfigured(getSettings());
320: return super.getArtifactPatterns();
321: }
322: }
|