1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.LinkedHashSet;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Properties;
29 import java.util.Set;
30
31 import org.apache.maven.model.DistributionManagement;
32 import org.apache.maven.model.Model;
33 import org.apache.maven.model.Relocation;
34 import org.apache.maven.model.building.DefaultModelBuilderFactory;
35 import org.apache.maven.model.building.DefaultModelBuildingRequest;
36 import org.apache.maven.model.building.FileModelSource;
37 import org.apache.maven.model.building.ModelBuilder;
38 import org.apache.maven.model.building.ModelBuildingException;
39 import org.apache.maven.model.building.ModelBuildingRequest;
40 import org.apache.maven.model.building.ModelProblem;
41 import org.apache.maven.model.resolution.UnresolvableModelException;
42 import org.eclipse.aether.RepositoryEvent;
43 import org.eclipse.aether.RepositoryEvent.EventType;
44 import org.eclipse.aether.RepositoryException;
45 import org.eclipse.aether.RepositorySystemSession;
46 import org.eclipse.aether.RequestTrace;
47 import org.eclipse.aether.artifact.Artifact;
48 import org.eclipse.aether.impl.ArtifactDescriptorReader;
49 import org.eclipse.aether.impl.ArtifactResolver;
50 import org.eclipse.aether.impl.RemoteRepositoryManager;
51 import org.eclipse.aether.impl.RepositoryEventDispatcher;
52 import org.eclipse.aether.impl.VersionRangeResolver;
53 import org.eclipse.aether.impl.VersionResolver;
54 import org.eclipse.aether.repository.WorkspaceReader;
55 import org.eclipse.aether.repository.WorkspaceRepository;
56 import org.eclipse.aether.resolution.ArtifactDescriptorException;
57 import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
58 import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest;
59 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
60 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
61 import org.eclipse.aether.resolution.ArtifactRequest;
62 import org.eclipse.aether.resolution.ArtifactResolutionException;
63 import org.eclipse.aether.resolution.ArtifactResult;
64 import org.eclipse.aether.resolution.VersionRequest;
65 import org.eclipse.aether.resolution.VersionResolutionException;
66 import org.eclipse.aether.resolution.VersionResult;
67 import org.eclipse.aether.spi.locator.Service;
68 import org.eclipse.aether.spi.locator.ServiceLocator;
69 import org.eclipse.aether.transfer.ArtifactNotFoundException;
70
71
72
73
74 @Named
75 @Singleton
76 public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader, Service {
77 private RemoteRepositoryManager remoteRepositoryManager;
78
79 private VersionResolver versionResolver;
80
81 private VersionRangeResolver versionRangeResolver;
82
83 private ArtifactResolver artifactResolver;
84
85 private RepositoryEventDispatcher repositoryEventDispatcher;
86
87 private ModelBuilder modelBuilder;
88
89 private ModelCacheFactory modelCacheFactory;
90
91 private final ArtifactDescriptorReaderDelegate artifactDescriptorReaderDelegate =
92 new ArtifactDescriptorReaderDelegate();
93
94 public DefaultArtifactDescriptorReader() {
95
96 }
97
98 @Inject
99 DefaultArtifactDescriptorReader(
100 RemoteRepositoryManager remoteRepositoryManager,
101 VersionResolver versionResolver,
102 VersionRangeResolver versionRangeResolver,
103 ArtifactResolver artifactResolver,
104 ModelBuilder modelBuilder,
105 RepositoryEventDispatcher repositoryEventDispatcher,
106 ModelCacheFactory modelCacheFactory) {
107 setRemoteRepositoryManager(remoteRepositoryManager);
108 setVersionResolver(versionResolver);
109 setVersionRangeResolver(versionRangeResolver);
110 setArtifactResolver(artifactResolver);
111 setModelBuilder(modelBuilder);
112 setRepositoryEventDispatcher(repositoryEventDispatcher);
113 setModelCacheFactory(modelCacheFactory);
114 }
115
116 public void initService(ServiceLocator locator) {
117 setRemoteRepositoryManager(locator.getService(RemoteRepositoryManager.class));
118 setVersionResolver(locator.getService(VersionResolver.class));
119 setVersionRangeResolver(locator.getService(VersionRangeResolver.class));
120 setArtifactResolver(locator.getService(ArtifactResolver.class));
121 modelBuilder = locator.getService(ModelBuilder.class);
122 if (modelBuilder == null) {
123 setModelBuilder(new DefaultModelBuilderFactory().newInstance());
124 }
125 setRepositoryEventDispatcher(locator.getService(RepositoryEventDispatcher.class));
126 setModelCacheFactory(locator.getService(ModelCacheFactory.class));
127 }
128
129 public DefaultArtifactDescriptorReader setRemoteRepositoryManager(RemoteRepositoryManager remoteRepositoryManager) {
130 this.remoteRepositoryManager =
131 Objects.requireNonNull(remoteRepositoryManager, "remoteRepositoryManager cannot be null");
132 return this;
133 }
134
135 public DefaultArtifactDescriptorReader setVersionResolver(VersionResolver versionResolver) {
136 this.versionResolver = Objects.requireNonNull(versionResolver, "versionResolver cannot be null");
137 return this;
138 }
139
140
141 public DefaultArtifactDescriptorReader setVersionRangeResolver(VersionRangeResolver versionRangeResolver) {
142 this.versionRangeResolver = Objects.requireNonNull(versionRangeResolver, "versionRangeResolver cannot be null");
143 return this;
144 }
145
146 public DefaultArtifactDescriptorReader setArtifactResolver(ArtifactResolver artifactResolver) {
147 this.artifactResolver = Objects.requireNonNull(artifactResolver, "artifactResolver cannot be null");
148 return this;
149 }
150
151 public DefaultArtifactDescriptorReader setRepositoryEventDispatcher(
152 RepositoryEventDispatcher repositoryEventDispatcher) {
153 this.repositoryEventDispatcher =
154 Objects.requireNonNull(repositoryEventDispatcher, "repositoryEventDispatcher cannot be null");
155 return this;
156 }
157
158 public DefaultArtifactDescriptorReader setModelBuilder(ModelBuilder modelBuilder) {
159 this.modelBuilder = Objects.requireNonNull(modelBuilder, "modelBuilder cannot be null");
160 return this;
161 }
162
163 public DefaultArtifactDescriptorReader setModelCacheFactory(ModelCacheFactory modelCacheFactory) {
164 this.modelCacheFactory = Objects.requireNonNull(modelCacheFactory, "modelCacheFactory cannot be null");
165 return this;
166 }
167
168 public ArtifactDescriptorResult readArtifactDescriptor(
169 RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
170 ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
171
172 Model model = loadPom(session, request, result);
173 if (model != null) {
174 Map<String, Object> config = session.getConfigProperties();
175 ArtifactDescriptorReaderDelegate delegate =
176 (ArtifactDescriptorReaderDelegate) config.get(ArtifactDescriptorReaderDelegate.class.getName());
177
178 if (delegate == null) {
179 delegate = artifactDescriptorReaderDelegate;
180 }
181
182 delegate.populateResult(session, result, model);
183 }
184
185 return result;
186 }
187
188 private Model loadPom(
189 RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
190 throws ArtifactDescriptorException {
191 RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
192
193 Set<String> visited = new LinkedHashSet<>();
194 for (Artifact a = request.getArtifact(); ; ) {
195 Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact(a);
196 try {
197 VersionRequest versionRequest =
198 new VersionRequest(a, request.getRepositories(), request.getRequestContext());
199 versionRequest.setTrace(trace);
200 VersionResult versionResult = versionResolver.resolveVersion(session, versionRequest);
201
202 a = a.setVersion(versionResult.getVersion());
203
204 versionRequest =
205 new VersionRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
206 versionRequest.setTrace(trace);
207 versionResult = versionResolver.resolveVersion(session, versionRequest);
208
209 pomArtifact = pomArtifact.setVersion(versionResult.getVersion());
210 } catch (VersionResolutionException e) {
211 result.addException(e);
212 throw new ArtifactDescriptorException(result);
213 }
214
215 if (!visited.add(a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion())) {
216 RepositoryException exception =
217 new RepositoryException("Artifact relocations form a cycle: " + visited);
218 invalidDescriptor(session, trace, a, exception);
219 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
220 return null;
221 }
222 result.addException(exception);
223 throw new ArtifactDescriptorException(result);
224 }
225
226 ArtifactResult resolveResult;
227 try {
228 ArtifactRequest resolveRequest =
229 new ArtifactRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
230 resolveRequest.setTrace(trace);
231 resolveResult = artifactResolver.resolveArtifact(session, resolveRequest);
232 pomArtifact = resolveResult.getArtifact();
233 result.setRepository(resolveResult.getRepository());
234 } catch (ArtifactResolutionException e) {
235 if (e.getCause() instanceof ArtifactNotFoundException) {
236 missingDescriptor(session, trace, a, (Exception) e.getCause());
237 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_MISSING) != 0) {
238 return null;
239 }
240 }
241 result.addException(e);
242 throw new ArtifactDescriptorException(result);
243 }
244
245 Model model;
246
247
248 final WorkspaceReader workspace = session.getWorkspaceReader();
249 if (workspace instanceof MavenWorkspaceReader) {
250 model = ((MavenWorkspaceReader) workspace).findModel(pomArtifact);
251 if (model != null) {
252 return model;
253 }
254 }
255
256 try {
257 ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
258 modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
259 modelRequest.setProcessPlugins(false);
260 modelRequest.setTwoPhaseBuilding(false);
261
262
263 modelRequest.setSystemProperties(
264 toProperties(session.getUserProperties(), session.getSystemProperties()));
265 modelRequest.setUserProperties(new Properties());
266 modelRequest.setModelCache(modelCacheFactory.createCache(session));
267 modelRequest.setModelResolver(new DefaultModelResolver(
268 session,
269 trace.newChild(modelRequest),
270 request.getRequestContext(),
271 artifactResolver,
272 versionRangeResolver,
273 remoteRepositoryManager,
274 request.getRepositories()));
275 if (resolveResult.getRepository() instanceof WorkspaceRepository) {
276 modelRequest.setPomFile(pomArtifact.getFile());
277 } else {
278 modelRequest.setModelSource(new FileModelSource(pomArtifact.getFile()));
279 }
280
281 model = modelBuilder.build(modelRequest).getEffectiveModel();
282 } catch (ModelBuildingException e) {
283 for (ModelProblem problem : e.getProblems()) {
284 if (problem.getException() instanceof UnresolvableModelException) {
285 result.addException(problem.getException());
286 throw new ArtifactDescriptorException(result);
287 }
288 }
289 invalidDescriptor(session, trace, a, e);
290 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
291 return null;
292 }
293 result.addException(e);
294 throw new ArtifactDescriptorException(result);
295 }
296
297 Relocation relocation = getRelocation(model);
298
299 if (relocation != null) {
300 result.addRelocation(a);
301 a = new RelocatedArtifact(
302 a,
303 relocation.getGroupId(),
304 relocation.getArtifactId(),
305 relocation.getVersion(),
306 relocation.getMessage());
307 result.setArtifact(a);
308 } else {
309 return model;
310 }
311 }
312 }
313
314 private Properties toProperties(Map<String, String> dominant, Map<String, String> recessive) {
315 Properties props = new Properties();
316 if (recessive != null) {
317 props.putAll(recessive);
318 }
319 if (dominant != null) {
320 props.putAll(dominant);
321 }
322 return props;
323 }
324
325 private Relocation getRelocation(Model model) {
326 Relocation relocation = null;
327 DistributionManagement distMgmt = model.getDistributionManagement();
328 if (distMgmt != null) {
329 relocation = distMgmt.getRelocation();
330 }
331 return relocation;
332 }
333
334 private void missingDescriptor(
335 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
336 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_MISSING);
337 event.setTrace(trace);
338 event.setArtifact(artifact);
339 event.setException(exception);
340
341 repositoryEventDispatcher.dispatch(event.build());
342 }
343
344 private void invalidDescriptor(
345 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
346 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_INVALID);
347 event.setTrace(trace);
348 event.setArtifact(artifact);
349 event.setException(exception);
350
351 repositoryEventDispatcher.dispatch(event.build());
352 }
353
354 private int getPolicy(RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request) {
355 ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
356 if (policy == null) {
357 return ArtifactDescriptorPolicy.STRICT;
358 }
359 return policy.getPolicy(session, new ArtifactDescriptorPolicyRequest(a, request.getRequestContext()));
360 }
361 }