1 package org.apache.maven.archiva.repository.scanner;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
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;
47 * RepositoryContentConsumerUtil
51 public class RepositoryContentConsumers implements ApplicationContextAware
53 private ApplicationContext applicationContext;
55 private ArchivaConfiguration archivaConfiguration;
57 private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
59 private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
61 public RepositoryContentConsumers(ArchivaConfiguration archivaConfiguration)
63 this.archivaConfiguration = archivaConfiguration;
66 public void setApplicationContext(ApplicationContext applicationContext)
69 this.applicationContext = applicationContext;
74 * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
75 * been selected in the configuration to execute.
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.
83 * @return the list of consumer ids that have been selected by the configuration.
85 public List<String> getSelectedKnownConsumerIds()
87 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
88 return scanning.getKnownContentConsumers();
93 * Get the list of Ids associated with those {@link InvalidRepositoryContentConsumer} that have
94 * been selected in the configuration to execute.
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.
102 * @return the list of consumer ids that have been selected by the configuration.
104 public List<String> getSelectedInvalidConsumerIds()
106 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
107 return scanning.getInvalidContentConsumers();
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.
114 * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
116 public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
118 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
120 for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
122 consumerMap.put( consumer.getId(), consumer );
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.
132 * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
134 public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
136 Map<String, InvalidRepositoryContentConsumer> consumerMap = new HashMap<String, InvalidRepositoryContentConsumer>();
138 for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
140 consumerMap.put( consumer.getId(), consumer );
147 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
148 * selected according to the active configuration.
150 * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
151 * by the active configuration.
153 public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
155 if ( selectedKnownConsumers == null )
157 List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
159 List<String> knownSelected = getSelectedKnownConsumerIds();
161 for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
163 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
168 this.selectedKnownConsumers = ret;
170 return selectedKnownConsumers;
174 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
175 * selected according to the active configuration.
177 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
178 * by the active configuration.
180 public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
182 if ( selectedInvalidConsumers == null )
184 List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
186 List<String> invalidSelected = getSelectedInvalidConsumerIds();
188 for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
190 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
195 selectedInvalidConsumers = ret;
197 return selectedInvalidConsumers;
201 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
202 * available and present in the classpath and as components in the IoC.
204 * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath
205 * and as a component in the IoC.
207 public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
209 return new ArrayList(applicationContext.getBeansOfType(KnownRepositoryContentConsumer.class).values());
213 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
214 * available and present in the classpath and as components in the IoC.
216 * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath
217 * and as a component in the IoC.
219 public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
221 return new ArrayList(applicationContext.getBeansOfType(InvalidRepositoryContentConsumer.class).values());
225 * A convienence method to execute all of the active selected consumers for a
226 * particular arbitrary file.
228 * @param repository the repository configuration to use.
229 * @param localFile the local file to execute the consumers against.
231 public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile )
233 // Run the repository consumers
236 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime() );
238 List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
239 List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
240 CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
241 CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
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 );
253 CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
255 if ( predicate.getWantedFileCount() <= 0 )
257 // Nothing known processed this file. It is invalid!
258 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
261 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure(repository);
263 CollectionUtils.forAllDo(selectedKnownConsumers, scanCompletedClosure);
264 CollectionUtils.forAllDo(selectedKnownConsumers, scanCompletedClosure);
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 );
275 public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
277 this.selectedKnownConsumers = selectedKnownConsumers;
280 public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
282 this.selectedInvalidConsumers = selectedInvalidConsumers;
285 protected Date getStartTime()
287 return new Date( System.currentTimeMillis() );
290 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
292 this.archivaConfiguration = archivaConfiguration;