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
22 import org.apache.commons.collections.Closure;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections.functors.IfClosure;
25 import org.apache.maven.archiva.common.utils.BaseFile;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
29 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
30 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.maven.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
32 import org.apache.maven.archiva.repository.scanner.functors.ConsumerWantsFilePredicate;
33 import org.apache.maven.archiva.repository.scanner.functors.TriggerBeginScanClosure;
34 import org.codehaus.plexus.logging.AbstractLogEnabled;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
43 * RepositoryContentConsumerUtil
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48 * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers"
50 public class RepositoryContentConsumers
51 extends AbstractLogEnabled
56 private ArchivaConfiguration archivaConfiguration;
59 * @plexus.requirement role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
61 private List<KnownRepositoryContentConsumer> availableKnownConsumers;
64 * @plexus.requirement role="org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer"
66 private List<InvalidRepositoryContentConsumer> availableInvalidConsumers;
70 * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
71 * been selected in the configuration to execute.
75 * NOTE: This list can be larger and contain entries that might not exist or be available
76 * in the classpath, or as a component.
79 * @return the list of consumer ids that have been selected by the configuration.
81 public List<String> getSelectedKnownConsumerIds()
83 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
84 return scanning.getKnownContentConsumers();
89 * Get the list of Ids associated with those {@link InvalidRepositoryContentConsumer} that have
90 * been selected in the configuration to execute.
94 * NOTE: This list can be larger and contain entries that might not exist or be available
95 * in the classpath, or as a component.
98 * @return the list of consumer ids that have been selected by the configuration.
100 public List<String> getSelectedInvalidConsumerIds()
102 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
103 return scanning.getInvalidContentConsumers();
107 * Get the map of {@link String} ids to {@link KnownRepositoryContentConsumer} implementations,
108 * for those consumers that have been selected according to the active configuration.
110 * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
112 public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
114 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
116 List<String> knownSelected = getSelectedKnownConsumerIds();
118 for ( KnownRepositoryContentConsumer consumer : availableKnownConsumers )
120 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
122 consumerMap.put( consumer.getId(), consumer );
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.
133 * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
135 public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
137 Map<String, InvalidRepositoryContentConsumer> consumerMap = new HashMap<String, InvalidRepositoryContentConsumer>();
139 List<String> invalidSelected = getSelectedInvalidConsumerIds();
141 for ( InvalidRepositoryContentConsumer consumer : availableInvalidConsumers )
143 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
145 consumerMap.put( consumer.getId(), consumer );
153 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
154 * selected according to the active configuration.
156 * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
157 * by the active configuration.
159 public List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
161 List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
163 List<String> knownSelected = getSelectedKnownConsumerIds();
165 for ( KnownRepositoryContentConsumer consumer : availableKnownConsumers )
167 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
177 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
178 * selected according to the active configuration.
180 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
181 * by the active configuration.
183 public List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
185 List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
187 List<String> invalidSelected = getSelectedInvalidConsumerIds();
189 for ( InvalidRepositoryContentConsumer consumer : availableInvalidConsumers )
191 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
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 availableKnownConsumers;
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 availableInvalidConsumers;
225 * Set the list of {@link KnownRepositoryContentConsumer} objects that are
228 * NOTE: This is an override for the base functionality as a component, this
229 * is used by archiva-cli and the unit testing framework.
231 * @return the list of available {@link KnownRepositoryContentConsumer}.
233 public void setAvailableKnownConsumers( List<KnownRepositoryContentConsumer> availableKnownConsumers )
235 this.availableKnownConsumers = availableKnownConsumers;
239 * Set the list of {@link InvalidRepositoryContentConsumer} objects that are
242 * NOTE: This is an override for the base functionality as a component, this
243 * is used by archiva-cli and the unit testing framework.
245 * @return the list of available {@link InvalidRepositoryContentConsumer}.
247 public void setAvailableInvalidConsumers( List<InvalidRepositoryContentConsumer> availableInvalidConsumers )
249 this.availableInvalidConsumers = availableInvalidConsumers;
253 * A convienence method to execute all of the active selected consumers for a
254 * particular arbitrary file.
256 * @param repository the repository configuration to use.
257 * @param localFile the local file to execute the consumers against.
259 public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile )
261 // Run the repository consumers
264 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getLogger() );
266 CollectionUtils.forAllDo( availableKnownConsumers, triggerBeginScan );
267 CollectionUtils.forAllDo( availableInvalidConsumers, triggerBeginScan );
269 // yuck. In case you can't read this, it says
270 // "process the file if the consumer has it in the includes list, and not in the excludes list"
271 BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
272 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
273 predicate.setBasefile( baseFile );
274 ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure( getLogger() );
275 closure.setBasefile( baseFile );
276 predicate.setCaseSensitive( false );
277 Closure processIfWanted = IfClosure.getInstance( predicate, closure );
279 CollectionUtils.forAllDo( availableKnownConsumers, processIfWanted );
281 if ( predicate.getWantedFileCount() <= 0 )
283 // Nothing known processed this file. It is invalid!
284 CollectionUtils.forAllDo( availableInvalidConsumers, closure );
289 /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
290 CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
291 CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );