]> source.dussan.org Git - archiva.git/blob
e81148620813240f51aeff32c9def6022611c1cb
[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.ArrayList;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.collections.Closure;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.collections.functors.IfClosure;
32 import org.apache.maven.archiva.common.utils.BaseFile;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
36 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
37 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
38 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
39 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
40 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
41 import org.apache.maven.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
42 import org.springframework.beans.BeansException;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.ApplicationContextAware;
45
46 /**
47  * RepositoryContentConsumerUtil 
48  *
49  * @version $Id$
50  */
51 public class RepositoryContentConsumers implements ApplicationContextAware
52 {
53     private ApplicationContext applicationContext;
54     
55     private ArchivaConfiguration archivaConfiguration;
56
57     private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
58
59     private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
60
61     public RepositoryContentConsumers(ArchivaConfiguration archivaConfiguration)
62     {
63         this.archivaConfiguration = archivaConfiguration;
64     }
65
66     public void setApplicationContext(ApplicationContext applicationContext)
67         throws BeansException
68     {
69         this.applicationContext = applicationContext;
70     }
71
72     /**
73      * <p>
74      * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
75      * been selected in the configuration to execute.
76      * </p>
77      * 
78      * <p>
79      * NOTE: This list can be larger and contain entries that might not exist or be available
80      * in the classpath, or as a component.
81      * </p>
82      * 
83      * @return the list of consumer ids that have been selected by the configuration.
84      */
85     public List<String> getSelectedKnownConsumerIds()
86     {
87         RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
88         return scanning.getKnownContentConsumers();
89     }
90
91     /**
92      * <p>
93      * Get the list of Ids associated with those {@link InvalidRepositoryContentConsumer} that have
94      * been selected in the configuration to execute.
95      * </p>
96      * 
97      * <p>
98      * NOTE: This list can be larger and contain entries that might not exist or be available
99      * in the classpath, or as a component.
100      * </p>
101      * 
102      * @return the list of consumer ids that have been selected by the configuration.
103      */
104     public List<String> getSelectedInvalidConsumerIds()
105     {
106         RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
107         return scanning.getInvalidContentConsumers();
108     }
109
110     /**
111      * Get the map of {@link String} ids to {@link KnownRepositoryContentConsumer} implementations,
112      * for those consumers that have been selected according to the active configuration. 
113      * 
114      * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
115      */
116     public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
117     {
118         Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
119
120         for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
121         {
122             consumerMap.put( consumer.getId(), consumer );
123         }
124
125         return consumerMap;
126     }
127
128     /**
129      * Get the map of {@link String} ids to {@link InvalidRepositoryContentConsumer} implementations,
130      * for those consumers that have been selected according to the active configuration. 
131      * 
132      * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
133      */
134     public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
135     {
136         Map<String, InvalidRepositoryContentConsumer> consumerMap = new HashMap<String, InvalidRepositoryContentConsumer>();
137
138         for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
139         {
140             consumerMap.put( consumer.getId(), consumer );
141         }
142
143         return consumerMap;
144     }
145
146     /**
147      * Get the list of {@link KnownRepositoryContentConsumer} objects that are
148      * selected according to the active configuration.
149      * 
150      * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
151      *         by the active configuration.
152      */
153     public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
154     {
155         if ( selectedKnownConsumers == null )
156         {
157             List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
158
159             List<String> knownSelected = getSelectedKnownConsumerIds();
160
161             for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
162             {
163                 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
164                 {
165                     ret.add( consumer );
166                 }
167             }
168             this.selectedKnownConsumers = ret;
169         }
170         return selectedKnownConsumers;
171     }
172
173     /**
174      * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
175      * selected according to the active configuration.
176      * 
177      * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
178      *         by the active configuration.
179      */
180     public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
181     {
182         if ( selectedInvalidConsumers == null )
183         {
184             List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
185
186             List<String> invalidSelected = getSelectedInvalidConsumerIds();
187
188             for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
189             {
190                 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
191                 {
192                     ret.add( consumer );
193                 }
194             }
195             selectedInvalidConsumers = ret;
196         }
197         return selectedInvalidConsumers;
198     }
199
200     /**
201      * Get the list of {@link KnownRepositoryContentConsumer} objects that are
202      * available and present in the classpath and as components in the IoC.
203      * 
204      * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath 
205      *         and as a component in the IoC.
206      */
207     public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
208     {
209         return new ArrayList(applicationContext.getBeansOfType(KnownRepositoryContentConsumer.class).values());
210     }
211
212     /**
213      * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
214      * available and present in the classpath and as components in the IoC.
215      * 
216      * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath 
217      *         and as a component in the IoC.
218      */
219     public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
220     {
221         return new ArrayList(applicationContext.getBeansOfType(InvalidRepositoryContentConsumer.class).values());
222     }
223
224     /**
225      * A convienence method to execute all of the active selected consumers for a 
226      * particular arbitrary file.
227      * 
228      * @param repository the repository configuration to use.
229      * @param localFile the local file to execute the consumers against.
230      */
231     public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile )
232     {
233         // Run the repository consumers
234         try
235         {   
236             Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime() );
237
238             List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
239             List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
240             CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
241             CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
242
243             // yuck. In case you can't read this, it says
244             // "process the file if the consumer has it in the includes list, and not in the excludes list"
245             BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
246             ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
247             predicate.setBasefile( baseFile );
248             ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
249             closure.setBasefile( baseFile );
250             predicate.setCaseSensitive( false );
251             Closure processIfWanted = IfClosure.getInstance( predicate, closure );
252
253             CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
254
255             if ( predicate.getWantedFileCount() <= 0 )
256             {
257                 // Nothing known processed this file.  It is invalid!
258                 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
259             }
260
261             TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure(repository);
262
263             CollectionUtils.forAllDo(selectedKnownConsumers, scanCompletedClosure);
264             CollectionUtils.forAllDo(selectedKnownConsumers, scanCompletedClosure);
265         }
266         finally
267         {
268             /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
269                         CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
270                         CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
271             */
272         }
273     }
274
275     public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
276     {
277         this.selectedKnownConsumers = selectedKnownConsumers;
278     }
279
280     public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
281     {
282         this.selectedInvalidConsumers = selectedInvalidConsumers;
283     }
284     
285     protected Date getStartTime()
286     {   
287         return new Date( System.currentTimeMillis() );
288     }
289     
290     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
291     {
292         this.archivaConfiguration = archivaConfiguration;
293     }
294 }