]> source.dussan.org Git - archiva.git/blob
63229aa9797122174c172db39aa185d1ca21a507
[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.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
27 import org.apache.commons.collections.Closure;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.collections.functors.IfClosure;
30 import org.apache.commons.lang.SystemUtils;
31 import org.apache.maven.archiva.common.utils.BaseFile;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
34 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
35 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
36 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
37 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
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() ), true );
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
96     public RepositoryScannerInstance( ManagedRepositoryConfiguration repository,
97                                       List<KnownRepositoryContentConsumer> knownContentConsumers,
98                                       List<InvalidRepositoryContentConsumer> invalidContentConsumers, long changesSince )
99     {
100         this( repository, knownContentConsumers, invalidContentConsumers );
101
102         consumerWantsFile.setChangesSince( changesSince );
103
104         this.changesSince = changesSince;
105     }
106
107     public RepositoryScanStatistics getStatistics()
108     {
109         return stats;
110     }
111
112     public void directoryWalkStarting( File basedir )
113     {
114         log.info( "Walk Started: [" + this.repository.getId() + "] " + this.repository.getLocation() );
115         stats.triggerStart();
116     }
117
118     public void directoryWalkStep( int percentage, File file )
119     {
120         log.debug( "Walk Step: " + percentage + ", " + file );
121
122         stats.increaseFileCount();
123
124         // consume files regardless - the predicate will check the timestamp
125         BaseFile basefile = new BaseFile( repository.getLocation(), file );
126         
127         // Timestamp finished points to the last successful scan, not this current one.
128         if ( file.lastModified() >= changesSince )
129         {
130             stats.increaseNewFileCount();             
131         }
132         
133         consumerProcessFile.setBasefile( basefile );
134         consumerProcessFile.setExecuteOnEntireRepo( true );
135         consumerWantsFile.setBasefile( basefile );
136         
137         Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
138         CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
139         
140         if ( consumerWantsFile.getWantedFileCount() <= 0 )
141         {
142             // Nothing known processed this file.  It is invalid!
143             CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
144         }
145     }
146
147     public void directoryWalkFinished()
148     {
149         TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
150         
151         CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
152         CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
153         
154         log.info( "Walk Finished: [" + this.repository.getId() + "] " + this.repository.getLocation() );
155         stats.triggerFinished();
156     }
157
158     /**
159      * Debug method from DirectoryWalker.
160      */
161     public void debug( String message )
162     {
163         log.debug( "Repository Scanner: " + message );
164     }
165     
166 }