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.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
27 import org.apache.commons.collections.Closure;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.collections.functors.IfClosure;
30 import org.apache.commons.lang.SystemUtils;
31 import org.apache.maven.archiva.common.utils.BaseFile;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
34 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
35 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
36 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
37 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
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() ), true );
87 CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
88 CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
90 if ( SystemUtils.IS_OS_WINDOWS )
92 consumerWantsFile.setCaseSensitive( false );
96 public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
97 List<KnownRepositoryContentConsumer> knownContentConsumers,
98 List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
100 this( repository, knownContentConsumers, invalidContentConsumers );
102 consumerWantsFile.setChangesSince( changesSince );
104 this.changesSince = changesSince;
107 public RepositoryScanStatistics getStatistics()
112 public void directoryWalkStarting( File basedir )
114 log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
115 stats.triggerStart();
118 public void directoryWalkStep( int percentage, File file )
120 log.debug( "Walk Step: " + percentage + ", " + file );
122 stats.increaseFileCount();
124 // consume files regardless - the predicate will check the timestamp
125 BaseFile basefile = new BaseFile( repository.getLocation(), file );
127 // Timestamp finished points to the last successful scan, not this current one.
128 if ( file.lastModified() >= changesSince )
130 stats.increaseNewFileCount();
133 consumerProcessFile.setBasefile( basefile );
134 consumerProcessFile.setExecuteOnEntireRepo( true );
135 consumerWantsFile.setBasefile( basefile );
137 Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
138 CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
140 if ( consumerWantsFile.getWantedFileCount() <= 0 )
142 // Nothing known processed this file. It is invalid!
143 CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
147 public void directoryWalkFinished()
149 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
151 CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
152 CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
154 log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
155 stats.triggerFinished();
159 * Debug method from DirectoryWalker.
161 public void debug( String message )
163 log.debug( "Repository Scanner: " + message );