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.List;
25 import org.apache.commons.collections.Closure;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections.functors.IfClosure;
28 import org.apache.commons.lang.SystemUtils;
29 import org.apache.maven.archiva.common.utils.BaseFile;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
32 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
33 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
34 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
35 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
36 import org.codehaus.plexus.util.DirectoryWalkListener;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 * RepositoryScannerInstance
42 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
45 public class RepositoryScannerInstance
46 implements DirectoryWalkListener
48 private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
51 * Consumers that process known content.
53 private List<KnownRepositoryContentConsumer> knownConsumers;
56 * Consumers that process unknown/invalid content.
58 private List<InvalidRepositoryContentConsumer> invalidConsumers;
60 private ManagedRepositoryConfiguration repository;
62 private RepositoryScanStatistics stats;
64 private long changesSince = 0;
66 private ConsumerProcessFileClosure consumerProcessFile;
68 private ConsumerWantsFilePredicate consumerWantsFile;
70 public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
71 List<KnownRepositoryContentConsumer> knownConsumerList,
72 List<InvalidRepositoryContentConsumer> invalidConsumerList )
74 this.repository = repository;
75 this.knownConsumers = knownConsumerList;
76 this.invalidConsumers = invalidConsumerList;
78 this.consumerProcessFile = new ConsumerProcessFileClosure();
79 this.consumerWantsFile = new ConsumerWantsFilePredicate();
81 stats = new RepositoryScanStatistics();
82 stats.setRepositoryId( repository.getId() );
84 Closure triggerBeginScan = new TriggerBeginScanClosure( repository );
86 CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
87 CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
89 if ( SystemUtils.IS_OS_WINDOWS )
91 consumerWantsFile.setCaseSensitive( false );
95 public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
96 List<KnownRepositoryContentConsumer> knownContentConsumers,
97 List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
99 this( repository, knownContentConsumers, invalidContentConsumers );
101 consumerWantsFile.setChangesSince( changesSince );
103 this.changesSince = changesSince;
106 public RepositoryScanStatistics getStatistics()
111 public void directoryWalkStarting( File basedir )
113 log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
114 stats.triggerStart();
117 public void directoryWalkStep( int percentage, File file )
119 log.debug( "Walk Step: " + percentage + ", " + file );
121 stats.increaseFileCount();
123 // Timestamp finished points to the last successful scan, not this current one.
124 if ( file.lastModified() >= changesSince )
126 stats.increaseNewFileCount();
129 // consume files regardless - the predicate will check the timestamp
130 BaseFile basefile = new BaseFile( repository.getLocation(), file );
132 consumerProcessFile.setBasefile( basefile );
133 consumerWantsFile.setBasefile( basefile );
135 Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
136 CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
138 if ( consumerWantsFile.getWantedFileCount() <= 0 )
140 // Nothing known processed this file. It is invalid!
141 CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
145 public void directoryWalkFinished()
147 log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
148 stats.triggerFinished();
152 * Debug method from DirectoryWalker.
154 public void debug( String message )
156 log.debug( "Repository Scanner: " + message );