[MRM-1504] repository admin module : implements delete ManagedRepository

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1163024 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2011-08-29 21:41:34 +00:00
parent f2dbfdf0a8
commit 8015b813c2
2 changed files with 91 additions and 4 deletions

View File

@ -19,14 +19,21 @@ package org.apache.archiva.admin.repository.managed;
*/ */
import org.apache.archiva.admin.repository.RepositoryAdminException; import org.apache.archiva.admin.repository.RepositoryAdminException;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.metadata.repository.RepositorySessionFactory;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler; import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
import org.apache.archiva.scheduler.repository.RepositoryTask; import org.apache.archiva.scheduler.repository.RepositoryTask;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.GenericValidator; import org.apache.commons.validator.GenericValidator;
import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.IndeterminateConfigurationException; import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
import org.codehaus.plexus.registry.Registry; import org.codehaus.plexus.registry.Registry;
import org.codehaus.plexus.registry.RegistryException; import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.plexus.taskqueue.TaskQueueException; import org.codehaus.plexus.taskqueue.TaskQueueException;
@ -41,6 +48,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* FIXME remove all generic Exception to have usefull ones * FIXME remove all generic Exception to have usefull ones
@ -71,6 +79,13 @@ public class DefaultManagedRepositoryAdmin
@Named( value = "archivaTaskScheduler#repository" ) @Named( value = "archivaTaskScheduler#repository" )
private RepositoryArchivaTaskScheduler repositoryTaskScheduler; private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
@Inject
private RepositorySessionFactory repositorySessionFactory;
@Inject
private RepositoryStatisticsManager repositoryStatisticsManager;
public List<ManagedRepository> getManagedRepositories() public List<ManagedRepository> getManagedRepositories()
throws RepositoryAdminException throws RepositoryAdminException
{ {
@ -226,14 +241,80 @@ public class DefaultManagedRepositoryAdmin
} }
public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo ) public Boolean deleteManagedRepository( String repositoryId )
throws RepositoryAdminException throws RepositoryAdminException
{ {
return null; //To change body of implemented methods use File | Settings | File Templates. Configuration config = archivaConfiguration.getConfiguration();
ManagedRepositoryConfiguration repository = config.findManagedRepositoryById( repositoryId );
if ( repository == null )
{
throw new RepositoryAdminException( "A repository with that id does not exist" );
}
RepositorySession repositorySession = repositorySessionFactory.createSession();
try
{
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeRepository( repository.getId() );
repositoryStatisticsManager.deleteStatistics( metadataRepository, repository.getId() );
repositorySession.save();
}
catch ( MetadataRepositoryException e )
{
throw new RepositoryAdminException( e.getMessage(), e );
}
finally
{
repositorySession.close();
}
config.removeManagedRepository( repository );
try
{
saveConfiguration( config );
}
catch ( Exception e )
{
throw new RepositoryAdminException( "Error saving configuration for delete action" + e.getMessage() );
}
// TODO could be async ? as directory can be huge
File dir = new File( repository.getLocation() );
if ( !FileUtils.deleteQuietly( dir ) )
{
throw new RepositoryAdminException( "Cannot delete repository " + dir );
}
List<ProxyConnectorConfiguration> proxyConnectors = config.getProxyConnectors();
for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
{
if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) )
{
archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector );
}
}
Map<String, List<String>> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap();
if ( repoToGroupMap != null )
{
if ( repoToGroupMap.containsKey( repository.getId() ) )
{
List<String> repoGroups = repoToGroupMap.get( repository.getId() );
for ( String repoGroup : repoGroups )
{
archivaConfiguration.getConfiguration().findRepositoryGroupById( repoGroup ).removeRepository(
repository.getId() );
}
}
}
return Boolean.TRUE;
} }
public Boolean deleteManagedRepository( String repositoryId ) public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo )
throws RepositoryAdminException throws RepositoryAdminException
{ {
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.

View File

@ -59,7 +59,7 @@ public class ManagedRepositoryAdminTest
} }
@Test @Test
public void addManagedRepo() public void addDeleteManagedRepo()
throws Exception throws Exception
{ {
List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories(); List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
@ -77,6 +77,12 @@ public class ManagedRepositoryAdminTest
assertEquals( initialSize + 1, repos.size() ); assertEquals( initialSize + 1, repos.size() );
assertNotNull( managedRepositoryAdmin.getManagedRepository( "test-new-one" ) ); assertNotNull( managedRepositoryAdmin.getManagedRepository( "test-new-one" ) );
managedRepositoryAdmin.deleteManagedRepository( "test-new-one" );
repos = managedRepositoryAdmin.getManagedRepositories();
assertNotNull( repos );
assertEquals( initialSize, repos.size() );
} }