Coverage Report - org.apache.maven.archiva.database.updater.DatabaseConsumers
 
Classes in this File Line Coverage Branch Coverage Complexity
DatabaseConsumers
0%
0/30
0%
0/6
0
DatabaseConsumers$SelectedCleanupConsumersPredicate
0%
0/7
0%
0/2
0
DatabaseConsumers$SelectedUnprocessedConsumersPredicate
0%
0/7
0%
0/2
0
 
 1  
 package org.apache.maven.archiva.database.updater;
 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.List;
 24  
 
 25  
 import org.apache.commons.collections.CollectionUtils;
 26  
 import org.apache.commons.collections.Predicate;
 27  
 import org.apache.commons.collections.functors.OrPredicate;
 28  
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 29  
 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
 30  
 import org.apache.maven.archiva.consumers.functors.PermanentConsumerPredicate;
 31  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 32  
 import org.slf4j.Logger;
 33  
 import org.slf4j.LoggerFactory;
 34  
 import org.springframework.beans.BeansException;
 35  
 import org.springframework.context.ApplicationContext;
 36  
 import org.springframework.context.ApplicationContextAware;
 37  
 
 38  
 /**
 39  
  * DatabaseConsumers 
 40  
  *
 41  
  * @version $Id: DatabaseConsumers.java 755266 2009-03-17 14:28:40Z brett $
 42  
  */
 43  0
 public class DatabaseConsumers
 44  
     implements ApplicationContextAware
 45  
 {    
 46  0
     private Logger log = LoggerFactory.getLogger( DatabaseConsumers.class );
 47  
     
 48  
     private ArchivaConfiguration archivaConfiguration;
 49  
 
 50  
     private Predicate selectedCleanupConsumers;
 51  
 
 52  
     private Predicate selectedUnprocessedConsumers;
 53  
     
 54  
     private ApplicationContext applicationContext;
 55  
 
 56  
     public DatabaseConsumers( ArchivaConfiguration archivaConfiguration )
 57  0
     {
 58  0
         this.archivaConfiguration = archivaConfiguration;
 59  
         
 60  0
         Predicate permanentConsumers = new PermanentConsumerPredicate();
 61  
 
 62  0
         selectedCleanupConsumers = new OrPredicate( permanentConsumers, new SelectedCleanupConsumersPredicate() );
 63  0
         selectedUnprocessedConsumers = new OrPredicate( permanentConsumers, new SelectedUnprocessedConsumersPredicate() );
 64  0
     }
 65  
     
 66  0
     class SelectedUnprocessedConsumersPredicate
 67  
         implements Predicate
 68  
     {
 69  
         public boolean evaluate( Object object )
 70  
         {
 71  0
             boolean satisfies = false;
 72  
 
 73  0
             if ( object instanceof DatabaseUnprocessedArtifactConsumer )
 74  
             {
 75  0
                 DatabaseUnprocessedArtifactConsumer consumer = (DatabaseUnprocessedArtifactConsumer) object;
 76  0
                 DatabaseScanningConfiguration config = archivaConfiguration.getConfiguration().getDatabaseScanning();
 77  
 
 78  0
                 return config.getUnprocessedConsumers().contains( consumer.getId() );
 79  
             }
 80  
 
 81  0
             return satisfies;
 82  
         }
 83  
     }
 84  
 
 85  0
     class SelectedCleanupConsumersPredicate
 86  
         implements Predicate
 87  
     {
 88  
         public boolean evaluate( Object object )
 89  
         {
 90  0
             boolean satisfies = false;
 91  
 
 92  0
             if ( object instanceof DatabaseCleanupConsumer )
 93  
             {
 94  0
                 DatabaseCleanupConsumer consumer = (DatabaseCleanupConsumer) object;
 95  0
                 DatabaseScanningConfiguration config = archivaConfiguration.getConfiguration().getDatabaseScanning();
 96  
 
 97  0
                 return config.getCleanupConsumers().contains( consumer.getId() );
 98  
             }
 99  
 
 100  0
             return satisfies;
 101  
         }
 102  
     }
 103  
 
 104  
     public void setApplicationContext( ApplicationContext applicationContext )
 105  
         throws BeansException
 106  
     {
 107  0
         this.applicationContext = applicationContext;
 108  0
     }
 109  
     
 110  
     /**
 111  
      * Get the {@link List} of {@link DatabaseUnprocessedArtifactConsumer} objects
 112  
      * for those consumers selected due to the configuration.
 113  
      * 
 114  
      * @return the list of selected {@link DatabaseUnprocessedArtifactConsumer} objects.
 115  
      */
 116  
     @SuppressWarnings("unchecked")
 117  
     public List<ArchivaArtifactConsumer> getSelectedUnprocessedConsumers()
 118  
     {
 119  0
         List<ArchivaArtifactConsumer> ret = new ArrayList<ArchivaArtifactConsumer>();
 120  0
         ret.addAll( CollectionUtils.select( getAvailableUnprocessedConsumers(), selectedUnprocessedConsumers ) );
 121  0
         return ret;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Get the {@link List} of {@link DatabaseCleanupConsumer} objects for those
 126  
      * consumers selected due to the configuration.
 127  
      * 
 128  
      * @return the list of selected {@link DatabaseCleanupConsumer} objects.
 129  
      */
 130  
     @SuppressWarnings("unchecked")
 131  
     public List<ArchivaArtifactConsumer> getSelectedCleanupConsumers()
 132  
     {
 133  0
         List<ArchivaArtifactConsumer> ret = new ArrayList<ArchivaArtifactConsumer>();
 134  0
         ret.addAll( CollectionUtils.select( getAvailableCleanupConsumers(), selectedCleanupConsumers ) );
 135  0
         return ret;
 136  
     }
 137  
 
 138  
     /**
 139  
      * Get the complete {@link List} of {@link DatabaseUnprocessedArtifactConsumer} objects
 140  
      * that are available in the system, regardless of configuration.
 141  
      * 
 142  
      * @return the list of all available {@link DatabaseUnprocessedArtifactConsumer} objects.
 143  
      */
 144  
     @SuppressWarnings("unchecked")
 145  
     public List<DatabaseUnprocessedArtifactConsumer> getAvailableUnprocessedConsumers()
 146  
     {       
 147  0
         return new ArrayList<DatabaseUnprocessedArtifactConsumer>( applicationContext.getBeansOfType( DatabaseUnprocessedArtifactConsumer.class ).values() );
 148  
     }
 149  
 
 150  
     /**
 151  
      * Get the complete {@link List} of {@link DatabaseCleanupConsumer} objects
 152  
      * that are available in the system, regardless of configuration.
 153  
      * 
 154  
      * @return the list of all available {@link DatabaseCleanupConsumer} objects.
 155  
      */
 156  
     @SuppressWarnings("unchecked")
 157  
     public List<DatabaseCleanupConsumer> getAvailableCleanupConsumers()
 158  
     {
 159  0
         return new ArrayList<DatabaseCleanupConsumer>( applicationContext.getBeansOfType( DatabaseCleanupConsumer.class ).values() );
 160  
     }
 161  
     
 162  
     /**
 163  
      * Execute the cleanup consumers to cleanup the specified artifact from the database and index.
 164  
      * 
 165  
      * @param artifact
 166  
      */
 167  
     public void executeCleanupConsumer( ArchivaArtifact artifact )
 168  
     {
 169  0
         List<ArchivaArtifactConsumer> consumers = getSelectedCleanupConsumers();
 170  0
         for ( ArchivaArtifactConsumer consumer : consumers )
 171  
         {
 172  0
             consumer.beginScan();
 173  
         }
 174  
         
 175  0
         if ( CollectionUtils.isEmpty( consumers ) )
 176  
         {
 177  0
             log.warn( "There are no selected consumers for artifact cleanup." );
 178  0
             return;
 179  
         }
 180  
         
 181  0
         ProcessArchivaArtifactClosure processArtifactClosure = new ProcessArchivaArtifactClosure();
 182  0
         processArtifactClosure.setArtifact( artifact );
 183  
         
 184  0
         CollectionUtils.forAllDo( consumers, processArtifactClosure );
 185  
         
 186  0
         for ( ArchivaArtifactConsumer consumer : consumers )
 187  
         {
 188  0
             consumer.completeScan();
 189  
         }
 190  0
     }
 191  
 }