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