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.artifact.Artifact;
53 import org.sonatype.aether.artifact.ArtifactType;
54 import org.sonatype.aether.artifact.ArtifactTypeRegistry;
55 import org.sonatype.aether.graph.Dependency;
56 import org.sonatype.aether.graph.Exclusion;
57 import org.sonatype.aether.impl.ArtifactDescriptorReader;
58 import org.sonatype.aether.impl.ArtifactResolver;
59 import org.sonatype.aether.impl.RemoteRepositoryManager;
60 import org.sonatype.aether.impl.VersionResolver;
61 import org.sonatype.aether.transfer.ArtifactNotFoundException;
62 import org.sonatype.aether.util.artifact.ArtifactProperties;
63 import org.sonatype.aether.util.artifact.DefaultArtifact;
64 import org.sonatype.aether.util.artifact.DefaultArtifactType;
65 import org.sonatype.aether.util.artifact.SubArtifact;
66 import org.sonatype.aether.util.listener.DefaultRepositoryEvent;
67 import org.sonatype.aether.repository.RemoteRepository;
68 import org.sonatype.aether.repository.RepositoryPolicy;
69 import org.sonatype.aether.repository.WorkspaceRepository;
70 import org.sonatype.aether.resolution.ArtifactDescriptorException;
71 import org.sonatype.aether.resolution.ArtifactDescriptorRequest;
72 import org.sonatype.aether.resolution.ArtifactDescriptorResult;
73 import org.sonatype.aether.resolution.ArtifactRequest;
74 import org.sonatype.aether.resolution.ArtifactResolutionException;
75 import org.sonatype.aether.resolution.ArtifactResult;
76 import org.sonatype.aether.resolution.VersionRequest;
77 import org.sonatype.aether.resolution.VersionResolutionException;
78 import org.sonatype.aether.resolution.VersionResult;
79 import org.sonatype.aether.spi.locator.Service;
80 import org.sonatype.aether.spi.locator.ServiceLocator;
81 import org.sonatype.aether.spi.log.Logger;
82 import org.sonatype.aether.spi.log.NullLogger;
83
84
85
86
87 @Component( role = ArtifactDescriptorReader.class )
88 public class DefaultArtifactDescriptorReader
89 implements ArtifactDescriptorReader, Service
90 {
91
92 @SuppressWarnings( "unused" )
93 @Requirement
94 private Logger logger = NullLogger.INSTANCE;
95
96 @Requirement
97 private RemoteRepositoryManager remoteRepositoryManager;
98
99 @Requirement
100 private VersionResolver versionResolver;
101
102 @Requirement
103 private ArtifactResolver artifactResolver;
104
105 @Requirement
106 private ModelBuilder modelBuilder;
107
108 public void initService( ServiceLocator locator )
109 {
110 setLogger( locator.getService( Logger.class ) );
111 setRemoteRepositoryManager( locator.getService( RemoteRepositoryManager.class ) );
112 setVersionResolver( locator.getService( VersionResolver.class ) );
113 setArtifactResolver( locator.getService( ArtifactResolver.class ) );
114 modelBuilder = locator.getService( ModelBuilder.class );
115 if ( modelBuilder == null )
116 {
117 setModelBuilder( new DefaultModelBuilderFactory().newInstance() );
118 }
119 }
120
121 public DefaultArtifactDescriptorReader setLogger( Logger logger )
122 {
123 this.logger = ( logger != null ) ? logger : NullLogger.INSTANCE;
124 return this;
125 }
126
127 public DefaultArtifactDescriptorReader setRemoteRepositoryManager( RemoteRepositoryManager remoteRepositoryManager )
128 {
129 if ( remoteRepositoryManager == null )
130 {
131 throw new IllegalArgumentException( "remote repository manager has not been specified" );
132 }
133 this.remoteRepositoryManager = remoteRepositoryManager;
134 return this;
135 }
136
137 public DefaultArtifactDescriptorReader setVersionResolver( VersionResolver versionResolver )
138 {
139 if ( versionResolver == null )
140 {
141 throw new IllegalArgumentException( "version resolver has not been specified" );
142 }
143 this.versionResolver = versionResolver;
144 return this;
145 }
146
147 public DefaultArtifactDescriptorReader setArtifactResolver( ArtifactResolver artifactResolver )
148 {
149 if ( artifactResolver == null )
150 {
151 throw new IllegalArgumentException( "artifact resolver has not been specified" );
152 }
153 this.artifactResolver = artifactResolver;
154 return this;
155 }
156
157 public DefaultArtifactDescriptorReader setModelBuilder( ModelBuilder modelBuilder )
158 {
159 if ( modelBuilder == null )
160 {
161 throw new IllegalArgumentException( "model builder has not been specified" );
162 }
163 this.modelBuilder = modelBuilder;
164 return this;
165 }
166
167 public ArtifactDescriptorResult readArtifactDescriptor( RepositorySystemSession session,
168 ArtifactDescriptorRequest request )
169 throws ArtifactDescriptorException
170 {
171 ArtifactDescriptorResult result = new ArtifactDescriptorResult( request );
172
173 Model model = loadPom( session, request, result );
174
175 if ( model != null )
176 {
177 ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
178
179 for ( Repository r : model.getRepositories() )
180 {
181 result.addRepository( convert( r ) );
182 }
183
184 for ( org.apache.maven.model.Dependency dependency : model.getDependencies() )
185 {
186 result.addDependency( convert( dependency, stereotypes ) );
187 }
188
189 DependencyManagement mngt = model.getDependencyManagement();
190 if ( mngt != null )
191 {
192 for ( org.apache.maven.model.Dependency dependency : mngt.getDependencies() )
193 {
194 result.addManagedDependency( convert( dependency, stereotypes ) );
195 }
196 }
197
198 Map<String, Object> properties = new LinkedHashMap<String, Object>();
199
200 Prerequisites prerequisites = model.getPrerequisites();
201 if ( prerequisites != null )
202 {
203 properties.put( "prerequisites.maven", prerequisites.getMaven() );
204 }
205
206 List<License> licenses = model.getLicenses();
207 properties.put( "license.count", Integer.valueOf( licenses.size() ) );
208 for ( int i = 0; i < licenses.size(); i++ )
209 {
210 License license = licenses.get( i );
211 properties.put( "license." + i + ".name", license.getName() );
212 properties.put( "license." + i + ".url", license.getUrl() );
213 properties.put( "license." + i + ".comments", license.getComments() );
214 properties.put( "license." + i + ".distribution", license.getDistribution() );
215 }
216
217 result.setProperties( properties );
218 }
219
220 return result;
221 }
222
223 private Model loadPom( RepositorySystemSession session, ArtifactDescriptorRequest request,
224 ArtifactDescriptorResult result )
225 throws ArtifactDescriptorException
226 {
227 Set<String> visited = new LinkedHashSet<String>();
228 for ( Artifact artifact = request.getArtifact();; )
229 {
230 try
231 {
232 VersionRequest versionRequest =
233 new VersionRequest( artifact, request.getRepositories(), request.getRequestContext() );
234 VersionResult versionResult = versionResolver.resolveVersion( session, versionRequest );
235
236 artifact = artifact.setVersion( versionResult.getVersion() );
237 }
238 catch ( VersionResolutionException e )
239 {
240 result.addException( e );
241 throw new ArtifactDescriptorException( result );
242 }
243
244 if ( !visited.add( artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion() ) )
245 {
246 RepositoryException exception =
247 new RepositoryException( "Artifact relocations form a cycle: " + visited );
248 invalidDescriptor( session, artifact, exception );
249 if ( session.isIgnoreInvalidArtifactDescriptor() )
250 {
251 return null;
252 }
253 result.addException( exception );
254 throw new ArtifactDescriptorException( result );
255 }
256
257 Artifact pomArtifact = artifact;
258 if ( pomArtifact.getClassifier().length() > 0 || !"pom".equals( pomArtifact.getExtension() ) )
259 {
260 pomArtifact = new SubArtifact( artifact, "", "pom" );
261 }
262
263 ArtifactResult resolveResult;
264 try
265 {
266 ArtifactRequest resolveRequest =
267 new ArtifactRequest( pomArtifact, request.getRepositories(), request.getRequestContext() );
268 resolveResult = artifactResolver.resolveArtifact( session, resolveRequest );
269 pomArtifact = resolveResult.getArtifact();
270 result.setRepository( resolveResult.getRepository() );
271 }
272 catch ( ArtifactResolutionException e )
273 {
274 if ( e.getCause() instanceof ArtifactNotFoundException )
275 {
276 missingDescriptor( session, artifact, (Exception) e.getCause() );
277 if ( session.isIgnoreMissingArtifactDescriptor() )
278 {
279 return null;
280 }
281 }
282 result.addException( e );
283 throw new ArtifactDescriptorException( result );
284 }
285
286 Model model;
287 try
288 {
289 ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
290 modelRequest.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
291 modelRequest.setProcessPlugins( false );
292 modelRequest.setTwoPhaseBuilding( false );
293 modelRequest.setSystemProperties( toProperties( session.getUserProperties(),
294 session.getSystemProperties() ) );
295 modelRequest.setModelCache( DefaultModelCache.newInstance( session ) );
296 modelRequest.setModelResolver( new DefaultModelResolver( session, request.getRequestContext(),
297 artifactResolver, 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, 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 static RemoteRepository convert( Repository repository )
408 {
409 RemoteRepository result =
410 new RemoteRepository( repository.getId(), repository.getLayout(), repository.getUrl() );
411 result.setPolicy( true, convert( repository.getSnapshots() ) );
412 result.setPolicy( false, convert( repository.getReleases() ) );
413 return result;
414 }
415
416 private static RepositoryPolicy convert( org.apache.maven.model.RepositoryPolicy policy )
417 {
418 boolean enabled = true;
419 String checksums = RepositoryPolicy.CHECKSUM_POLICY_WARN;
420 String updates = RepositoryPolicy.UPDATE_POLICY_DAILY;
421
422 if ( policy != null )
423 {
424 enabled = policy.isEnabled();
425 if ( policy.getUpdatePolicy() != null )
426 {
427 updates = policy.getUpdatePolicy();
428 }
429 if ( policy.getChecksumPolicy() != null )
430 {
431 checksums = policy.getChecksumPolicy();
432 }
433 }
434
435 return new RepositoryPolicy( enabled, updates, checksums );
436 }
437
438 private void missingDescriptor( RepositorySystemSession session, Artifact artifact, Exception exception )
439 {
440 RepositoryListener listener = session.getRepositoryListener();
441 if ( listener != null )
442 {
443 DefaultRepositoryEvent event = new DefaultRepositoryEvent( EventType.ARTIFACT_DESCRIPTOR_MISSING, session );
444 event.setArtifact( artifact );
445 event.setException( exception );
446 listener.artifactDescriptorMissing( event );
447 }
448 }
449
450 private void invalidDescriptor( RepositorySystemSession session, Artifact artifact, Exception exception )
451 {
452 RepositoryListener listener = session.getRepositoryListener();
453 if ( listener != null )
454 {
455 DefaultRepositoryEvent event = new DefaultRepositoryEvent( EventType.ARTIFACT_DESCRIPTOR_INVALID, session );
456 event.setArtifact( artifact );
457 event.setException( exception );
458 listener.artifactDescriptorInvalid( event );
459 }
460 }
461
462 }