This project has retired. For details please refer to its Attic page.
RepositoryScannerInstance xref
View Javadoc
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         // Safety check, if a parallel process removes the file
154         if (basefile.exists())
155         {
156 
157             // Timestamp finished points to the last successful scan, not this current one.
158             if ( file.lastModified( ) >= changesSince )
159             {
160                 stats.increaseNewFileCount( );
161             }
162 
163             consumerProcessFile.setBasefile( basefile );
164             consumerWantsFile.setBasefile( basefile );
165 
166             Closure processIfWanted = IfClosure.getInstance( consumerWantsFile, consumerProcessFile );
167             CollectionUtils.forAllDo( this.knownConsumers, processIfWanted );
168 
169             if ( consumerWantsFile.getWantedFileCount( ) <= 0 )
170             {
171                 // Nothing known processed this file.  It is invalid!
172                 CollectionUtils.forAllDo( this.invalidConsumers, consumerProcessFile );
173             }
174         }
175     }
176 
177     @Override
178     public void directoryWalkFinished()
179     {
180         TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, true );
181         CollectionUtils.forAllDo( knownConsumers, scanCompletedClosure );
182         CollectionUtils.forAllDo( invalidConsumers, scanCompletedClosure );
183 
184         stats.setConsumerTimings( consumerTimings );
185         stats.setConsumerCounts( consumerCounts );
186 
187         log.info( "Walk Finished: [{}] {}", this.repository.getId(), this.repository.getLocation() );
188         stats.triggerFinished();
189     }
190 
191     /**
192      * Debug method from DirectoryWalker.
193      */
194     @Override
195     public void debug( String message )
196     {
197         log.debug( "Repository Scanner: {}", message );
198     }
199 
200     public ManagedRepository getRepository()
201     {
202         return repository;
203     }
204 
205     public RepositoryScanStatistics getStats()
206     {
207         return stats;
208     }
209 
210     public long getChangesSince()
211     {
212         return changesSince;
213     }
214 }