From: Maria Odea B. Ching Date: Thu, 19 Aug 2010 07:15:58 +0000 (+0000) Subject: [MRM-1417] add basic managed repository management support on adding repository,... X-Git-Tag: archiva-1.4-M1~907 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7c73d3c9d18558896bf2a06ac157ded63de18d6e;p=archiva.git [MRM-1417] add basic managed repository management support on adding repository, deleting repository, and loading repository by id submitted by Catalino Pineda Jr. additional modification to the patch: o added invocation of repo mgnt service methods to sample client git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@987042 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/applicationContext.xml b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/applicationContext.xml index e1763d025..144eccb40 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/applicationContext.xml +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/applicationContext.xml @@ -57,6 +57,7 @@ + diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java index ba44c60b3..84e23433b 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java @@ -24,18 +24,20 @@ import java.util.List; import com.atlassian.xmlrpc.ServiceObject; import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository; import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; @ServiceObject( "AdministrationService" ) public interface AdministrationService -{ +{ /** * Executes repository scanner on the given repository. - * + * * @param repoId id of the repository to be scanned * @return * @throws Exception */ - public Boolean executeRepositoryScanner( String repoId ) throws Exception; + public Boolean executeRepositoryScanner( String repoId ) + throws Exception; /** * Gets all available repository consumers. @@ -43,8 +45,8 @@ public interface AdministrationService * @return */ public List getAllRepositoryConsumers(); - - // TODO should we already implement config of consumers per repository? + + // TODO should we already implement config of consumers per repository? /** * Configures (enable or disable) repository consumer. * @@ -53,8 +55,9 @@ public interface AdministrationService * @param enable * @return * @throws Exception - */ - public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) throws Exception; + */ + public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) + throws Exception; /** * Gets all managed repositories. @@ -81,9 +84,44 @@ public interface AdministrationService * @throws Exception */ public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) - throws Exception; - - //TODO + throws Exception; + + /** + * Create a new managed repository with the given parameters. + * + * @param repoId + * @param layout + * @param name + * @param location + * @param blockRedeployments + * @param releasesIncluded + * @param snapshotsIncluded + * @param cronExpression + * @return + * @throws Exception + */ + public Boolean addManagedRepository( String repoId, String layout, String name, String location, + boolean blockRedeployments, boolean releasesIncluded, + boolean snapshotsIncluded, String cronExpression ) + throws Exception; + + /** + * Deletes a managed repository with the given repository id. + * + * @param repoId + * @return + */ + public Boolean deleteManagedRepository( String repoId ) + throws Exception; + + /** + * Get a managed repository with the given repository id. + * @param repoId + * @return + * @throws Exception + */ + public ManagedRepository getManagedRepository(String repoId) throws Exception; + // TODO // consider the following as additional services: // - getAllConfiguredRepositoryConsumers( String repoId ) - list all enabled consumers for the repo // - getAllConfiguredDatabaseConsumers() - list all enabled db consumers diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-client/src/main/java/org/apache/archiva/web/xmlrpc/client/SampleClient.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-client/src/main/java/org/apache/archiva/web/xmlrpc/client/SampleClient.java index 8709be444..a85382bca 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-client/src/main/java/org/apache/archiva/web/xmlrpc/client/SampleClient.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-client/src/main/java/org/apache/archiva/web/xmlrpc/client/SampleClient.java @@ -98,7 +98,26 @@ public class SampleClient success = adminService.executeRepositoryScanner( "internal" ); System.out.println( "\nExecuted repo scanner of repository 'internal' : " + ( (Boolean) success ).booleanValue() ); - + + /** add, get and delete managed repo **/ + /* + * NOTE: change the location of the repository to be added depending on your platform! + * + success = adminService.addManagedRepository( "test", "default", "Test Repo", + "/tmp/archiva-repo/test/", true, true, false, "0 0 * * * ?" ); + System.out.println( "\nSuccessfully added managed repository 'test'" ); + + ManagedRepository repo = adminService.getManagedRepository( "test" ); + System.out.println( "****** managed repo info ******" ); + System.out.println( "ID: " + repo.getId() ); + System.out.println( "NAME: " + repo.getName() ); + System.out.println( "LAYOUT: " + repo.getLayout() ); + System.out.println( "URL: " + repo.getUrl() ); + + success = adminService.deleteManagedRepository( "test" ); + System.out.println( "\nSuccessfully deleted managed repository 'test'" ); + */ + /* delete artifact */ /* * NOTE: before enabling & invoking deleteArtifact, make sure that the repository and artifact exists first! diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-security/src/main/java/org/apache/archiva/web/xmlrpc/security/ServiceMethodsPermissionsMapping.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-security/src/main/java/org/apache/archiva/web/xmlrpc/security/ServiceMethodsPermissionsMapping.java index 97fe32990..d194a910d 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-security/src/main/java/org/apache/archiva/web/xmlrpc/security/ServiceMethodsPermissionsMapping.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-security/src/main/java/org/apache/archiva/web/xmlrpc/security/ServiceMethodsPermissionsMapping.java @@ -39,7 +39,10 @@ public class ServiceMethodsPermissionsMapping "AdministrationService.getAllManagedRepositories", "AdministrationService.getAllRemoteRepositories", "AdministrationService.getAllDatabaseConsumers", - "AdministrationService.getAllRepositoryConsumers", "AdministrationService.deleteArtifact" ); + "AdministrationService.getAllRepositoryConsumers", + "AdministrationService.deleteArtifact", + "AdministrationService.addManagedRepository", + "AdministrationService.deleteManagedRepository", "AdministrationService.getManagedRepository" ); public static final List SERVICE_METHODS_FOR_OPERATION_RUN_INDEXER = Arrays.asList( "AdministrationService.executeRepositoryScanner" ); diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java index 379288c11..33bea017b 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java @@ -21,6 +21,7 @@ package org.apache.archiva.web.xmlrpc.services; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.repository.MetadataRepository; +import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager; import org.apache.archiva.repository.events.RepositoryListener; import org.apache.archiva.repository.scanner.RepositoryContentConsumers; import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler; @@ -28,10 +29,13 @@ import org.apache.archiva.scheduler.repository.RepositoryTask; import org.apache.archiva.web.xmlrpc.api.AdministrationService; import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository; import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; 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.apache.maven.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration; import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer; @@ -42,17 +46,23 @@ import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.RepositoryContentFactory; import org.apache.maven.archiva.repository.RepositoryException; import org.apache.maven.archiva.repository.RepositoryNotFoundException; +import org.apache.maven.archiva.security.ArchivaRoleConstants; +import org.codehaus.plexus.redback.role.RoleManager; import org.codehaus.plexus.registry.RegistryException; +import org.codehaus.plexus.scheduler.CronExpressionValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; /** * AdministrationServiceImpl - * + * * @version $Id: AdministrationServiceImpl.java */ public class AdministrationServiceImpl @@ -72,10 +82,13 @@ public class AdministrationServiceImpl private MetadataRepository metadataRepository; + private RepositoryStatisticsManager repositoryStatisticsManager; + public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil, RepositoryContentFactory repoFactory, MetadataRepository metadataRepository, RepositoryArchivaTaskScheduler repositoryTaskScheduler, - Collection listeners ) + Collection listeners, + RepositoryStatisticsManager repositoryStatisticsManager ) { this.archivaConfiguration = archivaConfig; this.repoConsumersUtil = repoConsumersUtil; @@ -83,6 +96,7 @@ public class AdministrationServiceImpl this.repositoryTaskScheduler = repositoryTaskScheduler; this.listeners = listeners; this.metadataRepository = metadataRepository; + this.repositoryStatisticsManager = repositoryStatisticsManager; } /** @@ -98,9 +112,9 @@ public class AdministrationServiceImpl boolean found = false; boolean isKnownContentConsumer = false; - for( KnownRepositoryContentConsumer consumer : knownConsumers ) + for ( KnownRepositoryContentConsumer consumer : knownConsumers ) { - if( consumer.getId().equals( consumerId ) ) + if ( consumer.getId().equals( consumerId ) ) { found = true; isKnownContentConsumer = true; @@ -108,11 +122,11 @@ public class AdministrationServiceImpl } } - if( !found ) + if ( !found ) { - for( InvalidRepositoryContentConsumer consumer : invalidConsumers ) + for ( InvalidRepositoryContentConsumer consumer : invalidConsumers ) { - if( consumer.getId().equals( consumerId ) ) + if ( consumer.getId().equals( consumerId ) ) { found = true; break; @@ -120,7 +134,7 @@ public class AdministrationServiceImpl } } - if( !found ) + if ( !found ) { throw new Exception( "Invalid repository consumer." ); } @@ -128,7 +142,7 @@ public class AdministrationServiceImpl Configuration config = archivaConfiguration.getConfiguration(); RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning(); - if( isKnownContentConsumer ) + if ( isKnownContentConsumer ) { repoScanningConfig.addKnownContentConsumer( consumerId ); } @@ -154,7 +168,7 @@ public class AdministrationServiceImpl Configuration config = archivaConfiguration.getConfiguration(); ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId ); - if( repoConfig == null ) + if ( repoConfig == null ) { throw new Exception( "Repository does not exist." ); } @@ -179,11 +193,10 @@ public class AdministrationServiceImpl if ( artifact.getVersion().equals( version ) ) { metadataRepository.deleteArtifact( artifact.getRepositoryId(), artifact.getNamespace(), - artifact.getProject(), artifact.getVersion(), - artifact.getId() ); + artifact.getProject(), artifact.getVersion(), artifact.getId() ); // TODO: move into the metadata repository proper - need to differentiate attachment of - // repository metadata to an artifact + // repository metadata to an artifact for ( RepositoryListener listener : listeners ) { listener.deleteArtifact( repoId, artifact.getNamespace(), artifact.getProject(), @@ -211,10 +224,11 @@ public class AdministrationServiceImpl /** * @see AdministrationService#executeRepositoryScanner(String) */ - public Boolean executeRepositoryScanner( String repoId ) throws Exception + public Boolean executeRepositoryScanner( String repoId ) + throws Exception { Configuration config = archivaConfiguration.getConfiguration(); - if( config.findManagedRepositoryById( repoId ) == null ) + if ( config.findManagedRepositoryById( repoId ) == null ) { throw new Exception( "Repository does not exist." ); } @@ -242,12 +256,12 @@ public class AdministrationServiceImpl List knownConsumers = repoConsumersUtil.getAvailableKnownConsumers(); List invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers(); - for( KnownRepositoryContentConsumer consumer : knownConsumers ) + for ( KnownRepositoryContentConsumer consumer : knownConsumers ) { consumers.add( consumer.getId() ); } - for( InvalidRepositoryContentConsumer consumer : invalidConsumers ) + for ( InvalidRepositoryContentConsumer consumer : invalidConsumers ) { consumers.add( consumer.getId() ); } @@ -265,9 +279,9 @@ public class AdministrationServiceImpl Configuration config = archivaConfiguration.getConfiguration(); List managedRepoConfigs = config.getManagedRepositories(); - for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs ) + for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs ) { - // TODO fix resolution of repo url! + // TODO fix resolution of repo url! ManagedRepository repo = new ManagedRepository( repoConfig.getId(), repoConfig.getName(), "URL", repoConfig.getLayout(), repoConfig.isSnapshots(), repoConfig.isReleases() ); @@ -287,7 +301,7 @@ public class AdministrationServiceImpl Configuration config = archivaConfiguration.getConfiguration(); List remoteRepoConfigs = config.getRemoteRepositories(); - for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs ) + for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs ) { RemoteRepository repo = new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(), @@ -305,7 +319,7 @@ public class AdministrationServiceImpl { archivaConfiguration.save( config ); } - catch( RegistryException e ) + catch ( RegistryException e ) { throw new Exception( "Error occurred in the registry." ); } @@ -314,4 +328,135 @@ public class AdministrationServiceImpl throw new Exception( "Error occurred while saving the configuration." ); } } + + public Boolean addManagedRepository( String repoId, String layout, String name, String location, + boolean blockRedeployments, boolean releasesIncluded, + boolean snapshotsIncluded, String cronExpression ) + throws Exception + { + + Configuration config = archivaConfiguration.getConfiguration(); + + CronExpressionValidator validator = new CronExpressionValidator(); + + if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) ) + { + throw new Exception( "Unable to add new repository with id [" + repoId + + "], that id already exists as a managed repository." ); + } + else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) ) + { + throw new Exception( "Unable to add new repository with id [" + repoId + + "], that id already exists as a remote repository." ); + } + else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) ) + { + throw new Exception( "Unable to add new repository with id [" + repoId + + "], that id already exists as a repository group." ); + } + + if ( !validator.validate( cronExpression ) ) + { + throw new Exception( "Invalid cron expression." ); + } + + ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration(); + + repository.setId( repoId ); + repository.setBlockRedeployments( blockRedeployments ); + repository.setReleases( releasesIncluded ); + repository.setSnapshots( snapshotsIncluded ); + repository.setName( name ); + repository.setLocation( location ); + repository.setLayout( layout ); + repository.setRefreshCronExpression( cronExpression ); + + File file = new File( repository.getLocation() ); + repository.setLocation( file.getCanonicalPath() ); + + if ( !file.exists() ) + { + file.mkdirs(); + } + + if ( !file.exists() || !file.isDirectory() ) + { + throw new IOException( "Unable to add repository - no write access, can not create the root directory: " + + file ); + } + config.addManagedRepository( repository ); + saveConfiguration( config ); + return Boolean.TRUE; + } + + public Boolean deleteManagedRepository( String repoId ) + throws Exception + { + Configuration config = archivaConfiguration.getConfiguration(); + + ManagedRepositoryConfiguration repository = config.findManagedRepositoryById( repoId ); + + if ( repository == null ) + { + throw new Exception( "A repository with that id does not exist" ); + } + + metadataRepository.deleteRepository( repository.getId() ); + repositoryStatisticsManager.deleteStatistics( repository.getId() ); + config.removeManagedRepository( repository ); + + try + { + saveConfiguration( config ); + } + catch ( Exception e ) + { + throw new Exception( "Error saving configuration for delete action" + e.getMessage() ); + } + + FileUtils.deleteDirectory( new File( repository.getLocation() ) ); + + 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 ManagedRepository getManagedRepository( String repoId ) + throws Exception + { + Configuration config = archivaConfiguration.getConfiguration(); + ManagedRepositoryConfiguration managedRepository = config.findManagedRepositoryById( repoId ); + if ( managedRepository == null ) + { + throw new Exception( "A repository with that id does not exist" ); + } + ManagedRepository repo = + new ManagedRepository( managedRepository.getId(), managedRepository.getName(), "URL", + managedRepository.getLayout(), managedRepository.isSnapshots(), + managedRepository.isReleases() ); + + return repo; + } + } diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java index ffdb1048d..5b0a2bdd5 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java @@ -21,6 +21,7 @@ package org.apache.archiva.web.xmlrpc.services; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.repository.MetadataRepository; +import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager; import org.apache.archiva.repository.events.RepositoryListener; import org.apache.archiva.repository.scanner.RepositoryContentConsumers; import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler; @@ -105,6 +106,10 @@ public class AdministrationServiceImplTest private MetadataRepository metadataRepository; + private MockControl repositoryStatisticsManagerControl; + + private RepositoryStatisticsManager repositoryStatisticsManager; + protected void setUp() throws Exception { @@ -140,11 +145,15 @@ public class AdministrationServiceImplTest listenerControl = MockControl.createControl( RepositoryListener.class ); listener = (RepositoryListener) listenerControl.getMock(); + + repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class ); + repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock(); + service = new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, repositoryFactory, metadataRepository, repositoryTaskScheduler, - Collections.singletonList( listener ) ); + Collections.singletonList( listener ), repositoryStatisticsManager ); } /* Tests for repository consumers */