View Javadoc
1   package org.apache.maven.resolver.internal.ant;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  import org.apache.maven.model.Model;
29  import org.apache.maven.resolver.internal.ant.types.Pom;
30  import org.eclipse.aether.artifact.Artifact;
31  import org.eclipse.aether.artifact.DefaultArtifact;
32  import org.eclipse.aether.repository.WorkspaceReader;
33  import org.eclipse.aether.repository.WorkspaceRepository;
34  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
35  
36  /**
37   * Workspace reader caching available POMs and artifacts for ant builds.
38   * <p>
39   * &lt;pom&gt; elements are cached if they are defined by the 'file'-attribute, as they reference a backing pom.xml file that
40   * can be used for resolution with Aether. &lt;artifact&gt; elements are cached if they directly define a 'pom'-attribute
41   * or child. The POM may be file-based or in-memory.
42   */
43  public class ProjectWorkspaceReader
44      implements WorkspaceReader
45  {
46  
47      private static volatile ProjectWorkspaceReader instance;
48  
49      private static final Object LOCK = new Object();
50  
51      private Map<String, Artifact> artifacts = new ConcurrentHashMap<String, Artifact>();
52  
53      public void addPom( Pom pom )
54      {
55          if ( pom.getFile() != null )
56          {
57              Model model = pom.getModel( pom );
58              Artifact aetherArtifact =
59                  new DefaultArtifact( model.getGroupId(), model.getArtifactId(), null, "pom", model.getVersion() );
60              aetherArtifact = aetherArtifact.setFile( pom.getFile() );
61              String coords = coords( aetherArtifact );
62              artifacts.put( coords, aetherArtifact );
63          }
64      }
65  
66      public void addArtifact( org.apache.maven.resolver.internal.ant.types.Artifact artifact )
67      {
68          if ( artifact.getPom() != null )
69          {
70              Pom pom = artifact.getPom();
71              Artifact aetherArtifact;
72              if ( pom.getFile() != null )
73              {
74                  Model model = pom.getModel( pom );
75                  aetherArtifact =
76                      new DefaultArtifact( model.getGroupId(), model.getArtifactId(), artifact.getClassifier(),
77                                           artifact.getType(), model.getVersion() );
78              }
79              else
80              {
81                  aetherArtifact =
82                      new DefaultArtifact( pom.getGroupId(), pom.getArtifactId(), artifact.getClassifier(),
83                                           artifact.getType(), pom.getVersion() );
84              }
85              aetherArtifact = aetherArtifact.setFile( artifact.getFile() );
86  
87              String coords = coords( aetherArtifact );
88              artifacts.put( coords, aetherArtifact );
89          }
90      }
91  
92      private String coords( Artifact artifact )
93      {
94          return ArtifactIdUtils.toId( artifact );
95      }
96  
97      public WorkspaceRepository getRepository()
98      {
99          return new WorkspaceRepository( "ant" );
100     }
101 
102     public File findArtifact( Artifact artifact )
103     {
104         artifact = artifacts.get( coords( artifact ) );
105         return ( artifact != null ) ? artifact.getFile() : null;
106     }
107 
108     public List<String> findVersions( Artifact artifact )
109     {
110         List<String> versions = new ArrayList<String>();
111         for ( Artifact art : artifacts.values() )
112         {
113             if ( ArtifactIdUtils.equalsVersionlessId( artifact, art ) )
114             {
115                 versions.add( art.getVersion() );
116             }
117         }
118         return versions;
119     }
120 
121     ProjectWorkspaceReader()
122     {
123     }
124 
125     public static ProjectWorkspaceReader getInstance()
126     {
127         if ( instance == null )
128         {
129             synchronized ( LOCK )
130             {
131                 if ( instance == null )
132                 {
133                     instance = new ProjectWorkspaceReader();
134                 }
135             }
136         }
137         return instance;
138     }
139 
140     static void dropInstance()
141     {
142         instance = null;
143     }
144 }