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