fix spring annotations for missing checksum consumer

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1179635 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-10-06 14:13:37 +00:00
parent 4515893178
commit d66e72fcd2
2 changed files with 48 additions and 64 deletions

View File

@ -34,6 +34,7 @@ import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -45,15 +46,15 @@ import java.util.List;
* *
* @version $Id$ * @version $Id$
*/ */
@Service("knownRepositoryContentConsumer#create-missing-checksums") @Service( "knownRepositoryContentConsumer#create-missing-checksums" )
@Scope("prototype") @Scope( "prototype" )
public class ArtifactMissingChecksumsConsumer public class ArtifactMissingChecksumsConsumer
extends AbstractMonitoredConsumer extends AbstractMonitoredConsumer
implements KnownRepositoryContentConsumer, RegistryListener implements KnownRepositoryContentConsumer, RegistryListener
{ {
private String id; private String id = "create-missing-checksums";
private String description; private String description = "Create Missing and/or Fix Invalid Checksums (.sha1, .md5)";
private ArchivaConfiguration configuration; private ArchivaConfiguration configuration;
@ -62,40 +63,38 @@ public class ArtifactMissingChecksumsConsumer
private ChecksummedFile checksum; private ChecksummedFile checksum;
private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file"; private static final String TYPE_CHECKSUM_NOT_FILE = "checksum-bad-not-file";
private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure"; private static final String TYPE_CHECKSUM_CANNOT_CALC = "checksum-calc-failure";
private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure"; private static final String TYPE_CHECKSUM_CANNOT_CREATE = "checksum-create-failure";
private File repositoryDir; private File repositoryDir;
private List<String> includes = new ArrayList<String>(); private List<String> includes = new ArrayList<String>( );
public ArtifactMissingChecksumsConsumer(String id, @Inject
String description, public ArtifactMissingChecksumsConsumer( ArchivaConfiguration configuration,
ArchivaConfiguration configuration, FileTypes filetypes )
FileTypes filetypes) { {
this.id = id;
this.description = description;
this.configuration = configuration; this.configuration = configuration;
this.filetypes = filetypes; this.filetypes = filetypes;
configuration.addChangeListener( this ); configuration.addChangeListener( this );
initIncludes(); initIncludes( );
} }
public String getId() public String getId( )
{ {
return this.id; return this.id;
} }
public String getDescription() public String getDescription( )
{ {
return this.description; return this.description;
} }
public boolean isPermanent() public boolean isPermanent( )
{ {
return false; return false;
} }
@ -103,7 +102,7 @@ public class ArtifactMissingChecksumsConsumer
public void beginScan( ManagedRepository repo, Date whenGathered ) public void beginScan( ManagedRepository repo, Date whenGathered )
throws ConsumerException throws ConsumerException
{ {
this.repositoryDir = new File( repo.getLocation() ); this.repositoryDir = new File( repo.getLocation( ) );
} }
public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo ) public void beginScan( ManagedRepository repo, Date whenGathered, boolean executeOnEntireRepo )
@ -112,22 +111,22 @@ public class ArtifactMissingChecksumsConsumer
beginScan( repo, whenGathered ); beginScan( repo, whenGathered );
} }
public void completeScan() public void completeScan( )
{ {
/* do nothing */ /* do nothing */
} }
public void completeScan( boolean executeOnEntireRepo ) public void completeScan( boolean executeOnEntireRepo )
{ {
completeScan(); completeScan( );
} }
public List<String> getExcludes() public List<String> getExcludes( )
{ {
return getDefaultArtifactExclusions(); return getDefaultArtifactExclusions( );
} }
public List<String> getIncludes() public List<String> getIncludes( )
{ {
return includes; return includes;
} }
@ -135,64 +134,64 @@ public class ArtifactMissingChecksumsConsumer
public void processFile( String path ) public void processFile( String path )
throws ConsumerException throws ConsumerException
{ {
createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.SHA1 } ); createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.SHA1 } );
createFixChecksum( path, new ChecksumAlgorithm[] { ChecksumAlgorithm.MD5 } ); createFixChecksum( path, new ChecksumAlgorithm[]{ ChecksumAlgorithm.MD5 } );
} }
public void processFile( String path, boolean executeOnEntireRepo ) public void processFile( String path, boolean executeOnEntireRepo )
throws ConsumerException throws ConsumerException
{ {
processFile( path ); processFile( path );
} }
private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] ) private void createFixChecksum( String path, ChecksumAlgorithm checksumAlgorithm[] )
{ {
File artifactFile = new File( this.repositoryDir, path ); File artifactFile = new File( this.repositoryDir, path );
File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt() ); File checksumFile = new File( this.repositoryDir, path + checksumAlgorithm[0].getExt( ) );
if( checksumFile.exists() ) if ( checksumFile.exists( ) )
{ {
checksum = new ChecksummedFile( artifactFile ); checksum = new ChecksummedFile( artifactFile );
try try
{ {
if( !checksum.isValidChecksum( checksumAlgorithm[0] ) ) if ( !checksum.isValidChecksum( checksumAlgorithm[0] ) )
{ {
checksum.fixChecksums( checksumAlgorithm ); checksum.fixChecksums( checksumAlgorithm );
triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath() ); triggerConsumerInfo( "Fixed checksum file " + checksumFile.getAbsolutePath( ) );
} }
} }
catch ( IOException e ) catch ( IOException e )
{ {
triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile + triggerConsumerError( TYPE_CHECKSUM_CANNOT_CALC, "Cannot calculate checksum for file " + checksumFile +
": " + e.getMessage() ); ": " + e.getMessage( ) );
} }
} }
else if( !checksumFile.exists() ) else if ( !checksumFile.exists( ) )
{ {
checksum = new ChecksummedFile( artifactFile ); checksum = new ChecksummedFile( artifactFile );
try try
{ {
checksum.createChecksum( checksumAlgorithm[0] ); checksum.createChecksum( checksumAlgorithm[0] );
triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath() ); triggerConsumerInfo( "Created missing checksum file " + checksumFile.getAbsolutePath( ) );
} }
catch ( IOException e ) catch ( IOException e )
{ {
triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile + triggerConsumerError( TYPE_CHECKSUM_CANNOT_CREATE, "Cannot create checksum for file " + checksumFile +
": " + e.getMessage() ); ": " + e.getMessage( ) );
} }
} }
else else
{ {
triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE, triggerConsumerWarning( TYPE_CHECKSUM_NOT_FILE,
"Checksum file " + checksumFile.getAbsolutePath() + " is not a file." ); "Checksum file " + checksumFile.getAbsolutePath( ) + " is not a file." );
} }
} }
public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
{ {
if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
{ {
initIncludes(); initIncludes( );
} }
} }
@ -201,18 +200,18 @@ public class ArtifactMissingChecksumsConsumer
/* do nothing */ /* do nothing */
} }
private void initIncludes() private void initIncludes( )
{ {
includes.clear(); includes.clear( );
includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) ); includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
} }
@PostConstruct @PostConstruct
public void initialize() public void initialize( )
{ {
configuration.addChangeListener( this ); configuration.addChangeListener( this );
initIncludes(); initIncludes( );
} }
} }

View File

@ -30,19 +30,4 @@
<context:annotation-config/> <context:annotation-config/>
<context:component-scan base-package="org.apache.archiva.consumers.core"/> <context:component-scan base-package="org.apache.archiva.consumers.core"/>
<bean id="artifactMissingChecksumsConsumer" class="org.apache.archiva.consumers.core.ArtifactMissingChecksumsConsumer" scope="prototype">
<constructor-arg>
<value>create-missing-checksums</value>
</constructor-arg>
<constructor-arg>
<value>Create Missing and/or Fix Invalid Checksums (.sha1, .md5)</value>
</constructor-arg>
<constructor-arg>
<ref bean="archivaConfiguration"/>
</constructor-arg>
<constructor-arg>
<ref bean="fileTypes"/>
</constructor-arg>
</bean>
<alias name="artifactMissingChecksumsConsumer" alias="knownRepositoryContentConsumer#artifact-missing-checksums-consumer"/>
</beans> </beans>