1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.stream.Collectors;
30
31 import org.apache.maven.api.JavaPathType;
32 import org.apache.maven.api.Type;
33 import org.apache.maven.api.annotations.Nonnull;
34 import org.apache.maven.api.services.LanguageRegistry;
35 import org.apache.maven.api.services.TypeRegistry;
36 import org.apache.maven.api.spi.TypeProvider;
37 import org.apache.maven.artifact.handler.ArtifactHandler;
38 import org.apache.maven.artifact.handler.manager.LegacyArtifactHandlerManager;
39 import org.apache.maven.eventspy.AbstractEventSpy;
40 import org.apache.maven.execution.ExecutionEvent;
41 import org.apache.maven.internal.impl.resolver.type.DefaultType;
42
43 import static java.util.function.Function.identity;
44 import static org.apache.maven.internal.impl.Utils.nonNull;
45
46 @Named
47 @Singleton
48 public class DefaultTypeRegistry extends AbstractEventSpy implements TypeRegistry {
49 private final Map<String, Type> types;
50
51 private final LanguageRegistry languageRegistry;
52
53 private final ConcurrentHashMap<String, Type> usedTypes;
54
55 private final LegacyArtifactHandlerManager manager;
56
57 @Inject
58 public DefaultTypeRegistry(
59 List<TypeProvider> providers, LanguageRegistry languageRegistry, LegacyArtifactHandlerManager manager) {
60 this.types = nonNull(providers, "providers").stream()
61 .flatMap(p -> p.provides().stream())
62 .collect(Collectors.toMap(Type::id, identity()));
63 this.languageRegistry = nonNull(languageRegistry, "languageRegistry");
64 this.usedTypes = new ConcurrentHashMap<>();
65 this.manager = nonNull(manager, "artifactHandlerManager");
66 }
67
68 @Override
69 public void onEvent(Object event) {
70 if (event instanceof ExecutionEvent) {
71 ExecutionEvent executionEvent = (ExecutionEvent) event;
72 if (executionEvent.getType() == ExecutionEvent.Type.SessionEnded) {
73 usedTypes.clear();
74 }
75 }
76 }
77
78 @Override
79 public Optional<Type> lookup(String id) {
80 return Optional.of(require(id));
81 }
82
83 @Override
84 @Nonnull
85 public Type require(String id) {
86 nonNull(id, "id");
87 return usedTypes.computeIfAbsent(id, i -> {
88 Type type = types.get(id);
89 if (type == null) {
90
91 ArtifactHandler handler = manager.getArtifactHandler(id);
92 type = new DefaultType(
93 id,
94 languageRegistry.require(handler.getLanguage()),
95 handler.getExtension(),
96 handler.getClassifier(),
97 handler.isIncludesDependencies(),
98 JavaPathType.CLASSES,
99 JavaPathType.MODULES);
100 }
101 return type;
102 });
103 }
104 }