]> source.dussan.org Git - archiva.git/blob
80a6fb7306f21639349d34b5461e5170224418a0
[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  * @version $Id$
48  */
49 public class RepositoryScannerInstance
50     implements DirectoryWalkListener
51 {
52     private Logger log = LoggerFactory.getLogger( RepositoryScannerInstance.class );
53
54     /**
55      * Consumers that process known content.
56      */
57     private List<KnownRepositoryContentConsumer> knownConsumers;
58
59     /**
60      * Consumers that process unknown/invalid content.
61      */
62     private List<InvalidRepositoryContentConsumer> invalidConsumers;
63
64     private ManagedRepository repository;
65
66     private RepositoryScanStatistics stats;
67
68     private long changesSince = 0;
69
70     private ConsumerProcessFileClosure consumerProcessFile;
71
72     private ConsumerWantsFilePredicate consumerWantsFile;
73
74     private Map<String, Long> consumerTimings;
75
76     private Map<String, Long> consumerCounts;
77
78     public RepositoryScannerInstance( ManagedRepository repository,
79                                       List<KnownRepositoryContentConsumer> knownConsumerList,
80                                       List<InvalidRepositoryContentConsumer> invalidConsumerList )
81     {
82         this.repository = repository;
83         this.knownConsumers = knownConsumerList;
84         this.invalidConsumers = invalidConsumerList;
85
86         consumerTimings = new HashMap<String, Long>();
87         consumerCounts = new HashMap<String, Long>();
88
89         this.consumerProcessFile = new ConsumerProcessFileClosure();
90         consumerProcessFile.setExecuteOnEntireRepo( true );
91         consumerProcessFile.setConsumerTimings( consumerTimings );
92         consumerProcessFile.setConsumerCounts( consumerCounts );
93
94         this.consumerWantsFile = new ConsumerWantsFilePredicate();
95
96         stats = new RepositoryScanStatistics();
97         stats.setRepositoryId( repository.getId() );
98
99         Closure triggerBeginScan =
100             new TriggerBeginScanClosure( repository, new Date( System.currentTimeMillis() ), true );
101
102         CollectionUtils.forAllDo( knownConsumerList, triggerBeginScan );
103         CollectionUtils.forAllDo( invalidConsumerList, triggerBeginScan );
104
105         if ( SystemUtils.IS_OS_WINDOWS )
106         {
107             consumerWantsFile.setCaseSensitive( false );
108         }
109     }
110
111     public RepositoryScannerInstance( ManagedRepository repository,
112                                       List<KnownRepositoryContentConsumer> knownContentConsumers,
113                                       List<InvalidRepositoryContentConsumer> invalidContentConsumers,
114                                       long changesSince )
115     {
116         this( repository, knownContentConsumers, invalidContentConsumers );
117
118         consumerWantsFile.setChangesSince( changesSince );
119
120         this.changesSince = changesSince;
121     }
122
123     public RepositoryScanStatistics getStatistics()
124     {
125         return stats;
126     }
127
128     public Map<String, Long> getConsumerTimings()
129     {
130         return consumerTimings;
131     }
132
133     public Map<String, Long> getConsumerCounts()
134     {
135         return consumerCounts;
136     }
137
138     public void directoryWalkStarting( File basedir )
139     {
140         log.info( "Walk Started: [{}] {}", this.repository.getId(), this.repository.getLocation() );
141         stats.triggerStart();
142     }
143
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     public void directoryWalkFinished()
173     {
174         TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
175         CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
176         CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
177
178         stats.setConsumerTimings( consumerTimings );
179         stats.setConsumerCounts( consumerCounts );
180
181         log.info( "Walk Finished: [{}] {}", this.repository.getId(), this.repository.getLocation() );
182         stats.triggerFinished();
183     }
184
185     /**
186      * Debug method from DirectoryWalker.
187      */
188     public void debug( String message )
189     {
190         log.debug( "Repository Scanner: {}", message );
191     }
192
193     public ManagedRepository getRepository()
194     {
195         return repository;
196     }
197
198     public RepositoryScanStatistics getStats()
199     {
200         return stats;
201     }
202
203     public long getChangesSince()
204     {
205         return changesSince;
206     }
207 }