]> source.dussan.org Git - archiva.git/blob
1ae3e796061e61a28a8edc3ef20deccce0d5b174
[archiva.git] /
1 package org.apache.maven.archiva.repository.scanner;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
23 import java.util.Date;
24 import java.util.List;
25
26 import org.apache.commons.collections.Closure;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.collections.functors.IfClosure;
29 import org.apache.commons.lang.SystemUtils;
30 import org.apache.maven.archiva.common.utils.BaseFile;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
33 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
34 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
35 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
36 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
37 import org.apache.maven.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
38 import org.codehaus.plexus.util.DirectoryWalkListener;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 /**
42  * RepositoryScannerInstance 
43  *
44  * @version $Id$
45  */
46 public class RepositoryScannerInstance
47     implements DirectoryWalkListener
48 {
49     private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
50     
51     /**
52      * Consumers that process known content.
53      */
54     private List<KnownRepositoryContentConsumer> knownConsumers;
55
56     /**
57      * Consumers that process unknown/invalid content.
58      */
59     private List<InvalidRepositoryContentConsumer> invalidConsumers;
60
61     private ManagedRepositoryConfiguration repository;
62
63     private RepositoryScanStatistics stats;
64
65     private long changesSince = 0;
66
67     private ConsumerProcessFileClosure consumerProcessFile;
68
69     private ConsumerWantsFilePredicate consumerWantsFile;
70
71     public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
72                                       List<KnownRepositoryContentConsumer> knownConsumerList,
73                                       List<InvalidRepositoryContentConsumer> invalidConsumerList )
74     {
75         this.repository = repository;
76         this.knownConsumers = knownConsumerList;
77         this.invalidConsumers = invalidConsumerList;
78
79         this.consumerProcessFile = new ConsumerProcessFileClosure();
80         this.consumerWantsFile = new ConsumerWantsFilePredicate();
81
82         stats = new RepositoryScanStatistics();
83         stats.setRepositoryId( repository.getId() );
84
85         Closure triggerBeginScan = new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ) );
86
87         CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
88         CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
89
90         if ( SystemUtils.IS_OS_WINDOWS )
91         {
92             consumerWantsFile.setCaseSensitive( false );
93         }
94
95         TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure(repository);
96
97         CollectionUtils.forAllDo(knownConsumers, scanCompletedClosure);
98         CollectionUtils.forAllDo(invalidConsumerList, scanCompletedClosure);
99     }
100
101     public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
102                                       List<KnownRepositoryContentConsumer> knownContentConsumers,
103                                       List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
104     {
105         this( repository, knownContentConsumers, invalidContentConsumers );
106
107         consumerWantsFile.setChangesSince( changesSince );
108
109         this.changesSince = changesSince;
110     }
111
112     public RepositoryScanStatistics getStatistics()
113     {
114         return stats;
115     }
116
117     public void directoryWalkStarting( File basedir )
118     {
119         log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
120         stats.triggerStart();
121     }
122
123     public void directoryWalkStep( int percentage, File file )
124     {
125         log.debug( "Walk Step: " + percentage + ", " + file );
126
127         stats.increaseFileCount();
128
129         // consume files regardless - the predicate will check the timestamp
130         BaseFile basefile = new BaseFile( repository.getLocation(), file );
131         
132         // Timestamp finished points to the last successful scan, not this current one.
133         if ( file.lastModified() >= changesSince )
134         {
135             stats.increaseNewFileCount();             
136         }
137         
138         consumerProcessFile.setBasefile( basefile );
139         consumerWantsFile.setBasefile( basefile );
140         
141         Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
142         CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
143         
144         if ( consumerWantsFile.getWantedFileCount() <= 0 )
145         {
146             // Nothing known processed this file.  It is invalid!
147             CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
148         }
149     }
150
151     public void directoryWalkFinished()
152     {
153         log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
154         stats.triggerFinished();
155     }
156
157     /**
158      * Debug method from DirectoryWalker.
159      */
160     public void debug( String message )
161     {
162         log.debug( "Repository Scanner: " + message );
163     }
164     
165 }