|
| 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.apache.maven.repository.internal.type; |
| 20 | + |
| 21 | +import java.util.ArrayDeque; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.apache.maven.api.Type; |
| 28 | +import org.eclipse.aether.artifact.Artifact; |
| 29 | +import org.eclipse.aether.artifact.ArtifactProperties; |
| 30 | +import org.eclipse.aether.artifact.ArtifactType; |
| 31 | +import org.eclipse.aether.artifact.ArtifactTypeRegistry; |
| 32 | +import org.eclipse.aether.collection.DependencyGraphTransformationContext; |
| 33 | +import org.eclipse.aether.collection.DependencyGraphTransformer; |
| 34 | +import org.eclipse.aether.graph.DependencyNode; |
| 35 | +import org.eclipse.aether.graph.DependencyVisitor; |
| 36 | +import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +import static java.util.Objects.requireNonNull; |
| 41 | + |
| 42 | +/** |
| 43 | + * Type deriver, that handles special case of "processor" type: if a dependency node is of this type, all of its |
| 44 | + * children need to be remapped to certain processor type as well, to end up on proper path type. |
| 45 | + * |
| 46 | + * @since 4.0.0 |
| 47 | + * @deprecated since 4.0.0, this is internal detail of Maven. |
| 48 | + */ |
| 49 | +@Deprecated(since = "4.0.0") |
| 50 | +public class TypeDeriver implements DependencyGraphTransformer { |
| 51 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 52 | + |
| 53 | + @Override |
| 54 | + public DependencyNode transformGraph(DependencyNode root, DependencyGraphTransformationContext context) { |
| 55 | + if (logger.isDebugEnabled()) { |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + root.accept(new DependencyGraphDumper( |
| 58 | + l -> sb.append(l).append("\n"), |
| 59 | + DependencyGraphDumper.defaultsWith( |
| 60 | + List.of(DependencyGraphDumper.artifactProperties(List.of(ArtifactProperties.TYPE)))))); |
| 61 | + logger.debug("TYPES: Before transform:\n {}", sb); |
| 62 | + } |
| 63 | + root.accept(new TypeDeriverVisitor(context.getSession().getArtifactTypeRegistry())); |
| 64 | + if (logger.isDebugEnabled()) { |
| 65 | + StringBuilder sb = new StringBuilder(); |
| 66 | + root.accept(new DependencyGraphDumper( |
| 67 | + l -> sb.append(l).append("\n"), |
| 68 | + DependencyGraphDumper.defaultsWith( |
| 69 | + List.of(DependencyGraphDumper.artifactProperties(List.of(ArtifactProperties.TYPE)))))); |
| 70 | + logger.debug("TYPES: After transform:\n {}", sb); |
| 71 | + } |
| 72 | + return root; |
| 73 | + } |
| 74 | + |
| 75 | + private static class TypeDeriverVisitor implements DependencyVisitor { |
| 76 | + private final ArtifactTypeRegistry registry; |
| 77 | + private final ArtifactType jar; |
| 78 | + private final ArtifactType classpathJar; |
| 79 | + private final ArtifactType modularJar; |
| 80 | + private final ArtifactType processor; |
| 81 | + private final ArtifactType classpathProcessor; |
| 82 | + private final ArtifactType modularProcessor; |
| 83 | + private final Set<String> needsDerive; |
| 84 | + private final ArrayDeque<ArtifactType> stack; |
| 85 | + |
| 86 | + private TypeDeriverVisitor(ArtifactTypeRegistry registry) { |
| 87 | + this.registry = requireNonNull(registry); |
| 88 | + this.jar = requireType(Type.JAR); |
| 89 | + this.classpathJar = requireType(Type.CLASSPATH_JAR); |
| 90 | + this.modularJar = requireType(Type.MODULAR_JAR); |
| 91 | + this.processor = requireType(Type.PROCESSOR); |
| 92 | + this.classpathProcessor = requireType(Type.CLASSPATH_PROCESSOR); |
| 93 | + this.modularProcessor = requireType(Type.MODULAR_PROCESSOR); |
| 94 | + this.needsDerive = Set.of(Type.PROCESSOR, Type.CLASSPATH_PROCESSOR, Type.MODULAR_PROCESSOR); |
| 95 | + this.stack = new ArrayDeque<>(); |
| 96 | + } |
| 97 | + |
| 98 | + private ArtifactType requireType(String id) { |
| 99 | + return requireNonNull(registry.get(id), "Type " + id + " not found but is required"); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean visitEnter(DependencyNode node) { |
| 104 | + ArtifactType currentType = jar; |
| 105 | + if (node.getArtifact() != null) { |
| 106 | + if (node.getArtifact().getProperties().containsKey(ArtifactProperties.TYPE)) { |
| 107 | + currentType = registry.get(node.getArtifact() |
| 108 | + .getProperty( |
| 109 | + ArtifactProperties.TYPE, node.getArtifact().getExtension())); |
| 110 | + if (currentType == null) { |
| 111 | + currentType = jar; |
| 112 | + } |
| 113 | + } |
| 114 | + if (!stack.isEmpty()) { |
| 115 | + ArtifactType parentType = stack.peek(); |
| 116 | + if (needsDerive.contains(parentType.getId())) { |
| 117 | + Artifact artifact = node.getArtifact(); |
| 118 | + Map<String, String> props = new HashMap<>(artifact.getProperties()); |
| 119 | + ArtifactType derived = derive(parentType, currentType); |
| 120 | + props.putAll(derived.getProperties()); |
| 121 | + node.setArtifact(artifact.setProperties(props)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + stack.push(currentType); |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean visitLeave(DependencyNode node) { |
| 131 | + stack.pop(); |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + private ArtifactType derive(ArtifactType parentType, ArtifactType currentType) { |
| 136 | + ArtifactType result = currentType; |
| 137 | + if (jar.getId().equals(currentType.getId())) { |
| 138 | + result = processor; |
| 139 | + } else if (classpathJar.getId().equals(currentType.getId())) { |
| 140 | + result = classpathProcessor; |
| 141 | + } else if (modularJar.getId().equals(currentType.getId())) { |
| 142 | + result = modularProcessor; |
| 143 | + } |
| 144 | + return result; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments