View Javadoc

1   package org.apache.maven.artifact.resolver;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
24  import org.apache.maven.artifact.versioning.ArtifactVersion;
25  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.Set;
33  
34  public class ResolutionNode
35  {
36      private Artifact artifact;
37  
38      private List children;
39  
40      private final List parents;
41  
42      private final int depth;
43  
44      private final ResolutionNode parent;
45  
46      private final List remoteRepositories;
47  
48      private boolean active = true;
49  
50      private List trail;
51  
52      public ResolutionNode( Artifact artifact, List remoteRepositories )
53      {
54          this.artifact = artifact;
55          this.remoteRepositories = remoteRepositories;
56          depth = 0;
57          parents = Collections.EMPTY_LIST;
58          parent = null;
59      }
60  
61      public ResolutionNode( Artifact artifact, List remoteRepositories, ResolutionNode parent )
62      {
63          this.artifact = artifact;
64          this.remoteRepositories = remoteRepositories;
65          depth = parent.depth + 1;
66          parents = new ArrayList();
67          parents.addAll( parent.parents );
68          parents.add( parent.getKey() );
69          this.parent = parent;
70      }
71  
72      public void setArtifact( Artifact artifact )
73      {
74          this.artifact = artifact;
75      }
76  
77      public Artifact getArtifact()
78      {
79          return artifact;
80      }
81  
82      public Object getKey()
83      {
84          return artifact.getDependencyConflictId();
85      }
86  
87      public void addDependencies( Set artifacts, List remoteRepositories, ArtifactFilter filter )
88          throws CyclicDependencyException, OverConstrainedVersionException
89      {
90          if ( !artifacts.isEmpty() )
91          {
92              children = new ArrayList( artifacts.size() );
93  
94              for ( Iterator i = artifacts.iterator(); i.hasNext(); )
95              {
96                  Artifact a = (Artifact) i.next();
97  
98                  if ( parents.contains( a.getDependencyConflictId() ) )
99                  {
100                     a.setDependencyTrail( getDependencyTrail() );
101 
102                     throw new CyclicDependencyException( "A dependency has introduced a cycle", a );
103                 }
104 
105                 children.add( new ResolutionNode( a, remoteRepositories, this ) );
106             }
107         }
108         else
109         {
110             children = Collections.EMPTY_LIST;
111         }
112         trail = null;
113     }
114 
115     /**
116      * @return {@link List} < {@link String} > with artifact ids
117      * @throws OverConstrainedVersionException
118      */
119     public List getDependencyTrail()
120         throws OverConstrainedVersionException
121     {
122         List trial = getTrail();
123 
124         List ret = new ArrayList( trial.size() );
125         for ( Iterator i = trial.iterator(); i.hasNext(); )
126         {
127             Artifact artifact = (Artifact) i.next();
128             ret.add( artifact.getId() );
129         }
130         return ret;
131     }
132 
133     private List getTrail()
134         throws OverConstrainedVersionException
135     {
136         if ( trail == null )
137         {
138             List ids = new LinkedList();
139             ResolutionNode node = this;
140             while ( node != null )
141             {
142                 Artifact artifact = node.getArtifact();
143                 if ( artifact.getVersion() == null )
144                 {
145                     // set the recommended version
146                     ArtifactVersion selected = artifact.getSelectedVersion();
147                     //MNG-2123: null is a valid response to getSelectedVersion, don't
148                     //assume it won't ever be.
149                     if (selected != null)
150                     {
151                         artifact.selectVersion( selected.toString() );
152                     }
153                     else
154                     {
155                         throw new OverConstrainedVersionException("Unable to get a selected Version for "+ artifact.getArtifactId(),artifact);
156                     }
157                 }
158 
159                 ids.add( 0, artifact );
160                 node = node.parent;
161             }
162             trail = ids;
163         }
164         return trail;
165     }
166 
167     public boolean isResolved()
168     {
169         return children != null;
170     }
171 
172     public boolean isChildOfRootNode()
173     {
174         return parent != null && parent.parent == null;
175     }
176 
177     public Iterator getChildrenIterator()
178     {
179         return children.iterator();
180     }
181 
182     public int getDepth()
183     {
184         return depth;
185     }
186 
187     public List getRemoteRepositories()
188     {
189         return remoteRepositories;
190     }
191 
192     public boolean isActive()
193     {
194         return active;
195     }
196 
197     public void enable()
198     {
199         active = true;
200         // TODO: if it was null, we really need to go find them now... or is this taken care of by the ordering?
201         if ( children != null )
202         {
203             for ( Iterator i = children.iterator(); i.hasNext(); )
204             {
205                 ResolutionNode node = (ResolutionNode) i.next();
206                 node.enable();
207             }
208         }
209     }
210 
211     public void disable()
212     {
213         active = false;
214         if ( children != null )
215         {
216             for ( Iterator i = children.iterator(); i.hasNext(); )
217             {
218                 ResolutionNode node = (ResolutionNode) i.next();
219                 node.disable();
220             }
221         }
222     }
223 
224     public boolean filterTrail( ArtifactFilter filter )
225         throws OverConstrainedVersionException
226     {
227         boolean success = true;
228         if ( filter != null )
229         {
230             for ( Iterator i = getTrail().iterator(); i.hasNext() && success; )
231             {
232                 Artifact artifact = (Artifact) i.next();
233                 if ( !filter.include( artifact ) )
234                 {
235                     success = false;
236                 }
237             }
238         }
239         return success;
240     }
241 
242     public String toString()
243     {
244         return artifact.toString() + " (" + depth + "; " + ( active ? "enabled" : "disabled" ) + ")";
245     }
246 
247 }