View Javadoc
1   package org.eclipse.aether.util.graph.transformer;
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.IdentityHashMap;
23  import java.util.Map;
24  import static java.util.Objects.requireNonNull;
25  
26  import org.eclipse.aether.RepositoryException;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.collection.DependencyGraphTransformationContext;
29  import org.eclipse.aether.collection.DependencyGraphTransformer;
30  import org.eclipse.aether.graph.Dependency;
31  import org.eclipse.aether.graph.DependencyNode;
32  
33  /**
34   * Set "groupId:artId:classifier:extension" as conflict marker for every node.
35   */
36  class SimpleConflictMarker
37      implements DependencyGraphTransformer
38  {
39  
40      public DependencyNode transformGraph( DependencyNode node, DependencyGraphTransformationContext context )
41          throws RepositoryException
42      {
43          requireNonNull( node, "node cannot be null" );
44          requireNonNull( context, "context cannot be null" );
45          @SuppressWarnings( "unchecked" )
46          Map<DependencyNode, Object> conflictIds =
47              (Map<DependencyNode, Object>) context.get( TransformationContextKeys.CONFLICT_IDS );
48          if ( conflictIds == null )
49          {
50              conflictIds = new IdentityHashMap<>();
51              context.put( TransformationContextKeys.CONFLICT_IDS, conflictIds );
52          }
53  
54          mark( node, conflictIds );
55  
56          return node;
57      }
58  
59      private void mark( DependencyNode node, Map<DependencyNode, Object> conflictIds )
60      {
61          Dependency dependency = node.getDependency();
62          if ( dependency != null )
63          {
64              Artifact artifact = dependency.getArtifact();
65  
66              String key =
67                  String.format( "%s:%s:%s:%s", artifact.getGroupId(), artifact.getArtifactId(),
68                                 artifact.getClassifier(), artifact.getExtension() );
69  
70              if ( conflictIds.put( node, key ) != null )
71              {
72                  return;
73              }
74          }
75  
76          for ( DependencyNode child : node.getChildren() )
77          {
78              mark( child, conflictIds );
79          }
80      }
81  
82  }