implements RepositoryConverter
{
/**
- * @plexus.requirement
+ * @plexus.requirement role-hint="sha1"
+ */
+ private Digester sha1Digester;
+
+ /**
+ * @plexus.requirement role-hint="md5"
*/
- private Digester digester;
+ private Digester md5Digester;
/**
* @plexus.requirement
{
boolean result =
- verifyChecksum( file, file.getName() + ".md5", Digester.MD5, reporter, artifact, "failure.incorrect.md5" );
- result = result && verifyChecksum( file, file.getName() + ".sha1", Digester.SHA1, reporter, artifact,
+ verifyChecksum( file, file.getName() + ".md5", md5Digester, reporter, artifact, "failure.incorrect.md5" );
+ result = result && verifyChecksum( file, file.getName() + ".sha1", sha1Digester, reporter, artifact,
"failure.incorrect.sha1" );
return result;
}
- private boolean verifyChecksum( File file, String fileName, String algorithm, ArtifactReporter reporter,
+ private boolean verifyChecksum( File file, String fileName, Digester digester, ArtifactReporter reporter,
Artifact artifact, String key )
throws IOException
{
boolean result = true;
- File md5 = new File( file.getParentFile(), fileName );
- if ( md5.exists() )
+ File checksumFile = new File( file.getParentFile(), fileName );
+ if ( checksumFile.exists() )
{
- String checksum = FileUtils.fileRead( md5 );
+ String checksum = FileUtils.fileRead( checksumFile );
try
{
- digester.verifyChecksum( file, checksum, algorithm );
+ digester.verify( file, checksum );
}
catch ( DigesterException e )
{
<requirements>
<requirement>
<role>org.apache.maven.repository.digest.Digester</role>
- <field-name>digester</field-name>
+ <role-hint>sha1</role-hint>
+ <field-name>sha1Digester</field-name>
+ </requirement>
+ <requirement>
+ <role>org.apache.maven.repository.digest.Digester</role>
+ <role-hint>md5</role-hint>
+ <field-name>md5Digester</field-name>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
<requirements>
<requirement>
<role>org.apache.maven.repository.digest.Digester</role>
- <field-name>digester</field-name>
+ <role-hint>sha1</role-hint>
+ <field-name>sha1Digester</field-name>
+ </requirement>
+ <requirement>
+ <role>org.apache.maven.repository.digest.Digester</role>
+ <role-hint>md5</role-hint>
+ <field-name>md5Digester</field-name>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
}
scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
+ // TODO: Correct for extremely large repositories (artifact counts over 200,000 entries)
scanner.scan();
for ( Iterator files = Arrays.asList( scanner.getExcludedFiles() ).iterator(); files.hasNext(); )
extends AbstractLogEnabled
implements RepositoryIndexRecordFactory
{
- /**
- * @plexus.requirement
- */
- private Digester digester;
-
- protected String readChecksum( File file, String algorithm )
+ protected String readChecksum( File file, Digester digester )
{
String checksum;
try
{
- checksum = digester.createChecksum( file, algorithm ).toLowerCase();
+ checksum = digester.calc( file ).toLowerCase();
}
catch ( DigesterException e )
{
{
/* List of types to index. */
private static final Set INDEXED_TYPES = new HashSet( Arrays.asList( new String[]{"jar", "maven-plugin"} ) );
+ /**
+ * @plexus.requirement role-hint="sha1"
+ */
+ protected Digester sha1Digester;
+ /**
+ * @plexus.requirement role-hint="md5"
+ */
+ protected Digester md5Digester;
public RepositoryIndexRecord createRecord( Artifact artifact )
{
File file = artifact.getFile();
if ( file != null && INDEXED_TYPES.contains( artifact.getType() ) && file.exists() )
{
- String md5 = readChecksum( file, Digester.MD5 );
+ String md5 = readChecksum( file, md5Digester );
List files = null;
try
* @todo this should be smarter (perhaps use plexus archiver to look for an unarchiver, and make the ones for zip configurable since sar, par, etc can be added at random.
*/
private static final Set ARCHIVE_TYPES =
- new HashSet( Arrays.asList( new String[]{"jar", "zip", "ejb", "par", "sar", "war", "ear"} ) );
+ new HashSet( Arrays.asList( new String[]{"jar", "ejb", "par", "sar", "war", "ear", "rar"} ) );
/**
* @plexus.requirement
*/
private MavenProjectBuilder projectBuilder;
+ /**
+ * @plexus.requirement role-hint="sha1"
+ */
+ protected Digester sha1Digester;
+
+ /**
+ * @plexus.requirement role-hint="md5"
+ */
+ protected Digester md5Digester;
+
private static final String PLUGIN_METADATA_NAME = "META-INF/maven/plugin.xml";
private static final String ARCHETYPE_METADATA_NAME = "META-INF/maven/archetype.xml";
StandardArtifactIndexRecord record = null;
File file = artifact.getFile();
+
// TODO: is this condition really a possibility?
if ( file != null && file.exists() )
{
- String md5 = readChecksum( file, Digester.MD5 );
- String sha1 = readChecksum( file, Digester.SHA1 );
-
+ String md5 = readChecksum( file, md5Digester );
+ String sha1 = readChecksum( file, sha1Digester );
+
List files = null;
boolean archive = ARCHIVE_TYPES.contains( artifact.getType() );
try
implements ArtifactReportProcessor
{
/**
- * @plexus.requirement
+ * @plexus.requirement role-hint="sha1"
*/
- private Digester digester;
+ private Digester sha1Digester;
+ /**
+ * @plexus.requirement role-hint="md5"
+ */
+ private Digester md5Digester;
+
/**
* Validate the checksum of the specified artifact.
*
String path = repository.pathOf( artifact );
File file = new File( repository.getBasedir(), path );
- verifyChecksum( repository, path + ".md5", file, Digester.MD5, reporter, artifact );
- verifyChecksum( repository, path + ".sha1", file, Digester.SHA1, reporter, artifact );
+ verifyChecksum( repository, path + ".md5", file, md5Digester, reporter, artifact );
+ verifyChecksum( repository, path + ".sha1", file, sha1Digester, reporter, artifact );
}
- private void verifyChecksum( ArtifactRepository repository, String path, File file, String checksumAlgorithm,
+ private void verifyChecksum( ArtifactRepository repository, String path, File file, Digester digester,
ArtifactReporter reporter, Artifact artifact )
{
File checksumFile = new File( repository.getBasedir(), path );
{
try
{
- digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
+ digester.verify( file, FileUtils.fileRead( checksumFile ) );
reporter.addSuccess( artifact );
}
}
else
{
- reporter.addFailure( artifact, checksumAlgorithm + " checksum file does not exist." );
+ reporter.addFailure( artifact, digester.getAlgorithm() + " checksum file does not exist." );
}
}
}
implements MetadataReportProcessor
{
/**
- * @plexus.requirement
+ * @plexus.requirement role-hint="sha1"
*/
- private Digester digester;
+ private Digester sha1Digester;
+ /**
+ * @plexus.requirement role-hint="md5"
+ */
+ private Digester md5Digester;
+
/**
* Validate the checksums of the metadata. Get the metadata file from the
* repository then validate the checksum.
String path = repository.pathOfRemoteRepositoryMetadata( metadata );
File file = new File( repository.getBasedir(), path );
- verifyChecksum( repository, path + ".md5", file, Digester.MD5, reporter, metadata );
- verifyChecksum( repository, path + ".sha1", file, Digester.SHA1, reporter, metadata );
+ verifyChecksum( repository, path + ".md5", file, md5Digester, reporter, metadata );
+ verifyChecksum( repository, path + ".sha1", file, sha1Digester, reporter, metadata );
}
- private void verifyChecksum( ArtifactRepository repository, String path, File file, String checksumAlgorithm,
+ private void verifyChecksum( ArtifactRepository repository, String path, File file, Digester digester,
ArtifactReporter reporter, RepositoryMetadata metadata )
{
File checksumFile = new File( repository.getBasedir(), path );
{
try
{
- digester.verifyChecksum( file, FileUtils.fileRead( checksumFile ), checksumAlgorithm );
+ digester.verify( file, FileUtils.fileRead( checksumFile ) );
reporter.addSuccess( metadata );
}
}
else
{
- reporter.addFailure( metadata, checksumAlgorithm + " checksum file does not exist." );
+ reporter.addFailure( metadata, digester.getAlgorithm() + " checksum file does not exist." );
}
}
* limitations under the License.
*/
+import java.io.File;
+import java.util.Iterator;
+import java.util.List;
+
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
-import java.io.File;
-import java.util.Iterator;
-import java.util.List;
-
/**
* Validates an artifact file for duplicates within the same groupId based from what's available in a repository index.
*
implements ArtifactReportProcessor
{
/**
- * @plexus.requirement
+ * @plexus.requirement role-hint="md5"
*/
private Digester digester;
String checksum;
try
{
- checksum = digester.createChecksum( artifact.getFile(), Digester.MD5 );
+ checksum = digester.calc( artifact.getFile() );
}
catch ( DigesterException e )
{
private static final String metadataChecksumFilename = "maven-metadata";
- private Digester digester;
+ private Digester sha1Digest;
+
+ private Digester md5Digest;
public void setUp()
throws Exception
{
super.setUp();
- digester = (Digester) lookup( Digester.ROLE );
+ sha1Digest = (Digester) lookup( Digester.ROLE, "sha1" );
+ md5Digest = (Digester) lookup( Digester.ROLE, "md5" );
}
/**
File file = new File( path + ".md5" );
OutputStream os = new FileOutputStream( file );
OutputStreamWriter osw = new OutputStreamWriter( os );
- String sum = digester.createChecksum( new File( path ), Digester.MD5 );
+ String sum = md5Digest.calc( new File( path ) );
if ( !isValid )
{
osw.write( sum + "1" );
file = new File( path + ".sha1" );
os = new FileOutputStream( file );
osw = new OutputStreamWriter( os );
- String sha1sum = digester.createChecksum( new File( path ), Digester.SHA1 );
+ String sha1sum = sha1Digest.calc( new File( path ) );
if ( !isValid )
{
osw.write( sha1sum + "2" );
FileUtils.copyFile( new File( url ), new File( path ) );
//Create md5 and sha-1 checksum files..
-
File file = new File( path + ".md5" );
OutputStream os = new FileOutputStream( file );
OutputStreamWriter osw = new OutputStreamWriter( os );
- String md5sum = digester.createChecksum( new File( path ), Digester.MD5 );
+ String md5sum = md5Digest.calc( new File( path ) );
if ( !isValid )
{
osw.write( md5sum + "1" );
file = new File( path + ".sha1" );
os = new FileOutputStream( file );
osw = new OutputStreamWriter( os );
- String sha1sum = digester.createChecksum( new File( path ), Digester.SHA1 );
+ String sha1sum = sha1Digest.calc( new File( path ) );
if ( !isValid )
{
osw.write( sha1sum + "2" );
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.StringUtils;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Create a digest for a file.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public abstract class AbstractDigester
+ implements Digester
+{
+ private final StreamingDigester streamingDigester;
+
+ protected AbstractDigester( StreamingDigester streamingDigester )
+ throws NoSuchAlgorithmException
+ {
+ this.streamingDigester = streamingDigester;
+ }
+
+ public String getAlgorithm()
+ {
+ return streamingDigester.getAlgorithm();
+ }
+
+ public String calc( File file )
+ throws DigesterException
+ {
+ FileInputStream fis = null;
+ try
+ {
+ fis = new FileInputStream( file );
+ streamingDigester.reset();
+ streamingDigester.update( fis );
+ return streamingDigester.calc();
+ }
+ catch ( IOException e )
+ {
+ throw new DigesterException( "Unable to calculate the " + streamingDigester.getAlgorithm() +
+ " hashcode for " + file.getAbsolutePath() + ": " + e.getMessage(), e );
+ }
+ finally
+ {
+ IOUtil.close( fis );
+ }
+ }
+
+ public void verify( File file, String checksum )
+ throws DigesterException
+ {
+ String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
+
+ String algorithm = streamingDigester.getAlgorithm();
+
+ // Free-BSD / openssl
+ Matcher m = Pattern.compile( algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-zA-Z0-9]+)" )
+ .matcher( trimmedChecksum );
+ if ( m.matches() )
+ {
+ String filename = m.group( 1 );
+ if ( !filename.equals( file.getName() ) )
+ {
+ throw new DigesterException( "Supplied checksum does not match checksum pattern" );
+ }
+ trimmedChecksum = m.group( 2 );
+ }
+ else
+ {
+ // GNU tools
+ m = Pattern.compile( "([a-zA-Z0-9]+)\\s\\*?(.+)" ).matcher( trimmedChecksum );
+ if ( m.matches() )
+ {
+ String filename = m.group( 2 );
+ if ( !filename.equals( file.getName() ) )
+ {
+ throw new DigesterException( "Supplied checksum does not match checksum pattern" );
+ }
+ trimmedChecksum = m.group( 1 );
+ }
+ }
+
+ //Create checksum for jar file
+ String sum = calc( file );
+ if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) )
+ {
+ throw new DigesterException( "Checksum failed" );
+ }
+ }
+
+ public String toString()
+ {
+ return "[Digester:" + streamingDigester.getAlgorithm() + "]";
+ }
+}
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Gradually create a digest for a stream.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public abstract class AbstractStreamingDigester
+ implements StreamingDigester
+{
+ protected final MessageDigest md;
+
+ private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();
+
+ private static final int HI_MASK = 0xF0;
+
+ private static final int LO_MASK = 0x0F;
+
+ private static final int BUFFER_SIZE = 32768;
+
+ protected AbstractStreamingDigester( String algorithm )
+ throws NoSuchAlgorithmException
+ {
+ md = MessageDigest.getInstance( algorithm );
+ }
+
+ public String getAlgorithm()
+ {
+ return md.getAlgorithm();
+ }
+
+ public String calc()
+ throws DigesterException
+ {
+ return calc( this.md );
+ }
+
+ public void reset()
+ throws DigesterException
+ {
+ md.reset();
+ }
+
+ public void update( InputStream is )
+ throws DigesterException
+ {
+ update( is, md );
+ }
+
+ protected static String calc( MessageDigest md )
+ {
+ byte[] digest = md.digest();
+
+ char[] hash = new char[digest.length * 2];
+ for ( int i = 0; i < digest.length; i++ )
+ {
+ hash[i * 2] = HEX_CHARS[( digest[i] & HI_MASK ) >> 4];
+ hash[i * 2 + 1] = HEX_CHARS[( digest[i] & LO_MASK )];
+ }
+ return new String( hash );
+ }
+
+ protected static void update( InputStream is, MessageDigest digest )
+ throws DigesterException
+ {
+ try
+ {
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int size = is.read( buffer, 0, BUFFER_SIZE );
+ while ( size >= 0 )
+ {
+ digest.update( buffer, 0, size );
+ size = is.read( buffer, 0, BUFFER_SIZE );
+ }
+ }
+ catch ( IOException e )
+ {
+ throw new DigesterException( "Unable to update " + digest.getAlgorithm() + " hash: " + e.getMessage(), e );
+ }
+ }
+}
+++ /dev/null
-package org.apache.maven.repository.digest;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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.
- */
-
-import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.StringUtils;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * Create a digest for a file.
- *
- * @author <a href="mailto:brett@apache.org">Brett Porter</a>
- * @plexus.component role="org.apache.maven.repository.digest.Digester"
- */
-public class DefaultDigester
- implements Digester
-{
- private static final int CHECKSUM_BUFFER_SIZE = 16384;
-
- private static final int BYTE_MASK = 0xFF;
-
- public String createChecksum( File file, String algorithm )
- throws DigesterException
- {
- MessageDigest digest;
- try
- {
- digest = MessageDigest.getInstance( algorithm );
- }
- catch ( NoSuchAlgorithmException e )
- {
- throw new DigesterException( "Specified algorithm not found: " + algorithm, e );
- }
-
- InputStream fis;
- try
- {
- fis = new FileInputStream( file );
- }
- catch ( FileNotFoundException e )
- {
- throw new DigesterException( "Specified file not found: " + file.getAbsolutePath(), e );
- }
-
- try
- {
- byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
- int numRead;
- do
- {
- numRead = fis.read( buffer );
- if ( numRead > 0 )
- {
- digest.update( buffer, 0, numRead );
- }
- }
- while ( numRead != -1 );
- }
- catch ( IOException e )
- {
- throw new DigesterException( "Failed to read from file: " + file.getAbsolutePath(), e );
- }
- finally
- {
- IOUtil.close( fis );
- }
-
- return byteArrayToHexStr( digest.digest() );
- }
-
- public void verifyChecksum( File file, String checksum, String algorithm )
- throws DigesterException
- {
- String trimmedChecksum = checksum.replace( '\n', ' ' ).trim();
- // Free-BSD / openssl
- Matcher m =
- Pattern.compile( algorithm.replaceAll( "-", "" ) + "\\s*\\((.*?)\\)\\s*=\\s*([a-zA-Z0-9]+)" ).matcher(
- trimmedChecksum );
- if ( m.matches() )
- {
- String filename = m.group( 1 );
- if ( !filename.equals( file.getName() ) )
- {
- throw new DigesterException( "Supplied checksum does not match checksum pattern" );
- }
- trimmedChecksum = m.group( 2 );
- }
- else
- {
- // GNU tools
- m = Pattern.compile( "([a-zA-Z0-9]+)\\s\\*?(.+)" ).matcher( trimmedChecksum );
- if ( m.matches() )
- {
- String filename = m.group( 2 );
- if ( !filename.equals( file.getName() ) )
- {
- throw new DigesterException( "Supplied checksum does not match checksum pattern" );
- }
- trimmedChecksum = m.group( 1 );
- }
- }
-
- //Create checksum for jar file
- String sum = createChecksum( file, algorithm );
- if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) )
- {
- throw new DigesterException( "Checksum failed" );
- }
- }
-
- /**
- * Convert an incoming array of bytes into a string that represents each of
- * the bytes as two hex characters.
- *
- * @param data
- */
- private static String byteArrayToHexStr( byte[] data )
- {
- String output = "";
-
- for ( int cnt = 0; cnt < data.length; cnt++ )
- {
- //Deposit a byte into the 8 lsb of an int.
- int tempInt = data[cnt] & BYTE_MASK;
-
- //Get hex representation of the int as a string.
- String tempStr = Integer.toHexString( tempInt );
-
- //Append a leading 0 if necessary so that each hex string will contain 2 characters.
- if ( tempStr.length() == 1 )
- {
- tempStr = "0" + tempStr;
- }
-
- //Concatenate the two characters to the output string.
- output = output + tempStr;
- }
-
- return output.toUpperCase();
- }
-}
*/
import java.io.File;
+import java.io.InputStream;
/**
* Create a digest for a file.
{
String ROLE = Digester.class.getName();
- String SHA1 = "SHA-1";
+ /**
+ * Get the algorithm used for the checksum.
+ *
+ * @return the algorithm
+ */
+ String getAlgorithm();
- String MD5 = "MD5";
-
- String createChecksum( File file, String algorithm )
+ /**
+ * Calculate a checksum for a file.
+ *
+ * @param file the file to calculate the checksum for
+ * @return the current checksum.
+ * @throws DigesterException if there was a problem computing the hashcode.
+ */
+ String calc( File file )
throws DigesterException;
- void verifyChecksum( File file, String checksum, String algorithm )
+ /**
+ * Verify that a checksum is correct.
+ *
+ * @param file the file to compute the checksum for
+ * @param checksum the checksum to compare to
+ * @throws DigesterException if there was a problem computing the hashcode.
+ */
+ void verify( File file, String checksum )
throws DigesterException;
}
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Digester that does MD5 Message Digesting Only.
+ *
+ * @plexus.component role="org.apache.maven.repository.digest.Digester" role-hint="md5"
+ */
+public class Md5Digester
+ extends AbstractDigester
+{
+ public Md5Digester()
+ throws NoSuchAlgorithmException
+ {
+ super( new StreamingMd5Digester() );
+ }
+}
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Digester that does SHA1 Message Digesting Only.
+ *
+ * @plexus.component role="org.apache.maven.repository.digest.Digester" role-hint="sha1"
+ */
+public class Sha1Digester
+ extends AbstractDigester
+{
+ public Sha1Digester()
+ throws NoSuchAlgorithmException
+ {
+ super( new StreamingSha1Digester() );
+ }
+}
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.io.InputStream;
+
+/**
+ * Gradually create a digest for a stream.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public interface StreamingDigester
+{
+ String ROLE = StreamingDigester.class.getName();
+
+ /**
+ * Get the algorithm used for the checksum.
+ *
+ * @return the algorithm
+ */
+ String getAlgorithm();
+
+ /**
+ * Reset the hashcode calculation algorithm.
+ * Only useful when performing incremental hashcodes based on repeated use of {@link #update(InputStream)}
+ *
+ * @throws DigesterException if there was a problem with the internal message digest
+ */
+ void reset()
+ throws DigesterException;
+
+ /**
+ * Calculate the current checksum.
+ *
+ * @return the current checksum.
+ * @throws DigesterException if there was a problem computing the hashcode.
+ */
+ String calc()
+ throws DigesterException;
+
+ /**
+ * Update the checksum with the content of the input stream.
+ *
+ * @param is the input stream
+ * @throws DigesterException if there was a problem computing the hashcode.
+ */
+ void update( InputStream is )
+ throws DigesterException;
+
+}
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * An MD5 implementation of the streaming digester.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ * @plexus.component role="org.apache.maven.repository.digest.StreamingDigester" role-hint="md5"
+ */
+public class StreamingMd5Digester
+ extends AbstractStreamingDigester
+{
+ public StreamingMd5Digester()
+ throws NoSuchAlgorithmException
+ {
+ super( "MD5" );
+ }
+}
--- /dev/null
+package org.apache.maven.repository.digest;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * An SHA-1 implementation of the streaming digester.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ * @plexus.component role="org.apache.maven.repository.digest.StreamingDigester" role-hint="sha1"
+ */
+public class StreamingSha1Digester
+ extends AbstractStreamingDigester
+{
+ public StreamingSha1Digester()
+ throws NoSuchAlgorithmException
+ {
+ super( "SHA-1" );
+ }
+}
* limitations under the License.
*/
-import junit.framework.TestCase;
+import org.codehaus.plexus.PlexusTestCase;
import java.io.File;
import java.io.IOException;
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
*/
public class DigesterTest
- extends TestCase
+ extends PlexusTestCase
{
- private Digester digester = new DefaultDigester();
-
private static final String MD5 = "adbc688ce77fa2aece4bb72cad9f98ba";
private static final String SHA1 = "2a7b459938e12a2dc35d1bf6cff35e9c2b592fa9";
private static final String WRONG_SHA1 = "4d8703779816556cdb8be7f6bb5c954f4b5730e2";
+ private Digester sha1Digest;
+
+ private Digester md5Digest;
+
+ protected void setUp()
+ throws Exception
+ {
+ super.setUp();
+
+ sha1Digest = (Digester) lookup( Digester.ROLE, "sha1" );
+ md5Digest = (Digester) lookup( Digester.ROLE, "md5" );
+ }
+
+ public void testAlgorithm()
+ {
+ assertEquals( "SHA-1", sha1Digest.getAlgorithm() );
+ assertEquals( "MD5", md5Digest.getAlgorithm() );
+ }
+
public void testBareDigestFormat()
throws DigesterException, IOException
{
try
{
- digester.verifyChecksum( file, MD5, Digester.MD5 );
+ md5Digest.verify( file, MD5 );
}
catch ( DigesterException e )
{
try
{
- digester.verifyChecksum( file, SHA1, Digester.SHA1 );
+ sha1Digest.verify( file, SHA1 );
}
catch ( DigesterException e )
{
try
{
- digester.verifyChecksum( file, WRONG_SHA1, Digester.SHA1 );
+ sha1Digest.verify( file, WRONG_SHA1 );
fail( "wrong checksum must throw an exception" );
}
catch ( DigesterException e )
}
public void testOpensslDigestFormat()
- throws IOException
+ throws IOException, DigesterException
{
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
+
try
{
- digester.verifyChecksum( file, "MD5(test-file.txt)= " + MD5, Digester.MD5 );
+ md5Digest.verify( file, "MD5(test-file.txt)= " + MD5 );
}
catch ( DigesterException e )
{
try
{
- digester.verifyChecksum( file, "SHA1(test-file.txt)= " + SHA1, Digester.SHA1 );
+ md5Digest.verify( file, "MD5 (test-file.txt) = " + MD5 );
}
catch ( DigesterException e )
{
- fail( "OpenSSL SHA1 format must not cause exception" );
+ fail( "FreeBSD MD5 format must not cause exception" );
}
try
{
- digester.verifyChecksum( file, "MD5 (test-file.txt) = " + MD5, Digester.MD5 );
+ sha1Digest.verify( file, "SHA1(test-file.txt)= " + SHA1 );
}
catch ( DigesterException e )
{
- fail( "FreeBSD MD5 format must not cause exception" );
+ fail( "OpenSSL SHA1 format must not cause exception" );
}
try
{
- digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + SHA1, Digester.SHA1 );
+ sha1Digest.verify( file, "SHA1 (test-file.txt) = " + SHA1 );
}
catch ( DigesterException e )
{
try
{
- digester.verifyChecksum( file, "SHA1 (FOO) = " + SHA1, Digester.SHA1 );
+ sha1Digest.verify( file, "SHA1 (FOO) = " + SHA1 );
fail( "Wrong filename should cause an exception" );
}
catch ( DigesterException e )
try
{
- digester.verifyChecksum( file, "SHA1 (test-file.txt) = " + WRONG_SHA1, Digester.SHA1 );
+ sha1Digest.verify( file, "SHA1 (test-file.txt) = " + WRONG_SHA1 );
fail( "Wrong sha1 should cause an exception" );
}
catch ( DigesterException e )
}
public void testGnuDigestFormat()
- throws NoSuchAlgorithmException, IOException
+ throws NoSuchAlgorithmException, IOException, DigesterException
{
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
+
try
{
- digester.verifyChecksum( file, MD5 + " *test-file.txt", Digester.MD5 );
+ md5Digest.verify( file, MD5 + " *test-file.txt" );
}
catch ( DigesterException e )
{
try
{
- digester.verifyChecksum( file, SHA1 + " *test-file.txt", Digester.SHA1 );
+ md5Digest.verify( file, MD5 + " test-file.txt" );
}
catch ( DigesterException e )
{
- fail( "GNU format SHA1 must not cause exception" );
+ fail( "GNU text format MD5 must not cause exception" );
}
try
{
- digester.verifyChecksum( file, MD5 + " test-file.txt", Digester.MD5 );
+ sha1Digest.verify( file, SHA1 + " *test-file.txt" );
}
catch ( DigesterException e )
{
- fail( "GNU text format MD5 must not cause exception" );
+ fail( "GNU format SHA1 must not cause exception" );
}
try
{
- digester.verifyChecksum( file, SHA1 + " test-file.txt", Digester.SHA1 );
+ sha1Digest.verify( file, SHA1 + " test-file.txt" );
}
catch ( DigesterException e )
{
try
{
- digester.verifyChecksum( file, SHA1 + " FOO", Digester.SHA1 );
+ sha1Digest.verify( file, SHA1 + " FOO" );
fail( "Wrong filename cause an exception" );
}
catch ( DigesterException e )
try
{
- digester.verifyChecksum( file, WRONG_SHA1 + " test-file.txt", Digester.SHA1 );
+ sha1Digest.verify( file, WRONG_SHA1 + " test-file.txt" );
fail( "Wrong SHA1 cause an exception" );
}
catch ( DigesterException e )
File file = new File( getClass().getResource( "/test-file.txt" ).getPath() );
try
{
- digester.verifyChecksum( file, SHA1 + " *test-file.txt \n", Digester.SHA1 );
+ sha1Digest.verify( file, SHA1 + " *test-file.txt \n" );
}
catch ( DigesterException e )
{
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
- <configuration>
- <instrumentation>
- <!-- TODO: should this module have tests? -->
- <excludes>
- <exclude>**/**</exclude>
- </excludes>
- </instrumentation>
- </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>clean</goal>
+ </goals>
+ </execution>
+ </executions>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>