001    package org.apache.archiva.checksum;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    
024    import org.apache.commons.io.FilenameUtils;
025    
026    /**
027     * Enumeration of available ChecksumAlgorithm techniques.
028     *
029     *
030     */
031    public enum ChecksumAlgorithm {
032        SHA1("SHA-1", "sha1", "SHA1"),
033        MD5("MD5", "md5", "MD5");
034    
035        public static ChecksumAlgorithm getByExtension( File file )
036        {
037            String ext = FilenameUtils.getExtension( file.getName() ).toLowerCase();
038            if ( ChecksumAlgorithm.SHA1.getExt().equals( ext ) )
039            {
040                return ChecksumAlgorithm.SHA1;
041            }
042            else if ( ChecksumAlgorithm.MD5.getExt().equals( ext ) )
043            {
044                return ChecksumAlgorithm.MD5;
045            }
046    
047            throw new IllegalArgumentException( "Filename " + file.getName() + " has no associated extension." );
048        }
049    
050        /**
051         * The MessageDigest algorithm for this hash.
052         */
053        private String algorithm;
054    
055        /**
056         * The file extension for this ChecksumAlgorithm.
057         */
058        private String ext;
059    
060        /**
061         * The checksum type, the key that you see in checksum files.
062         */
063        private String type;
064    
065        /**
066         * Construct a ChecksumAlgorithm
067         * 
068         * @param algorithm the MessageDigest algorithm
069         * @param ext the file extension.
070         * @param type the checksum type.
071         */
072        private ChecksumAlgorithm( String algorithm, String ext, String type )
073        {
074            this.algorithm = algorithm;
075            this.ext = ext;
076            this.type = type;
077        }
078    
079        public String getAlgorithm()
080        {
081            return algorithm;
082        }
083    
084        public String getExt()
085        {
086            return ext;
087        }
088    
089        public String getType()
090        {
091            return type;
092        }
093        
094        
095    }