KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > resolver > IvyRepResolver


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.resolver;
7
8 import java.io.File JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.text.ParseException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.helpers.DefaultHandler JavaDoc;
23
24 import fr.jayasoft.ivy.Artifact;
25 import fr.jayasoft.ivy.DependencyDescriptor;
26 import fr.jayasoft.ivy.Ivy;
27 import fr.jayasoft.ivy.ResolveData;
28 import fr.jayasoft.ivy.ResolvedModuleRevision;
29 import fr.jayasoft.ivy.report.DownloadReport;
30 import fr.jayasoft.ivy.util.IvyPatternHelper;
31 import fr.jayasoft.ivy.util.Message;
32 import fr.jayasoft.ivy.util.XMLHelper;
33
34 /**
35  * IvyRepResolver is a resolver which can be used to resolve dependencies found
36  * in the ivy official repository for ivy files and ibiblio maven repository for the artifacts,
37  * or similar repositories.
38  * For more flexibility with url and patterns, see {@link fr.jayasoft.ivy.resolver.URLResolver}.
39  */

40 public class IvyRepResolver extends URLResolver {
41     public static final String JavaDoc DEFAULT_IVYPATTERN = "[organisation]/[module]/ivy-[revision].xml";
42     public static final String JavaDoc DEFAULT_IVYROOT = "http://ivyrep.jayasoft.org/";
43     private String JavaDoc _ivyroot = null;
44     private String JavaDoc _ivypattern = null;
45
46     private String JavaDoc _artroot = null;
47     private String JavaDoc _artpattern = null;
48
49     public IvyRepResolver() {
50     }
51
52     private void ensureArtifactConfigured(Ivy ivy) {
53         if (ivy != null && (_artroot == null || _artpattern == null)) {
54             if (_artroot == null) {
55                 String JavaDoc root = ivy.getVariable("ivy.ivyrep.default.artifact.root");
56                 if (root != null) {
57                     _artroot = root;
58                 } else {
59                     ivy.configureRepositories(true);
60                     _artroot = ivy.getVariable("ivy.ivyrep.default.artifact.root");
61                 }
62             }
63             if (_artpattern == null) {
64                 String JavaDoc pattern = ivy.getVariable("ivy.ivyrep.default.artifact.pattern");
65                 if (pattern != null) {
66                     _artpattern = pattern;
67                 } else {
68                     ivy.configureRepositories(false);
69                     _artpattern = ivy.getVariable("ivy.ivyrep.default.artifact.pattern");
70                 }
71             }
72             updateWholeArtPattern();
73         }
74     }
75
76     private void ensureIvyConfigured(Ivy ivy) {
77         if (ivy != null && (_ivyroot == null || _ivypattern == null)) {
78             if (_ivyroot == null) {
79                 String JavaDoc root = ivy.getVariable("ivy.ivyrep.default.ivy.root");
80                 if (root != null) {
81                     _ivyroot = root;
82                 } else {
83                     ivy.configureRepositories(true);
84                     _ivyroot = ivy.getVariable("ivy.ivyrep.default.ivy.root");
85                 }
86             }
87             if (_ivypattern == null) {
88                 String JavaDoc pattern = ivy.getVariable("ivy.ivyrep.default.ivy.pattern");
89                 if (pattern != null) {
90                     _ivypattern = pattern;
91                 } else {
92                     ivy.configureRepositories(false);
93                     _ivypattern = ivy.getVariable("ivy.ivyrep.default.ivy.pattern");
94                 }
95             }
96             updateWholeIvyPattern();
97         }
98     }
99
100     private String JavaDoc getWholeIvyPattern() {
101         if (_ivyroot == null || _ivypattern == null) {
102             return null;
103         }
104         return _ivyroot + _ivypattern;
105     }
106     private String JavaDoc getWholeArtPattern() {
107         return _artroot + _artpattern;
108     }
109     public String JavaDoc getIvypattern() {
110         return _ivypattern;
111     }
112     public void setIvypattern(String JavaDoc pattern) {
113         if (pattern == null) {
114             throw new NullPointerException JavaDoc("pattern must not be null");
115         }
116         _ivypattern = pattern;
117         ensureIvyConfigured(getIvy());
118         updateWholeIvyPattern();
119     }
120     public String JavaDoc getIvyroot() {
121         return _ivyroot;
122     }
123     /**
124      * Sets the root of the maven like repository.
125      * The maven like repository is necessarily an http repository.
126      * @param root the root of the maven like repository
127      * @throws IllegalArgumentException if root does not start with "http://"
128      */

129     public void setIvyroot(String JavaDoc root) {
130         if (root == null) {
131             throw new NullPointerException JavaDoc("root must not be null");
132         }
133         if (!root.endsWith("/")) {
134             _ivyroot = root + "/";
135         } else {
136             _ivyroot = root;
137         }
138         ensureIvyConfigured(getIvy());
139         updateWholeIvyPattern();
140     }
141     
142     public void setM2compatible(boolean m2compatible) {
143         if (m2compatible) {
144             throw new IllegalArgumentException JavaDoc("ivyrep does not support maven2 compatibility. Please use ibiblio resolver instead, or even url or filesystem resolvers for more specific needs.");
145         }
146     }
147     
148     private void updateWholeIvyPattern() {
149         setIvyPatterns(Collections.singletonList(getWholeIvyPattern()));
150     }
151     private void updateWholeArtPattern() {
152         setArtifactPatterns(Collections.singletonList(getWholeArtPattern()));
153     }
154     public void publish(Artifact artifact, File JavaDoc src) {
155         throw new UnsupportedOperationException JavaDoc("publish not supported by IBiblioResolver");
156     }
157
158     public String JavaDoc getArtroot() {
159         return _artroot;
160     }
161     
162
163     public String JavaDoc getArtpattern() {
164         return _artpattern;
165     }
166     
167
168     public void setArtpattern(String JavaDoc pattern) {
169         if (pattern == null) {
170             throw new NullPointerException JavaDoc("pattern must not be null");
171         }
172         _artpattern = pattern;
173         ensureArtifactConfigured(getIvy());
174         updateWholeArtPattern();
175     }
176     
177     public void setArtroot(String JavaDoc root) {
178         if (root == null) {
179             throw new NullPointerException JavaDoc("root must not be null");
180         }
181         if (!root.endsWith("/")) {
182             _artroot = root + "/";
183         } else {
184             _artroot = root;
185         }
186         ensureArtifactConfigured(getIvy());
187         updateWholeArtPattern();
188     }
189     
190     public OrganisationEntry[] listOrganisations() {
191         ensureIvyConfigured(getIvy());
192         try {
193             URL JavaDoc content = new URL JavaDoc(_ivyroot + "content.xml");
194             final List JavaDoc ret = new ArrayList JavaDoc();
195             XMLHelper.parse(content, null, new DefaultHandler JavaDoc() {
196                 public void startElement(String JavaDoc uri,String JavaDoc localName,String JavaDoc qName,org.xml.sax.Attributes JavaDoc attributes) throws SAXException JavaDoc {
197                     if ("organisation".equals(qName)) {
198                         String JavaDoc org = attributes.getValue("name");
199                         if (org != null) {
200                             ret.add(new OrganisationEntry(IvyRepResolver.this, org));
201                         }
202                     }
203                 }
204             });
205             return (OrganisationEntry[])ret.toArray(new OrganisationEntry[ret.size()]);
206         } catch (MalformedURLException JavaDoc e) {
207         } catch (Exception JavaDoc e) {
208             Message.warn("unable to parse content.xml file on ivyrep: "+e.getMessage());
209         }
210         return super.listOrganisations();
211     }
212
213     // overwrite parent to use only ivy patterns (and not artifact ones, cause ibiblio is too slow to answer)
214
public ModuleEntry[] listModules(OrganisationEntry org) {
215         ensureIvyConfigured(getIvy());
216         Map JavaDoc tokenValues = new HashMap JavaDoc();
217         tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org.getOrganisation());
218         Collection JavaDoc names = findIvyNames(tokenValues, IvyPatternHelper.MODULE_KEY);
219         ModuleEntry[] ret = new ModuleEntry[names.size()];
220         int i =0;
221         for (Iterator JavaDoc iter = names.iterator(); iter.hasNext(); i++) {
222             String JavaDoc name = (String JavaDoc)iter.next();
223             ret[i] = new ModuleEntry(org, name);
224         }
225         return ret;
226     }
227     
228     public RevisionEntry[] listRevisions(ModuleEntry mod) {
229         ensureIvyConfigured(getIvy());
230         ensureArtifactConfigured(getIvy());
231         return super.listRevisions(mod);
232     }
233
234     public String JavaDoc getTypeName() {
235         return "ivyrep";
236     }
237     
238     // override some methods to ensure configuration
239
public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException JavaDoc {
240         ensureIvyConfigured(data.getIvy());
241         return super.getDependency(dd, data);
242     }
243     
244     protected ResolvedResource findArtifactRef(Artifact artifact, Date JavaDoc date) {
245         ensureArtifactConfigured(getIvy());
246         return super.findArtifactRef(artifact, date);
247     }
248     
249     public DownloadReport download(Artifact[] artifacts, Ivy ivy, File JavaDoc cache, boolean useOrigin) {
250         ensureArtifactConfigured(ivy);
251         return super.download(artifacts, ivy, cache, useOrigin);
252     }
253     public boolean exists(Artifact artifact) {
254         ensureArtifactConfigured(getIvy());
255         return super.exists(artifact);
256     }
257     public List JavaDoc getIvyPatterns() {
258         ensureIvyConfigured(getIvy());
259         return super.getIvyPatterns();
260     }
261     public List JavaDoc getArtifactPatterns() {
262         ensureArtifactConfigured(getIvy());
263         return super.getArtifactPatterns();
264     }
265 }
266
Popular Tags