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