PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/maven-3.0.5/maven-core/src/main/java/org/apache/maven/artifact/resolver/ResolutionNode.java

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