1 package org.apache.maven.resolver.internal.ant;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
41
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 }