KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
10 import java.text.ParseException JavaDoc;
11
12 import fr.jayasoft.ivy.Artifact;
13 import fr.jayasoft.ivy.DependencyDescriptor;
14 import fr.jayasoft.ivy.DependencyResolver;
15 import fr.jayasoft.ivy.Ivy;
16 import fr.jayasoft.ivy.ResolveData;
17 import fr.jayasoft.ivy.ResolvedModuleRevision;
18 import fr.jayasoft.ivy.report.DownloadReport;
19 import fr.jayasoft.ivy.util.Message;
20
21 /**
22  * DualResolver is used to resolve dependencies with one dependency revolver, called ivy resolver,
23  * and then download artifacts found in the resolved dependencies from a second dependency resolver,
24  * called artifact resolver.
25  *
26  * It is especially useful with resolvers using repository where there is a lot of artifact, but no
27  * ivy file, like the maven ibiblio repository.
28  *
29  * If no ivy file is found by the ivy resolver, the artifact resolver is used to check if there is
30  * artifact corresponding to the request (with default ivy file).
31  *
32  * For artifact download, however, only the artifact resolver is used.
33  *
34  * Exactly two resolvers should be added to this resolver for it to work properly. The first resolver added
35  * if the ivy resolver, the second is the artifact one.
36  */

37 public class DualResolver extends AbstractResolver {
38     private DependencyResolver _ivyResolver;
39     private DependencyResolver _artifactResolver;
40     private boolean _allownomd = true;
41
42     public void add(DependencyResolver resolver) {
43         if (_ivyResolver == null) {
44             _ivyResolver = resolver;
45         } else if (_artifactResolver == null) {
46             _artifactResolver = resolver;
47         } else {
48             throw new IllegalStateException JavaDoc("exactly two resolvers must be added: ivy(1) and artifact(2) one");
49         }
50     }
51     
52
53     public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException JavaDoc {
54         if (_ivyResolver == null || _artifactResolver == null) {
55             throw new IllegalStateException JavaDoc("exactly two resolvers must be added: ivy(1) and artifact(2) one");
56         }
57         data = new ResolveData(data, doValidate(data));
58         final ResolvedModuleRevision mr = _ivyResolver.getDependency(dd, data);
59         if (mr == null) {
60             if (getIvy() != null && getIvy().isInterrupted()) {
61                 throw new RuntimeException JavaDoc("interrupted");
62             }
63             if (isAllownomd()) {
64                 Message.verbose("ivy resolver didn't find "+dd.getDependencyRevisionId()+": trying with artifact resolver");
65                 return _artifactResolver.getDependency(dd, data);
66             } else {
67                 return null;
68             }
69         } else {
70             return new ResolvedModuleRevisionProxy(mr, this);
71         }
72     }
73     
74     public void reportFailure() {
75         _ivyResolver.reportFailure();
76         _artifactResolver.reportFailure();
77     }
78
79     public void reportFailure(Artifact art) {
80         _ivyResolver.reportFailure(art);
81         _artifactResolver.reportFailure(art);
82     }
83
84     public DownloadReport download(Artifact[] artifacts, Ivy ivy, File JavaDoc cache, boolean useOrigin) {
85         return _artifactResolver.download(artifacts, ivy, cache, useOrigin);
86     }
87
88     public DependencyResolver getArtifactResolver() {
89         return _artifactResolver;
90     }
91     public void setArtifactResolver(DependencyResolver artifactResolver) {
92         _artifactResolver = artifactResolver;
93     }
94     public DependencyResolver getIvyResolver() {
95         return _ivyResolver;
96     }
97     public void setIvyResolver(DependencyResolver ivyResolver) {
98         _ivyResolver = ivyResolver;
99     }
100     public void publish(Artifact artifact, File JavaDoc src, boolean overwrite) throws IOException JavaDoc {
101         if ("ivy".equals(artifact.getType())) {
102             _ivyResolver.publish(artifact, src, overwrite);
103         } else {
104             _artifactResolver.publish(artifact, src, overwrite);
105         }
106     }
107     
108     public void dumpConfig() {
109         if (_ivyResolver == null || _artifactResolver == null) {
110             throw new IllegalStateException JavaDoc("exactly two resolvers must be added: ivy(1) and artifact(2) one");
111         }
112         Message.verbose("\t"+getName()+" [dual "+_ivyResolver.getName()+" "+_artifactResolver.getName()+"]");
113     }
114     
115     public boolean exists(Artifact artifact) {
116         return _artifactResolver.exists(artifact);
117     }
118
119
120     public boolean isAllownomd() {
121         return _allownomd;
122     }
123
124
125     public void setAllownomd(boolean allownomd) {
126         _allownomd = allownomd;
127     }
128 }
129
Popular Tags