KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > resolver > ResolutionNode


1 package org.apache.maven.artifact.resolver;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29
30 public class ResolutionNode
31 {
32     private final Artifact artifact;
33
34     private List JavaDoc children;
35
36     private final List JavaDoc parents;
37
38     private final int depth;
39
40     private final ResolutionNode parent;
41
42     private final List JavaDoc remoteRepositories;
43
44     private boolean active = true;
45
46     private List JavaDoc trail;
47
48     public ResolutionNode( Artifact artifact, List JavaDoc 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 JavaDoc remoteRepositories, ResolutionNode parent )
58     {
59         this.artifact = artifact;
60         this.remoteRepositories = remoteRepositories;
61         this.depth = parent.depth + 1;
62         this.parents = new ArrayList JavaDoc();
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 JavaDoc getKey()
74     {
75         return artifact.getDependencyConflictId();
76     }
77
78     public void addDependencies( Set JavaDoc artifacts, List JavaDoc remoteRepositories, ArtifactFilter filter )
79         throws CyclicDependencyException, OverConstrainedVersionException
80     {
81         if ( !artifacts.isEmpty() )
82         {
83             children = new ArrayList JavaDoc( artifacts.size() );
84
85             for ( Iterator JavaDoc 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 JavaDoc getDependencyTrail()
107         throws OverConstrainedVersionException
108     {
109         List JavaDoc trial = getTrail();
110
111         List JavaDoc ret = new ArrayList JavaDoc( trial.size() );
112         for ( Iterator JavaDoc 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 JavaDoc getTrail()
121         throws OverConstrainedVersionException
122     {
123         if ( trail == null )
124         {
125             List JavaDoc ids = new LinkedList JavaDoc();
126             ResolutionNode node = this;
127             while ( node != null )
128             {
129                 Artifact artifact = node.getArtifact();
130                 if ( artifact.getVersion() == null )
131                 {
132                     // set the recommended version
133
String JavaDoc 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 JavaDoc getChildrenIterator()
156     {
157         return children.iterator();
158     }
159
160     public int getDepth()
161     {
162         return depth;
163     }
164
165     public List JavaDoc 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         // TODO: if it was null, we really need to go find them now... or is this taken care of by the ordering?
179
if ( children != null )
180         {
181             for ( Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc toString()
221     {
222         return artifact.toString() + " (" + depth + "; " + ( active ? "enabled" : "disabled" ) + ")";
223     }
224
225 }
226
Popular Tags