1 package org.apache.maven.artifact.resolver; 2 3 18 19 import org.apache.maven.artifact.Artifact; 20 import org.apache.maven.artifact.resolver.filter.ArtifactFilter; 21 import org.apache.maven.artifact.versioning.OverConstrainedVersionException; 22 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 public class ResolutionNode 31 { 32 private final Artifact artifact; 33 34 private List children; 35 36 private final List parents; 37 38 private final int depth; 39 40 private final ResolutionNode parent; 41 42 private final List remoteRepositories; 43 44 private boolean active = true; 45 46 private List trail; 47 48 public ResolutionNode( Artifact artifact, List remoteRepositories ) 49 { 50 this.artifact = artifact; 51 this.remoteRepositories = remoteRepositories; 52 this.depth = 0; 53 this.parents = Collections.EMPTY_LIST; 54 this.parent = null; 55 } 56 57 public ResolutionNode( Artifact artifact, List remoteRepositories, ResolutionNode parent ) 58 { 59 this.artifact = artifact; 60 this.remoteRepositories = remoteRepositories; 61 this.depth = parent.depth + 1; 62 this.parents = new ArrayList (); 63 this.parents.addAll( parent.parents ); 64 this.parents.add( parent.getKey() ); 65 this.parent = parent; 66 } 67 68 public Artifact getArtifact() 69 { 70 return artifact; 71 } 72 73 public Object getKey() 74 { 75 return artifact.getDependencyConflictId(); 76 } 77 78 public void addDependencies( Set artifacts, List remoteRepositories, ArtifactFilter filter ) 79 throws CyclicDependencyException, OverConstrainedVersionException 80 { 81 if ( !artifacts.isEmpty() ) 82 { 83 children = new ArrayList ( artifacts.size() ); 84 85 for ( Iterator i = artifacts.iterator(); i.hasNext(); ) 86 { 87 Artifact a = (Artifact) i.next(); 88 89 if ( parents.contains( a.getDependencyConflictId() ) ) 90 { 91 a.setDependencyTrail( getDependencyTrail() ); 92 93 throw new CyclicDependencyException( "A dependency has introduced a cycle", a ); 94 } 95 96 children.add( new ResolutionNode( a, remoteRepositories, this ) ); 97 } 98 } 99 else 100 { 101 children = Collections.EMPTY_LIST; 102 } 103 trail = null; 104 } 105 106 public List getDependencyTrail() 107 throws OverConstrainedVersionException 108 { 109 List trial = getTrail(); 110 111 List ret = new ArrayList ( trial.size() ); 112 for ( Iterator i = trial.iterator(); i.hasNext(); ) 113 { 114 Artifact artifact = (Artifact) i.next(); 115 ret.add( artifact.getId() ); 116 } 117 return ret; 118 } 119 120 private List getTrail() 121 throws OverConstrainedVersionException 122 { 123 if ( trail == null ) 124 { 125 List ids = new LinkedList (); 126 ResolutionNode node = this; 127 while ( node != null ) 128 { 129 Artifact artifact = node.getArtifact(); 130 if ( artifact.getVersion() == null ) 131 { 132 String version = artifact.getSelectedVersion().toString(); 134 artifact.selectVersion( version ); 135 } 136 137 ids.add( 0, artifact ); 138 node = node.parent; 139 } 140 trail = ids; 141 } 142 return trail; 143 } 144 145 public boolean isResolved() 146 { 147 return children != null; 148 } 149 150 public boolean isChildOfRootNode() 151 { 152 return parent != null && parent.parent == null; 153 } 154 155 public Iterator getChildrenIterator() 156 { 157 return children.iterator(); 158 } 159 160 public int getDepth() 161 { 162 return depth; 163 } 164 165 public List getRemoteRepositories() 166 { 167 return remoteRepositories; 168 } 169 170 public boolean isActive() 171 { 172 return active; 173 } 174 175 public void enable() 176 { 177 this.active = true; 178 if ( children != null ) 180 { 181 for ( Iterator i = children.iterator(); i.hasNext(); ) 182 { 183 ResolutionNode node = (ResolutionNode) i.next(); 184 node.enable(); 185 } 186 } 187 } 188 189 public void disable() 190 { 191 this.active = false; 192 if ( children != null ) 193 { 194 for ( Iterator i = children.iterator(); i.hasNext(); ) 195 { 196 ResolutionNode node = (ResolutionNode) i.next(); 197 node.disable(); 198 } 199 } 200 } 201 202 public boolean filterTrail( ArtifactFilter filter ) 203 throws OverConstrainedVersionException 204 { 205 boolean success = true; 206 if ( filter != null ) 207 { 208 for ( Iterator i = getTrail().iterator(); i.hasNext() && success; ) 209 { 210 Artifact artifact = (Artifact) i.next(); 211 if ( !filter.include( artifact ) ) 212 { 213 success = false; 214 } 215 } 216 } 217 return success; 218 } 219 220 public String toString() 221 { 222 return artifact.toString() + " (" + depth + "; " + ( active ? "enabled" : "disabled" ) + ")"; 223 } 224 225 } 226 | Popular Tags |