1 package org.apache.maven.archiva.repository.scanner;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.util.Date;
24 import java.util.List;
26 import org.apache.commons.collections.Closure;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.collections.functors.IfClosure;
29 import org.apache.commons.lang.SystemUtils;
30 import org.apache.maven.archiva.common.utils.BaseFile;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
33 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
34 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
35 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
36 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
37 import org.apache.maven.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
38 import org.codehaus.plexus.util.DirectoryWalkListener;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 * RepositoryScannerInstance
46 public class RepositoryScannerInstance
47 implements DirectoryWalkListener
49 private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
52 * Consumers that process known content.
54 private List<KnownRepositoryContentConsumer> knownConsumers;
57 * Consumers that process unknown/invalid content.
59 private List<InvalidRepositoryContentConsumer> invalidConsumers;
61 private ManagedRepositoryConfiguration repository;
63 private RepositoryScanStatistics stats;
65 private long changesSince = 0;
67 private ConsumerProcessFileClosure consumerProcessFile;
69 private ConsumerWantsFilePredicate consumerWantsFile;
71 public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
72 List<KnownRepositoryContentConsumer> knownConsumerList,
73 List<InvalidRepositoryContentConsumer> invalidConsumerList )
75 this.repository = repository;
76 this.knownConsumers = knownConsumerList;
77 this.invalidConsumers = invalidConsumerList;
79 this.consumerProcessFile = new ConsumerProcessFileClosure();
80 this.consumerWantsFile = new ConsumerWantsFilePredicate();
82 stats = new RepositoryScanStatistics();
83 stats.setRepositoryId( repository.getId() );
85 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ) );
87 CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
88 CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
90 if ( SystemUtils.IS_OS_WINDOWS )
92 consumerWantsFile.setCaseSensitive( false );
95 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure(repository);
97 CollectionUtils.forAllDo(knownConsumers, scanCompletedClosure);
98 CollectionUtils.forAllDo(invalidConsumerList, scanCompletedClosure);
101 public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
102 List<KnownRepositoryContentConsumer> knownContentConsumers,
103 List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
105 this( repository, knownContentConsumers, invalidContentConsumers );
107 consumerWantsFile.setChangesSince( changesSince );
109 this.changesSince = changesSince;
112 public RepositoryScanStatistics getStatistics()
117 public void directoryWalkStarting( File basedir )
119 log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
120 stats.triggerStart();
123 public void directoryWalkStep( int percentage, File file )
125 log.debug( "Walk Step: " + percentage + ", " + file );
127 stats.increaseFileCount();
129 // consume files regardless - the predicate will check the timestamp
130 BaseFile basefile = new BaseFile( repository.getLocation(), file );
132 // Timestamp finished points to the last successful scan, not this current one.
133 if ( file.lastModified() >= changesSince )
135 stats.increaseNewFileCount();
138 consumerProcessFile.setBasefile( basefile );
139 consumerWantsFile.setBasefile( basefile );
141 Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
142 CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
144 if ( consumerWantsFile.getWantedFileCount() <= 0 )
146 // Nothing known processed this file. It is invalid!
147 CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
151 public void directoryWalkFinished()
153 log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
154 stats.triggerFinished();
158 * Debug method from DirectoryWalker.
160 public void debug( String message )
162 log.debug( "Repository Scanner: " + message );