This project has retired. For details please refer to its Attic page.
ChecksumAlgorithm xref
View Javadoc
1   package org.apache.archiva.checksum;
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 java.io.File;
23  
24  import org.apache.commons.io.FilenameUtils;
25  
26  /**
27   * Enumeration of available ChecksumAlgorithm techniques.
28   *
29   *
30   */
31  public enum ChecksumAlgorithm {
32      SHA1("SHA-1", "sha1", "SHA1"),
33      MD5("MD5", "md5", "MD5");
34  
35      public static ChecksumAlgorithm getByExtension( File file )
36      {
37          String ext = FilenameUtils.getExtension( file.getName() ).toLowerCase();
38          if ( ChecksumAlgorithm.SHA1.getExt().equals( ext ) )
39          {
40              return ChecksumAlgorithm.SHA1;
41          }
42          else if ( ChecksumAlgorithm.MD5.getExt().equals( ext ) )
43          {
44              return ChecksumAlgorithm.MD5;
45          }
46  
47          throw new IllegalArgumentException( "Filename " + file.getName() + " has no associated extension." );
48      }
49  
50      /**
51       * The MessageDigest algorithm for this hash.
52       */
53      private final String algorithm;
54  
55      /**
56       * The file extension for this ChecksumAlgorithm.
57       */
58      private final String ext;
59  
60      /**
61       * The checksum type, the key that you see in checksum files.
62       */
63      private final String type;
64  
65      /**
66       * Construct a ChecksumAlgorithm
67       * 
68       * @param algorithm the MessageDigest algorithm
69       * @param ext the file extension.
70       * @param type the checksum type.
71       */
72      private ChecksumAlgorithm( String algorithm, String ext, String type )
73      {
74          this.algorithm = algorithm;
75          this.ext = ext;
76          this.type = type;
77      }
78  
79      public String getAlgorithm()
80      {
81          return algorithm;
82      }
83  
84      public String getExt()
85      {
86          return ext;
87      }
88  
89      public String getType()
90      {
91          return type;
92      }
93      
94      
95  }