Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JarFileHashAnalyzer |
|
| 2.0;2 |
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.Digester; | |
25 | import org.codehaus.plexus.digest.DigesterException; | |
26 | import org.codehaus.plexus.logging.AbstractLogEnabled; | |
27 | ||
28 | /** | |
29 | * Analyzer that calculates the hash code for the entire file. Can be used to detect an exact copy of the file. | |
30 | * <p/> | |
31 | * If you are not using Plexus, you must call {@link #setDigester(org.codehaus.plexus.digest.Digester)} before use | |
32 | * | |
33 | * @plexus.component role="org.apache.maven.shared.jar.identification.hash.JarHashAnalyzer" role-hint="file" | |
34 | */ | |
35 | 0 | public class JarFileHashAnalyzer |
36 | extends AbstractLogEnabled | |
37 | implements JarHashAnalyzer | |
38 | { | |
39 | /** | |
40 | * The digester to use for computing the hash. Under Plexus, the default is SHA-1. | |
41 | * | |
42 | * @plexus.requirement role-hint="sha1" | |
43 | */ | |
44 | private Digester digester; | |
45 | ||
46 | public String computeHash( JarAnalyzer jarAnalyzer ) | |
47 | { | |
48 | 0 | JarData jarData = jarAnalyzer.getJarData(); |
49 | ||
50 | 0 | String result = jarData.getFileHash(); |
51 | 0 | if ( result == null ) |
52 | { | |
53 | try | |
54 | { | |
55 | 0 | result = digester.calc( jarData.getFile() ); |
56 | 0 | jarData.setFileHash( result ); |
57 | } | |
58 | 0 | catch ( DigesterException e ) |
59 | { | |
60 | 0 | getLogger().warn( "Unable to calculate the hashcode.", e ); |
61 | 0 | } |
62 | } | |
63 | 0 | return result; |
64 | } | |
65 | ||
66 | public void setDigester( Digester digester ) | |
67 | { | |
68 | 0 | this.digester = digester; |
69 | 0 | } |
70 | } |