Coverage Report - org.apache.maven.index.DefaultArtifactContextProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArtifactContextProducer
95 %
38/40
87 %
21/24
6,667
 
 1  
 package org.apache.maven.index;
 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.io.File;
 23  
 
 24  
 import org.apache.maven.index.artifact.ArtifactPackagingMapper;
 25  
 import org.apache.maven.index.artifact.Gav;
 26  
 import org.apache.maven.index.context.IndexingContext;
 27  
 import org.apache.maven.index.locator.ArtifactLocator;
 28  
 import org.apache.maven.index.locator.GavHelpedLocator;
 29  
 import org.apache.maven.index.locator.Locator;
 30  
 import org.apache.maven.index.locator.MetadataLocator;
 31  
 import org.apache.maven.index.locator.PomLocator;
 32  
 import org.codehaus.plexus.component.annotations.Component;
 33  
 import org.codehaus.plexus.component.annotations.Requirement;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 
 36  
 /**
 37  
  * A default implementation of the {@link ArtifactContextProducer}.
 38  
  * 
 39  
  * @author Tamas Cservenak
 40  
  * @author Eugene Kuleshov
 41  
  */
 42  
 @Component( role = ArtifactContextProducer.class )
 43  208
 public class DefaultArtifactContextProducer
 44  
     implements ArtifactContextProducer
 45  
 {
 46  
     @Requirement
 47  
     private ArtifactPackagingMapper mapper;
 48  
 
 49  208
     private GavHelpedLocator pl = new PomLocator();
 50  
 
 51  208
     private Locator ml = new MetadataLocator();
 52  
 
 53  
     /**
 54  
      * Get ArtifactContext for given pom or artifact (jar, war, etc). A file can be
 55  
      */
 56  
     public ArtifactContext getArtifactContext( IndexingContext context, File file )
 57  
     {
 58  
         // TODO shouldn't this use repository layout instead?
 59  
 
 60  27789
         String repositoryPath = context.getRepository().getAbsolutePath();
 61  27789
         String artifactPath = file.getAbsolutePath();
 62  
 
 63  
         // protection from IndexOutOfBounds
 64  27789
         if ( artifactPath.length() <= repositoryPath.length() )
 65  
         {
 66  0
             return null; // not an artifact
 67  
         }
 68  
 
 69  27789
         if ( !isIndexable( file ) )
 70  
         {
 71  18116
             return null; // skipped
 72  
         }
 73  
 
 74  9673
         Gav gav = getGavFromPath( context, repositoryPath, artifactPath );
 75  
 
 76  9673
         if ( gav == null )
 77  
         {
 78  572
             return null; // not an artifact, but rather metadata
 79  
         }
 80  
 
 81  
         File pom;
 82  
         File artifact;
 83  
 
 84  9101
         if ( file.getName().endsWith( ".pom" ) )
 85  
         {
 86  3311
             ArtifactLocator al = new ArtifactLocator( mapper );
 87  3311
             artifact = al.locate( file, context.getGavCalculator(), gav );
 88  
 
 89  
             // If we found the matching artifact, switch over to indexing that, instead of the pom
 90  3311
             if ( artifact != null )
 91  
             {
 92  3165
                 gav = getGavFromPath( context, repositoryPath, artifact.getAbsolutePath() );
 93  
             }
 94  
 
 95  3311
             pom = file;
 96  3311
         }
 97  
         else
 98  
         {
 99  5790
             artifact = file;
 100  5790
             pom = pl.locate( file, context.getGavCalculator(), gav );
 101  
         }
 102  
 
 103  9101
         String groupId = gav.getGroupId();
 104  
 
 105  9101
         String artifactId = gav.getArtifactId();
 106  
 
 107  9101
         String version = gav.getBaseVersion();
 108  
 
 109  9101
         String classifier = gav.getClassifier();
 110  
 
 111  9101
         ArtifactInfo ai = new ArtifactInfo( context.getRepositoryId(), groupId, artifactId, version, classifier );
 112  
 
 113  
         // store extension if classifier is not empty
 114  9101
         if ( !StringUtils.isEmpty( ai.classifier ) )
 115  
         {
 116  2542
             ai.packaging = gav.getExtension();
 117  
         }
 118  
 
 119  9101
         ai.fname = file.getName();
 120  9101
         ai.fextension = gav.getExtension();
 121  
 
 122  9101
         File metadata = ml.locate( pom );
 123  
 
 124  9101
         return new ArtifactContext( pom, artifact, metadata, ai, gav );
 125  
     }
 126  
 
 127  
     private boolean isIndexable( File file )
 128  
     {
 129  27789
         if ( file == null )
 130  
         {
 131  0
             return false;
 132  
         }
 133  
 
 134  27789
         String filename = file.getName();
 135  
 
 136  27789
         if ( filename.equals( "maven-metadata.xml" )
 137  
         // || filename.endsWith( "-javadoc.jar" )
 138  
         // || filename.endsWith( "-javadocs.jar" )
 139  
         // || filename.endsWith( "-sources.jar" )
 140  
             || filename.endsWith( ".properties" )
 141  
             // || filename.endsWith( ".xml" ) // NEXUS-3029
 142  
             || filename.endsWith( ".asc" ) || filename.endsWith( ".md5" ) || filename.endsWith( ".sha1" ) )
 143  
         {
 144  18116
             return false;
 145  
         }
 146  
 
 147  9673
         return true;
 148  
     }
 149  
 
 150  
     private Gav getGavFromPath( IndexingContext context, String repositoryPath, String artifactPath )
 151  
     {
 152  12838
         String path = artifactPath.substring( repositoryPath.length() + 1 ).replace( '\\', '/' );
 153  
 
 154  12838
         return context.getGavCalculator().pathToGav( path );
 155  
     }
 156  
 
 157  
 }