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.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * RepositoryContentConsumerUtil
47 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers"
52 public class RepositoryContentConsumers
54 private Logger log = LoggerFactory.getLogger( RepositoryContentConsumers.class );
59 private ArchivaConfiguration archivaConfiguration;
62 * @plexus.requirement role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
64 private List<KnownRepositoryContentConsumer> availableKnownConsumers;
67 * @plexus.requirement role="org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer"
69 private List<InvalidRepositoryContentConsumer> availableInvalidConsumers;
71 private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
73 private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
75 private Date startTime;
79 * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
80 * been selected in the configuration to execute.
84 * NOTE: This list can be larger and contain entries that might not exist or be available
85 * in the classpath, or as a component.
88 * @return the list of consumer ids that have been selected by the configuration.
90 public List<String> getSelectedKnownConsumerIds()
92 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
93 return scanning.getKnownContentConsumers();
98 * Get the list of Ids associated with those {@link InvalidRepositoryContentConsumer} that have
99 * been selected in the configuration to execute.
103 * NOTE: This list can be larger and contain entries that might not exist or be available
104 * in the classpath, or as a component.
107 * @return the list of consumer ids that have been selected by the configuration.
109 public List<String> getSelectedInvalidConsumerIds()
111 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
112 return scanning.getInvalidContentConsumers();
116 * Get the map of {@link String} ids to {@link KnownRepositoryContentConsumer} implementations,
117 * for those consumers that have been selected according to the active configuration.
119 * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
121 public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
123 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
125 for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
127 consumerMap.put( consumer.getId(), consumer );
134 * Get the map of {@link String} ids to {@link InvalidRepositoryContentConsumer} implementations,
135 * for those consumers that have been selected according to the active configuration.
137 * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
139 public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
141 Map<String, InvalidRepositoryContentConsumer> consumerMap = new HashMap<String, InvalidRepositoryContentConsumer>();
143 for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
145 consumerMap.put( consumer.getId(), consumer );
152 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
153 * selected according to the active configuration.
155 * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
156 * by the active configuration.
158 public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
160 if ( selectedKnownConsumers == null )
162 List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
164 List<String> knownSelected = getSelectedKnownConsumerIds();
166 for ( KnownRepositoryContentConsumer consumer : availableKnownConsumers )
168 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
173 this.selectedKnownConsumers = ret;
175 return selectedKnownConsumers;
179 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
180 * selected according to the active configuration.
182 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
183 * by the active configuration.
185 public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
187 if ( selectedInvalidConsumers == null )
189 List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
191 List<String> invalidSelected = getSelectedInvalidConsumerIds();
193 for ( InvalidRepositoryContentConsumer consumer : availableInvalidConsumers )
195 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
200 selectedInvalidConsumers = ret;
202 return selectedInvalidConsumers;
206 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
207 * available and present in the classpath and as components in the IoC.
209 * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath
210 * and as a component in the IoC.
212 public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
214 return availableKnownConsumers;
218 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
219 * available and present in the classpath and as components in the IoC.
221 * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath
222 * and as a component in the IoC.
224 public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
226 return availableInvalidConsumers;
230 * Set the list of {@link KnownRepositoryContentConsumer} objects that are
233 * NOTE: This is an override for the base functionality as a component, this
234 * is used by archiva-cli and the unit testing framework.
236 * @return the list of available {@link KnownRepositoryContentConsumer}.
238 public void setAvailableKnownConsumers( List<KnownRepositoryContentConsumer> availableKnownConsumers )
240 this.availableKnownConsumers = availableKnownConsumers;
244 * Set the list of {@link InvalidRepositoryContentConsumer} objects that are
247 * NOTE: This is an override for the base functionality as a component, this
248 * is used by archiva-cli and the unit testing framework.
250 * @return the list of available {@link InvalidRepositoryContentConsumer}.
252 public void setAvailableInvalidConsumers( List<InvalidRepositoryContentConsumer> availableInvalidConsumers )
254 this.availableInvalidConsumers = availableInvalidConsumers;
258 * A convienence method to execute all of the active selected consumers for a
259 * particular arbitrary file.
261 * @param repository the repository configuration to use.
262 * @param localFile the local file to execute the consumers against.
264 public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile )
266 // Run the repository consumers
269 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime() );
271 List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
272 List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
273 CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
274 CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
276 // yuck. In case you can't read this, it says
277 // "process the file if the consumer has it in the includes list, and not in the excludes list"
278 BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
279 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
280 predicate.setBasefile( baseFile );
281 ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
282 closure.setBasefile( baseFile );
283 predicate.setCaseSensitive( false );
284 Closure processIfWanted = IfClosure.getInstance( predicate, closure );
286 CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
288 if ( predicate.getWantedFileCount() <= 0 )
290 // Nothing known processed this file. It is invalid!
291 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
296 /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
297 CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
298 CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
303 public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
305 this.selectedKnownConsumers = selectedKnownConsumers;
308 public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
310 this.selectedInvalidConsumers = selectedInvalidConsumers;
313 public void setStartTime( Date startTime )
315 this.startTime = startTime;
318 public Date getStartTime()
320 startTime = new Date( System.currentTimeMillis() );
325 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
327 this.archivaConfiguration = archivaConfiguration;