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