Coverage Report - org.apache.maven.shared.jar.identification.hash.JarBytecodeHashAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
JarBytecodeHashAnalyzer
0%
0/24
0%
0/4
2.333
 
 1  
 package org.apache.maven.shared.jar.identification.hash;
 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 org.apache.maven.shared.jar.JarAnalyzer;
 23  
 import org.apache.maven.shared.jar.JarData;
 24  
 import org.codehaus.plexus.digest.DigesterException;
 25  
 import org.codehaus.plexus.digest.StreamingDigester;
 26  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 27  
 import org.codehaus.plexus.util.IOUtil;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.io.InputStream;
 31  
 import java.util.Iterator;
 32  
 import java.util.jar.JarEntry;
 33  
 
 34  
 /**
 35  
  * Analyzer that calculates the hash code for the entire file. Can be used to detect an exact copy of the file's class
 36  
  * data. Useful to see thru a recompile, recompression, or timestamp change.
 37  
  * <p/>
 38  
  * If you are not using Plexus, you must call {@link #setDigester(org.codehaus.plexus.digest.StreamingDigester)} before use
 39  
  *
 40  
  * @plexus.component role="org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer" role-hint="bytecode"
 41  
  */
 42  0
 public class JarBytecodeHashAnalyzer
 43  
     extends AbstractLogEnabled
 44  
     implements JarHashAnalyzer
 45  
 {
 46  
     /**
 47  
      * The streaming digester to use for computing the hash. Under Plexus, the default is SHA-1.
 48  
      *
 49  
      * @plexus.requirement role-hint="sha1"
 50  
      */
 51  
     private StreamingDigester digester;
 52  
 
 53  
     public String computeHash( JarAnalyzer jarAnalyzer )
 54  
     {
 55  0
         JarData jarData = jarAnalyzer.getJarData();
 56  
 
 57  0
         String result = jarData.getBytecodeHash();
 58  0
         if ( result == null )
 59  
         {
 60  0
             Iterator it = jarAnalyzer.getClassEntries().iterator();
 61  
 
 62  
             try
 63  
             {
 64  0
                 digester.reset();
 65  0
                 while ( it.hasNext() )
 66  
                 {
 67  0
                     JarEntry entry = (JarEntry) it.next();
 68  0
                     computeEntryBytecodeHash( jarAnalyzer.getEntryInputStream( entry ) );
 69  0
                 }
 70  0
                 result = digester.calc();
 71  0
                 jarData.setBytecodeHash( result );
 72  
             }
 73  0
             catch ( DigesterException e )
 74  
             {
 75  0
                 getLogger().warn( "Unable to calculate the hashcode.", e );
 76  
             }
 77  0
             catch ( IOException e )
 78  
             {
 79  0
                 getLogger().warn( "Unable to calculate the hashcode.", e );
 80  0
             }
 81  
         }
 82  0
         return result;
 83  
     }
 84  
 
 85  
     private void computeEntryBytecodeHash( InputStream is )
 86  
         throws IOException, DigesterException
 87  
     {
 88  
         try
 89  
         {
 90  0
             digester.update( is );
 91  
         }
 92  
         finally
 93  
         {
 94  0
             IOUtil.close( is );
 95  0
         }
 96  0
     }
 97  
 
 98  
     public void setDigester( StreamingDigester digester )
 99  
     {
 100  0
         this.digester = digester;
 101  0
     }
 102  
 }