Author: krosenvold Date: Wed Jul 31 18:53:27 2013 New Revision: 1508987 URL: http://svn.apache.org/r1508987 Log: Made a DualDigester to avoid reading files twice
Added: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/DualDigester.java maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/DualDigesterTest.java maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/SimpleDigester.java - copied, changed from r1508731, maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java Removed: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java Modified: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java?rev=1508987&r1=1508986&r2=1508987&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/AbstractInstallMojo.java Wed Jul 31 18:53:27 2013 @@ -73,10 +73,7 @@ public abstract class AbstractInstallMoj @Parameter( property = "updateReleaseInfo", defaultValue = "false" ) protected boolean updateReleaseInfo; - protected final SimpleDigester md5Digester = SimpleDigester.md5(); - - protected final SimpleDigester sha1Digester = SimpleDigester.sha1(); - + protected final DualDigester digester = new DualDigester(); /** * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist * (yet). @@ -166,40 +163,41 @@ public abstract class AbstractInstallMoj boolean signatureFile = installedFile.getName().endsWith( ".asc" ); if ( installedFile.isFile() && !signatureFile ) { - installChecksum( installedFile, installedFile, md5Digester, ".md5" ); - installChecksum( installedFile, installedFile, sha1Digester, ".sha1" ); + + getLog().debug( "Calculating checksums for " + installedFile ); + digester.calculate( installedFile ); + installChecksum( installedFile, ".md5", digester.getMd5() ); + installChecksum( installedFile, ".sha1", digester.getSha1() ); } } /** * Installs a checksum for the specified file. * - * @param originalFile The path to the file from which the checksum is generated, must not be <code>null</code>. + * + * * @param installedFile The base path from which the path to the checksum files is derived by appending the given * file extension, must not be <code>null</code>. - * @param digester The checksum algorithm to use, must not be <code>null</code>. * @param ext The file extension (including the leading dot) to use for the checksum file, must not be * <code>null</code>. + * @param checksum * @throws MojoExecutionException If the checksum could not be installed. */ - private void installChecksum( File originalFile, File installedFile, SimpleDigester digester, String ext ) + private void installChecksum( File installedFile, String ext, String checksum ) throws MojoExecutionException { - String checksum; - getLog().debug( "Calculating " + digester.getAlgorithm() + " checksum for " + originalFile ); - checksum = digester.calculate( originalFile ); - File checksumFile = new File( installedFile.getAbsolutePath() + ext ); getLog().debug( "Installing checksum to " + checksumFile ); try { //noinspection ResultOfMethodCallIgnored checksumFile.getParentFile().mkdirs(); - FileUtils.fileWrite(checksumFile.getAbsolutePath(), "UTF-8", checksum); + FileUtils.fileWrite(checksumFile.getAbsolutePath(), "UTF-8", checksum ); } catch ( IOException e ) { throw new MojoExecutionException( "Failed to install checksum to " + checksumFile, e ); } } + } Added: maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/DualDigester.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/DualDigester.java?rev=1508987&view=auto ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/DualDigester.java (added) +++ maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/DualDigester.java Wed Jul 31 18:53:27 2013 @@ -0,0 +1,109 @@ +package org.apache.maven.plugin.install; + +import org.apache.commons.codec.binary.Hex; +import org.apache.maven.plugin.MojoExecutionException; +import org.codehaus.plexus.util.IOUtil; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Calculates md5 and sha1 digest. + * + * Todo: Consider using a thread to calculate one of the digests when the files are large; it's fairly slow ! + * @author Kristian Rosenvold + */ +public class DualDigester +{ + private final MessageDigest md5 = getDigester( "MD5" ); + + private final MessageDigest sh1 = getDigester( "SHA-1" ); + + private static final int bufsize = 65536*2; + + private final byte[] buffer = new byte[bufsize]; + + static MessageDigest getDigester( String algorithm ) + { + try + { + return MessageDigest.getInstance( algorithm ); + } + catch ( NoSuchAlgorithmException e ) + { + throw new RuntimeException( "Unable to initialize digest " + algorithm + " : " + + e.getMessage() ); + } + } + + public void calculate( File file ) throws MojoExecutionException + { + FileInputStream fis = null; + BufferedInputStream bis = null; + + try + { + fis = new FileInputStream( file ); + calculate( fis ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Failed to calculate digest checksum for " + file, e ); + } + finally + { + IOUtil.close( bis ); + IOUtil.close( fis ); + } + } + + void calculate( InputStream stream ) throws IOException + { + md5.reset(); + sh1.reset(); + update( stream ); + } + + public String getMd5(){ + return Hex.encodeHexString( md5.digest() ); + } + + public String getSha1(){ + return Hex.encodeHexString( sh1.digest() ); + } + + private void update( InputStream is ) + throws IOException { + int size = is.read( buffer, 0, bufsize ); + while ( size >= 0 ) + { + md5.update( buffer, 0, size ); + sh1.update( buffer, 0, size ); + size = is.read( buffer, 0, bufsize ); + } + } +} Added: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/DualDigesterTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/DualDigesterTest.java?rev=1508987&view=auto ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/DualDigesterTest.java (added) +++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/DualDigesterTest.java Wed Jul 31 18:53:27 2013 @@ -0,0 +1,22 @@ +package org.apache.maven.plugin.install; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import java.io.ByteArrayInputStream; + +public class DualDigesterTest extends TestCase +{ + public void testGetMd5() + throws Exception + { + DualDigester dualDigester = new DualDigester(); + dualDigester.calculate( new ByteArrayInputStream( "A Dog And A Cat".getBytes() ) ); + Assert.assertEquals( "39bc6b34be719cab3a3dc922445aae7c", dualDigester.getMd5() ); + Assert.assertEquals( "d07b1e7ecc7986b3f1126ddf1b67e3601ec362a9", dualDigester.getSha1() ); + dualDigester.calculate( new ByteArrayInputStream( "Yep, we do it again".getBytes() ) ); + Assert.assertEquals( "8cd83a9cbbd7076f668c2bcc0379ed49", dualDigester.getMd5() ); + Assert.assertEquals( "194ebcb8d168cffdc25c3b854d6187b568cf6273", dualDigester.getSha1() ); + } + +} Modified: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java?rev=1508987&r1=1508986&r2=1508987&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallFileMojoTest.java Wed Jul 31 18:53:27 2013 @@ -249,8 +249,9 @@ public class InstallFileMojoTest mojo.execute(); //get the actual checksum of the artifact - String actualMd5Sum = mojo.md5Digester.calculate( file ); - String actualSha1Sum = mojo.sha1Digester.calculate( file ); + mojo.digester.calculate( file ); + String actualMd5Sum = mojo.digester.getMd5( ); + String actualSha1Sum = mojo.digester.getSha1(); String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version; Modified: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java?rev=1508987&r1=1508986&r2=1508987&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/InstallMojoTest.java Wed Jul 31 18:53:27 2013 @@ -248,12 +248,14 @@ public class InstallMojoTest assertTrue( pom.exists() ); //get the actual checksum of the pom - String actualPomMd5Sum = mojo.md5Digester.calculate( pom ); - String actualPomSha1Sum = mojo.sha1Digester.calculate( pom ); + mojo.digester.calculate( pom ); + String actualPomMd5Sum = mojo.digester.getMd5(); + String actualPomSha1Sum = mojo.digester.getSha1( ); //get the actual checksum of the artifact - String actualMd5Sum = mojo.md5Digester.calculate( file ); - String actualSha1Sum = mojo.sha1Digester.calculate( file ); + mojo.digester.calculate( file ); + String actualMd5Sum = mojo.digester.getMd5(); + String actualSha1Sum = mojo.digester.getSha1(); String groupId = dotToSlashReplacer( artifact.getGroupId() ); Copied: maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/SimpleDigester.java (from r1508731, maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java) URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/SimpleDigester.java?p2=maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/SimpleDigester.java&p1=maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java&r1=1508731&r2=1508987&rev=1508987&view=diff ============================================================================== --- maven/plugins/trunk/maven-install-plugin/src/main/java/org/apache/maven/plugin/install/SimpleDigester.java (original) +++ maven/plugins/trunk/maven-install-plugin/src/test/java/org/apache/maven/plugin/install/SimpleDigester.java Wed Jul 31 18:53:27 2013 @@ -26,7 +26,6 @@ import org.codehaus.plexus.util.StringUt import java.io.*; import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,15 +38,7 @@ public class SimpleDigester { private static final int bufsize = 65536; public SimpleDigester(String algorithm) { - try - { - messageDigest = MessageDigest.getInstance( algorithm ); - } - catch ( NoSuchAlgorithmException e ) - { - throw new RuntimeException( "Unable to initialize digest " + algorithm + " : " - + e.getMessage() ); - } + messageDigest = DualDigester.getDigester( algorithm ); } public static SimpleDigester md5(){ @@ -58,10 +49,6 @@ public class SimpleDigester { return new SimpleDigester("SHA-1"); } - public String getAlgorithm() { - return messageDigest.getAlgorithm(); - } - public String calculate( File file ) throws MojoExecutionException { FileInputStream fis = null; BufferedInputStream bis = null;