KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.ParseException JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import fr.jayasoft.ivy.Artifact;
16 import fr.jayasoft.ivy.DefaultArtifact;
17 import fr.jayasoft.ivy.DependencyDescriptor;
18 import fr.jayasoft.ivy.Ivy;
19 import fr.jayasoft.ivy.ModuleRevisionId;
20 import fr.jayasoft.ivy.ResolveData;
21 import fr.jayasoft.ivy.ResolvedModuleRevision;
22 import fr.jayasoft.ivy.report.DownloadReport;
23 import fr.jayasoft.ivy.util.IvyPatternHelper;
24
25 /**
26  * IBiblioResolver is a resolver which can be used to resolve dependencies found
27  * in the ibiblio maven repository, or similar repositories.
28  * For more flexibility with url and patterns, see {@link fr.jayasoft.ivy.resolver.URLResolver}.
29  */

30 public class IBiblioResolver extends URLResolver {
31     public static final String JavaDoc DEFAULT_PATTERN = "[module]/[type]s/[artifact]-[revision].[ext]";
32     public static final String JavaDoc DEFAULT_ROOT = "http://www.ibiblio.org/maven/";
33     private String JavaDoc _root = null;
34     private String JavaDoc _pattern = null;
35     
36     // use poms if m2 compatible is true
37
private boolean _usepoms = true;
38     
39     public IBiblioResolver() {
40     }
41     
42     protected ResolvedResource findIvyFileRef(DependencyDescriptor dd, ResolveData data) {
43         if (isM2compatible() && isUsepoms()) {
44             ModuleRevisionId mrid = dd.getDependencyRevisionId();
45             mrid = convertM2IdForResourceSearch(mrid);
46             ResolvedResource rres = findResourceUsingPatterns(mrid, getIvyPatterns(), DefaultArtifact.newPomArtifact(mrid, data.getDate()), getRMDParser(dd, data), data.getDate());
47             return rres;
48         } else {
49             return null;
50         }
51     }
52     
53     protected void logIvyNotFound(ModuleRevisionId mrid) {
54         if (isM2compatible() && isUsepoms()) {
55             Artifact artifact = DefaultArtifact.newPomArtifact(mrid, null);
56             logMdNotFound(mrid, artifact);
57         }
58     }
59
60     public void setM2compatible(boolean m2compatible) {
61         super.setM2compatible(m2compatible);
62         if (m2compatible) {
63             _root = "http://www.ibiblio.org/maven2/";
64             _pattern = "[organisation]/[module]/[revision]/[artifact]-[revision].[ext]";
65             updateWholePattern();
66         }
67     }
68     
69     public void ensureConfigured(Ivy ivy) {
70         if (ivy != null && (_root == null || _pattern == null)) {
71             if (_root == null) {
72                 String JavaDoc root = ivy.getVariable("ivy.ibiblio.default.artifact.root");
73                 if (root != null) {
74                     _root = root;
75                 } else {
76                     ivy.configureRepositories(true);
77                     _root = ivy.getVariable("ivy.ibiblio.default.artifact.root");
78                 }
79             }
80             if (_pattern == null) {
81                 String JavaDoc pattern = ivy.getVariable("ivy.ibiblio.default.artifact.pattern");
82                 if (pattern != null) {
83                     _pattern = pattern;
84                 } else {
85                     ivy.configureRepositories(false);
86                     _pattern = ivy.getVariable("ivy.ibiblio.default.artifact.pattern");
87                 }
88             }
89             updateWholePattern();
90         }
91     }
92
93     private String JavaDoc getWholePattern() {
94         return _root + _pattern;
95     }
96     public String JavaDoc getPattern() {
97         return _pattern;
98     }
99     public void setPattern(String JavaDoc pattern) {
100         if (pattern == null) {
101             throw new NullPointerException JavaDoc("pattern must not be null");
102         }
103         _pattern = pattern;
104         ensureConfigured(getIvy());
105         updateWholePattern();
106     }
107     public String JavaDoc getRoot() {
108         return _root;
109     }
110     /**
111      * Sets the root of the maven like repository.
112      * The maven like repository is necessarily an http repository.
113      * @param root the root of the maven like repository
114      * @throws IllegalArgumentException if root does not start with "http://"
115      */

116     public void setRoot(String JavaDoc root) {
117         if (root == null) {
118             throw new NullPointerException JavaDoc("root must not be null");
119         }
120         if (!root.endsWith("/")) {
121             _root = root + "/";
122         } else {
123             _root = root;
124         }
125         ensureConfigured(getIvy());
126         updateWholePattern();
127     }
128     
129     private void updateWholePattern() {
130         if (isM2compatible() && isUsepoms()) {
131             setIvyPatterns(Collections.singletonList(getWholePattern()));
132         }
133         setArtifactPatterns(Collections.singletonList(getWholePattern()));
134     }
135     public void publish(Artifact artifact, File JavaDoc src) {
136         throw new UnsupportedOperationException JavaDoc("publish not supported by IBiblioResolver");
137     }
138     // we do not allow to list organisations on ibiblio, nor modules in ibiblio 1
139
public String JavaDoc[] listTokenValues(String JavaDoc token, Map JavaDoc otherTokenValues) {
140         if (IvyPatternHelper.ORGANISATION_KEY.equals(token)) {
141             return new String JavaDoc[0];
142         }
143         if (IvyPatternHelper.MODULE_KEY.equals(token) && !isM2compatible()) {
144             return new String JavaDoc[0];
145         }
146         ensureConfigured(getIvy());
147         return super.listTokenValues(token, otherTokenValues);
148     }
149     public OrganisationEntry[] listOrganisations() {
150         return new OrganisationEntry[0];
151     }
152     public ModuleEntry[] listModules(OrganisationEntry org) {
153         if (isM2compatible()) {
154             ensureConfigured(getIvy());
155             return super.listModules(org);
156         }
157         return new ModuleEntry[0];
158     }
159     public RevisionEntry[] listRevisions(ModuleEntry mod) {
160         ensureConfigured(getIvy());
161         return super.listRevisions(mod);
162     }
163     public String JavaDoc getTypeName() {
164         return "ibiblio";
165     }
166
167     // override some methods to ensure configuration
168
public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException JavaDoc {
169         ensureConfigured(data.getIvy());
170         return super.getDependency(dd, data);
171     }
172     
173     protected ResolvedResource findArtifactRef(Artifact artifact, Date JavaDoc date) {
174         ensureConfigured(getIvy());
175         return super.findArtifactRef(artifact, date);
176     }
177     
178     public DownloadReport download(Artifact[] artifacts, Ivy ivy, File JavaDoc cache, boolean useOrigin) {
179         ensureConfigured(ivy);
180         return super.download(artifacts, ivy, cache, useOrigin);
181     }
182     public boolean exists(Artifact artifact) {
183         ensureConfigured(getIvy());
184         return super.exists(artifact);
185     }
186     public List JavaDoc getArtifactPatterns() {
187         ensureConfigured(getIvy());
188         return super.getArtifactPatterns();
189     }
190
191     public boolean isUsepoms() {
192         return _usepoms;
193     }
194
195     public void setUsepoms(boolean usepoms) {
196         _usepoms = usepoms;
197         updateWholePattern();
198     }
199 }
200
Popular Tags