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 @Deprecated
95 public DefaultArtifactDescriptorReader() {
96
97 }
98
99 @Inject
100 public DefaultArtifactDescriptorReader(
101 RemoteRepositoryManager remoteRepositoryManager,
102 VersionResolver versionResolver,
103 VersionRangeResolver versionRangeResolver,
104 ArtifactResolver artifactResolver,
105 ModelBuilder modelBuilder,
106 RepositoryEventDispatcher repositoryEventDispatcher,
107 ModelCacheFactory modelCacheFactory) {
108 setRemoteRepositoryManager(remoteRepositoryManager);
109 setVersionResolver(versionResolver);
110 setVersionRangeResolver(versionRangeResolver);
111 setArtifactResolver(artifactResolver);
112 setModelBuilder(modelBuilder);
113 setRepositoryEventDispatcher(repositoryEventDispatcher);
114 setModelCacheFactory(modelCacheFactory);
115 }
116
117 @Deprecated
118 public void initService(ServiceLocator locator) {
119 setRemoteRepositoryManager(locator.getService(RemoteRepositoryManager.class));
120 setVersionResolver(locator.getService(VersionResolver.class));
121 setVersionRangeResolver(locator.getService(VersionRangeResolver.class));
122 setArtifactResolver(locator.getService(ArtifactResolver.class));
123 modelBuilder = locator.getService(ModelBuilder.class);
124 if (modelBuilder == null) {
125 setModelBuilder(new DefaultModelBuilderFactory().newInstance());
126 }
127 setRepositoryEventDispatcher(locator.getService(RepositoryEventDispatcher.class));
128 setModelCacheFactory(locator.getService(ModelCacheFactory.class));
129 }
130
131 public DefaultArtifactDescriptorReader setRemoteRepositoryManager(RemoteRepositoryManager remoteRepositoryManager) {
132 this.remoteRepositoryManager =
133 Objects.requireNonNull(remoteRepositoryManager, "remoteRepositoryManager cannot be null");
134 return this;
135 }
136
137 public DefaultArtifactDescriptorReader setVersionResolver(VersionResolver versionResolver) {
138 this.versionResolver = Objects.requireNonNull(versionResolver, "versionResolver cannot be null");
139 return this;
140 }
141
142
143 public DefaultArtifactDescriptorReader setVersionRangeResolver(VersionRangeResolver versionRangeResolver) {
144 this.versionRangeResolver = Objects.requireNonNull(versionRangeResolver, "versionRangeResolver cannot be null");
145 return this;
146 }
147
148 public DefaultArtifactDescriptorReader setArtifactResolver(ArtifactResolver artifactResolver) {
149 this.artifactResolver = Objects.requireNonNull(artifactResolver, "artifactResolver cannot be null");
150 return this;
151 }
152
153 public DefaultArtifactDescriptorReader setRepositoryEventDispatcher(
154 RepositoryEventDispatcher repositoryEventDispatcher) {
155 this.repositoryEventDispatcher =
156 Objects.requireNonNull(repositoryEventDispatcher, "repositoryEventDispatcher cannot be null");
157 return this;
158 }
159
160 public DefaultArtifactDescriptorReader setModelBuilder(ModelBuilder modelBuilder) {
161 this.modelBuilder = Objects.requireNonNull(modelBuilder, "modelBuilder cannot be null");
162 return this;
163 }
164
165 public DefaultArtifactDescriptorReader setModelCacheFactory(ModelCacheFactory modelCacheFactory) {
166 this.modelCacheFactory = Objects.requireNonNull(modelCacheFactory, "modelCacheFactory cannot be null");
167 return this;
168 }
169
170 @Override
171 public ArtifactDescriptorResult readArtifactDescriptor(
172 RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
173 ArtifactDescriptorResult result = new ArtifactDescriptorResult(request);
174
175 Model model = loadPom(session, request, result);
176 if (model != null) {
177 Map<String, Object> config = session.getConfigProperties();
178 ArtifactDescriptorReaderDelegate delegate =
179 (ArtifactDescriptorReaderDelegate) config.get(ArtifactDescriptorReaderDelegate.class.getName());
180
181 if (delegate == null) {
182 delegate = artifactDescriptorReaderDelegate;
183 }
184
185 delegate.populateResult(session, result, model);
186 }
187
188 return result;
189 }
190
191 private Model loadPom(
192 RepositorySystemSession session, ArtifactDescriptorRequest request, ArtifactDescriptorResult result)
193 throws ArtifactDescriptorException {
194 RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
195
196 Set<String> visited = new LinkedHashSet<>();
197 for (Artifact a = request.getArtifact(); ; ) {
198 Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact(a);
199 try {
200 VersionRequest versionRequest =
201 new VersionRequest(a, request.getRepositories(), request.getRequestContext());
202 versionRequest.setTrace(trace);
203 VersionResult versionResult = versionResolver.resolveVersion(session, versionRequest);
204
205 a = a.setVersion(versionResult.getVersion());
206
207 versionRequest =
208 new VersionRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
209 versionRequest.setTrace(trace);
210 versionResult = versionResolver.resolveVersion(session, versionRequest);
211
212 pomArtifact = pomArtifact.setVersion(versionResult.getVersion());
213 } catch (VersionResolutionException e) {
214 result.addException(e);
215 throw new ArtifactDescriptorException(result);
216 }
217
218 if (!visited.add(a.getGroupId() + ':' + a.getArtifactId() + ':' + a.getBaseVersion())) {
219 RepositoryException exception =
220 new RepositoryException("Artifact relocations form a cycle: " + visited);
221 invalidDescriptor(session, trace, a, exception);
222 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
223 return null;
224 }
225 result.addException(exception);
226 throw new ArtifactDescriptorException(result);
227 }
228
229 ArtifactResult resolveResult;
230 try {
231 ArtifactRequest resolveRequest =
232 new ArtifactRequest(pomArtifact, request.getRepositories(), request.getRequestContext());
233 resolveRequest.setTrace(trace);
234 resolveResult = artifactResolver.resolveArtifact(session, resolveRequest);
235 pomArtifact = resolveResult.getArtifact();
236 result.setRepository(resolveResult.getRepository());
237 } catch (ArtifactResolutionException e) {
238 if (e.getCause() instanceof ArtifactNotFoundException) {
239 missingDescriptor(session, trace, a, (Exception) e.getCause());
240 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_MISSING) != 0) {
241 return null;
242 }
243 }
244 result.addException(e);
245 throw new ArtifactDescriptorException(result);
246 }
247
248 Model model;
249
250
251 final WorkspaceReader workspace = session.getWorkspaceReader();
252 if (workspace instanceof MavenWorkspaceReader) {
253 model = ((MavenWorkspaceReader) workspace).findModel(pomArtifact);
254 if (model != null) {
255 return model;
256 }
257 }
258
259 try {
260 ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
261 modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
262 modelRequest.setProcessPlugins(false);
263 modelRequest.setTwoPhaseBuilding(false);
264
265
266 modelRequest.setSystemProperties(
267 toProperties(session.getUserProperties(), session.getSystemProperties()));
268 modelRequest.setUserProperties(new Properties());
269 modelRequest.setModelCache(modelCacheFactory.createCache(session));
270 modelRequest.setModelResolver(new DefaultModelResolver(
271 session,
272 trace.newChild(modelRequest),
273 request.getRequestContext(),
274 artifactResolver,
275 versionRangeResolver,
276 remoteRepositoryManager,
277 request.getRepositories()));
278 if (resolveResult.getRepository() instanceof WorkspaceRepository) {
279 modelRequest.setPomFile(pomArtifact.getFile());
280 } else {
281 modelRequest.setModelSource(new FileModelSource(pomArtifact.getFile()));
282 }
283
284 model = modelBuilder.build(modelRequest).getEffectiveModel();
285 } catch (ModelBuildingException e) {
286 for (ModelProblem problem : e.getProblems()) {
287 if (problem.getException() instanceof UnresolvableModelException) {
288 result.addException(problem.getException());
289 throw new ArtifactDescriptorException(result);
290 }
291 }
292 invalidDescriptor(session, trace, a, e);
293 if ((getPolicy(session, a, request) & ArtifactDescriptorPolicy.IGNORE_INVALID) != 0) {
294 return null;
295 }
296 result.addException(e);
297 throw new ArtifactDescriptorException(result);
298 }
299
300 Relocation relocation = getRelocation(model);
301
302 if (relocation != null) {
303 result.addRelocation(a);
304 a = new RelocatedArtifact(
305 a,
306 relocation.getGroupId(),
307 relocation.getArtifactId(),
308 relocation.getVersion(),
309 relocation.getMessage());
310 result.setArtifact(a);
311 } else {
312 return model;
313 }
314 }
315 }
316
317 private Properties toProperties(Map<String, String> dominant, Map<String, String> recessive) {
318 Properties props = new Properties();
319 if (recessive != null) {
320 props.putAll(recessive);
321 }
322 if (dominant != null) {
323 props.putAll(dominant);
324 }
325 return props;
326 }
327
328 private Relocation getRelocation(Model model) {
329 Relocation relocation = null;
330 DistributionManagement distMgmt = model.getDistributionManagement();
331 if (distMgmt != null) {
332 relocation = distMgmt.getRelocation();
333 }
334 return relocation;
335 }
336
337 private void missingDescriptor(
338 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
339 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_MISSING);
340 event.setTrace(trace);
341 event.setArtifact(artifact);
342 event.setException(exception);
343
344 repositoryEventDispatcher.dispatch(event.build());
345 }
346
347 private void invalidDescriptor(
348 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Exception exception) {
349 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_DESCRIPTOR_INVALID);
350 event.setTrace(trace);
351 event.setArtifact(artifact);
352 event.setException(exception);
353
354 repositoryEventDispatcher.dispatch(event.build());
355 }
356
357 private int getPolicy(RepositorySystemSession session, Artifact a, ArtifactDescriptorRequest request) {
358 ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
359 if (policy == null) {
360 return ArtifactDescriptorPolicy.STRICT;
361 }
362 return policy.getPolicy(session, new ArtifactDescriptorPolicyRequest(a, request.getRequestContext()));
363 }
364 }