1 package org.apache.maven.shared.jar.identification.hash;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41
42 public class JarBytecodeHashAnalyzer
43 extends AbstractLogEnabled
44 implements JarHashAnalyzer
45 {
46
47
48
49
50
51 private StreamingDigester digester;
52
53 public String computeHash( JarAnalyzer jarAnalyzer )
54 {
55 JarData jarData = jarAnalyzer.getJarData();
56
57 String result = jarData.getBytecodeHash();
58 if ( result == null )
59 {
60 Iterator it = jarAnalyzer.getClassEntries().iterator();
61
62 try
63 {
64 digester.reset();
65 while ( it.hasNext() )
66 {
67 JarEntry entry = (JarEntry) it.next();
68 computeEntryBytecodeHash( jarAnalyzer.getEntryInputStream( entry ) );
69 }
70 result = digester.calc();
71 jarData.setBytecodeHash( result );
72 }
73 catch ( DigesterException e )
74 {
75 getLogger().warn( "Unable to calculate the hashcode.", e );
76 }
77 catch ( IOException e )
78 {
79 getLogger().warn( "Unable to calculate the hashcode.", e );
80 }
81 }
82 return result;
83 }
84
85 private void computeEntryBytecodeHash( InputStream is )
86 throws IOException, DigesterException
87 {
88 try
89 {
90 digester.update( is );
91 }
92 finally
93 {
94 IOUtil.close( is );
95 }
96 }
97
98 public void setDigester( StreamingDigester digester )
99 {
100 this.digester = digester;
101 }
102 }