1 package org.apache.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
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.BaseFile;
24 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
25 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
27 import org.apache.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
28 import org.apache.archiva.repository.scanner.functors.TriggerBeginScanClosure;
29 import org.apache.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
30 import org.apache.commons.collections.Closure;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.apache.commons.collections.functors.IfClosure;
33 import org.apache.commons.lang.SystemUtils;
34 import org.codehaus.plexus.util.DirectoryWalkListener;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 import java.util.Date;
40 import java.util.HashMap;
41 import java.util.List;
45 * RepositoryScannerInstance
47 public class RepositoryScannerInstance
48 implements DirectoryWalkListener
50 private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
53 * Consumers that process known content.
55 private List<KnownRepositoryContentConsumer> knownConsumers;
58 * Consumers that process unknown/invalid content.
60 private List<InvalidRepositoryContentConsumer> invalidConsumers;
62 private ManagedRepository repository;
64 private RepositoryScanStatistics stats;
66 private long changesSince = 0;
68 private ConsumerProcessFileClosure consumerProcessFile;
70 private ConsumerWantsFilePredicate consumerWantsFile;
72 private Map<String, Long> consumerTimings;
74 private Map<String, Long> consumerCounts;
76 public RepositoryScannerInstance( ManagedRepository repository,
77 List<KnownRepositoryContentConsumer> knownConsumerList,
78 List<InvalidRepositoryContentConsumer> invalidConsumerList )
80 this.repository = repository;
81 this.knownConsumers = knownConsumerList;
82 this.invalidConsumers = invalidConsumerList;
84 consumerTimings = new HashMap<>();
85 consumerCounts = new HashMap<>();
87 this.consumerProcessFile = new ConsumerProcessFileClosure();
88 consumerProcessFile.setExecuteOnEntireRepo( true );
89 consumerProcessFile.setConsumerTimings( consumerTimings );
90 consumerProcessFile.setConsumerCounts( consumerCounts );
92 this.consumerWantsFile = new ConsumerWantsFilePredicate( repository );
94 stats = new RepositoryScanStatistics();
95 stats.setRepositoryId( repository.getId() );
97 Closure triggerBeginScan =
98 new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ), true );
100 CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
101 CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
103 if ( SystemUtils.IS_OS_WINDOWS )
105 consumerWantsFile.setCaseSensitive( false );
109 public RepositoryScannerInstance( ManagedRepository repository,
110 List<KnownRepositoryContentConsumer> knownContentConsumers,
111 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
114 this( repository, knownContentConsumers, invalidContentConsumers );
116 consumerWantsFile.setChangesSince( changesSince );
118 this.changesSince = changesSince;
121 public RepositoryScanStatistics getStatistics()
126 public Map<String, Long> getConsumerTimings()
128 return consumerTimings;
131 public Map<String, Long> getConsumerCounts()
133 return consumerCounts;
137 public void directoryWalkStarting( File basedir )
139 log.info( "Walk Started: [{}] {}", this.repository.getId(), this.repository.getLocation() );
140 stats.triggerStart();
144 public void directoryWalkStep( int percentage, File file )
146 log.debug( "Walk Step: {}, {}", percentage, file );
148 stats.increaseFileCount();
150 // consume files regardless - the predicate will check the timestamp
151 BaseFile basefile = new BaseFile( repository.getLocation(), file );
153 // Timestamp finished points to the last successful scan, not this current one.
154 if ( file.lastModified() >= changesSince )
156 stats.increaseNewFileCount();
159 consumerProcessFile.setBasefile( basefile );
160 consumerWantsFile.setBasefile( basefile );
162 Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
163 CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
165 if ( consumerWantsFile.getWantedFileCount() <= 0 )
167 // Nothing known processed this file. It is invalid!
168 CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
173 public void directoryWalkFinished()
175 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
176 CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
177 CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
179 stats.setConsumerTimings( consumerTimings );
180 stats.setConsumerCounts( consumerCounts );
182 log.info( "Walk Finished: [{}] {}", this.repository.getId(), this.repository.getLocation() );
183 stats.triggerFinished();
187 * Debug method from DirectoryWalker.
190 public void debug( String message )
192 log.debug( "Repository Scanner: {}", message );
195 public ManagedRepository getRepository()
200 public RepositoryScanStatistics getStats()
205 public long getChangesSince()