[MRM-1324] repository statistics report doesn't appear to be working correctly

Submitted by: Greg Michael Meneses



git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1140450 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2011-06-28 06:44:30 +00:00
parent c03b07252e
commit 2db95ef56d
9 changed files with 192 additions and 11 deletions

View File

@ -82,7 +82,9 @@ public class DefaultRepositoryScanner
File repositoryBase = new File( repository.getLocation() );
if ( !repositoryBase.exists() )
//MRM-1342 Repository statistics report doesn't appear to be working correctly
//create the repo if not existing to have an empty stats
if ( !repositoryBase.exists() && !repositoryBase.mkdirs() )
{
throw new UnsupportedOperationException( "Unable to scan a repository, directory "
+ repositoryBase.getPath() + " does not exist." );

View File

@ -19,6 +19,8 @@ package org.apache.maven.archiva.web.action.admin.repositories;
* under the License.
*/
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
@ -27,11 +29,12 @@ import org.apache.maven.archiva.security.ArchivaRoleConstants;
import org.codehaus.plexus.redback.role.RoleManager;
import org.codehaus.plexus.redback.role.RoleManagerException;
import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.taskqueue.TaskQueueException;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.io.IOException;
import javax.inject.Inject;
import javax.inject.Named;
/**
* Abstract ManagedRepositories Action.
@ -57,6 +60,13 @@ public abstract class AbstractManagedRepositoriesAction
@Inject
@Named( value = "commons-configuration" )
private Registry registry;
/**
* plexus.requirement role="org.apache.archiva.scheduler.ArchivaTaskScheduler" role-hint="repository"
*/
@Inject
@Named( value = "archivaTaskScheduler#repository" )
private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
public static final String CONFIRM = "confirm";
@ -74,6 +84,11 @@ public abstract class AbstractManagedRepositoriesAction
{
this.registry = registry;
}
public void setRepositoryTaskScheduler( RepositoryArchivaTaskScheduler repositoryTaskScheduler )
{
this.repositoryTaskScheduler = repositoryTaskScheduler;
}
protected void addRepository( ManagedRepositoryConfiguration repository, Configuration configuration )
throws IOException
@ -158,5 +173,19 @@ public abstract class AbstractManagedRepositoriesAction
value = StringUtils.replace( value, "${appserver.home}",
registry.getString( "appserver.home", "${appserver.home}" ) );
return value;
}
//MRM-1342 Repository statistics report doesn't appear to be working correctly
//provide a method to scan repository
protected void executeRepositoryScanner( String repoId )
throws TaskQueueException
{
RepositoryTask task = new RepositoryTask();
task.setRepositoryId( repoId );
if ( repositoryTaskScheduler.isProcessingRepositoryTask( repoId ) )
{
repositoryTaskScheduler.queueTask( task );
}
}
}

View File

@ -26,6 +26,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.codehaus.plexus.redback.role.RoleManagerException;
import org.codehaus.plexus.taskqueue.TaskQueueException;
import org.codehaus.redback.components.scheduler.CronExpressionValidator;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
@ -112,6 +113,27 @@ public class AddManagedRepositoryAction
}
result = saveConfiguration( configuration );
//MRM-1342 Repository statistics report doesn't appear to be working correctly
//scan repository when adding of repository is successful
if ( result.equals( SUCCESS ) )
{
try
{
executeRepositoryScanner( repository.getId() );
if ( stageNeeded )
{
ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig();
executeRepositoryScanner( stagingRepository.getId() );
}
}
catch ( TaskQueueException e )
{
log.warn( new StringBuilder( "Unable to scan repository [" ).append( repository.getId() ).append( "]: " ).append(
e.getMessage() ).toString(), e );
}
}
}
catch ( RoleManagerException e )
{

View File

@ -29,13 +29,14 @@ import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.codehaus.plexus.redback.role.RoleManagerException;
import org.codehaus.plexus.taskqueue.TaskQueueException;
import org.codehaus.redback.components.scheduler.CronExpressionValidator;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import javax.inject.Inject;
/**
* AddManagedRepositoryAction
@ -167,6 +168,27 @@ public class EditManagedRepositoryAction
repositoryStatisticsManager.deleteStatistics( repositorySession.getRepository(), repository.getId() );
repositorySession.save();
}
if ( result.equals( SUCCESS ) )
{
//MRM-1342 Repository statistics report doesn't appear to be working correctly
//scan repository when modification of repository is successful
if ( result.equals( SUCCESS ) )
{
try
{
executeRepositoryScanner( repository.getId() );
if ( stageNeeded )
{
executeRepositoryScanner( stagingRepository.getId() );
}
}
catch ( TaskQueueException e )
{
log.warn( new StringBuilder( "Unable to scan repository [" ).append( repository.getId() ).append( "]: " ).append(
e.getMessage() ).toString(), e );
}
}
}
}
catch ( IOException e )
{

View File

@ -20,6 +20,8 @@ package org.apache.maven.archiva.web.action.admin.repositories;
*/
import com.opensymphony.xwork2.Action;
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
@ -31,6 +33,7 @@ import org.codehaus.plexus.registry.Registry;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException;
import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import java.io.File;
import java.util.ArrayList;
@ -60,6 +63,10 @@ public class AddManagedRepositoryActionTest
private MockControl registryControl;
private ArchivaConfiguration archivaConfiguration;
private MockControl repositoryTaskSchedulerControl;
private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
@Override
protected void setUp()
@ -81,6 +88,10 @@ public class AddManagedRepositoryActionTest
registry = (Registry) registryControl.getMock();
action.setRegistry( registry );
repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
repositoryTaskScheduler = ( RepositoryArchivaTaskScheduler ) repositoryTaskSchedulerControl.getMock();
action.setRepositoryTaskScheduler( repositoryTaskScheduler );
location = new File( "target/test/location" );
}
@ -146,6 +157,14 @@ public class AddManagedRepositoryActionTest
registryControl.setReturnValue( "target/test" );
registryControl.replay();
RepositoryTask task = new RepositoryTask();
task.setRepositoryId( REPO_ID );
repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
repositoryTaskSchedulerControl.setReturnValue( false );
repositoryTaskScheduler.queueTask( task );
repositoryTaskSchedulerControl.setVoidCallable();
repositoryTaskSchedulerControl.replay();
Configuration configuration = new Configuration();
archivaConfiguration.getConfiguration();

View File

@ -24,6 +24,8 @@ import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.commons.io.FileUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration;
@ -35,6 +37,7 @@ import org.codehaus.plexus.registry.Registry;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException;
import org.easymock.MockControl;
import org.easymock.classextension.MockClassControl;
import java.io.File;
import java.io.IOException;
@ -70,6 +73,10 @@ public class EditManagedRepositoryActionTest
private MockControl registryControl;
private MetadataRepository metadataRepository;
private MockControl repositoryTaskSchedulerControl;
private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
@Override
protected void setUp()
@ -90,6 +97,10 @@ public class EditManagedRepositoryActionTest
registryControl = MockControl.createControl( Registry.class );
registry = (Registry) registryControl.getMock();
action.setRegistry( registry );
repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
repositoryTaskScheduler = ( RepositoryArchivaTaskScheduler ) repositoryTaskSchedulerControl.getMock();
action.setRepositoryTaskScheduler( repositoryTaskScheduler );
location = new File( "target/test/location" );
@ -148,10 +159,12 @@ public class EditManagedRepositoryActionTest
public void testEditRepository()
throws Exception
{
String stageRepoId = REPO_ID + "-stage";
roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
roleManagerControl.setReturnValue( false );
roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID + "-stage" );
roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, stageRepoId );
roleManagerControl.setReturnValue( false );
roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
@ -159,7 +172,7 @@ public class EditManagedRepositoryActionTest
roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
roleManagerControl.setReturnValue( false );
roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID + "-stage" );
roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, stageRepoId );
roleManagerControl.setReturnValue( false );
roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
@ -174,6 +187,22 @@ public class EditManagedRepositoryActionTest
registryControl.replay();
RepositoryTask task = new RepositoryTask();
task.setRepositoryId( REPO_ID );
repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
repositoryTaskSchedulerControl.setReturnValue( false );
repositoryTaskScheduler.queueTask( task );
repositoryTaskSchedulerControl.setVoidCallable();
RepositoryTask stageTask = new RepositoryTask();
stageTask.setRepositoryId( stageRepoId );
repositoryTaskScheduler.isProcessingRepositoryTask( stageRepoId );
repositoryTaskSchedulerControl.setReturnValue( false );
repositoryTaskScheduler.queueTask( stageTask );
repositoryTaskSchedulerControl.setVoidCallable();
repositoryTaskSchedulerControl.replay();
Configuration configuration = createConfigurationForEditing( createRepository() );
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration );
@ -248,6 +277,15 @@ public class EditManagedRepositoryActionTest
registryControl.replay();
RepositoryTask task = new RepositoryTask();
task.setRepositoryId( REPO_ID );
repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
repositoryTaskSchedulerControl.setReturnValue( false );
repositoryTaskScheduler.queueTask( task );
repositoryTaskSchedulerControl.setVoidCallable();
repositoryTaskSchedulerControl.replay();
Configuration configuration = createConfigurationForEditing( createRepository() );
archivaConfiguration.getConfiguration();
archivaConfigurationControl.setReturnValue( configuration );

View File

@ -433,6 +433,24 @@ public class AdministrationServiceImpl
}
saveConfiguration( config );
//MRM-1342 Repository statistics report doesn't appear to be working correctly
//scan repository when adding of repository is successful
try
{
executeRepositoryScanner( repoId );
if ( stageRepoNeeded )
{
ManagedRepositoryConfiguration stagingRepository = getStageRepoConfig( repository );
executeRepositoryScanner( stagingRepository.getId() );
}
}
catch ( Exception e )
{
log.warn( new StringBuilder( "Unable to scan repository [" ).append( repoId ).append( "]: " ).append(
e.getMessage() ).toString(), e );
}
return Boolean.TRUE;
}

View File

@ -959,9 +959,31 @@ public class AdministrationServiceImplTest
archivaConfig.save( config );
archivaConfigControl.setVoidCallable();
//managed repo
repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( repoId ), false );
RepositoryTask task = new RepositoryTask();
task.setRepositoryId( repoId );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
configControl.expectAndReturn( config.findManagedRepositoryById( repoId ), managedRepo );
repositoryTaskScheduler.queueTask( task );
repositoryTaskSchedulerControl.setVoidCallable();
//staged repo
repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( repoId + STAGE ), false );
task = new RepositoryTask();
task.setRepositoryId( repoId + STAGE );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
configControl.expectAndReturn( config.findManagedRepositoryById( repoId + STAGE ), managedRepo );
repositoryTaskScheduler.queueTask( task );
repositoryTaskSchedulerControl.setVoidCallable();
archivaConfigControl.replay();
configControl.replay();
registryControl.replay();
repositoryTaskSchedulerControl.replay();
assertFalse( new File( releaseLocation ).isDirectory() );
assertFalse( new File( stageLocation ).isDirectory() );
boolean success = service.addManagedRepository( repoId, layout, name, "${appserver.base}/test-repository/" + projId + ".releases", true, true, false, true,

View File

@ -19,14 +19,14 @@ package org.apache.archiva.metadata.repository.stats;
* under the License.
*/
import org.apache.archiva.metadata.model.MetadataFacet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import org.apache.archiva.metadata.model.MetadataFacet;
public class RepositoryStatistics
implements MetadataFacet
{
@ -50,7 +50,7 @@ public class RepositoryStatistics
static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
private Map<String, Long> totalCountForType = new HashMap<String, Long>();
private Map<String, Long> totalCountForType = new ZeroForNullHashMap<String, Long>();
private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
@ -279,12 +279,21 @@ public class RepositoryStatistics
public long getTotalCountForType( String type )
{
Long value = totalCountForType.get( type );
return value != null ? value : 0;
return totalCountForType.get( type );
}
public void setTotalCountForType( String type, long count )
{
totalCountForType.put( type, count );
}
private static final class ZeroForNullHashMap<K, V extends Long> extends HashMap<K, V>
{
@Override
public V get(Object key) {
V value = super.get( key );
return value != null ? value : ( V ) Long.valueOf( 0L );
}
}
}