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