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