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.visitor;
20  
21  import java.util.ArrayDeque;
22  import java.util.Collection;
23  import java.util.Deque;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Objects;
27  import java.util.function.Consumer;
28  
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.graph.Dependency;
31  import org.eclipse.aether.graph.DependencyNode;
32  import org.eclipse.aether.graph.DependencyVisitor;
33  import org.eclipse.aether.graph.Exclusion;
34  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
35  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
36  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
37  
38  import static java.util.Objects.requireNonNull;
39  
40  /**
41   * A dependency visitor that dumps the graph to any {@link Consumer}{@code <String>}. Meant for diagnostic and testing, as
42   * it may output the graph to standard output, error or even some logging interface.
43   *
44   * @since 1.9.8
45   */
46  public class DependencyGraphDumper implements DependencyVisitor {
47  
48      private final Consumer<String> consumer;
49  
50      private final Deque<DependencyNode> nodes = new ArrayDeque<>();
51  
52      public DependencyGraphDumper(Consumer<String> consumer) {
53          this.consumer = requireNonNull(consumer);
54      }
55  
56      @Override
57      public boolean visitEnter(DependencyNode node) {
58          nodes.push(node);
59          consumer.accept(formatLine(nodes));
60          return true;
61      }
62  
63      @Override
64      public boolean visitLeave(DependencyNode node) {
65          if (!nodes.isEmpty()) {
66              nodes.pop();
67          }
68          return true;
69      }
70  
71      protected String formatLine(Deque<DependencyNode> nodes) {
72          return formatIndentation(nodes) + formatNode(nodes);
73      }
74  
75      protected String formatIndentation(Deque<DependencyNode> nodes) {
76          StringBuilder buffer = new StringBuilder(128);
77          Iterator<DependencyNode> iter = nodes.descendingIterator();
78          DependencyNode parent = iter.hasNext() ? iter.next() : null;
79          DependencyNode child = iter.hasNext() ? iter.next() : null;
80          while (parent != null && child != null) {
81              boolean lastChild = parent.getChildren().get(parent.getChildren().size() - 1) == child;
82              boolean end = child == nodes.peekFirst();
83              String indent;
84              if (end) {
85                  indent = lastChild ? "\\- " : "+- ";
86              } else {
87                  indent = lastChild ? "   " : "|  ";
88              }
89              buffer.append(indent);
90              parent = child;
91              child = iter.hasNext() ? iter.next() : null;
92          }
93          return buffer.toString();
94      }
95  
96      protected String formatNode(Deque<DependencyNode> nodes) {
97          DependencyNode node = requireNonNull(nodes.peek(), "bug: should not happen");
98          StringBuilder buffer = new StringBuilder(128);
99          Artifact a = node.getArtifact();
100         buffer.append(a);
101         Dependency d = node.getDependency();
102         if (d != null && !d.getScope().isEmpty()) {
103             buffer.append(" [").append(d.getScope());
104             if (d.isOptional()) {
105                 buffer.append(", optional");
106             }
107             buffer.append("]");
108         }
109         String premanaged = DependencyManagerUtils.getPremanagedVersion(node);
110         if (premanaged != null && !premanaged.equals(a.getBaseVersion())) {
111             buffer.append(" (version managed from ").append(premanaged).append(")");
112         }
113 
114         premanaged = DependencyManagerUtils.getPremanagedScope(node);
115         if (premanaged != null && d != null && !premanaged.equals(d.getScope())) {
116             buffer.append(" (scope managed from ").append(premanaged).append(")");
117         }
118 
119         Boolean premanagedOptional = DependencyManagerUtils.getPremanagedOptional(node);
120         if (premanagedOptional != null && d != null && !premanagedOptional.equals(d.getOptional())) {
121             buffer.append(" (optionality managed from ")
122                     .append(premanagedOptional)
123                     .append(")");
124         }
125 
126         Collection<Exclusion> premanagedExclusions = DependencyManagerUtils.getPremanagedExclusions(node);
127         if (premanagedExclusions != null && d != null && !equals(premanagedExclusions, d.getExclusions())) {
128             buffer.append(" (exclusions managed from ")
129                     .append(premanagedExclusions)
130                     .append(")");
131         }
132 
133         Map<String, String> premanagedProperties = DependencyManagerUtils.getPremanagedProperties(node);
134         if (premanagedProperties != null && !equals(premanagedProperties, a.getProperties())) {
135             buffer.append(" (properties managed from ")
136                     .append(premanagedProperties)
137                     .append(")");
138         }
139 
140         DependencyNode winner = (DependencyNode) node.getData().get(ConflictResolver.NODE_DATA_WINNER);
141         if (winner != null) {
142             if (ArtifactIdUtils.equalsId(a, winner.getArtifact())) {
143                 buffer.append(" (nearer exists)");
144             } else {
145                 Artifact w = winner.getArtifact();
146                 buffer.append(" (conflicts with ");
147                 if (ArtifactIdUtils.toVersionlessId(a).equals(ArtifactIdUtils.toVersionlessId(w))) {
148                     buffer.append(w.getVersion());
149                 } else {
150                     buffer.append(w);
151                 }
152                 buffer.append(")");
153             }
154         }
155         return buffer.toString();
156     }
157 
158     private boolean equals(Collection<Exclusion> c1, Collection<Exclusion> c2) {
159         return c1 != null && c2 != null && c1.size() == c2.size() && c1.containsAll(c2);
160     }
161 
162     private boolean equals(Map<String, String> m1, Map<String, String> m2) {
163         return m1 != null
164                 && m2 != null
165                 && m1.size() == m2.size()
166                 && m1.entrySet().stream().allMatch(entry -> Objects.equals(m2.get(entry.getKey()), entry.getValue()));
167     }
168 }