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.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.maven.archiva.common.utils.BaseFile;
34 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
35 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
36 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
37 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
38 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
39 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
40 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
41 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
42 import org.springframework.beans.BeansException;
43 import org.springframework.context.ApplicationContext;
44 import org.springframework.context.ApplicationContextAware;
47 * RepositoryContentConsumerUtil
51 public class RepositoryContentConsumers
52 implements ApplicationContextAware
54 private ApplicationContext applicationContext;
56 private ArchivaConfiguration archivaConfiguration;
58 private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
60 private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
62 public RepositoryContentConsumers( ArchivaConfiguration archivaConfiguration )
64 this.archivaConfiguration = archivaConfiguration;
67 public void setApplicationContext( ApplicationContext applicationContext )
70 this.applicationContext = applicationContext;
75 * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have been selected in the
76 * configuration to execute.
79 * NOTE: This list can be larger and contain entries that might not exist or be available in the classpath, or as a
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 been selected in the
94 * configuration to execute.
97 * NOTE: This list can be larger and contain entries that might not exist or be available in the classpath, or as a
101 * @return the list of consumer ids that have been selected by the configuration.
103 public List<String> getSelectedInvalidConsumerIds()
105 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
106 return scanning.getInvalidContentConsumers();
110 * Get the map of {@link String} ids to {@link KnownRepositoryContentConsumer} implementations, for those consumers
111 * that have been selected according to the active configuration.
113 * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
115 public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
117 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
119 for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
121 consumerMap.put( consumer.getId(), consumer );
128 * Get the map of {@link String} ids to {@link InvalidRepositoryContentConsumer} implementations, for those
129 * consumers that have been selected according to the active configuration.
131 * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
133 public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
135 Map<String, InvalidRepositoryContentConsumer> consumerMap =
136 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 selected according to the active
150 * @return the list of {@link KnownRepositoryContentConsumer} that have been selected by the active configuration.
152 public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
154 if ( selectedKnownConsumers == null )
156 List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
158 List<String> knownSelected = getSelectedKnownConsumerIds();
160 for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
162 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
167 this.selectedKnownConsumers = ret;
169 return selectedKnownConsumers;
173 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are selected according to the active
176 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected by the active configuration.
178 public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
180 if ( selectedInvalidConsumers == null )
182 List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
184 List<String> invalidSelected = getSelectedInvalidConsumerIds();
186 for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
188 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
193 selectedInvalidConsumers = ret;
195 return selectedInvalidConsumers;
199 * Get the list of {@link KnownRepositoryContentConsumer} objects that are available and present in the classpath
200 * and as components in the IoC.
202 * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath and as a
203 * component in the IoC.
205 @SuppressWarnings("unchecked")
206 public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
208 return new ArrayList<KnownRepositoryContentConsumer>( applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class ).values() );
212 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are available and present in the classpath
213 * and as components in the IoC.
215 * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath and as a
216 * component in the IoC.
218 @SuppressWarnings("unchecked")
219 public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
221 return new ArrayList<InvalidRepositoryContentConsumer>( applicationContext.getBeansOfType( InvalidRepositoryContentConsumer.class ).values() );
225 * A convienence method to execute all of the active selected consumers for a particular arbitrary file.
227 * @param repository the repository configuration to use.
228 * @param localFile the local file to execute the consumers against.
230 public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile )
232 // Run the repository consumers
235 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime() );
237 List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
238 List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
239 CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
240 CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
242 // yuck. In case you can't read this, it says
243 // "process the file if the consumer has it in the includes list, and not in the excludes list"
244 BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
245 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
246 predicate.setBasefile( baseFile );
247 ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
248 closure.setBasefile( baseFile );
249 predicate.setCaseSensitive( false );
250 Closure processIfWanted = IfClosure.getInstance( predicate, closure );
252 CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
254 if ( predicate.getWantedFileCount() <= 0 )
256 // Nothing known processed this file. It is invalid!
257 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
260 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository );
262 CollectionUtils.forAllDo( selectedKnownConsumers, scanCompletedClosure );
267 * TODO: This is never called by the repository scanner instance, so not calling here either - but it
268 * probably should be? CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
269 * CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
274 public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
276 this.selectedKnownConsumers = selectedKnownConsumers;
279 public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
281 this.selectedInvalidConsumers = selectedInvalidConsumers;
284 protected Date getStartTime()
286 return new Date( System.currentTimeMillis() );
289 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
291 this.archivaConfiguration = archivaConfiguration;