View Javadoc
1   package org.eclipse.aether.collection;
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 java.util.Collection;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  
28  import org.eclipse.aether.RepositoryException;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.graph.DependencyNode;
31  import org.eclipse.aether.version.VersionConstraint;
32  
33  /**
34   * Thrown in case of an unsolvable conflict between different version constraints for a dependency.
35   */
36  public class UnsolvableVersionConflictException
37      extends RepositoryException
38  {
39  
40      private final transient Collection<String> versions;
41  
42      private final transient Collection<? extends List<? extends DependencyNode>> paths;
43  
44      /**
45       * Creates a new exception with the specified paths to conflicting nodes in the dependency graph.
46       * 
47       * @param paths The paths to the dependency nodes that participate in the version conflict, may be {@code null}.
48       */
49      public UnsolvableVersionConflictException( Collection<? extends List<? extends DependencyNode>> paths )
50      {
51          super( "Could not resolve version conflict among " + toPaths( paths ) );
52          if ( paths == null )
53          {
54              this.paths = Collections.emptyList();
55              this.versions = Collections.emptyList();
56          }
57          else
58          {
59              this.paths = paths;
60              this.versions = new LinkedHashSet<>();
61              for ( List<? extends DependencyNode> path : paths )
62              {
63                  VersionConstraint constraint = path.get( path.size() - 1 ).getVersionConstraint();
64                  if ( constraint != null && constraint.getRange() != null )
65                  {
66                      versions.add( constraint.toString() );
67                  }
68              }
69          }
70      }
71  
72      private static String toPaths( Collection<? extends List<? extends DependencyNode>> paths )
73      {
74          String result = "";
75  
76          if ( paths != null )
77          {
78              Collection<String> strings = new LinkedHashSet<>();
79  
80              for ( List<? extends DependencyNode> path : paths )
81              {
82                  strings.add( toPath( path ) );
83              }
84  
85              result = strings.toString();
86          }
87  
88          return result;
89      }
90  
91      private static String toPath( List<? extends DependencyNode> path )
92      {
93          StringBuilder buffer = new StringBuilder( 256 );
94  
95          for ( Iterator<? extends DependencyNode> it = path.iterator(); it.hasNext(); )
96          {
97              DependencyNode node = it.next();
98              if ( node.getDependency() == null )
99              {
100                 continue;
101             }
102 
103             Artifact artifact = node.getDependency().getArtifact();
104             buffer.append( artifact.getGroupId() );
105             buffer.append( ':' ).append( artifact.getArtifactId() );
106             buffer.append( ':' ).append( artifact.getExtension() );
107             if ( artifact.getClassifier().length() > 0 )
108             {
109                 buffer.append( ':' ).append( artifact.getClassifier() );
110             }
111             buffer.append( ':' ).append( node.getVersionConstraint() );
112 
113             if ( it.hasNext() )
114             {
115                 buffer.append( " -> " );
116             }
117         }
118 
119         return buffer.toString();
120     }
121 
122     /**
123      * Gets the paths leading to the conflicting dependencies.
124      * 
125      * @return The (read-only) paths leading to the conflicting dependencies, never {@code null}.
126      */
127     public Collection<? extends List<? extends DependencyNode>> getPaths()
128     {
129         return paths;
130     }
131 
132     /**
133      * Gets the conflicting version constraints of the dependency.
134      * 
135      * @return The (read-only) conflicting version constraints, never {@code null}.
136      */
137     public Collection<String> getVersions()
138     {
139         return versions;
140     }
141 
142 }