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
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
28 import org.apache.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
29 import org.apache.archiva.repository.scanner.functors.TriggerBeginScanClosure;
30 import org.apache.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
31 import org.apache.commons.collections.Closure;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.apache.commons.collections.functors.IfClosure;
34 import org.apache.commons.lang.SystemUtils;
35 import org.apache.maven.archiva.common.utils.BaseFile;
36 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
37 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
38 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
39 import org.apache.maven.archiva.consumers.functors.ConsumerWantsFilePredicate;
40 import org.codehaus.plexus.util.DirectoryWalkListener;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 * RepositoryScannerInstance
48 public class RepositoryScannerInstance
49 implements DirectoryWalkListener
51 private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
54 * Consumers that process known content.
56 private List<KnownRepositoryContentConsumer> knownConsumers;
59 * Consumers that process unknown/invalid content.
61 private List<InvalidRepositoryContentConsumer> invalidConsumers;
63 private ManagedRepositoryConfiguration repository;
65 private RepositoryScanStatistics stats;
67 private long changesSince = 0;
69 private ConsumerProcessFileClosure consumerProcessFile;
71 private ConsumerWantsFilePredicate consumerWantsFile;
73 private Map<String, Long> consumerTimings;
75 private Map<String, Long> consumerCounts;
77 public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
78 List<KnownRepositoryContentConsumer> knownConsumerList,
79 List<InvalidRepositoryContentConsumer> invalidConsumerList )
81 this.repository = repository;
82 this.knownConsumers = knownConsumerList;
83 this.invalidConsumers = invalidConsumerList;
85 consumerTimings = new HashMap<String,Long>();
86 consumerCounts = new HashMap<String,Long>();
88 this.consumerProcessFile = new ConsumerProcessFileClosure();
89 consumerProcessFile.setExecuteOnEntireRepo( true );
90 consumerProcessFile.setConsumerTimings( consumerTimings );
91 consumerProcessFile.setConsumerCounts( consumerCounts );
93 this.consumerWantsFile = new ConsumerWantsFilePredicate();
95 stats = new RepositoryScanStatistics();
96 stats.setRepositoryId( repository.getId() );
98 Closure triggerBeginScan = 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( ManagedRepositoryConfiguration repository,
110 List<KnownRepositoryContentConsumer> knownContentConsumers,
111 List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
113 this( repository, knownContentConsumers, invalidContentConsumers );
115 consumerWantsFile.setChangesSince( changesSince );
117 this.changesSince = changesSince;
120 public RepositoryScanStatistics getStatistics()
125 public Map<String, Long> getConsumerTimings()
127 return consumerTimings;
130 public Map<String, Long> getConsumerCounts()
132 return consumerCounts;
135 public void directoryWalkStarting( File basedir )
137 log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
138 stats.triggerStart();
141 public void directoryWalkStep( int percentage, File file )
143 log.debug( "Walk Step: {}, {}", percentage, file );
145 stats.increaseFileCount();
147 // consume files regardless - the predicate will check the timestamp
148 BaseFile basefile = new BaseFile( repository.getLocation(), file );
150 // Timestamp finished points to the last successful scan, not this current one.
151 if ( file.lastModified() >= changesSince )
153 stats.increaseNewFileCount();
156 consumerProcessFile.setBasefile( basefile );
157 consumerWantsFile.setBasefile( basefile );
159 Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
160 CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
162 if ( consumerWantsFile.getWantedFileCount() <= 0 )
164 // Nothing known processed this file. It is invalid!
165 CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
169 public void directoryWalkFinished()
171 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
172 CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
173 CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
175 stats.setConsumerTimings( consumerTimings );
176 stats.setConsumerCounts( consumerCounts );
178 log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
179 stats.triggerFinished();
183 * Debug method from DirectoryWalker.
185 public void debug( String message )
187 log.debug( "Repository Scanner: {}", message );
190 public ManagedRepositoryConfiguration getRepository()
195 public RepositoryScanStatistics getStats()
200 public long getChangesSince()