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
49 public class RepositoryScannerInstance
50 implements DirectoryWalkListener
52 private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
55 * Consumers that process known content.
57 private List<KnownRepositoryContentConsumer> knownConsumers;
60 * Consumers that process unknown/invalid content.
62 private List<InvalidRepositoryContentConsumer> invalidConsumers;
64 private ManagedRepository repository;
66 private RepositoryScanStatistics stats;
68 private long changesSince = 0;
70 private ConsumerProcessFileClosure consumerProcessFile;
72 private ConsumerWantsFilePredicate consumerWantsFile;
74 private Map<String, Long> consumerTimings;
76 private Map<String, Long> consumerCounts;
78 public RepositoryScannerInstance( ManagedRepository repository,
79 List<KnownRepositoryContentConsumer> knownConsumerList,
80 List<InvalidRepositoryContentConsumer> invalidConsumerList )
82 this.repository = repository;
83 this.knownConsumers = knownConsumerList;
84 this.invalidConsumers = invalidConsumerList;
86 consumerTimings = new HashMap<String, Long>();
87 consumerCounts = new HashMap<String, Long>();
89 this.consumerProcessFile = new ConsumerProcessFileClosure();
90 consumerProcessFile.setExecuteOnEntireRepo( true );
91 consumerProcessFile.setConsumerTimings( consumerTimings );
92 consumerProcessFile.setConsumerCounts( consumerCounts );
94 this.consumerWantsFile = new ConsumerWantsFilePredicate();
96 stats = new RepositoryScanStatistics();
97 stats.setRepositoryId( repository.getId() );
99 Closure triggerBeginScan =
100 new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ), true );
102 CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
103 CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
105 if ( SystemUtils.IS_OS_WINDOWS )
107 consumerWantsFile.setCaseSensitive( false );
111 public RepositoryScannerInstance( ManagedRepository repository,
112 List<KnownRepositoryContentConsumer> knownContentConsumers,
113 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
116 this( repository, knownContentConsumers, invalidContentConsumers );
118 consumerWantsFile.setChangesSince( changesSince );
120 this.changesSince = changesSince;
123 public RepositoryScanStatistics getStatistics()
128 public Map<String, Long> getConsumerTimings()
130 return consumerTimings;
133 public Map<String, Long> getConsumerCounts()
135 return consumerCounts;
138 public void directoryWalkStarting( File basedir )
140 log.info( "Walk Started: [{}] {}", this.repository.getId(), this.repository.getLocation() );
141 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 );
172 public void directoryWalkFinished()
174 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
175 CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
176 CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
178 stats.setConsumerTimings( consumerTimings );
179 stats.setConsumerCounts( consumerCounts );
181 log.info( "Walk Finished: [{}] {}", this.repository.getId(), this.repository.getLocation() );
182 stats.triggerFinished();
186 * Debug method from DirectoryWalker.
188 public void debug( String message )
190 log.debug( "Repository Scanner: {}", message );
193 public ManagedRepository getRepository()
198 public RepositoryScanStatistics getStats()
203 public long getChangesSince()