Coverage Report - org.apache.maven.archiva.repository.scanner.RepositoryScannerInstance
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryScannerInstance
0%
0/56
0%
0/6
0
 
 1  
 package org.apache.maven.archiva.repository.scanner;
 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.io.File;
 23  
 import java.util.Date;
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import org.apache.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
 29  
 import org.apache.commons.collections.Closure;
 30  
 import org.apache.commons.collections.CollectionUtils;
 31  
 import org.apache.commons.collections.functors.IfClosure;
 32  
 import org.apache.commons.lang.SystemUtils;
 33  
 import org.apache.maven.archiva.common.utils.BaseFile;
 34  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 35  
 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
 36  
 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
 37  
 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
 38  
 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
 39  
 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
 40  
 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
 41  
 import org.codehaus.plexus.util.DirectoryWalkListener;
 42  
 import org.slf4j.Logger;
 43  
 import org.slf4j.LoggerFactory;
 44  
 /**
 45  
  * RepositoryScannerInstance 
 46  
  *
 47  
  * @version $Id: RepositoryScannerInstance.java 1042689 2010-12-06 15:56:35Z brett $
 48  
  */
 49  
 public class RepositoryScannerInstance
 50  
     implements DirectoryWalkListener
 51  
 {
 52  0
     private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
 53  
     
 54  
     /**
 55  
      * Consumers that process known content.
 56  
      */
 57  
     private List<KnownRepositoryContentConsumer> knownConsumers;
 58  
 
 59  
     /**
 60  
      * Consumers that process unknown/invalid content.
 61  
      */
 62  
     private List<InvalidRepositoryContentConsumer> invalidConsumers;
 63  
 
 64  
     private ManagedRepositoryConfiguration repository;
 65  
 
 66  
     private RepositoryScanStatistics stats;
 67  
 
 68  0
     private long changesSince = 0;
 69  
 
 70  
     private ConsumerProcessFileClosure consumerProcessFile;
 71  
 
 72  
     private ConsumerWantsFilePredicate consumerWantsFile;
 73  
 
 74  
     private Map<String, Long> consumerTimings;
 75  
 
 76  
     private Map<String, Long> consumerCounts;
 77  
 
 78  
     public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
 79  
                                       List<KnownRepositoryContentConsumer> knownConsumerList,
 80  
                                       List<InvalidRepositoryContentConsumer> invalidConsumerList )
 81  0
     {
 82  0
         this.repository = repository;
 83  0
         this.knownConsumers = knownConsumerList;
 84  0
         this.invalidConsumers = invalidConsumerList;
 85  
 
 86  0
         consumerTimings = new HashMap<String,Long>();
 87  0
         consumerCounts = new HashMap<String,Long>();
 88  
 
 89  0
         this.consumerProcessFile = new ConsumerProcessFileClosure();
 90  0
         consumerProcessFile.setExecuteOnEntireRepo( true );
 91  0
         consumerProcessFile.setConsumerTimings( consumerTimings );
 92  0
         consumerProcessFile.setConsumerCounts( consumerCounts );
 93  
 
 94  0
         this.consumerWantsFile = new ConsumerWantsFilePredicate();
 95  
 
 96  0
         stats = new RepositoryScanStatistics();
 97  0
         stats.setRepositoryId( repository.getId() );
 98  
 
 99  0
         Closure triggerBeginScan = new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ), true );
 100  
 
 101  0
         CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
 102  0
         CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
 103  
 
 104  0
         if ( SystemUtils.IS_OS_WINDOWS )
 105  
         {
 106  0
             consumerWantsFile.setCaseSensitive( false );
 107  
         }
 108  0
     }
 109  
 
 110  
     public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
 111  
                                       List<KnownRepositoryContentConsumer> knownContentConsumers,
 112  
                                       List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
 113  
     {
 114  0
         this( repository, knownContentConsumers, invalidContentConsumers );
 115  
 
 116  0
         consumerWantsFile.setChangesSince( changesSince );
 117  
 
 118  0
         this.changesSince = changesSince;
 119  0
     }
 120  
 
 121  
     public RepositoryScanStatistics getStatistics()
 122  
     {
 123  0
         return stats;
 124  
     }
 125  
 
 126  
     public Map<String, Long> getConsumerTimings()
 127  
     {
 128  0
         return consumerTimings;
 129  
     }
 130  
 
 131  
     public Map<String, Long> getConsumerCounts()
 132  
     {
 133  0
         return consumerCounts;
 134  
     }
 135  
 
 136  
     public void directoryWalkStarting( File basedir )
 137  
     {
 138  0
         log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
 139  0
         stats.triggerStart();
 140  0
     }
 141  
 
 142  
     public void directoryWalkStep( int percentage, File file )
 143  
     {
 144  0
         log.debug( "Walk Step: " + percentage + ", " + file );
 145  
 
 146  0
         stats.increaseFileCount();
 147  
 
 148  
         // consume files regardless - the predicate will check the timestamp
 149  0
         BaseFile basefile = new BaseFile( repository.getLocation(), file );
 150  
 
 151  
         // Timestamp finished points to the last successful scan, not this current one.
 152  0
         if ( file.lastModified() >= changesSince )
 153  
         {
 154  0
             stats.increaseNewFileCount();
 155  
         }
 156  
 
 157  0
         consumerProcessFile.setBasefile( basefile );
 158  0
         consumerWantsFile.setBasefile( basefile );
 159  
 
 160  0
         Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
 161  0
         CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
 162  
 
 163  0
         if ( consumerWantsFile.getWantedFileCount() <= 0 )
 164  
         {
 165  
             // Nothing known processed this file.  It is invalid!
 166  0
             CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
 167  
         }
 168  0
     }
 169  
 
 170  
     public void directoryWalkFinished()
 171  
     {
 172  0
         TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
 173  
         
 174  0
         CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
 175  0
         CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
 176  
 
 177  0
         stats.setConsumerTimings( consumerTimings );
 178  0
         stats.setConsumerCounts( consumerCounts );
 179  
         
 180  0
         log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
 181  0
         stats.triggerFinished();
 182  0
     }
 183  
 
 184  
     /**
 185  
      * Debug method from DirectoryWalker.
 186  
      */
 187  
     public void debug( String message )
 188  
     {
 189  0
         log.debug( "Repository Scanner: " + message );
 190  0
     }
 191  
     
 192  
     public ManagedRepositoryConfiguration getRepository()
 193  
     {
 194  0
         return repository;
 195  
     }
 196  
 
 197  
     public RepositoryScanStatistics getStats()
 198  
     {
 199  0
         return stats;
 200  
     }
 201  
 
 202  
     public long getChangesSince()
 203  
     {
 204  0
         return changesSince;
 205  
     }
 206  
 }