Coverage Report - org.apache.maven.shared.io.location.ArtifactLocatorStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
ArtifactLocatorStrategy
85 %
47/55
100 %
16/16
3,5
 
 1  
 package org.apache.maven.shared.io.location;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.List;
 23  
 
 24  
 import org.apache.maven.artifact.Artifact;
 25  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 26  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 27  
 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
 28  
 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 29  
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 30  
 import org.apache.maven.shared.io.logging.MessageHolder;
 31  
 
 32  
 public class ArtifactLocatorStrategy
 33  
     implements LocatorStrategy
 34  
 {
 35  
     private final ArtifactFactory factory;
 36  
 
 37  
     private final ArtifactResolver resolver;
 38  
 
 39  13
     private String defaultArtifactType = "jar";
 40  
 
 41  
     private final ArtifactRepository localRepository;
 42  
 
 43  
     private final List remoteRepositories;
 44  
 
 45  13
     private String defaultClassifier = null;
 46  
 
 47  
     public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
 48  
                                     ArtifactRepository localRepository, List remoteRepositories )
 49  9
     {
 50  9
         this.factory = factory;
 51  9
         this.resolver = resolver;
 52  9
         this.localRepository = localRepository;
 53  9
         this.remoteRepositories = remoteRepositories;
 54  9
     }
 55  
 
 56  
     public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
 57  
                                     ArtifactRepository localRepository, List remoteRepositories,
 58  
                                     String defaultArtifactType )
 59  4
     {
 60  4
         this.factory = factory;
 61  4
         this.resolver = resolver;
 62  4
         this.localRepository = localRepository;
 63  4
         this.remoteRepositories = remoteRepositories;
 64  4
         this.defaultArtifactType = defaultArtifactType;
 65  4
     }
 66  
 
 67  
     public ArtifactLocatorStrategy( ArtifactFactory factory, ArtifactResolver resolver,
 68  
                                     ArtifactRepository localRepository, List remoteRepositories,
 69  
                                     String defaultArtifactType, String defaultClassifier )
 70  0
     {
 71  0
         this.factory = factory;
 72  0
         this.resolver = resolver;
 73  0
         this.localRepository = localRepository;
 74  0
         this.remoteRepositories = remoteRepositories;
 75  0
         this.defaultArtifactType = defaultArtifactType;
 76  0
         this.defaultClassifier = defaultClassifier;
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Assumes artifact identity is given in a set of comma-delimited tokens of
 81  
      * the form: <code>groupId:artifactId:version:type:classifier</code>, where
 82  
      * type and classifier are optional.
 83  
      */
 84  
     public Location resolve( String locationSpecification, MessageHolder messageHolder )
 85  
     {
 86  11
         String[] parts = locationSpecification.split( ":" );
 87  
 
 88  11
         Location location = null;
 89  
 
 90  11
         if ( parts.length > 2 )
 91  
         {
 92  9
             String groupId = parts[0];
 93  9
             String artifactId = parts[1];
 94  9
             String version = parts[2];
 95  
 
 96  9
             String type = defaultArtifactType;
 97  9
             if ( parts.length > 3 )
 98  
             {
 99  4
                 if ( parts[3].trim().length() > 0 )
 100  
                 {
 101  3
                     type = parts[3];
 102  
                 }
 103  
             }
 104  
 
 105  9
             String classifier = defaultClassifier;
 106  9
             if ( parts.length > 4 )
 107  
             {
 108  3
                 classifier = parts[4];
 109  
             }
 110  
 
 111  9
             if ( parts.length > 5 )
 112  
             {
 113  1
                 messageHolder.newMessage().append( "Location specification has unused tokens: \'" );
 114  
 
 115  3
                 for ( int i = 5; i < parts.length; i++ )
 116  
                 {
 117  2
                     messageHolder.append( ":" + parts[i] );
 118  
                 }
 119  
             }
 120  
 
 121  
             Artifact artifact;
 122  9
             if ( classifier == null )
 123  
             {
 124  6
                 artifact = factory.createArtifact( groupId, artifactId, version, null, type );
 125  
             }
 126  
             else
 127  
             {
 128  3
                 artifact = factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
 129  
             }
 130  
 
 131  
             try
 132  
             {
 133  9
                 resolver.resolve( artifact, remoteRepositories, localRepository );
 134  
 
 135  7
                 if ( artifact.getFile() != null )
 136  
                 {
 137  6
                     location = new ArtifactLocation( artifact, locationSpecification );
 138  
                 }
 139  
                 else
 140  
                 {
 141  1
                     messageHolder.addMessage( "Supposedly resolved artifact: " + artifact.getId()
 142  
                         + " does not have an associated file." );
 143  
                 }
 144  
             }
 145  1
             catch ( ArtifactResolutionException e )
 146  
             {
 147  1
                 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
 148  
                     + locationSpecification, e );
 149  
             }
 150  1
             catch ( ArtifactNotFoundException e )
 151  
             {
 152  1
                 messageHolder.addMessage( "Failed to resolve artifact: " + artifact.getId() + " for location: "
 153  
                     + locationSpecification, e );
 154  8
             }
 155  9
         }
 156  
         else
 157  
         {
 158  2
             messageHolder.addMessage( "Invalid artifact specification: \'" + locationSpecification
 159  
                 + "\'. Must contain at least three fields, separated by ':'." );
 160  
         }
 161  
 
 162  11
         return location;
 163  
     }
 164  
 
 165  
 }