]> source.dussan.org Git - archiva.git/blob
65cb6603c14d484146e9b1c1fdea10bae50fce3b
[archiva.git] /
1 package org.apache.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 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;
37
38 import java.io.File;
39 import java.util.Date;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 /**
45  * RepositoryScannerInstance
46  */
47 public class RepositoryScannerInstance
48     implements DirectoryWalkListener
49 {
50     private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
51
52     /**
53      * Consumers that process known content.
54      */
55     private List<KnownRepositoryContentConsumer> knownConsumers;
56
57     /**
58      * Consumers that process unknown/invalid content.
59      */
60     private List<InvalidRepositoryContentConsumer> invalidConsumers;
61
62     private ManagedRepository repository;
63
64     private RepositoryScanStatistics stats;
65
66     private long changesSince = 0;
67
68     private ConsumerProcessFileClosure consumerProcessFile;
69
70     private ConsumerWantsFilePredicate consumerWantsFile;
71
72     private Map<String, Long> consumerTimings;
73
74     private Map<String, Long> consumerCounts;
75
76     public RepositoryScannerInstance( ManagedRepository repository,
77                                       List<KnownRepositoryContentConsumer> knownConsumerList,
78                                       List<InvalidRepositoryContentConsumer> invalidConsumerList )
79     {
80         this.repository = repository;
81         this.knownConsumers = knownConsumerList;
82         this.invalidConsumers = invalidConsumerList;
83
84         consumerTimings = new HashMap<>();
85         consumerCounts = new HashMap<>();
86
87         this.consumerProcessFile = new ConsumerProcessFileClosure();
88         consumerProcessFile.setExecuteOnEntireRepo( true );
89         consumerProcessFile.setConsumerTimings( consumerTimings );
90         consumerProcessFile.setConsumerCounts( consumerCounts );
91
92         this.consumerWantsFile = new ConsumerWantsFilePredicate( repository );
93
94         stats = new RepositoryScanStatistics();
95         stats.setRepositoryId( repository.getId() );
96
97         Closure triggerBeginScan =
98             new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ), true );
99
100         CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
101         CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
102
103         if ( SystemUtils.IS_OS_WINDOWS )
104         {
105             consumerWantsFile.setCaseSensitive( false );
106         }
107     }
108
109     public RepositoryScannerInstance( ManagedRepository repository,
110                                       List<KnownRepositoryContentConsumer> knownContentConsumers,
111                                       List<InvalidRepositoryContentConsumer> invalidContentConsumers,
112                                       long changesSince )
113     {
114         this( repository, knownContentConsumers, invalidContentConsumers );
115
116         consumerWantsFile.setChangesSince( changesSince );
117
118         this.changesSince = changesSince;
119     }
120
121     public RepositoryScanStatistics getStatistics()
122     {
123         return stats;
124     }
125
126     public Map<String, Long> getConsumerTimings()
127     {
128         return consumerTimings;
129     }
130
131     public Map<String, Long> getConsumerCounts()
132     {
133         return consumerCounts;
134     }
135
136     @Override
137     public void directoryWalkStarting( File basedir )
138     {
139         log.info( "Walk Started: [{}] {}", this.repository.getId(), this.repository.getLocation() );
140         stats.triggerStart();
141     }
142
143     @Override
144     public void directoryWalkStep( int percentage, File file )
145     {
146         log.debug( "Walk Step: {}, {}", percentage, file );
147
148         stats.increaseFileCount();
149
150         // consume files regardless - the predicate will check the timestamp
151         BaseFile basefile = new BaseFile( repository.getLocation(), file );
152
153         // Timestamp finished points to the last successful scan, not this current one.
154         if ( file.lastModified() >= changesSince )
155         {
156             stats.increaseNewFileCount();
157         }
158
159         consumerProcessFile.setBasefile( basefile );
160         consumerWantsFile.setBasefile( basefile );
161
162         Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
163         CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
164
165         if ( consumerWantsFile.getWantedFileCount() <= 0 )
166         {
167             // Nothing known processed this file.  It is invalid!
168             CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
169         }
170     }
171
172     @Override
173     public void directoryWalkFinished()
174     {
175         TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
176         CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
177         CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
178
179         stats.setConsumerTimings( consumerTimings );
180         stats.setConsumerCounts( consumerCounts );
181
182         log.info( "Walk Finished: [{}] {}", this.repository.getId(), this.repository.getLocation() );
183         stats.triggerFinished();
184     }
185
186     /**
187      * Debug method from DirectoryWalker.
188      */
189     @Override
190     public void debug( String message )
191     {
192         log.debug( "Repository Scanner: {}", message );
193     }
194
195     public ManagedRepository getRepository()
196     {
197         return repository;
198     }
199
200     public RepositoryScanStatistics getStats()
201     {
202         return stats;
203     }
204
205     public long getChangesSince()
206     {
207         return changesSince;
208     }
209 }