Coverage Report - org.apache.maven.shared.io.location.ArtifactLocatorStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactLocatorStrategy
100%
48/48
100%
8/8
4.333
 
 1  
 package org.apache.maven.shared.io.location;
 2  
 
 3  
 import java.util.List;
 4  
 
 5  
 import org.apache.maven.artifact.Artifact;
 6  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 7  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 8  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 9  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 10  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 11  
 import org.apache.maven.shared.io.logging.MessageHolder;
 12  
 
 13  
 public class ArtifactLocatorStrategy
 14  
     implements LocatorStrategy
 15  
 {
 16  
     private final ArtifactFactory factory;
 17  
 
 18  
     private final ArtifactResolver resolver;
 19  
 
 20  13
     private String defaultArtifactType = "jar";
 21  
 
 22  
     private final ArtifactRepository localRepository;
 23  
 
 24  
     private final List remoteRepositories;
 25  
 
 26  9
     public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
 27  
                                     ArtifactRepository localRepository, List remoteRepositories )
 28  
     {
 29  9
         this.factory = factory;
 30  9
         this.resolver = resolver;
 31  9
         this.localRepository = localRepository;
 32  9
         this.remoteRepositories = remoteRepositories;
 33  9
     }
 34  
 
 35  4
     public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
 36  
                                     ArtifactRepository localRepository, List remoteRepositories,
 37  
                                     String defaultArtifactType )
 38  
     {
 39  4
         this.factory = factory;
 40  4
         this.resolver = resolver;
 41  4
         this.localRepository = localRepository;
 42  4
         this.remoteRepositories = remoteRepositories;
 43  4
         this.defaultArtifactType = defaultArtifactType;
 44  4
     }
 45  
 
 46  
     /**
 47  
      * Assumes artifact identity is given in a set of comma-delimited tokens of
 48  
      * the form: <code>groupId:artifactId:version:type:classifier</code>, where
 49  
      * type and classifier are optional.
 50  
      */
 51  
     public Location resolve( String locationSpecification, MessageHolder messageHolder )
 52  
     {
 53  11
         String[] parts = locationSpecification.split( ":" );
 54  
 
 55  11
         Location location = null;
 56  
 
 57  11
         if ( parts.length > 2 )
 58  
         {
 59  9
             String groupId = parts[0];
 60  9
             String artifactId = parts[1];
 61  9
             String version = parts[2];
 62  
 
 63  9
             String type = defaultArtifactType;
 64  9
             if ( parts.length > 3 )
 65  
             {
 66  4
                 if ( parts[3].trim().length() > 0 )
 67  
                 {
 68  3
                     type = parts[3];
 69  
                 }
 70  
             }
 71  
 
 72  9
             String classifier = null;
 73  9
             if ( parts.length > 4 )
 74  
             {
 75  3
                 classifier = parts[4];
 76  
             }
 77  
 
 78  9
             if ( parts.length > 5 )
 79  
             {
 80  1
                 messageHolder.newMessage().append( "Location specification has unused tokens: \'" );
 81  
 
 82  3
                 for ( int i = 5; i < parts.length; i++ )
 83  
                 {
 84  2
                     messageHolder.append( ":" + parts[i] );
 85  
                 }
 86  
             }
 87  
 
 88  
             Artifact artifact;
 89  9
             if ( classifier == null )
 90  
             {
 91  6
                 artifact = factory.createArtifact( groupId, artifactId, version, null, type );
 92  
             }
 93  
             else
 94  
             {
 95  3
                 artifact = factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
 96  
             }
 97  
 
 98  
             try
 99  
             {
 100  9
                 resolver.resolve( artifact, remoteRepositories, localRepository );
 101  
 
 102  7
                 if ( artifact.getFile() != null )
 103  
                 {
 104  6
                     location = new ArtifactLocation( artifact, locationSpecification );
 105  
                 }
 106  
                 else
 107  
                 {
 108  2
                     messageHolder.addMessage( "Supposedly resolved artifact: " + artifact.getId()
 109  1
                         + " does not have an associated file." );
 110  
                 }
 111  
             }
 112  1
             catch ( ArtifactResolutionException e )
 113  
             {
 114  2
                 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
 115  1
                     + locationSpecification, e );
 116  
             }
 117  1
             catch ( ArtifactNotFoundException e )
 118  
             {
 119  2
                 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
 120  1
                     + locationSpecification, e );
 121  
             }
 122  
         }
 123  
         else
 124  
         {
 125  4
             messageHolder.addMessage( "Invalid artifact specification: \'" + locationSpecification
 126  2
                 + "\'. Must contain at least three fields, separated by ':'." );
 127  
         }
 128  
 
 129  11
         return location;
 130  
     }
 131  
 
 132  
 }