001package org.apache.maven.repository.legacy.resolver.transform;
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
022import java.util.List;
023
024import org.apache.maven.artifact.Artifact;
025import org.apache.maven.artifact.repository.ArtifactRepository;
026import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
027import org.apache.maven.artifact.repository.RepositoryRequest;
028import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
029import org.apache.maven.artifact.repository.metadata.Metadata;
030import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
031import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
032import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
033import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
034import org.apache.maven.artifact.repository.metadata.Versioning;
035import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
036import org.apache.maven.artifact.resolver.ArtifactResolutionException;
037import org.apache.maven.repository.legacy.WagonManager;
038import org.codehaus.plexus.component.annotations.Requirement;
039import org.codehaus.plexus.logging.AbstractLogEnabled;
040
041/**
042 * Describes a version transformation during artifact resolution.
043 *
044 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
045 * @todo try and refactor to remove abstract methods - not particular happy about current design
046 */
047public abstract class AbstractVersionTransformation
048    extends AbstractLogEnabled
049    implements ArtifactTransformation
050{
051    @Requirement
052    protected RepositoryMetadataManager repositoryMetadataManager;
053
054    @Requirement
055    protected WagonManager wagonManager;
056
057    public void transformForResolve( Artifact artifact, List<ArtifactRepository> remoteRepositories,
058                                     ArtifactRepository localRepository )
059        throws ArtifactResolutionException, ArtifactNotFoundException
060    {
061        RepositoryRequest request = new DefaultRepositoryRequest();
062        request.setLocalRepository( localRepository );
063        request.setRemoteRepositories( remoteRepositories );
064        transformForResolve( artifact, request );
065    }
066
067    protected String resolveVersion( Artifact artifact, ArtifactRepository localRepository,
068                                     List<ArtifactRepository> remoteRepositories )
069        throws RepositoryMetadataResolutionException
070    {
071        RepositoryRequest request = new DefaultRepositoryRequest();
072        request.setLocalRepository( localRepository );
073        request.setRemoteRepositories( remoteRepositories );
074        return resolveVersion( artifact, request );
075    }
076
077    protected String resolveVersion( Artifact artifact, RepositoryRequest request )
078        throws RepositoryMetadataResolutionException
079    {
080        RepositoryMetadata metadata;
081        // Don't use snapshot metadata for LATEST (which isSnapshot returns true for)
082        if ( !artifact.isSnapshot() || Artifact.LATEST_VERSION.equals( artifact.getBaseVersion() ) )
083        {
084            metadata = new ArtifactRepositoryMetadata( artifact );
085        }
086        else
087        {
088            metadata = new SnapshotArtifactRepositoryMetadata( artifact );
089        }
090
091        repositoryMetadataManager.resolve( metadata, request );
092
093        artifact.addMetadata( metadata );
094
095        Metadata repoMetadata = metadata.getMetadata();
096        String version = null;
097        if ( repoMetadata != null && repoMetadata.getVersioning() != null )
098        {
099            version = constructVersion( repoMetadata.getVersioning(), artifact.getBaseVersion() );
100        }
101
102        if ( version == null )
103        {
104            // use the local copy, or if it doesn't exist - go to the remote repo for it
105            version = artifact.getBaseVersion();
106        }
107
108        // TODO: also do this logging for other metadata?
109        // TODO: figure out way to avoid duplicated message
110        if ( getLogger().isDebugEnabled() )
111        {
112            if ( !version.equals( artifact.getBaseVersion() ) )
113            {
114                String message = artifact.getArtifactId() + ": resolved to version " + version;
115                if ( artifact.getRepository() != null )
116                {
117                    message += " from repository " + artifact.getRepository().getId();
118                }
119                else
120                {
121                    message += " from local repository";
122                }
123                getLogger().debug( message );
124            }
125            else
126            {
127                // Locally installed file is newer, don't use the resolved version
128                getLogger().debug( artifact.getArtifactId() + ": using locally installed snapshot" );
129            }
130        }
131        return version;
132    }
133
134    protected abstract String constructVersion( Versioning versioning, String baseVersion );
135}