KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Map JavaDoc;
10
11 import fr.jayasoft.ivy.Artifact;
12 import fr.jayasoft.ivy.DependencyDescriptor;
13 import fr.jayasoft.ivy.DependencyResolver;
14 import fr.jayasoft.ivy.Ivy;
15 import fr.jayasoft.ivy.IvyAware;
16 import fr.jayasoft.ivy.IvyNode;
17 import fr.jayasoft.ivy.LatestStrategy;
18 import fr.jayasoft.ivy.ModuleDescriptor;
19 import fr.jayasoft.ivy.ModuleRevisionId;
20 import fr.jayasoft.ivy.ResolveData;
21 import fr.jayasoft.ivy.ResolvedModuleRevision;
22 import fr.jayasoft.ivy.matcher.Matcher;
23 import fr.jayasoft.ivy.matcher.NoMatcher;
24 import fr.jayasoft.ivy.matcher.PatternMatcher;
25 import fr.jayasoft.ivy.namespace.NameSpaceHelper;
26 import fr.jayasoft.ivy.namespace.Namespace;
27 import fr.jayasoft.ivy.report.ArtifactDownloadReport;
28 import fr.jayasoft.ivy.report.DownloadReport;
29 import fr.jayasoft.ivy.report.DownloadStatus;
30 import fr.jayasoft.ivy.util.Message;
31
32 /**
33  * This abstract resolver only provides handling for resolver name
34  */

35 public abstract class AbstractResolver implements DependencyResolver, IvyAware, HasLatestStrategy {
36
37     /**
38      * True if parsed ivy files should be validated against xsd, false if they should not,
39      * null if default behaviour should be used
40      */

41     private Boolean JavaDoc _validate = null;
42     private String JavaDoc _name;
43     private String JavaDoc _changingPattern;
44     private String JavaDoc _changingMatcherName = PatternMatcher.EXACT_OR_REGEXP;
45     
46     private Ivy _ivy;
47
48     /**
49      * The latest strategy to use to find latest among several artifacts
50      */

51     private LatestStrategy _latestStrategy;
52
53     private String JavaDoc _latestStrategyName;
54
55     /**
56      * The namespace to which this resolver belongs
57      */

58     private Namespace _namespace;
59
60     private String JavaDoc _namespaceName;
61
62     public Ivy getIvy() {
63         return _ivy;
64     }
65
66     public void setIvy(Ivy ivy) {
67         _ivy = ivy;
68     }
69     
70     public String JavaDoc getName() {
71         return _name;
72     }
73
74     public void setName(String JavaDoc name) {
75         _name = name;
76     }
77
78
79     /**
80      * this method should remove sensitive information from a location to be displayed in a log
81      * @param name location
82      * @return location with sensitive data replaced by stars
83      */

84     public String JavaDoc hidePassword(String JavaDoc name) {
85         return name;
86     }
87
88     protected boolean doValidate(ResolveData data) {
89         if (_validate != null) {
90             return _validate.booleanValue();
91         } else {
92             return data.isValidate();
93         }
94     }
95
96     public boolean isValidate() {
97         return _validate == null ? true: _validate.booleanValue();
98     }
99     
100
101     public void setValidate(boolean validate) {
102         _validate = Boolean.valueOf(validate);
103     }
104
105
106     protected void checkInterrupted() {
107         if (_ivy != null) {
108             _ivy.checkInterrupted();
109         }
110     }
111
112     public void reportFailure() {
113         Message.verbose("no failure report implemented by "+getName());
114     }
115
116     public void reportFailure(Artifact art) {
117         Message.verbose("no failure report implemented by "+getName());
118     }
119
120     public String JavaDoc[] listTokenValues(String JavaDoc token, Map JavaDoc otherTokenValues) {
121         return new String JavaDoc[0];
122     }
123     public OrganisationEntry[] listOrganisations() {
124         return new OrganisationEntry[0];
125     }
126     public ModuleEntry[] listModules(OrganisationEntry org) {
127         return new ModuleEntry[0];
128     }
129     public RevisionEntry[] listRevisions(ModuleEntry module) {
130         return new RevisionEntry[0];
131     }
132
133     public String JavaDoc toString() {
134         return getName();
135     }
136     public void dumpConfig() {
137         Message.verbose("\t"+getName()+" ["+getTypeName()+"]");
138         Message.debug("\t\tchangingPattern: "+getChangingPattern());
139         Message.debug("\t\tchangingMatcher: "+getChangingMatcherName());
140     }
141
142     public String JavaDoc getTypeName() {
143         return getClass().getName();
144     }
145     /**
146      * Default implementation actually download the artifact
147      * Subclasses should overwrite this to avoid the download
148      */

149     public boolean exists(Artifact artifact) {
150         DownloadReport dr = download(new Artifact[] {artifact}, getIvy(), getIvy().getDefaultCache(), true);
151         ArtifactDownloadReport adr = dr.getArtifactReport(artifact);
152         return adr.getDownloadStatus() != DownloadStatus.FAILED;
153     }
154     
155     public DownloadReport download(Artifact[] artifacts, Ivy ivy, File JavaDoc cache) {
156         return download(artifacts, ivy, cache, false);
157     }
158
159     public LatestStrategy getLatestStrategy() {
160         if (_latestStrategy == null) {
161             if (getIvy() != null) {
162                 if (_latestStrategyName != null && !"default".equals(_latestStrategyName)) {
163                     _latestStrategy = getIvy().getLatestStrategy(_latestStrategyName);
164                     if (_latestStrategy == null) {
165                         Message.error("unknown latest strategy: "+_latestStrategyName);
166                         _latestStrategy = getIvy().getDefaultLatestStrategy();
167                     }
168                 } else {
169                     _latestStrategy = getIvy().getDefaultLatestStrategy();
170                     Message.debug(getName()+": no latest strategy defined: using default");
171                 }
172             } else {
173                 throw new IllegalStateException JavaDoc("no ivy instance found: impossible to get a latest strategy without ivy instance");
174             }
175         }
176         return _latestStrategy;
177     }
178     
179
180     public void setLatestStrategy(LatestStrategy latestStrategy) {
181         _latestStrategy = latestStrategy;
182     }
183
184     public void setLatest(String JavaDoc strategyName) {
185         _latestStrategyName = strategyName;
186     }
187     
188     public String JavaDoc getLatest() {
189         if (_latestStrategyName == null) {
190             _latestStrategyName = "default";
191         }
192         return _latestStrategyName;
193     }
194
195     public Namespace getNamespace() {
196         if (_namespace == null) {
197             if (getIvy() != null) {
198                 if (_namespaceName != null) {
199                     _namespace = getIvy().getNamespace(_namespaceName);
200                     if (_namespace == null) {
201                         Message.error("unknown namespace: "+_namespaceName);
202                         _namespace = getIvy().getSystemNamespace();
203                     }
204                 } else {
205                     _namespace = getIvy().getSystemNamespace();
206                     Message.debug(getName()+": no namespace defined: using system");
207                 }
208             } else {
209                 Message.verbose(getName()+": no namespace defined nor ivy instance: using system namespace");
210                 _namespace = Namespace.SYSTEM_NAMESPACE;
211             }
212         }
213         return _namespace;
214     }
215     
216     public void setNamespace(String JavaDoc namespaceName) {
217         _namespaceName = namespaceName;
218     }
219
220     
221     // Namespace conversion methods
222
protected ModuleDescriptor toSystem(ModuleDescriptor md) {
223         return NameSpaceHelper.toSystem(md, getNamespace());
224     }
225
226     protected Artifact fromSystem(Artifact artifact) {
227         return NameSpaceHelper.transform(artifact, getNamespace().getFromSystemTransformer());
228     }
229
230     protected Artifact toSystem(Artifact artifact) {
231         return NameSpaceHelper.transform(artifact, getNamespace().getToSystemTransformer());
232     }
233
234     protected ResolvedModuleRevision toSystem(ResolvedModuleRevision rmr) {
235         return NameSpaceHelper.toSystem(rmr, getNamespace());
236     }
237
238     protected ModuleRevisionId toSystem(ModuleRevisionId resolvedMrid) {
239         return getNamespace().getToSystemTransformer().transform(resolvedMrid);
240     }
241
242     protected DependencyDescriptor fromSystem(DependencyDescriptor dd) {
243         return NameSpaceHelper.transform(dd, getNamespace().getFromSystemTransformer(), true);
244     }
245
246     protected IvyNode getSystemNode(ResolveData data, ModuleRevisionId resolvedMrid) {
247         return data.getNode(toSystem(resolvedMrid));
248     }
249
250     protected ResolvedModuleRevision findModuleInCache(ResolveData data, ModuleRevisionId mrid) {
251         ResolvedModuleRevision moduleFromCache = data.getIvy().findModuleInCache(toSystem(mrid), data.getCache(), doValidate(data));
252         if (moduleFromCache == null) {
253             return null;
254         }
255         if ((getName() == null ?
256                 moduleFromCache.getResolver().getName() == null :
257                     moduleFromCache.getResolver() == null ? false :
258                         getName().equals(moduleFromCache.getResolver().getName()))) {
259             return moduleFromCache;
260         } else {
261             Message.debug("found module in cache but with a different resolver: discarding: "+moduleFromCache);
262             return null;
263         }
264     }
265
266     public String JavaDoc getChangingMatcherName() {
267         return _changingMatcherName;
268     }
269
270     public void setChangingMatcher(String JavaDoc changingMatcherName) {
271         _changingMatcherName = changingMatcherName;
272     }
273
274     public String JavaDoc getChangingPattern() {
275         return _changingPattern;
276     }
277
278     public void setChangingPattern(String JavaDoc changingPattern) {
279         _changingPattern = changingPattern;
280     }
281
282     public Matcher getChangingMatcher() {
283         if (_changingPattern == null) {
284             return NoMatcher.getInstance();
285         }
286         PatternMatcher matcher = _ivy.getMatcher(_changingMatcherName);
287         if (matcher == null) {
288             throw new IllegalStateException JavaDoc("unknown matcher '"+_changingMatcherName+"'. It is set as changing matcher in "+this);
289         }
290         return matcher.getMatcher(_changingPattern);
291     }
292
293 }
294
Popular Tags