001    package org.apache.maven.repository.internal;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.FileInputStream;
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.HashMap;
026    import java.util.List;
027    import java.util.Map;
028    
029    import org.apache.maven.artifact.repository.metadata.Versioning;
030    import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
031    import org.codehaus.plexus.component.annotations.Component;
032    import org.codehaus.plexus.component.annotations.Requirement;
033    import org.codehaus.plexus.util.IOUtil;
034    import org.sonatype.aether.RepositoryEvent.EventType;
035    import org.sonatype.aether.RepositorySystemSession;
036    import org.sonatype.aether.RequestTrace;
037    import org.sonatype.aether.SyncContext;
038    import org.sonatype.aether.util.DefaultRequestTrace;
039    import org.sonatype.aether.util.listener.DefaultRepositoryEvent;
040    import org.sonatype.aether.util.metadata.DefaultMetadata;
041    import org.sonatype.aether.util.version.GenericVersionScheme;
042    import org.sonatype.aether.version.InvalidVersionSpecificationException;
043    import org.sonatype.aether.version.Version;
044    import org.sonatype.aether.version.VersionConstraint;
045    import org.sonatype.aether.version.VersionScheme;
046    import org.sonatype.aether.impl.MetadataResolver;
047    import org.sonatype.aether.impl.RepositoryEventDispatcher;
048    import org.sonatype.aether.impl.SyncContextFactory;
049    import org.sonatype.aether.impl.VersionRangeResolver;
050    import org.sonatype.aether.metadata.Metadata;
051    import org.sonatype.aether.repository.ArtifactRepository;
052    import org.sonatype.aether.repository.RemoteRepository;
053    import org.sonatype.aether.repository.WorkspaceReader;
054    import org.sonatype.aether.resolution.MetadataRequest;
055    import org.sonatype.aether.resolution.MetadataResult;
056    import org.sonatype.aether.resolution.VersionRangeRequest;
057    import org.sonatype.aether.resolution.VersionRangeResolutionException;
058    import org.sonatype.aether.resolution.VersionRangeResult;
059    import org.sonatype.aether.spi.locator.Service;
060    import org.sonatype.aether.spi.locator.ServiceLocator;
061    import org.sonatype.aether.spi.log.Logger;
062    import org.sonatype.aether.spi.log.NullLogger;
063    
064    /**
065     * @author Benjamin Bentmann
066     */
067    @Component( role = VersionRangeResolver.class )
068    public class DefaultVersionRangeResolver
069        implements VersionRangeResolver, Service
070    {
071    
072        private static final String MAVEN_METADATA_XML = "maven-metadata.xml";
073    
074        @SuppressWarnings( "unused" )
075        @Requirement
076        private Logger logger = NullLogger.INSTANCE;
077    
078        @Requirement
079        private MetadataResolver metadataResolver;
080    
081        @Requirement
082        private SyncContextFactory syncContextFactory;
083    
084        @Requirement
085        private RepositoryEventDispatcher repositoryEventDispatcher;
086    
087        public void initService( ServiceLocator locator )
088        {
089            setLogger( locator.getService( Logger.class ) );
090            setMetadataResolver( locator.getService( MetadataResolver.class ) );
091            setSyncContextFactory( locator.getService( SyncContextFactory.class ) );
092            setRepositoryEventDispatcher( locator.getService( RepositoryEventDispatcher.class ) );
093        }
094    
095        public DefaultVersionRangeResolver setLogger( Logger logger )
096        {
097            this.logger = ( logger != null ) ? logger : NullLogger.INSTANCE;
098            return this;
099        }
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    }