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.resolver.internal.ant;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  import org.apache.maven.model.Model;
28  import org.apache.maven.resolver.internal.ant.types.Pom;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.artifact.DefaultArtifact;
31  import org.eclipse.aether.repository.WorkspaceReader;
32  import org.eclipse.aether.repository.WorkspaceRepository;
33  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
34  
35  /**
36   * Workspace reader caching available POMs and artifacts for ant builds.
37   * <p>
38   * &lt;pom&gt; elements are cached if they are defined by the 'file'-attribute, as they reference a backing pom.xml file that
39   * can be used for resolution with Aether. &lt;artifact&gt; elements are cached if they directly define a 'pom'-attribute
40   * or child. The POM may be file-based or in-memory.
41   */
42  public class ProjectWorkspaceReader implements WorkspaceReader {
43  
44      private static volatile ProjectWorkspaceReader instance;
45  
46      private static final Object LOCK = new Object();
47  
48      private Map<String, Artifact> artifacts = new ConcurrentHashMap<>();
49  
50      public void addPom(Pom pom) {
51          if (pom.getFile() != null) {
52              Model model = pom.getModel(pom);
53              Artifact aetherArtifact =
54                      new DefaultArtifact(model.getGroupId(), model.getArtifactId(), null, "pom", model.getVersion());
55              aetherArtifact = aetherArtifact.setFile(pom.getFile());
56              String coords = coords(aetherArtifact);
57              artifacts.put(coords, aetherArtifact);
58          }
59      }
60  
61      public void addArtifact(org.apache.maven.resolver.internal.ant.types.Artifact artifact) {
62          if (artifact.getPom() != null) {
63              Pom pom = artifact.getPom();
64              Artifact aetherArtifact;
65              if (pom.getFile() != null) {
66                  Model model = pom.getModel(pom);
67                  aetherArtifact = new DefaultArtifact(
68                          model.getGroupId(),
69                          model.getArtifactId(),
70                          artifact.getClassifier(),
71                          artifact.getType(),
72                          model.getVersion());
73              } else {
74                  aetherArtifact = new DefaultArtifact(
75                          pom.getGroupId(),
76                          pom.getArtifactId(),
77                          artifact.getClassifier(),
78                          artifact.getType(),
79                          pom.getVersion());
80              }
81              aetherArtifact = aetherArtifact.setFile(artifact.getFile());
82  
83              String coords = coords(aetherArtifact);
84              artifacts.put(coords, aetherArtifact);
85          }
86      }
87  
88      private String coords(Artifact artifact) {
89          return ArtifactIdUtils.toId(artifact);
90      }
91  
92      public WorkspaceRepository getRepository() {
93          return new WorkspaceRepository("ant");
94      }
95  
96      public File findArtifact(Artifact artifact) {
97          artifact = artifacts.get(coords(artifact));
98          return (artifact != null) ? artifact.getFile() : null;
99      }
100 
101     public List<String> findVersions(Artifact artifact) {
102         List<String> versions = new ArrayList<>();
103         for (Artifact art : artifacts.values()) {
104             if (ArtifactIdUtils.equalsVersionlessId(artifact, art)) {
105                 versions.add(art.getVersion());
106             }
107         }
108         return versions;
109     }
110 
111     ProjectWorkspaceReader() {}
112 
113     public static ProjectWorkspaceReader getInstance() {
114         if (instance == null) {
115             synchronized (LOCK) {
116                 if (instance == null) {
117                     instance = new ProjectWorkspaceReader();
118                 }
119             }
120         }
121         return instance;
122     }
123 
124     static void dropInstance() {
125         instance = null;
126     }
127 }