Coverage Report - org.apache.archiva.web.xmlrpc.services.AdministrationServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AdministrationServiceImpl
0%
0/140
0%
0/56
0
 
 1  
 package org.apache.archiva.web.xmlrpc.services;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.archiva.web.xmlrpc.api.AdministrationService;
 27  
 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
 28  
 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
 29  
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 30  
 import org.apache.maven.archiva.configuration.Configuration;
 31  
 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
 32  
 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
 33  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 34  
 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
 35  
 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
 36  
 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
 37  
 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
 38  
 import org.apache.maven.archiva.database.ArchivaDatabaseException;
 39  
 import org.apache.maven.archiva.database.ArtifactDAO;
 40  
 import org.apache.maven.archiva.database.constraints.ArtifactVersionsConstraint;
 41  
 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
 42  
 import org.apache.maven.archiva.database.updater.DatabaseConsumers;
 43  
 import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer;
 44  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 45  
 import org.apache.maven.archiva.model.VersionedReference;
 46  
 import org.apache.maven.archiva.repository.ContentNotFoundException;
 47  
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 48  
 import org.apache.maven.archiva.repository.RepositoryContentFactory;
 49  
 import org.apache.maven.archiva.repository.RepositoryException;
 50  
 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
 51  
 import org.apache.maven.archiva.repository.events.RepositoryListener;
 52  
 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
 53  
 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
 54  
 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
 55  
 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
 56  
 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
 57  
 import org.codehaus.plexus.registry.RegistryException;
 58  
 import org.slf4j.Logger;
 59  
 import org.slf4j.LoggerFactory;
 60  
 
 61  
 /**
 62  
  * AdministrationServiceImpl
 63  
  * 
 64  
  * @version $Id: AdministrationServiceImpl.java
 65  
  */
 66  
 public class AdministrationServiceImpl
 67  
     implements AdministrationService
 68  
 {    
 69  0
     protected Logger log = LoggerFactory.getLogger( getClass() );
 70  
 
 71  
     private ArchivaConfiguration archivaConfiguration;
 72  
         
 73  
     private RepositoryContentConsumers repoConsumersUtil;
 74  
         
 75  
     private DatabaseConsumers dbConsumersUtil;
 76  
             
 77  
     private RepositoryContentFactory repoFactory;
 78  
     
 79  
     private ArtifactDAO artifactDAO;
 80  
     
 81  
     private ArchivaTaskScheduler taskScheduler;
 82  
     
 83  
     private Collection<RepositoryListener> listeners;
 84  
 
 85  
     public AdministrationServiceImpl( ArchivaConfiguration archivaConfig, RepositoryContentConsumers repoConsumersUtil,
 86  
                                       DatabaseConsumers dbConsumersUtil, RepositoryContentFactory repoFactory,
 87  
                                       ArtifactDAO artifactDAO, ArchivaTaskScheduler taskScheduler,
 88  
                                       Collection<RepositoryListener> listeners )
 89  0
     {
 90  0
         this.archivaConfiguration = archivaConfig;
 91  0
         this.repoConsumersUtil = repoConsumersUtil;
 92  0
         this.dbConsumersUtil = dbConsumersUtil;
 93  0
         this.repoFactory = repoFactory;
 94  0
         this.artifactDAO = artifactDAO;
 95  0
         this.taskScheduler = taskScheduler;
 96  0
         this.listeners = listeners;
 97  0
     }
 98  
         
 99  
     /**
 100  
      * @see AdministrationService#configureDatabaseConsumer(String, boolean)
 101  
      */
 102  
     public Boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception
 103  
     {
 104  0
         List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
 105  0
         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers =
 106  
             dbConsumersUtil.getAvailableUnprocessedConsumers();
 107  
         
 108  0
         boolean found = false;
 109  0
         boolean isCleanupConsumer = false;        
 110  0
         for( DatabaseCleanupConsumer consumer : cleanupConsumers )
 111  
         {
 112  0
             if( consumer.getId().equals( consumerId ) )
 113  
             {
 114  0
                 found = true;
 115  0
                 isCleanupConsumer = true;
 116  0
                 break;
 117  
             }
 118  
         }
 119  
         
 120  0
         if( !found )
 121  
         {
 122  0
             for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
 123  
             {
 124  0
                 if( consumer.getId().equals( consumerId ) )
 125  
                 {
 126  0
                     found = true;
 127  0
                     break;
 128  
                 }
 129  
             }
 130  
         }
 131  
         
 132  0
         if( !found )
 133  
         {
 134  0
             throw new Exception( "Invalid database consumer." );
 135  
         }
 136  
         
 137  0
         Configuration config = archivaConfiguration.getConfiguration();
 138  0
         DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning();
 139  
         
 140  0
         if( isCleanupConsumer )
 141  
         {
 142  0
             dbScanningConfig.addCleanupConsumer( consumerId );            
 143  
         }
 144  
         else
 145  
         {
 146  0
             dbScanningConfig.addUnprocessedConsumer( consumerId );
 147  
         }
 148  
         
 149  0
         config.setDatabaseScanning( dbScanningConfig );        
 150  0
         saveConfiguration( config );
 151  
         
 152  0
         return new Boolean( true );
 153  
     }
 154  
 
 155  
     /**
 156  
      * @see AdministrationService#configureRepositoryConsumer(String, String, boolean)
 157  
      */
 158  
     public Boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable )
 159  
         throws Exception
 160  
     {
 161  
         // TODO use repoId once consumers are configured per repository! (MRM-930)
 162  
         
 163  0
         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
 164  0
         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
 165  
         
 166  0
         boolean found = false;
 167  0
         boolean isKnownContentConsumer = false;
 168  0
         for( KnownRepositoryContentConsumer consumer : knownConsumers )
 169  
         {
 170  0
             if( consumer.getId().equals( consumerId ) )
 171  
             {
 172  0
                 found = true;
 173  0
                 isKnownContentConsumer = true;
 174  0
                 break;
 175  
             }
 176  
         }
 177  
         
 178  0
         if( !found )
 179  
         {
 180  0
             for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
 181  
             {
 182  0
                 if( consumer.getId().equals( consumerId ) )
 183  
                 {
 184  0
                     found = true;
 185  0
                     break;
 186  
                 }
 187  
             }
 188  
         }
 189  
         
 190  0
         if( !found )
 191  
         {
 192  0
             throw new Exception( "Invalid repository consumer." );
 193  
         }
 194  
         
 195  0
         Configuration config = archivaConfiguration.getConfiguration();
 196  0
         RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning();
 197  
         
 198  0
         if( isKnownContentConsumer )
 199  
         {
 200  0
             repoScanningConfig.addKnownContentConsumer( consumerId );
 201  
         }
 202  
         else
 203  
         {
 204  0
             repoScanningConfig.addInvalidContentConsumer( consumerId );
 205  
         }
 206  
         
 207  0
         config.setRepositoryScanning( repoScanningConfig );        
 208  0
         saveConfiguration( config );
 209  
         
 210  0
         return new Boolean( true );
 211  
     }
 212  
     
 213  
     /**
 214  
      * @see AdministrationService#deleteArtifact(String, String, String, String)
 215  
      */
 216  
     public Boolean deleteArtifact( String repoId, String groupId, String artifactId, String version )
 217  
         throws Exception
 218  
     {
 219  0
         Configuration config = archivaConfiguration.getConfiguration();
 220  0
         ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repoId );
 221  
         
 222  0
         if( repoConfig == null )
 223  
         {
 224  0
             throw new Exception( "Repository does not exist." );
 225  
         }
 226  
             
 227  
         try
 228  
         {
 229  0
             ManagedRepositoryContent repoContent = repoFactory.getManagedRepositoryContent( repoId );            
 230  0
             VersionedReference ref = new VersionedReference();
 231  0
             ref.setGroupId( groupId );
 232  0
             ref.setArtifactId( artifactId );
 233  0
             ref.setVersion( version );
 234  
             
 235  
             // delete from file system
 236  0
             repoContent.deleteVersion( ref );
 237  
             
 238  0
             ArtifactVersionsConstraint constraint = new ArtifactVersionsConstraint( repoId, groupId, artifactId, false );
 239  0
             List<ArchivaArtifact> artifacts = null;
 240  
             
 241  
             try
 242  
             {
 243  0
                 artifacts = artifactDAO.queryArtifacts( constraint );
 244  
             }
 245  0
             catch ( ArchivaDatabaseException e )
 246  
             {
 247  0
                 throw new Exception( "Error occurred while cleaning up database." );
 248  0
             }            
 249  
                
 250  0
             if ( artifacts != null )
 251  
             {
 252  0
                 for ( ArchivaArtifact artifact : artifacts )
 253  
                 {
 254  0
                     if ( artifact.getVersion().equals( version ) )
 255  
                     {
 256  0
                         for ( RepositoryListener listener : listeners )
 257  
                         {
 258  0
                             listener.deleteArtifact( repoContent, artifact );
 259  
                         }
 260  
                     }
 261  
                 }
 262  
             }
 263  
         }
 264  0
         catch ( ContentNotFoundException e )
 265  
         {
 266  0
             throw new Exception( "Artifact does not exist." );
 267  
         }
 268  0
         catch ( RepositoryNotFoundException e )
 269  
         {
 270  0
             throw new Exception( "Repository does not exist." );
 271  
         }
 272  0
         catch ( RepositoryException e )
 273  
         {
 274  0
             throw new Exception( "Repository exception occurred." );
 275  0
         }
 276  
         
 277  0
         return new Boolean( true );
 278  
     }
 279  
 
 280  
     /**
 281  
      * @see AdministrationService#executeDatabaseScanner()
 282  
      */
 283  
     public Boolean executeDatabaseScanner() throws Exception
 284  
     {
 285  0
         if ( taskScheduler.isProcessingDatabaseTask() )
 286  
         {
 287  0
             return false;
 288  
         }
 289  
 
 290  0
         log.info( "Queueing database task on request from administration service" );
 291  0
         DatabaseTask task = new DatabaseTask();
 292  
         
 293  0
         taskScheduler.queueDatabaseTask( task );           
 294  
         
 295  0
         return new Boolean( true );
 296  
     }
 297  
 
 298  
     /**
 299  
      * @see AdministrationService#executeRepositoryScanner(String)
 300  
      */
 301  
     public Boolean executeRepositoryScanner( String repoId ) throws Exception
 302  
     {
 303  0
         Configuration config = archivaConfiguration.getConfiguration();
 304  0
         if( config.findManagedRepositoryById( repoId ) == null )
 305  
         {
 306  0
             throw new Exception( "Repository does not exist." );
 307  
         }
 308  
         
 309  0
         if ( taskScheduler.isProcessingRepositoryTask( repoId ) )
 310  
         {
 311  0
             return false;
 312  
         }
 313  
 
 314  0
         RepositoryTask task = TaskCreator.createRepositoryTask( repoId );
 315  
 
 316  0
         taskScheduler.queueRepositoryTask( task );          
 317  
         
 318  0
         return new Boolean( true );
 319  
     }
 320  
 
 321  
     /**
 322  
      * @see AdministrationService#getAllDatabaseConsumers()
 323  
      */
 324  
     public List<String> getAllDatabaseConsumers()
 325  
     {
 326  0
         List<String> consumers = new ArrayList<String>();
 327  
         
 328  0
         List<DatabaseCleanupConsumer> cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers();
 329  0
         List<DatabaseUnprocessedArtifactConsumer> unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers();
 330  
         
 331  0
         for( DatabaseCleanupConsumer consumer : cleanupConsumers )
 332  
         {
 333  0
             consumers.add( consumer.getId() );
 334  
         }  
 335  
         
 336  0
         for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers )
 337  
         {
 338  0
             consumers.add( consumer.getId() );
 339  
         } 
 340  
         
 341  0
         return consumers;
 342  
     }
 343  
 
 344  
     /**
 345  
      * @see AdministrationService#getAllRepositoryConsumers()
 346  
      */
 347  
     public List<String> getAllRepositoryConsumers()
 348  
     {
 349  0
         List<String> consumers = new ArrayList<String>();
 350  
                 
 351  0
         List<KnownRepositoryContentConsumer> knownConsumers = repoConsumersUtil.getAvailableKnownConsumers();
 352  0
         List<InvalidRepositoryContentConsumer> invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers();
 353  
         
 354  0
         for( KnownRepositoryContentConsumer consumer : knownConsumers )
 355  
         {
 356  0
             consumers.add( consumer.getId() );
 357  
         }
 358  
         
 359  0
         for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
 360  
         {
 361  0
             consumers.add( consumer.getId() );
 362  
         }
 363  
 
 364  0
         return consumers;
 365  
     }
 366  
 
 367  
     /**
 368  
      * @see AdministrationService#getAllManagedRepositories()
 369  
      */
 370  
     public List<ManagedRepository> getAllManagedRepositories()
 371  
     {
 372  0
         List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>();
 373  
         
 374  0
         Configuration config = archivaConfiguration.getConfiguration();
 375  0
         List<ManagedRepositoryConfiguration> managedRepoConfigs = config.getManagedRepositories();
 376  
         
 377  0
         for( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
 378  
         {
 379  
             // TODO fix resolution of repo url!            
 380  0
             ManagedRepository repo =
 381  
                 new ManagedRepository( repoConfig.getId(), repoConfig.getName(), "URL", repoConfig.getLayout(),
 382  
                                        repoConfig.isSnapshots(), repoConfig.isReleases() );  
 383  0
             managedRepos.add( repo );
 384  0
         }
 385  
         
 386  0
         return managedRepos;
 387  
     }
 388  
 
 389  
     /**
 390  
      * @see AdministrationService#getAllRemoteRepositories()
 391  
      */
 392  
     public List<RemoteRepository> getAllRemoteRepositories()
 393  
     {
 394  0
         List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>();
 395  
         
 396  0
         Configuration config = archivaConfiguration.getConfiguration();
 397  0
         List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
 398  
         
 399  0
         for( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
 400  
         {
 401  0
             RemoteRepository repo =
 402  
                 new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
 403  
                                       repoConfig.getLayout() );
 404  0
             remoteRepos.add( repo );
 405  0
         }
 406  
         
 407  0
         return remoteRepos;
 408  
     }
 409  
 
 410  
     private void saveConfiguration( Configuration config )
 411  
         throws Exception
 412  
     {
 413  
         try
 414  
         {
 415  0
             archivaConfiguration.save( config );
 416  
         }
 417  0
         catch(  RegistryException e )
 418  
         {
 419  0
             throw new Exception( "Error occurred in the registry." );
 420  
         }
 421  0
         catch ( IndeterminateConfigurationException e )
 422  
         {
 423  0
             throw new Exception( "Error occurred while saving the configuration." );    
 424  0
         }
 425  0
     }
 426  
 }