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.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.Set;
31
32 import javax.inject.Inject;
33 import javax.inject.Named;
34
35 import org.apache.maven.model.DependencyManagement;
36 import org.apache.maven.model.DistributionManagement;
37 import org.apache.maven.model.License;
38 import org.apache.maven.model.Model;
39 import org.apache.maven.model.Prerequisites;
40 import org.apache.maven.model.Relocation;
41 import org.apache.maven.model.Repository;
42 import org.apache.maven.model.building.DefaultModelBuilderFactory;
43 import org.apache.maven.model.building.DefaultModelBuildingRequest;
44 import org.apache.maven.model.building.FileModelSource;
45 import org.apache.maven.model.building.ModelBuilder;
46 import org.apache.maven.model.building.ModelBuildingException;
47 import org.apache.maven.model.building.ModelBuildingRequest;
48 import org.apache.maven.model.building.ModelProblem;
49 import org.apache.maven.model.resolution.UnresolvableModelException;
50 import org.codehaus.plexus.component.annotations.Component;
51 import org.codehaus.plexus.component.annotations.Requirement;
52 import org.eclipse.aether.RepositoryException;
53 import org.eclipse.aether.RepositoryEvent.EventType;
54 import org.eclipse.aether.RepositoryEvent;
55 import org.eclipse.aether.RepositorySystemSession;
56 import org.eclipse.aether.RequestTrace;
57 import org.eclipse.aether.artifact.Artifact;
58 import org.eclipse.aether.artifact.ArtifactProperties;
59 import org.eclipse.aether.artifact.ArtifactType;
60 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
61 import org.eclipse.aether.artifact.DefaultArtifact;
62 import org.eclipse.aether.artifact.DefaultArtifactType;
63 import org.eclipse.aether.graph.Dependency;
64 import org.eclipse.aether.graph.Exclusion;
65 import org.eclipse.aether.impl.ArtifactDescriptorReader;
66 import org.eclipse.aether.impl.ArtifactResolver;
67 import org.eclipse.aether.impl.RemoteRepositoryManager;
68 import org.eclipse.aether.impl.RepositoryEventDispatcher;
69 import org.eclipse.aether.impl.VersionResolver;
70 import org.eclipse.aether.repository.WorkspaceRepository;
71 import org.eclipse.aether.resolution.ArtifactDescriptorException;
72 import org.eclipse.aether.resolution.ArtifactDescriptorPolicy;
73 import org.eclipse.aether.resolution.ArtifactDescriptorPolicyRequest;
74 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
75 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
76 import org.eclipse.aether.resolution.ArtifactRequest;
77 import org.eclipse.aether.resolution.ArtifactResolutionException;
78 import org.eclipse.aether.resolution.ArtifactResult;
79 import org.eclipse.aether.resolution.VersionRequest;
80 import org.eclipse.aether.resolution.VersionResolutionException;
81 import org.eclipse.aether.resolution.VersionResult;
82 import org.eclipse.aether.spi.locator.Service;
83 import org.eclipse.aether.spi.locator.ServiceLocator;
84 import org.eclipse.aether.spi.log.Logger;
85 import org.eclipse.aether.spi.log.LoggerFactory;
86 import org.eclipse.aether.spi.log.NullLoggerFactory;
87 import org.eclipse.aether.transfer.ArtifactNotFoundException;
88
89
90
91
92 @Named
93 @Component( role = ArtifactDescriptorReader.class )
94 public class DefaultArtifactDescriptorReader
95 implements ArtifactDescriptorReader, Service
96 {
97
98 @SuppressWarnings( "unused" )
99 @Requirement( role = LoggerFactory.class )
100 private Logger logger = NullLoggerFactory.LOGGER;
101
102 @Requirement
103 private RemoteRepositoryManager remoteRepositoryManager;
104
105 @Requirement
106 private VersionResolver versionResolver;
107
108 @Requirement
109 private ArtifactResolver artifactResolver;
110
111 @Requirement
112 private RepositoryEventDispatcher repositoryEventDispatcher;
113
114 @Requirement
115 private ModelBuilder modelBuilder;
116
117 public DefaultArtifactDescriptorReader()
118 {
119
120 }
121
122 @Inject
123 DefaultArtifactDescriptorReader( RemoteRepositoryManager remoteRepositoryManager, VersionResolver versionResolver,
124 ArtifactResolver artifactResolver, ModelBuilder modelBuilder,
125 RepositoryEventDispatcher repositoryEventDispatcher, LoggerFactory loggerFactory )
126 {
127 setRemoteRepositoryManager( remoteRepositoryManager );
128 setVersionResolver( versionResolver );
129 setArtifactResolver( artifactResolver );
130 setModelBuilder( modelBuilder );
131 setLoggerFactory( loggerFactory );
132 setRepositoryEventDispatcher( repositoryEventDispatcher );
133 }
134
135 public void initService( ServiceLocator locator )
136 {
137 setLoggerFactory( locator.getService( LoggerFactory.class ) );
138 setRemoteRepositoryManager( locator.getService( RemoteRepositoryManager.class ) );
139 setVersionResolver( locator.getService( VersionResolver.class ) );
140 setArtifactResolver( locator.getService( ArtifactResolver.class ) );
141 setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) );
142 modelBuilder = locator.getService( ModelBuilder.class );
143 if ( modelBuilder == null )
144 {
145 setModelBuilder( new DefaultModelBuilderFactory().newInstance() );
146 }
147 }
148
149 public DefaultArtifactDescriptorReader setLoggerFactory( LoggerFactory loggerFactory )
150 {
151 this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
152 return this;
153 }
154
155 void setLogger( LoggerFactory loggerFactory )
156 {
157
158 setLoggerFactory( loggerFactory );
159 }
160
161 public DefaultArtifactDescriptorReader setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager )
162 {
163 if ( remoteRepositoryManager == null )
164 {
165 throw new IllegalArgumentException( "remote repository manager has not been specified" );
166 }
167 this.remoteRepositoryManager = remoteRepositoryManager;
168 return this;
169 }
170
171 public DefaultArtifactDescriptorReader setVersionResolver( VersionResolver versionResolver )
172 {
173 if ( versionResolver == null )
174 {
175 throw new IllegalArgumentException( "version resolver has not been specified" );
176 }
177 this.versionResolver = versionResolver;
178 return this;
179 }
180
181 public DefaultArtifactDescriptorReader setArtifactResolver( ArtifactResolver artifactResolver )
182 {
183 if ( artifactResolver == null )
184 {
185 throw new IllegalArgumentException( "artifact resolver has not been specified" );
186 }
187 this.artifactResolver = artifactResolver;
188 return this;
189 }
190
191 public DefaultArtifactDescriptorReader setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher )
192 {
193 if ( repositoryEventDispatcher == null )
194 {
195 throw new IllegalArgumentException( "repository event dispatcher has not been specified" );
196 }
197 this.repositoryEventDispatcher = repositoryEventDispatcher;
198 return this;
199 }
200
201 public DefaultArtifactDescriptorReader setModelBuilder( ModelBuilder modelBuilder )
202 {
203 if ( modelBuilder == null )
204 {
205 throw new IllegalArgumentException( "model builder has not been specified" );
206 }
207 this.modelBuilder = modelBuilder;
208 return this;
209 }
210
211 public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session,
212 ArtifactDescriptorRequest request )
213 throws ArtifactDescriptorException
214 {
215 ArtifactDescriptorResult result = new ArtifactDescriptorResult( request );
216
217 Model model = loadPom( session, request, result );
218
219 if ( model != null )
220 {
221 ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
222
223 for ( Repository r : model.getRepositories() )
224 {
225 result.addRepository( ArtifactDescriptorUtils.toRemoteRepository( r ) );
226 }
227
228 for ( org.apache.maven.model.Dependency dependency : model.getDependencies() )
229 {
230 result.addDependency( convert( dependency, stereotypes ) );
231 }
232
233 DependencyManagement mngt = model.getDependencyManagement();
234 if ( mngt != null )
235 {
236 for ( org.apache.maven.model.Dependency dependency : mngt.getDependencies() )
237 {
238 result.addManagedDependency( convert( dependency, stereotypes ) );
239 }
240 }
241
242 Map<String, Object> properties = new LinkedHashMap<String, Object>();
243
244 Prerequisites prerequisites = model.getPrerequisites();
245 if ( prerequisites != null )
246 {
247 properties.put( "prerequisites.maven", prerequisites.getMaven() );
248 }
249
250 List<License> licenses = model.getLicenses();
251 properties.put( "license.count", licenses.size() );
252 for ( int i = 0; i < licenses.size(); i++ )
253 {
254 License license = licenses.get( i );
255 properties.put( "license." + i + ".name", license.getName() );
256 properties.put( "license." + i + ".url", license.getUrl() );
257 properties.put( "license." + i + ".comments", license.getComments() );
258 properties.put( "license." + i + ".distribution", license.getDistribution() );
259 }
260
261 result.setProperties( properties );
262
263 setArtifactProperties( result, model );
264 }
265
266 return result;
267 }
268
269 private Model loadPom( RepositorySystemSession session, ArtifactDescriptorRequest request,
270 ArtifactDescriptorResult result )
271 throws ArtifactDescriptorException
272 {
273 RequestTrace trace = RequestTrace.newChild( request.getTrace(), request );
274
275 Set<String> visited = new LinkedHashSet<String>();
276 for ( Artifact artifact = request.getArtifact();; )
277 {
278 Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact( artifact );
279 try
280 {
281 VersionRequest versionRequest =
282 new VersionRequest( artifact, request.getRepositories(), request.getRequestContext() );
283 versionRequest.setTrace( trace );
284 VersionResult versionResult = versionResolver.resolveVersion( session, versionRequest );
285
286 artifact = artifact.setVersion( versionResult.getVersion() );
287
288 versionRequest =
289 new VersionRequest( pomArtifact, request.getRepositories(), request.getRequestContext() );
290 versionRequest.setTrace( trace );
291 versionResult = versionResolver.resolveVersion( session, versionRequest );
292
293 pomArtifact = pomArtifact.setVersion( versionResult.getVersion() );
294 }
295 catch ( VersionResolutionException e )
296 {
297 result.addException( e );
298 throw new ArtifactDescriptorException( result );
299 }
300
301 if ( !visited.add( artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion() ) )
302 {
303 RepositoryException exception =
304 new RepositoryException( "Artifact relocations form a cycle: " + visited );
305 invalidDescriptor( session, trace, artifact, exception );
306 if ( ( getPolicy( session, artifact, request ) & ArtifactDescriptorPolicy.IGNORE_INVALID ) != 0 )
307 {
308 return null;
309 }
310 result.addException( exception );
311 throw new ArtifactDescriptorException( result );
312 }
313
314 ArtifactResult resolveResult;
315 try
316 {
317 ArtifactRequest resolveRequest =
318 new ArtifactRequest( pomArtifact, request.getRepositories(), request.getRequestContext() );
319 resolveRequest.setTrace( trace );
320 resolveResult = artifactResolver.resolveArtifact( session, resolveRequest );
321 pomArtifact = resolveResult.getArtifact();
322 result.setRepository( resolveResult.getRepository() );
323 }
324 catch ( ArtifactResolutionException e )
325 {
326 if ( e.getCause() instanceof ArtifactNotFoundException )
327 {
328 missingDescriptor( session, trace, artifact, (Exception) e.getCause() );
329 if ( ( getPolicy( session, artifact, request ) & ArtifactDescriptorPolicy.IGNORE_MISSING ) != 0 )
330 {
331 return null;
332 }
333 }
334 result.addException( e );
335 throw new ArtifactDescriptorException( result );
336 }
337
338 Model model;
339 try
340 {
341 ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
342 modelRequest.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
343 modelRequest.setProcessPlugins( false );
344 modelRequest.setTwoPhaseBuilding( false );
345 modelRequest.setSystemProperties( toProperties( session.getUserProperties(),
346 session.getSystemProperties() ) );
347 modelRequest.setModelCache( DefaultModelCache.newInstance( session ) );
348 modelRequest.setModelResolver( new DefaultModelResolver( session, trace.newChild( modelRequest ),
349 request.getRequestContext(), artifactResolver,
350 remoteRepositoryManager,
351 request.getRepositories() ) );
352 if ( resolveResult.getRepository() instanceof WorkspaceRepository )
353 {
354 modelRequest.setPomFile( pomArtifact.getFile() );
355 }
356 else
357 {
358 modelRequest.setModelSource( new FileModelSource( pomArtifact.getFile() ) );
359 }
360
361 model = modelBuilder.build( modelRequest ).getEffectiveModel();
362 }
363 catch ( ModelBuildingException e )
364 {
365 for ( ModelProblem problem : e.getProblems() )
366 {
367 if ( problem.getException() instanceof UnresolvableModelException )
368 {
369 result.addException( problem.getException() );
370 throw new ArtifactDescriptorException( result );
371 }
372 }
373 invalidDescriptor( session, trace, artifact, e );
374 if ( ( getPolicy( session, artifact, request ) & ArtifactDescriptorPolicy.IGNORE_INVALID ) != 0 )
375 {
376 return null;
377 }
378 result.addException( e );
379 throw new ArtifactDescriptorException( result );
380 }
381
382 Relocation relocation = getRelocation( model );
383
384 if ( relocation != null )
385 {
386 result.addRelocation( artifact );
387 artifact =
388 new RelocatedArtifact( artifact, relocation.getGroupId(), relocation.getArtifactId(),
389 relocation.getVersion() );
390 result.setArtifact( artifact );
391 }
392 else
393 {
394 return model;
395 }
396 }
397 }
398
399 private Properties toProperties( Map<String, String> dominant, Map<String, String> recessive )
400 {
401 Properties props = new Properties();
402 if ( recessive != null )
403 {
404 props.putAll( recessive );
405 }
406 if ( dominant != null )
407 {
408 props.putAll( dominant );
409 }
410 return props;
411 }
412
413 private Relocation getRelocation( Model model )
414 {
415 Relocation relocation = null;
416 DistributionManagement distMngt = model.getDistributionManagement();
417 if ( distMngt != null )
418 {
419 relocation = distMngt.getRelocation();
420 }
421 return relocation;
422 }
423
424 private void setArtifactProperties( ArtifactDescriptorResult result, Model model )
425 {
426 String downloadUrl = null;
427 DistributionManagement distMngt = model.getDistributionManagement();
428 if ( distMngt != null )
429 {
430 downloadUrl = distMngt.getDownloadUrl();
431 }
432 if ( downloadUrl != null && downloadUrl.length() > 0 )
433 {
434 Artifact artifact = result.getArtifact();
435 Map<String, String> props = new HashMap<String, String>( artifact.getProperties() );
436 props.put( ArtifactProperties.DOWNLOAD_URL, downloadUrl );
437 result.setArtifact( artifact.setProperties( props ) );
438 }
439 }
440
441 private Dependency convert( org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes )
442 {
443 ArtifactType stereotype = stereotypes.get( dependency.getType() );
444 if ( stereotype == null )
445 {
446 stereotype = new DefaultArtifactType( dependency.getType() );
447 }
448
449 boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
450
451 Map<String, String> props = null;
452 if ( system )
453 {
454 props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
455 }
456
457 Artifact artifact =
458 new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
459 dependency.getVersion(), props, stereotype );
460
461 List<Exclusion> exclusions = new ArrayList<Exclusion>( dependency.getExclusions().size() );
462 for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
463 {
464 exclusions.add( convert( exclusion ) );
465 }
466
467 Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
468
469 return result;
470 }
471
472 private Exclusion convert( org.apache.maven.model.Exclusion exclusion )
473 {
474 return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
475 }
476
477 private void missingDescriptor( RepositorySystemSession session, RequestTrace trace, Artifact artifact,
478 Exception exception )
479 {
480 RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_DESCRIPTOR_MISSING );
481 event.setTrace( trace );
482 event.setArtifact( artifact );
483 event.setException( exception );
484
485 repositoryEventDispatcher.dispatch( event.build() );
486 }
487
488 private void invalidDescriptor( RepositorySystemSession session, RequestTrace trace, Artifact artifact,
489 Exception exception )
490 {
491 RepositoryEvent.Builder event = new RepositoryEvent.Builder( session, EventType.ARTIFACT_DESCRIPTOR_INVALID );
492 event.setTrace( trace );
493 event.setArtifact( artifact );
494 event.setException( exception );
495
496 repositoryEventDispatcher.dispatch( event.build() );
497 }
498
499 private int getPolicy( RepositorySystemSession session, Artifact artifact, ArtifactDescriptorRequest request )
500 {
501 ArtifactDescriptorPolicy policy = session.getArtifactDescriptorPolicy();
502 if ( policy == null )
503 {
504 return ArtifactDescriptorPolicy.STRICT;
505 }
506 return policy.getPolicy( session, new ArtifactDescriptorPolicyRequest( artifact, request.getRequestContext() ) );
507 }
508
509 }