]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1417] add basic managed repository management support on adding repository,...
authorMaria Odea B. Ching <oching@apache.org>
Thu, 19 Aug 2010 07:15:58 +0000 (07:15 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Thu, 19 Aug 2010 07:15:58 +0000 (07:15 +0000)
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

archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/applicationContext.xml
archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java
archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-client/src/main/java/org/apache/archiva/web/xmlrpc/client/SampleClient.java
archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-security/src/main/java/org/apache/archiva/web/xmlrpc/security/ServiceMethodsPermissionsMapping.java
archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java
archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java

index e1763d02573a8946a589c56265b98fbeebcdaa31..144eccb40feebcc452ed005a0553e913b946e7ea 100644 (file)
@@ -57,6 +57,7 @@
     <constructor-arg>
       <bean class="org.apache.archiva.web.spring.RepositoryListenerFactoryBean" />
     </constructor-arg>       
+    <constructor-arg ref="repositoryStatisticsManager"/>    
   </bean> 
 
   <bean name="xmlrpcServicesList" lazy-init="true" scope="singleton" class="java.util.ArrayList">
index ba44c60b3797295cd4450c2a204bc68fffc1a304..84e23433b53a541943f05c029aafed57212b36b9 100644 (file)
@@ -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<String> 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
index 8709be4448097870a687d184b8453e166bf04e22..a85382bca2a76a0af435cbb787df07725d060f98 100644 (file)
@@ -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!
index 97fe329906cec096174f1dc043c7cc756c0fb065..d194a910d19e556277df7ece781b6d6892fe9ee6 100644 (file)
@@ -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<String> SERVICE_METHODS_FOR_OPERATION_RUN_INDEXER =
         Arrays.asList( "AdministrationService.executeRepositoryScanner" );
index 379288c116c79373969a527423fce111e8a43b7f..33bea017b9bd5984953b82424357671b4dac836b 100644 (file)
@@ -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<RepositoryListener> listeners )
+                                      Collection<RepositoryListener> 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<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
         List<InvalidRepositoryContentConsumer> 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<ManagedRepositoryConfiguration> 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<RemoteRepositoryConfiguration> 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<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 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;
+    }
+
 }
index ffdb1048db2ff43e64a1792aaecb6b4957108206..5b0a2bdd5d2b28a8a4fa37ad2c4ebc5cbe5030e6 100644 (file)
@@ -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  */