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  
24  import java.io.File;
25  import java.nio.file.Path;
26  import java.util.Map;
27  import java.util.Optional;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.apache.maven.SessionScoped;
31  import org.apache.maven.api.Artifact;
32  import org.apache.maven.api.annotations.Nonnull;
33  import org.apache.maven.api.services.ArtifactManager;
34  import org.apache.maven.project.MavenProject;
35  
36  @Named
37  @SessionScoped
38  public class DefaultArtifactManager implements ArtifactManager {
39  
40      @Nonnull
41      private final DefaultSession session;
42  
43      private final Map<String, Path> paths = new ConcurrentHashMap<>();
44  
45      @Inject
46      public DefaultArtifactManager(@Nonnull DefaultSession session) {
47          this.session = session;
48      }
49  
50      @Nonnull
51      @Override
52      public Optional<Path> getPath(@Nonnull Artifact artifact) {
53          String id = id(artifact);
54          if (session.getMavenSession().getAllProjects() != null) {
55              for (MavenProject project : session.getMavenSession().getAllProjects()) {
56                  if (id.equals(id(project.getArtifact()))
57                          && project.getArtifact().getFile() != null) {
58                      return Optional.of(project.getArtifact().getFile().toPath());
59                  }
60              }
61          }
62          Path path = paths.get(id);
63          if (path == null && artifact instanceof DefaultArtifact) {
64              File file = ((DefaultArtifact) artifact).getArtifact().getFile();
65              if (file != null) {
66                  path = file.toPath();
67              }
68          }
69          return Optional.ofNullable(path);
70      }
71  
72      @Override
73      public void setPath(@Nonnull Artifact artifact, Path path) {
74          String id = id(artifact);
75          if (session.getMavenSession().getAllProjects() != null) {
76              for (MavenProject project : session.getMavenSession().getAllProjects()) {
77                  if (id.equals(id(project.getArtifact()))) {
78                      project.getArtifact().setFile(path != null ? path.toFile() : null);
79                      break;
80                  }
81              }
82          }
83          if (path == null) {
84              paths.remove(id);
85          } else {
86              paths.put(id, path);
87          }
88      }
89  
90      private String id(org.apache.maven.artifact.Artifact artifact) {
91          return artifact.getGroupId()
92                  + ":" + artifact.getArtifactId()
93                  + ":" + artifact.getType()
94                  + (artifact.getClassifier() == null || artifact.getClassifier().isEmpty()
95                          ? ""
96                          : ":" + artifact.getClassifier())
97                  + ":" + artifact.getVersion();
98      }
99  
100     private String id(Artifact artifact) {
101         return artifact.key();
102     }
103 }