KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > DefaultDependencyDescriptor


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;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.LinkedHashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 import fr.jayasoft.ivy.matcher.MatcherHelper;
22 import fr.jayasoft.ivy.namespace.NameSpaceHelper;
23 import fr.jayasoft.ivy.namespace.Namespace;
24 import fr.jayasoft.ivy.namespace.NamespaceTransformer;
25
26 /**
27  * This class can be used as the default implementation for DependencyDescriptor.
28  * It implements required methods and enables to fill dependency information
29  * with the addDependencyConfiguration method.
30  *
31  * @author Xavier Hanin
32  *
33  */

34 public class DefaultDependencyDescriptor implements DependencyDescriptor {
35     private static final Pattern JavaDoc SELF_FALLBACK_PATTERN = Pattern.compile("@(\\(.*\\))?");
36     private static final Pattern JavaDoc THIS_FALLBACK_PATTERN = Pattern.compile("#(\\(.*\\))?");
37     
38     /**
39      * Transforms the given dependency descriptor of the given namespace and return
40      * a new dependency descriptor in the system namespace.
41      *
42      * <i>Note that exclude rules are not converted in system namespace, because they aren't
43      * transformable (the name space hasn't the ability to convert regular expressions).
44      * However, method doesExclude will work with system artifacts.</i>
45      *
46      * @param md
47      * @param ns
48      * @return
49      */

50     public static DependencyDescriptor transformInstance(DependencyDescriptor dd, Namespace ns) {
51         NamespaceTransformer t = ns.getToSystemTransformer();
52         if (t.isIdentity()) {
53             return dd;
54         }
55         DefaultDependencyDescriptor newdd = transformInstance(dd, t, false);
56         newdd._namespace = ns;
57         return newdd;
58     }
59     
60     /**
61      * Transforms a dependency descriptor using the given transformer.
62      *
63      * Note that no namespace info will be attached to the transformed dependency descriptor,
64      * so calling doesExclude is not recommended (doesExclude only works when namespace is properly set)
65      * @param dd
66      * @param t
67      * @return
68      */

69     public static DefaultDependencyDescriptor transformInstance(DependencyDescriptor dd, NamespaceTransformer t, boolean fromSystem) {
70         ModuleRevisionId transformParentId = t.transform(dd.getParentRevisionId());
71         ModuleRevisionId transformMrid = t.transform(dd.getDependencyRevisionId());
72         DefaultDependencyDescriptor newdd = new DefaultDependencyDescriptor();
73         newdd._parentId = transformParentId;
74         newdd._revId = transformMrid;
75         newdd._force = dd.isForce();
76         newdd._changing = dd.isChanging();
77         newdd._transitive = dd.isTransitive();
78         String JavaDoc[] moduleConfs = dd.getModuleConfigurations();
79         if (moduleConfs.length == 1 && "*".equals(moduleConfs[0])) {
80             if (dd instanceof DefaultDependencyDescriptor) {
81                 DefaultDependencyDescriptor ddd = (DefaultDependencyDescriptor)dd;
82                 newdd._confs = new HashMap JavaDoc(ddd._confs);
83                 newdd._artifactsExcludes = new HashMap JavaDoc(ddd._artifactsExcludes);
84                 newdd._artifactsIncludes = new HashMap JavaDoc(ddd._artifactsIncludes);
85             } else {
86                 throw new IllegalArgumentException JavaDoc("dependency descriptor transformation does not support * module confs with descriptors which aren't DefaultDependencyDescriptor");
87             }
88         } else {
89             for (int i = 0; i < moduleConfs.length; i++) {
90                 newdd._confs.put(moduleConfs[i], new ArrayList JavaDoc(Arrays.asList(dd.getDependencyConfigurations(moduleConfs[i]))));
91                 newdd._artifactsExcludes.put(moduleConfs[i], new ArrayList JavaDoc(Arrays.asList(dd.getDependencyArtifactsExcludes(moduleConfs[i]))));
92                 newdd._artifactsIncludes.put(moduleConfs[i], new ArrayList JavaDoc(Arrays.asList(dd.getDependencyArtifactsIncludes(moduleConfs[i]))));
93             }
94         }
95         if (fromSystem) {
96             newdd._asSystem = dd;
97         }
98         return newdd;
99     }
100     
101     private ModuleRevisionId _revId;
102     private Map JavaDoc _confs = new HashMap JavaDoc();
103     private Map JavaDoc _artifactsIncludes = new HashMap JavaDoc(); // Map (String masterConf -> Collection(DependencyArtifactDescriptor))
104
private Map JavaDoc _artifactsExcludes = new HashMap JavaDoc(); // Map (String masterConf -> Collection(DependencyArtifactDescriptor))
105
private Set JavaDoc _extends = new HashSet JavaDoc();
106     
107     /**
108      * Used to indicate that this revision must be used in case of conflicts, independently
109      * of conflicts manager
110      */

111     private boolean _force;
112     /**
113      * Used to indicate that the dependency is a changing one, i.e. that ivy should not rely on the version to know if it can trust artifacts in cache
114      */

115     private boolean _changing;
116     private ModuleRevisionId _parentId;
117     
118     private boolean _transitive = true;
119     
120     /**
121      * This namespace should be used to check
122      */

123     private Namespace _namespace = null;
124     private ModuleDescriptor _md;
125     private DependencyDescriptor _asSystem = this;
126     
127     public DefaultDependencyDescriptor(DependencyDescriptor dd, String JavaDoc revision) {
128         _parentId = dd.getParentRevisionId();
129         _revId = ModuleRevisionId.newInstance(dd.getDependencyRevisionId(), revision);
130         _force = dd.isForce();
131         _changing = dd.isChanging();
132         _transitive = dd.isTransitive();
133         String JavaDoc[] moduleConfs = dd.getModuleConfigurations();
134         for (int i = 0; i < moduleConfs.length; i++) {
135             _confs.put(moduleConfs[i], new ArrayList JavaDoc(Arrays.asList(dd.getDependencyConfigurations(moduleConfs[i]))));
136             _artifactsExcludes.put(moduleConfs[i], new ArrayList JavaDoc(Arrays.asList(dd.getDependencyArtifactsExcludes(moduleConfs[i]))));
137             _artifactsIncludes.put(moduleConfs[i], new ArrayList JavaDoc(Arrays.asList(dd.getDependencyArtifactsIncludes(moduleConfs[i]))));
138         }
139     }
140     
141     public DefaultDependencyDescriptor(ModuleDescriptor md, ModuleRevisionId mrid, boolean force, boolean changing, boolean transitive) {
142         _md = md;
143         _revId = mrid;
144         _force = force;
145         _changing = changing;
146         _transitive = transitive;
147     }
148     
149     public DefaultDependencyDescriptor(ModuleRevisionId mrid, boolean force) {
150         this(mrid, force, false);
151     }
152     
153     public DefaultDependencyDescriptor(ModuleRevisionId mrid, boolean force, boolean changing) {
154         _revId = mrid;
155         _force = force;
156         _changing = changing;
157     }
158     
159     private DefaultDependencyDescriptor() {
160     }
161     
162     public ModuleId getDependencyId() {
163         return getDependencyRevisionId().getModuleId();
164     }
165     
166     public ModuleRevisionId getDependencyRevisionId() {
167         return _revId;
168     }
169     
170     public String JavaDoc[] getModuleConfigurations() {
171         return (String JavaDoc[]) _confs.keySet().toArray(new String JavaDoc[_confs.keySet().size()]);
172     }
173     
174     public String JavaDoc[] getDependencyConfigurations(String JavaDoc moduleConfiguration) {
175         return getDependencyConfigurations(moduleConfiguration, moduleConfiguration);
176     }
177     
178     /**
179      * Return the dependency configurations mapped to the given moduleConfiguration, actually resolved
180      * because of the given requestedConfiguration
181      *
182      * Usually requestedConfiguration and moduleConfiguration are the same, except when
183      * a conf extends another, then the moduleConfiguration is the configuration currently resolved
184      * (the extended one), and requestedConfiguration is the one actually requested initially (the
185      * extending one).
186      *
187      * Both moduleConfiguration and requestedConfiguration are configurations of the caller,
188      * the array returned is composed of the required configurations of the dependency described by this
189      * descriptor.
190      */

191     public String JavaDoc[] getDependencyConfigurations(String JavaDoc moduleConfiguration, String JavaDoc requestedConfiguration) {
192         List JavaDoc confs = (List JavaDoc)_confs.get(moduleConfiguration);
193         if (confs == null) {
194             // there is no mapping defined for this configuration, add the 'other' mappings.
195
confs = (List JavaDoc)_confs.get("%");
196         }
197         List JavaDoc defConfs = (List JavaDoc)_confs.get("*");
198         Collection JavaDoc ret = new LinkedHashSet JavaDoc();
199         if (confs != null) {
200             ret.addAll(confs);
201         }
202         if (defConfs != null) {
203             ret.addAll(defConfs);
204         }
205         
206         Collection JavaDoc replacedRet = new LinkedHashSet JavaDoc();
207         for (Iterator JavaDoc iter = ret.iterator(); iter.hasNext();) {
208             String JavaDoc c = (String JavaDoc)iter.next();
209             String JavaDoc replacedConf = replaceSelfFallbackPattern( c, moduleConfiguration);
210             if (replacedConf==null) {
211                 replacedConf = replaceThisFallbackPattern( c, requestedConfiguration);
212             }
213             if (replacedConf!=null) {
214                 c = replacedConf;
215             }
216             replacedRet.add(c);
217         }
218         ret = replacedRet;
219         if (ret.remove("*")) {
220             StringBuffer JavaDoc r = new StringBuffer JavaDoc("*");
221             // merge excluded configurations as one conf like *!A!B
222
for (Iterator JavaDoc iter = ret.iterator(); iter.hasNext();) {
223                 String JavaDoc c = (String JavaDoc)iter.next();
224                 if (c.startsWith("!")) {
225                     r.append(c);
226                 }
227             }
228             return new String JavaDoc[] {r.toString()};
229         }
230         return (String JavaDoc[])ret.toArray(new String JavaDoc[ret.size()]);
231     }
232     
233     protected static String JavaDoc replaceSelfFallbackPattern(final String JavaDoc conf, final String JavaDoc moduleConfiguration) {
234         return replaceFallbackConfigurationPattern(SELF_FALLBACK_PATTERN, conf, moduleConfiguration);
235     }
236     
237     protected static String JavaDoc replaceThisFallbackPattern(final String JavaDoc conf, final String JavaDoc requestedConfiguration) {
238         return replaceFallbackConfigurationPattern(THIS_FALLBACK_PATTERN, conf, requestedConfiguration);
239     }
240     
241     /**
242      * Replaces fallback patterns with correct values if fallback pattern exists.
243      * @param pattern pattern to look for
244      * @param conf configuration mapping from dependency element
245      * @param moduleConfiguration module's configuration to use for replacement
246      * @return Replaced string if pattern matched. Otherwise null.
247      */

248     protected static String JavaDoc replaceFallbackConfigurationPattern(final Pattern JavaDoc pattern, final String JavaDoc conf, final String JavaDoc moduleConfiguration) {
249         Matcher JavaDoc matcher = pattern.matcher(conf);
250         if (matcher.matches()) {
251             if (matcher.group(1) != null) {
252                 return moduleConfiguration+matcher.group(1);
253             } else {
254                 return moduleConfiguration;
255             }
256         }
257         return null;
258     }
259     
260     public String JavaDoc[] getDependencyConfigurations(String JavaDoc[] moduleConfigurations) {
261         Set JavaDoc confs = new LinkedHashSet JavaDoc();
262         for (int i = 0; i < moduleConfigurations.length; i++) {
263             confs.addAll(Arrays.asList(getDependencyConfigurations(moduleConfigurations[i])));
264         }
265         if (confs.contains("*")) {
266             return new String JavaDoc[] {"*"};
267         }
268         return (String JavaDoc[]) confs.toArray(new String JavaDoc[confs.size()]);
269     }
270     
271     public DependencyArtifactDescriptor[] getDependencyArtifactsIncludes(String JavaDoc moduleConfiguration) {
272         return getDependencyArtifacts(moduleConfiguration, _artifactsIncludes);
273     }
274     
275     public DependencyArtifactDescriptor[] getDependencyArtifactsExcludes(String JavaDoc moduleConfiguration) {
276         return getDependencyArtifacts(moduleConfiguration, _artifactsExcludes);
277     }
278     
279     private DependencyArtifactDescriptor[] getDependencyArtifacts(String JavaDoc moduleConfiguration, Map JavaDoc artifactsMap) {
280         if (artifactsMap.isEmpty()) {
281             return new DependencyArtifactDescriptor[0];
282         }
283         Collection JavaDoc artifacts = (Collection JavaDoc)artifactsMap.get(moduleConfiguration);
284         Collection JavaDoc defArtifacts = (Collection JavaDoc)artifactsMap.get("*");
285         Set JavaDoc ret = new HashSet JavaDoc();
286         if (artifacts != null) {
287             ret.addAll(artifacts);
288         }
289         if (defArtifacts != null) {
290             ret.addAll(defArtifacts);
291         }
292         return (DependencyArtifactDescriptor[])ret.toArray(new DependencyArtifactDescriptor[ret.size()]);
293     }
294     
295     public DependencyArtifactDescriptor[] getDependencyArtifactsIncludes(String JavaDoc[] moduleConfigurations) {
296         Set JavaDoc artifacts = new HashSet JavaDoc();
297         for (int i = 0; i < moduleConfigurations.length; i++) {
298             artifacts.addAll(Arrays.asList(getDependencyArtifactsIncludes(moduleConfigurations[i])));
299         }
300         return (DependencyArtifactDescriptor[]) artifacts.toArray(new DependencyArtifactDescriptor[artifacts.size()]);
301     }
302     
303     public DependencyArtifactDescriptor[] getDependencyArtifactsExcludes(String JavaDoc[] moduleConfigurations) {
304         Set JavaDoc artifacts = new HashSet JavaDoc();
305         for (int i = 0; i < moduleConfigurations.length; i++) {
306             artifacts.addAll(Arrays.asList(getDependencyArtifactsExcludes(moduleConfigurations[i])));
307         }
308         return (DependencyArtifactDescriptor[]) artifacts.toArray(new DependencyArtifactDescriptor[artifacts.size()]);
309     }
310     
311     public DependencyArtifactDescriptor[] getAllDependencyArtifactsIncludes() {
312         return getAllDependencyArtifacts(_artifactsIncludes);
313     }
314     
315     public DependencyArtifactDescriptor[] getAllDependencyArtifactsExcludes() {
316         return getAllDependencyArtifacts(_artifactsExcludes);
317     }
318     
319     private DependencyArtifactDescriptor[] getAllDependencyArtifacts(Map JavaDoc artifactsMap) {
320         Set JavaDoc ret = new HashSet JavaDoc();
321         for (Iterator JavaDoc it = artifactsMap.values().iterator(); it.hasNext();) {
322             Collection JavaDoc artifacts = (Collection JavaDoc)it.next();
323             ret.addAll(artifacts);
324         }
325         return (DependencyArtifactDescriptor[])ret.toArray(new DependencyArtifactDescriptor[ret.size()]);
326     }
327     
328     public void addDependencyConfiguration(String JavaDoc masterConf, String JavaDoc depConf) {
329         List JavaDoc confs = (List JavaDoc)_confs.get(masterConf);
330         if (confs == null) {
331             confs = new ArrayList JavaDoc();
332             _confs.put(masterConf, confs);
333         }
334         if (!confs.contains(depConf)) {
335             confs.add(depConf);
336         }
337     }
338     
339     public void addDependencyArtifactIncludes(String JavaDoc masterConf, DependencyArtifactDescriptor dad) {
340         addDependencyArtifacts(masterConf, dad, _artifactsIncludes);
341     }
342     
343     public void addDependencyArtifactExcludes(String JavaDoc masterConf, DependencyArtifactDescriptor dad) {
344         addDependencyArtifacts(masterConf, dad, _artifactsExcludes);
345     }
346     
347     private void addDependencyArtifacts(String JavaDoc masterConf, DependencyArtifactDescriptor dad, Map JavaDoc artifactsMap) {
348         Collection JavaDoc artifacts = (Collection JavaDoc)artifactsMap.get(masterConf);
349         if (artifacts == null) {
350             artifacts = new ArrayList JavaDoc();
351             artifactsMap.put(masterConf, artifacts);
352         }
353         artifacts.add(dad);
354     }
355     
356     /**
357      * only works when namespace is properly set. The behaviour is not specified if namespace is not set
358      */

359     public boolean doesExclude(String JavaDoc[] moduleConfigurations, ArtifactId artifactId) {
360         if (_namespace != null) {
361             artifactId = NameSpaceHelper.transform(artifactId, _namespace.getFromSystemTransformer());
362         }
363         DependencyArtifactDescriptor[] dads = getDependencyArtifactsExcludes(moduleConfigurations);
364         for (int i = 0; i < dads.length; i++) {
365             if (MatcherHelper.matches(dads[i].getMatcher(), dads[i].getId(), artifactId)) {
366                 return true;
367             }
368         }
369         return false;
370     }
371     
372     /**
373      * Returns true if this descriptor contains any exclusion rule
374      * @return
375      */

376     public boolean canExclude() {
377         return !_artifactsExcludes.isEmpty();
378     }
379     
380     public void addExtends(String JavaDoc conf) {
381         _extends.add(conf);
382     }
383     
384     public String JavaDoc toString() {
385         return "dependency: "+_revId+" "+_confs;
386     }
387     
388     public boolean isForce() {
389         return _force;
390     }
391     
392     public ModuleRevisionId getParentRevisionId() {
393         return _md != null ? _md.getResolvedModuleRevisionId() : _parentId;
394     }
395     
396     public boolean isChanging() {
397         return _changing;
398     }
399     
400     public boolean isTransitive() {
401         return _transitive;
402     }
403     
404     public Namespace getNamespace() {
405         return _namespace;
406     }
407
408     public String JavaDoc getAttribute(String JavaDoc attName) {
409         return _revId.getAttribute(attName);
410     }
411
412     public Map JavaDoc getAttributes() {
413         return _revId.getAttributes();
414     }
415
416     public String JavaDoc getExtraAttribute(String JavaDoc attName) {
417         return _revId.getExtraAttribute(attName);
418     }
419
420     public Map JavaDoc getExtraAttributes() {
421         return _revId.getExtraAttributes();
422     }
423
424     public String JavaDoc getStandardAttribute(String JavaDoc attName) {
425         return _revId.getStandardAttribute(attName);
426     }
427
428     public Map JavaDoc getStandardAttributes() {
429         return _revId.getStandardAttributes();
430     }
431
432     public DependencyDescriptor asSystem() {
433         return _asSystem;
434     }
435     
436 }
437
Popular Tags