Coverage Report - org.apache.maven.index.creator.MavenArchetypeArtifactInfoIndexCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenArchetypeArtifactInfoIndexCreator
81 %
22/27
92 %
13/14
2,429
 
 1  
 package org.apache.maven.index.creator;
 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  
 import java.io.IOException;
 24  
 import java.util.Arrays;
 25  
 import java.util.Collection;
 26  
 import java.util.Collections;
 27  
 
 28  
 import org.apache.lucene.document.Document;
 29  
 import org.apache.maven.index.ArtifactContext;
 30  
 import org.apache.maven.index.ArtifactInfo;
 31  
 import org.apache.maven.index.IndexerField;
 32  
 import org.apache.maven.index.context.IndexCreator;
 33  
 import org.apache.maven.index.util.zip.ZipFacade;
 34  
 import org.apache.maven.index.util.zip.ZipHandle;
 35  
 import org.codehaus.plexus.component.annotations.Component;
 36  
 
 37  
 /**
 38  
  * A Maven Archetype index creator used to detect and correct the artifact packaging to "maven-archetype" if the
 39  
  * inspected JAR is an Archetype. Since packaging is already handled by Minimal creator, this Creator only alters the
 40  
  * supplied ArtifactInfo packaging field during processing, but does not interferes with Lucene document fill-up or the
 41  
  * ArtifactInfo fill-up (the update* methods are empty).
 42  
  * 
 43  
  * @author cstamas
 44  
  */
 45  
 @Component( role = IndexCreator.class, hint = MavenArchetypeArtifactInfoIndexCreator.ID )
 46  
 public class MavenArchetypeArtifactInfoIndexCreator
 47  
     extends AbstractIndexCreator
 48  
 {
 49  
     public static final String ID = "maven-archetype";
 50  
 
 51  
     private static final String MAVEN_ARCHETYPE_PACKAGING = "maven-archetype";
 52  
 
 53  1
     private static final String[] ARCHETYPE_XML_LOCATIONS = { "META-INF/maven/archetype.xml", "META-INF/archetype.xml",
 54  
         "META-INF/maven/archetype-metadata.xml" };
 55  
 
 56  
     public MavenArchetypeArtifactInfoIndexCreator()
 57  
     {
 58  200
         super( ID, Arrays.asList( MinimalArtifactInfoIndexCreator.ID ) );
 59  200
     }
 60  
 
 61  
     public void populateArtifactInfo( ArtifactContext ac )
 62  
     {
 63  5236
         File artifact = ac.getArtifact();
 64  
 
 65  5236
         ArtifactInfo ai = ac.getArtifactInfo();
 66  
 
 67  
         // we need the file to perform these checks, and those may be only JARs
 68  5236
         if ( artifact != null && artifact.isFile() && !MAVEN_ARCHETYPE_PACKAGING.equals( ai.packaging )
 69  
             && artifact.getName().endsWith( ".jar" ) )
 70  
         {
 71  
             // TODO: recheck, is the following true? "Maven plugins and Maven Archetypes can be only JARs?"
 72  
 
 73  
             // check for maven archetype, since Archetypes seems to not have consistent packaging,
 74  
             // and depending on the contents of the JAR, this call will override the packaging to "maven-archetype"!
 75  4161
             checkMavenArchetype( ai, artifact );
 76  
         }
 77  5236
     }
 78  
 
 79  
     /**
 80  
      * Archetypes that are added will have their packaging types set correctly (to maven-archetype)
 81  
      * 
 82  
      * @param ai
 83  
      * @param artifact
 84  
      */
 85  
     private void checkMavenArchetype( ArtifactInfo ai, File artifact )
 86  
     {
 87  4161
         ZipHandle handle = null;
 88  
 
 89  
         try
 90  
         {
 91  4161
             handle = ZipFacade.getZipHandle( artifact );
 92  
 
 93  15279
             for ( String path : ARCHETYPE_XML_LOCATIONS )
 94  
             {
 95  11573
                 if ( handle.hasEntry( path ) )
 96  
                 {
 97  364
                     ai.packaging = MAVEN_ARCHETYPE_PACKAGING;
 98  
 
 99  
                     return;
 100  
                 }
 101  
             }
 102  
         }
 103  91
         catch ( Exception e )
 104  
         {
 105  91
             if ( getLogger().isDebugEnabled() )
 106  
             {
 107  0
                 getLogger().info(
 108  
                     "Failed to parse Maven artifact " + artifact.getAbsolutePath() + " due to exception:", e );
 109  
             }
 110  
             else
 111  
             {
 112  91
                 getLogger().info(
 113  
                     "Failed to parse Maven artifact " + artifact.getAbsolutePath() + " due to " + e.getMessage() );
 114  
             }
 115  
         }
 116  
         finally
 117  
         {
 118  0
             try
 119  
             {
 120  4161
                 ZipFacade.close( handle );
 121  
             }
 122  0
             catch ( IOException ex )
 123  
             {
 124  4525
             }
 125  0
         }
 126  3797
     }
 127  
 
 128  
     public void updateDocument( ArtifactInfo ai, Document doc )
 129  
     {
 130  
         // nothing to update, minimal will maintain it.
 131  6348
     }
 132  
 
 133  
     public boolean updateArtifactInfo( Document doc, ArtifactInfo ai )
 134  
     {
 135  
         // nothing to update, minimal will maintain it.
 136  
 
 137  221519
         return false;
 138  
     }
 139  
 
 140  
     // ==
 141  
 
 142  
     @Override
 143  
     public String toString()
 144  
     {
 145  0
         return ID;
 146  
     }
 147  
 
 148  
     public Collection<IndexerField> getIndexerFields()
 149  
     {
 150  
         // it does not "add" any new field, it actually updates those already maintained by minimal creator.
 151  329
         return Collections.emptyList();
 152  
     }
 153  
 }