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.artifact.handler.manager;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import org.apache.maven.api.JavaPathType;
30  import org.apache.maven.api.Type;
31  import org.apache.maven.api.services.TypeRegistry;
32  import org.apache.maven.artifact.handler.ArtifactHandler;
33  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
34  import org.apache.maven.eventspy.AbstractEventSpy;
35  import org.apache.maven.execution.ExecutionEvent;
36  
37  import static java.util.Objects.requireNonNull;
38  
39  /**
40   */
41  @Named
42  @Singleton
43  public class DefaultArtifactHandlerManager extends AbstractEventSpy implements ArtifactHandlerManager {
44      private final TypeRegistry typeRegistry;
45  
46      private final ConcurrentHashMap<String, ArtifactHandler> allHandlers;
47  
48      @Inject
49      public DefaultArtifactHandlerManager(TypeRegistry typeRegistry) {
50          this.typeRegistry = requireNonNull(typeRegistry, "null typeRegistry");
51          this.allHandlers = new ConcurrentHashMap<>();
52      }
53  
54      @Override
55      public void onEvent(Object event) {
56          if (event instanceof ExecutionEvent) {
57              ExecutionEvent executionEvent = (ExecutionEvent) event;
58              if (executionEvent.getType() == ExecutionEvent.Type.SessionEnded) {
59                  allHandlers.clear();
60              }
61          }
62      }
63  
64      @Override
65      public ArtifactHandler getArtifactHandler(String id) {
66          return allHandlers.computeIfAbsent(id, k -> {
67              Type type = typeRegistry.require(id);
68              return new DefaultArtifactHandler(
69                      id,
70                      type.getExtension(),
71                      type.getClassifier(),
72                      null,
73                      null,
74                      type.isIncludesDependencies(),
75                      type.getLanguage().id(),
76                      type.getPathTypes().contains(JavaPathType.CLASSES));
77              // TODO: watch out for module path
78          });
79  
80          // Note: here, type decides is artifact added to "build path" (for example during resolution)
81          // and "build path" is intermediate data that is used to create actual Java classpath/modulepath
82          // but to create those, proper filtering should happen via Type properties.
83      }
84  
85      @Override
86      public void addHandlers(Map<String, ArtifactHandler> handlers) {
87          throw new UnsupportedOperationException("Adding handlers programmatically is not supported anymore");
88      }
89  
90      @Deprecated
91      public Set<String> getHandlerTypes() {
92          throw new UnsupportedOperationException("Querying handlers programmatically is not supported anymore");
93      }
94  }