1 package org.apache.maven.repository.internal;
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.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.apache.maven.model.Repository;
29 import org.apache.maven.model.building.FileModelSource;
30 import org.apache.maven.model.building.ModelSource;
31 import org.apache.maven.model.resolution.InvalidRepositoryException;
32 import org.apache.maven.model.resolution.ModelResolver;
33 import org.apache.maven.model.resolution.UnresolvableModelException;
34 import org.sonatype.aether.RepositorySystemSession;
35 import org.sonatype.aether.RequestTrace;
36 import org.sonatype.aether.artifact.Artifact;
37 import org.sonatype.aether.impl.ArtifactResolver;
38 import org.sonatype.aether.impl.RemoteRepositoryManager;
39 import org.sonatype.aether.repository.RemoteRepository;
40 import org.sonatype.aether.resolution.ArtifactRequest;
41 import org.sonatype.aether.resolution.ArtifactResolutionException;
42 import org.sonatype.aether.util.artifact.DefaultArtifact;
43
44
45
46
47
48
49
50
51 class DefaultModelResolver
52 implements ModelResolver
53 {
54
55 private final RepositorySystemSession session;
56
57 private final RequestTrace trace;
58
59 private final String context;
60
61 private List<RemoteRepository> repositories;
62
63 private final ArtifactResolver resolver;
64
65 private final RemoteRepositoryManager remoteRepositoryManager;
66
67 private final Set<String> repositoryIds;
68
69 public DefaultModelResolver( RepositorySystemSession session, RequestTrace trace, String context,
70 ArtifactResolver resolver, RemoteRepositoryManager remoteRepositoryManager,
71 List<RemoteRepository> repositories )
72 {
73 this.session = session;
74 this.trace = trace;
75 this.context = context;
76 this.resolver = resolver;
77 this.remoteRepositoryManager = remoteRepositoryManager;
78 this.repositories = repositories;
79 this.repositoryIds = new HashSet<String>();
80 }
81
82 private DefaultModelResolver( DefaultModelResolver original )
83 {
84 this.session = original.session;
85 this.trace = original.trace;
86 this.context = original.context;
87 this.resolver = original.resolver;
88 this.remoteRepositoryManager = original.remoteRepositoryManager;
89 this.repositories = original.repositories;
90 this.repositoryIds = new HashSet<String>( original.repositoryIds );
91 }
92
93 public void addRepository( Repository repository )
94 throws InvalidRepositoryException
95 {
96 if ( !repositoryIds.add( repository.getId() ) )
97 {
98 return;
99 }
100
101 List<RemoteRepository> newRepositories =
102 Collections.singletonList( ArtifactDescriptorUtils.toRemoteRepository( repository ) );
103
104 this.repositories =
105 remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true );
106 }
107
108 public ModelResolver newCopy()
109 {
110 return new DefaultModelResolver( this );
111 }
112
113 public ModelSource resolveModel( String groupId, String artifactId, String version )
114 throws UnresolvableModelException
115 {
116 Artifact pomArtifact = new DefaultArtifact( groupId, artifactId, "", "pom", version );
117
118 try
119 {
120 ArtifactRequest request = new ArtifactRequest( pomArtifact, repositories, context );
121 request.setTrace( trace );
122 pomArtifact = resolver.resolveArtifact( session, request ).getArtifact();
123 }
124 catch ( ArtifactResolutionException e )
125 {
126 throw new UnresolvableModelException( e.getMessage(), groupId, artifactId, version, e );
127 }
128
129 File pomFile = pomArtifact.getFile();
130
131 return new FileModelSource( pomFile );
132 }
133
134 }