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.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                  // Copy data as the ArtifactHandler is not immutable, but Type should be.
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 }