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.internal.impl.scope;
20  
21  import org.eclipse.aether.collection.DependencyGraphTransformationContext;
22  import org.eclipse.aether.collection.DependencyGraphTransformer;
23  import org.eclipse.aether.graph.Dependency;
24  import org.eclipse.aether.graph.DependencyNode;
25  import org.eclipse.aether.impl.scope.BuildScope;
26  import org.eclipse.aether.impl.scope.InternalScopeManager;
27  import org.eclipse.aether.scope.DependencyScope;
28  
29  import static java.util.Objects.requireNonNull;
30  
31  /**
32   * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by
33   * appending the buildpath type to which the node belongs. For instance, a compile-time project dependency will be
34   * assigned the request context "project/compile".
35   * <p>
36   * This class also "bridges" between {@link DependencyScope} and Resolver that uses plain string labels for scopes.
37   *
38   * @see DependencyNode#getRequestContext()
39   */
40  public final class ManagedDependencyContextRefiner implements DependencyGraphTransformer {
41      private final InternalScopeManager scopeManager;
42  
43      public ManagedDependencyContextRefiner(InternalScopeManager scopeManager) {
44          this.scopeManager = requireNonNull(scopeManager, "scopeManager");
45      }
46  
47      @Override
48      public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context) {
49          requireNonNull(node, "node cannot be null");
50          requireNonNull(context, "context cannot be null");
51          String ctx = node.getRequestContext();
52  
53          if ("project".equals(ctx)) {
54              String scope = getBuildpathScope(node);
55              if (scope != null) {
56                  ctx += '/' + scope;
57                  node.setRequestContext(ctx);
58              }
59          }
60  
61          for (DependencyNode child : node.getChildren()) {
62              transformGraph(child, context);
63          }
64  
65          return node;
66      }
67  
68      private String getBuildpathScope(DependencyNode node) {
69          Dependency dependency = node.getDependency();
70          if (dependency == null) {
71              return null;
72          }
73  
74          return scopeManager
75                  .getDependencyScope(dependency.getScope())
76                  .flatMap(s ->
77                          scopeManager.getDependencyScopeMainProjectBuildScope(s).map(BuildScope::getId))
78                  .orElse(null);
79      }
80  }