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