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