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.io.FileInputStream;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.maven.artifact.repository.metadata.Versioning;
30 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
31 import org.codehaus.plexus.component.annotations.Component;
32 import org.codehaus.plexus.component.annotations.Requirement;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.sonatype.aether.RepositoryEvent.EventType;
35 import org.sonatype.aether.RepositorySystemSession;
36 import org.sonatype.aether.RequestTrace;
37 import org.sonatype.aether.SyncContext;
38 import org.sonatype.aether.util.DefaultRequestTrace;
39 import org.sonatype.aether.util.listener.DefaultRepositoryEvent;
40 import org.sonatype.aether.util.metadata.DefaultMetadata;
41 import org.sonatype.aether.util.version.GenericVersionScheme;
42 import org.sonatype.aether.version.InvalidVersionSpecificationException;
43 import org.sonatype.aether.version.Version;
44 import org.sonatype.aether.version.VersionConstraint;
45 import org.sonatype.aether.version.VersionScheme;
46 import org.sonatype.aether.impl.MetadataResolver;
47 import org.sonatype.aether.impl.RepositoryEventDispatcher;
48 import org.sonatype.aether.impl.SyncContextFactory;
49 import org.sonatype.aether.impl.VersionRangeResolver;
50 import org.sonatype.aether.metadata.Metadata;
51 import org.sonatype.aether.repository.ArtifactRepository;
52 import org.sonatype.aether.repository.RemoteRepository;
53 import org.sonatype.aether.repository.WorkspaceReader;
54 import org.sonatype.aether.resolution.MetadataRequest;
55 import org.sonatype.aether.resolution.MetadataResult;
56 import org.sonatype.aether.resolution.VersionRangeRequest;
57 import org.sonatype.aether.resolution.VersionRangeResolutionException;
58 import org.sonatype.aether.resolution.VersionRangeResult;
59 import org.sonatype.aether.spi.locator.Service;
60 import org.sonatype.aether.spi.locator.ServiceLocator;
61 import org.sonatype.aether.spi.log.Logger;
62 import org.sonatype.aether.spi.log.NullLogger;
63
64
65
66
67 @Component( role = VersionRangeResolver.class )
68 public class DefaultVersionRangeResolver
69 implements VersionRangeResolver, Service
70 {
71
72 private static final String MAVEN_METADATA_XML = "maven-metadata.xml";
73
74 @SuppressWarnings( "unused" )
75 @Requirement
76 private Logger logger = NullLogger.INSTANCE;
77
78 @Requirement
79 private MetadataResolver metadataResolver;
80
81 @Requirement
82 private SyncContextFactory syncContextFactory;
83
84 @Requirement
85 private RepositoryEventDispatcher repositoryEventDispatcher;
86
87 public void initService( ServiceLocator locator )
88 {
89 setLogger( locator.getService( Logger.class ) );
90 setMetadataResolver( locator.getService( MetadataResolver.class ) );
91 setSyncContextFactory( locator.getService( SyncContextFactory.class ) );
92 setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) );
93 }
94
95 public DefaultVersionRangeResolver setLogger( Logger logger )
96 {
97 this.logger = ( logger != null ) ? logger : NullLogger.INSTANCE;
98 return this;
99 }
100
101 public DefaultVersionRangeResolver setMetadataResolver( MetadataResolver metadataResolver )
102 {
103 if ( metadataResolver == null )
104 {
105 throw new IllegalArgumentException( "metadata resolver has not been specified" );
106 }
107 this.metadataResolver = metadataResolver;
108 return this;
109 }
110
111 public DefaultVersionRangeResolver setSyncContextFactory( SyncContextFactory syncContextFactory )
112 {
113 if ( syncContextFactory == null )
114 {
115 throw new IllegalArgumentException( "sync context factory has not been specified" );
116 }
117 this.syncContextFactory = syncContextFactory;
118 return this;
119 }
120
121 public DefaultVersionRangeResolver setRepositoryEventDispatcher( RepositoryEventDispatcher repositoryEventDispatcher )
122 {
123 if ( repositoryEventDispatcher == null )
124 {
125 throw new IllegalArgumentException( "repository event dispatcher has not been specified" );
126 }
127 this.repositoryEventDispatcher = repositoryEventDispatcher;
128 return this;
129 }
130
131 public VersionRangeResult resolveVersionRange( RepositorySystemSession session, VersionRangeRequest request )
132 throws VersionRangeResolutionException
133 {
134 VersionRangeResult result = new VersionRangeResult( request );
135
136 VersionScheme versionScheme = new GenericVersionScheme();
137
138 VersionConstraint versionConstraint;
139 try
140 {
141 versionConstraint = versionScheme.parseVersionConstraint( request.getArtifact().getVersion() );
142 }
143 catch ( InvalidVersionSpecificationException e )
144 {
145 result.addException( e );
146 throw new VersionRangeResolutionException( result );
147 }
148
149 result.setVersionConstraint( versionConstraint );
150
151 if ( versionConstraint.getRanges().isEmpty() )
152 {
153 result.addVersion( versionConstraint.getVersion() );
154 }
155 else
156 {
157 Map<String, ArtifactRepository> versionIndex = getVersions( session, result, request );
158
159 List<Version> versions = new ArrayList<Version>();
160 for ( Map.Entry<String, ArtifactRepository> v : versionIndex.entrySet() )
161 {
162 try
163 {
164 Version ver = versionScheme.parseVersion( v.getKey() );
165 if ( versionConstraint.containsVersion( ver ) )
166 {
167 versions.add( ver );
168 result.setRepository( ver, v.getValue() );
169 }
170 }
171 catch ( InvalidVersionSpecificationException e )
172 {
173 result.addException( e );
174 }
175 }
176
177 Collections.sort( versions );
178 result.setVersions( versions );
179 }
180
181 return result;
182 }
183
184 private Map<String, ArtifactRepository> getVersions( RepositorySystemSession session, VersionRangeResult result,
185 VersionRangeRequest request )
186 {
187 RequestTrace trace = DefaultRequestTrace.newChild( request.getTrace(), request );
188
189 Map<String, ArtifactRepository> versionIndex = new HashMap<String, ArtifactRepository>();
190
191 Metadata metadata =
192 new DefaultMetadata( request.getArtifact().getGroupId(), request.getArtifact().getArtifactId(),
193 MAVEN_METADATA_XML, Metadata.Nature.RELEASE_OR_SNAPSHOT );
194
195 List<MetadataRequest> metadataRequests = new ArrayList<MetadataRequest>( request.getRepositories().size() );
196
197 metadataRequests.add( new MetadataRequest( metadata, null, request.getRequestContext() ) );
198
199 for ( RemoteRepository repository : request.getRepositories() )
200 {
201 MetadataRequest metadataRequest = new MetadataRequest( metadata, repository, request.getRequestContext() );
202 metadataRequest.setDeleteLocalCopyIfMissing( true );
203 metadataRequest.setTrace( trace );
204 metadataRequests.add( metadataRequest );
205 }
206
207 List<MetadataResult> metadataResults = metadataResolver.resolveMetadata( session, metadataRequests );
208
209 WorkspaceReader workspace = session.getWorkspaceReader();
210 if ( workspace != null )
211 {
212 List<String> versions = workspace.findVersions( request.getArtifact() );
213 for ( String version : versions )
214 {
215 versionIndex.put( version, workspace.getRepository() );
216 }
217 }
218
219 for ( MetadataResult metadataResult : metadataResults )
220 {
221 result.addException( metadataResult.getException() );
222
223 ArtifactRepository repository = metadataResult.getRequest().getRepository();
224 if ( repository == null )
225 {
226 repository = session.getLocalRepository();
227 }
228
229 Versioning versioning = readVersions( session, trace, metadataResult.getMetadata(), repository, result );
230 for ( String version : versioning.getVersions() )
231 {
232 if ( !versionIndex.containsKey( version ) )
233 {
234 versionIndex.put( version, repository );
235 }
236 }
237 }
238
239 return versionIndex;
240 }
241
242 private Versioning readVersions( RepositorySystemSession session, RequestTrace trace, Metadata metadata,
243 ArtifactRepository repository, VersionRangeResult result )
244 {
245 Versioning versioning = null;
246
247 FileInputStream fis = null;
248 try
249 {
250 if ( metadata != null )
251 {
252 SyncContext syncContext = syncContextFactory.newInstance( session, true );
253
254 try
255 {
256 syncContext.acquire( null, Collections.singleton( metadata ) );
257
258 if ( metadata.getFile() != null && metadata.getFile().exists() )
259 {
260 fis = new FileInputStream( metadata.getFile() );
261 org.apache.maven.artifact.repository.metadata.Metadata m =
262 new MetadataXpp3Reader().read( fis, false );
263 versioning = m.getVersioning();
264 }
265 }
266 finally
267 {
268 syncContext.release();
269 }
270 }
271 }
272 catch ( Exception e )
273 {
274 invalidMetadata( session, trace, metadata, repository, e );
275 result.addException( e );
276 }
277 finally
278 {
279 IOUtil.close( fis );
280 }
281
282 return ( versioning != null ) ? versioning : new Versioning();
283 }
284
285 private void invalidMetadata( RepositorySystemSession session, RequestTrace trace, Metadata metadata,
286 ArtifactRepository repository, Exception exception )
287 {
288 DefaultRepositoryEvent event = new DefaultRepositoryEvent( EventType.METADATA_INVALID, session, trace );
289 event.setMetadata( metadata );
290 event.setException( exception );
291 event.setRepository( repository );
292
293 repositoryEventDispatcher.dispatch( event );
294 }
295
296 }