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.apache.maven.shared.utils.io.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
39 * use
40 *
41 * @plexus.component role="org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer" role-hint="bytecode"
42 */
43 public class JarBytecodeHashAnalyzer
44 extends AbstractLogEnabled
45 implements JarHashAnalyzer
46 {
47 /**
48 * The streaming digester to use for computing the hash. Under Plexus, the default is SHA-1.
49 *
50 * @plexus.requirement role-hint="sha1"
51 */
52 private StreamingDigester digester;
53
54 public String computeHash( JarAnalyzer jarAnalyzer )
55 {
56 JarData jarData = jarAnalyzer.getJarData();
57
58 String result = jarData.getBytecodeHash();
59 if ( result == null )
60 {
61 Iterator it = jarAnalyzer.getClassEntries().iterator();
62
63 try
64 {
65 digester.reset();
66 while ( it.hasNext() )
67 {
68 JarEntry entry = (JarEntry) it.next();
69 computeEntryBytecodeHash( jarAnalyzer.getEntryInputStream( entry ) );
70 }
71 result = digester.calc();
72 jarData.setBytecodeHash( result );
73 }
74 catch ( DigesterException e )
75 {
76 getLogger().warn( "Unable to calculate the hashcode.", e );
77 }
78 catch ( IOException e )
79 {
80 getLogger().warn( "Unable to calculate the hashcode.", e );
81 }
82 }
83 return result;
84 }
85
86 private void computeEntryBytecodeHash( InputStream is )
87 throws IOException, DigesterException
88 {
89 try
90 {
91 digester.update( is );
92 }
93 finally
94 {
95 IOUtil.close( is );
96 }
97 }
98
99 public void setDigester( StreamingDigester digester )
100 {
101 this.digester = digester;
102 }
103 }