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