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.Type;
30  import org.apache.maven.api.services.TypeRegistry;
31  import org.apache.maven.artifact.handler.ArtifactHandler;
32  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
33  import org.apache.maven.eventspy.AbstractEventSpy;
34  import org.apache.maven.execution.ExecutionEvent;
35  
36  import static java.util.Objects.requireNonNull;
37  
38  /**
39   */
40  @Named
41  @Singleton
42  public class DefaultArtifactHandlerManager extends AbstractEventSpy implements ArtifactHandlerManager {
43      private final TypeRegistry typeRegistry;
44  
45      private final ConcurrentHashMap<String, ArtifactHandler> allHandlers;
46  
47      @Inject
48      public DefaultArtifactHandlerManager(TypeRegistry typeRegistry) {
49          this.typeRegistry = requireNonNull(typeRegistry, "null typeRegistry");
50          this.allHandlers = new ConcurrentHashMap<>();
51      }
52  
53      @Override
54      public void onEvent(Object event) {
55          if (event instanceof ExecutionEvent) {
56              ExecutionEvent executionEvent = (ExecutionEvent) event;
57              if (executionEvent.getType() == ExecutionEvent.Type.SessionEnded) {
58                  allHandlers.clear();
59              }
60          }
61      }
62  
63      public ArtifactHandler getArtifactHandler(String id) {
64          return allHandlers.computeIfAbsent(id, k -> {
65              Type type = typeRegistry.getType(id);
66              return new DefaultArtifactHandler(
67                      id,
68                      type.getExtension(),
69                      type.getClassifier(),
70                      null,
71                      null,
72                      type.isIncludesDependencies(),
73                      type.getLanguage(),
74                      type.isAddedToClassPath()); // TODO: watch out for module path
75          });
76      }
77  
78      public void addHandlers(Map<String, ArtifactHandler> handlers) {
79          throw new UnsupportedOperationException("Adding handlers programmatically is not supported anymore");
80      }
81  
82      @Deprecated
83      public Set<String> getHandlerTypes() {
84          throw new UnsupportedOperationException("Querying handlers programmatically is not supported anymore");
85      }
86  }