KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > conflict > LatestConflictManager


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.conflict;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import fr.jayasoft.ivy.ArtifactInfo;
15 import fr.jayasoft.ivy.DependencyDescriptor;
16 import fr.jayasoft.ivy.IvyNode;
17 import fr.jayasoft.ivy.LatestStrategy;
18 import fr.jayasoft.ivy.util.Message;
19
20 public class LatestConflictManager extends AbstractConflictManager {
21     private static class IvyNodeArtifactInfo implements ArtifactInfo {
22         private final IvyNode _node;
23
24         private IvyNodeArtifactInfo(IvyNode dep) {
25             _node = dep;
26         }
27
28         public long getLastModified() {
29             return _node.getPublication();
30         }
31
32         public String JavaDoc getRevision() {
33             return _node.getId().getRevision();
34         }
35         
36         public IvyNode getNode() {
37             return _node;
38         }
39     }
40
41
42
43     private LatestStrategy _strategy;
44     private String JavaDoc _strategyName;
45
46     public LatestConflictManager() {
47     }
48
49     public LatestConflictManager(LatestStrategy strategy) {
50         _strategy = strategy;
51     }
52
53     public LatestConflictManager(String JavaDoc name, LatestStrategy strategy) {
54         setName(name);
55         _strategy = strategy;
56     }
57
58     public Collection JavaDoc resolveConflicts(IvyNode parent, Collection JavaDoc conflicts) {
59         if (conflicts.size() < 2) {
60             return conflicts;
61         }
62         for (Iterator JavaDoc iter = conflicts.iterator(); iter.hasNext();) {
63             IvyNode node = (IvyNode)iter.next();
64             DependencyDescriptor dd = node.getDependencyDescriptor(parent);
65             if (dd != null && dd.isForce() && parent.getResolvedId().equals(dd.getParentRevisionId())) {
66                 return Collections.singleton(node);
67             }
68         }
69         ArtifactInfo latest = getStrategy().findLatest(toArtifactInfo(conflicts), null);
70         if (latest != null) {
71             return Collections.singleton(((IvyNodeArtifactInfo)latest).getNode());
72         } else {
73             return conflicts;
74         }
75     }
76
77     private ArtifactInfo[] toArtifactInfo(Collection JavaDoc conflicts) {
78         List JavaDoc artifacts = new ArrayList JavaDoc(conflicts.size());
79         for (Iterator JavaDoc iter = conflicts.iterator(); iter.hasNext();) {
80             IvyNode node = (IvyNode)iter.next();
81             artifacts.add(new IvyNodeArtifactInfo(node));
82         }
83         return (ArtifactInfo[])artifacts.toArray(new ArtifactInfo[artifacts.size()]);
84     }
85
86     public LatestStrategy getStrategy() {
87         if (_strategy == null) {
88             if (_strategyName != null) {
89                 _strategy = getIvy().getLatestStrategy(_strategyName);
90                 if (_strategy == null) {
91                     Message.error("unknown latest strategy: "+_strategyName);
92                     _strategy = getIvy().getDefaultLatestStrategy();
93                 }
94             } else {
95                 _strategy = getIvy().getDefaultLatestStrategy();
96             }
97         }
98         return _strategy;
99     }
100     
101
102     /**
103      * To conform to configurator API
104      * @param latestStrategy
105      */

106     public void setLatest(String JavaDoc strategyName) {
107         _strategyName = strategyName;
108     }
109     
110     public void setStrategy(LatestStrategy strategy) {
111         _strategy = strategy;
112     }
113     
114
115     public String JavaDoc toString() {
116         return String.valueOf(_strategy);
117     }
118 }
119
Popular Tags