001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl.scope; 020 021import org.eclipse.aether.collection.DependencyGraphTransformationContext; 022import org.eclipse.aether.collection.DependencyGraphTransformer; 023import org.eclipse.aether.graph.Dependency; 024import org.eclipse.aether.graph.DependencyNode; 025import org.eclipse.aether.impl.scope.BuildScope; 026import org.eclipse.aether.impl.scope.InternalScopeManager; 027import org.eclipse.aether.scope.DependencyScope; 028 029import static java.util.Objects.requireNonNull; 030 031/** 032 * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by 033 * appending the buildpath type to which the node belongs. For instance, a compile-time project dependency will be 034 * assigned the request context "project/compile". 035 * <p> 036 * This class also "bridges" between {@link DependencyScope} and Resolver that uses plain string labels for scopes. 037 * 038 * @see DependencyNode#getRequestContext() 039 */ 040public final class ManagedDependencyContextRefiner implements DependencyGraphTransformer { 041 private final InternalScopeManager scopeManager; 042 043 public ManagedDependencyContextRefiner(InternalScopeManager scopeManager) { 044 this.scopeManager = requireNonNull(scopeManager, "scopeManager"); 045 } 046 047 @Override 048 public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context) { 049 requireNonNull(node, "node cannot be null"); 050 requireNonNull(context, "context cannot be null"); 051 String ctx = node.getRequestContext(); 052 053 if ("project".equals(ctx)) { 054 String scope = getBuildpathScope(node); 055 if (scope != null) { 056 ctx += '/' + scope; 057 node.setRequestContext(ctx); 058 } 059 } 060 061 for (DependencyNode child : node.getChildren()) { 062 transformGraph(child, context); 063 } 064 065 return node; 066 } 067 068 private String getBuildpathScope(DependencyNode node) { 069 Dependency dependency = node.getDependency(); 070 if (dependency == null) { 071 return null; 072 } 073 074 return scopeManager 075 .getDependencyScope(dependency.getScope()) 076 .flatMap(s -> 077 scopeManager.getDependencyScopeMainProjectBuildScope(s).map(BuildScope::getId)) 078 .orElse(null); 079 } 080}