View Javadoc

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  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          JarData jarData = jarAnalyzer.getJarData();
49  
50          String result = jarData.getFileHash();
51          if ( result == null )
52          {
53              try
54              {
55                  result = digester.calc( jarData.getFile() );
56                  jarData.setFileHash( result );
57              }
58              catch ( DigesterException e )
59              {
60                  getLogger().warn( "Unable to calculate the hashcode.", e );
61              }
62          }
63          return result;
64      }
65  
66      public void setDigester( Digester digester )
67      {
68          this.digester = digester;
69      }
70  }