1 6 package fr.jayasoft.ivy.conflict; 7 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Collections ; 11 import java.util.Iterator ; 12 import java.util.List ; 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 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 _strategyName; 45 46 public LatestConflictManager() { 47 } 48 49 public LatestConflictManager(LatestStrategy strategy) { 50 _strategy = strategy; 51 } 52 53 public LatestConflictManager(String name, LatestStrategy strategy) { 54 setName(name); 55 _strategy = strategy; 56 } 57 58 public Collection resolveConflicts(IvyNode parent, Collection conflicts) { 59 if (conflicts.size() < 2) { 60 return conflicts; 61 } 62 for (Iterator 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 conflicts) { 78 List artifacts = new ArrayList (conflicts.size()); 79 for (Iterator 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 106 public void setLatest(String strategyName) { 107 _strategyName = strategyName; 108 } 109 110 public void setStrategy(LatestStrategy strategy) { 111 _strategy = strategy; 112 } 113 114 115 public String toString() { 116 return String.valueOf(_strategy); 117 } 118 } 119 | Popular Tags |