diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java index 67ea533c5..c2f3a9db2 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java @@ -19,14 +19,21 @@ package org.apache.archiva.admin.repository.managed; */ 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.RepositoryTask; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.validator.GenericValidator; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.IndeterminateConfigurationException; 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.RegistryException; import org.codehaus.plexus.taskqueue.TaskQueueException; @@ -41,6 +48,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; /** * FIXME remove all generic Exception to have usefull ones @@ -71,6 +79,13 @@ public class DefaultManagedRepositoryAdmin @Named( value = "archivaTaskScheduler#repository" ) private RepositoryArchivaTaskScheduler repositoryTaskScheduler; + @Inject + private RepositorySessionFactory repositorySessionFactory; + + + @Inject + private RepositoryStatisticsManager repositoryStatisticsManager; + public List getManagedRepositories() throws RepositoryAdminException { @@ -226,14 +241,80 @@ public class DefaultManagedRepositoryAdmin } - public Boolean updateManagedRepository( ManagedRepository managedRepository, boolean needStageRepo ) + public Boolean deleteManagedRepository( String repositoryId ) 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 proxyConnectors = config.getProxyConnectors(); + for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors ) + { + if ( StringUtils.equals( proxyConnector.getSourceRepoId(), repository.getId() ) ) + { + archivaConfiguration.getConfiguration().removeProxyConnector( proxyConnector ); + } + } + + Map> repoToGroupMap = archivaConfiguration.getConfiguration().getRepositoryToGroupMap(); + if ( repoToGroupMap != null ) + { + if ( repoToGroupMap.containsKey( repository.getId() ) ) + { + List 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 { return null; //To change body of implemented methods use File | Settings | File Templates. diff --git a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java index 3d73e3dc6..393696ace 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/src/test/java/org/apache/archiva/admin/repository/managed/ManagedRepositoryAdminTest.java @@ -59,7 +59,7 @@ public class ManagedRepositoryAdminTest } @Test - public void addManagedRepo() + public void addDeleteManagedRepo() throws Exception { List repos = managedRepositoryAdmin.getManagedRepositories(); @@ -77,6 +77,12 @@ public class ManagedRepositoryAdminTest assertEquals( initialSize + 1, repos.size() ); assertNotNull( managedRepositoryAdmin.getManagedRepository( "test-new-one" ) ); + + managedRepositoryAdmin.deleteManagedRepository( "test-new-one" ); + + repos = managedRepositoryAdmin.getManagedRepositories(); + assertNotNull( repos ); + assertEquals( initialSize, repos.size() ); }