1 package org.apache.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.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;
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.HashMap;
43 import java.util.List;
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
76 * been selected in the configuration to execute.
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.
84 * @return the list of consumer ids that have been selected by the configuration.
86 public List<String> getSelectedKnownConsumerIds()
88 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
89 return scanning.getKnownContentConsumers();
94 * Get the list of Ids associated with those {@link InvalidRepositoryContentConsumer} that have
95 * been selected in the configuration to execute.
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.
103 * @return the list of consumer ids that have been selected by the configuration.
105 public List<String> getSelectedInvalidConsumerIds()
107 RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
108 return scanning.getInvalidContentConsumers();
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.
115 * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
117 public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
119 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
121 for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
123 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 =
138 new HashMap<String, InvalidRepositoryContentConsumer>();
140 for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
142 consumerMap.put( consumer.getId(), consumer );
149 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
150 * selected according to the active configuration.
152 * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
153 * by the active configuration.
155 public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
157 if ( selectedKnownConsumers == null )
159 List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
161 List<String> knownSelected = getSelectedKnownConsumerIds();
163 for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
165 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
170 this.selectedKnownConsumers = ret;
172 return selectedKnownConsumers;
176 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
177 * selected according to the active configuration.
179 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
180 * by the active configuration.
182 public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
184 if ( selectedInvalidConsumers == null )
186 List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
188 List<String> invalidSelected = getSelectedInvalidConsumerIds();
190 for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
192 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
197 selectedInvalidConsumers = ret;
199 return selectedInvalidConsumers;
203 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
204 * available and present in the classpath and as components in the IoC.
206 * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath
207 * and as a component in the IoC.
209 public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
211 return new ArrayList<KnownRepositoryContentConsumer>(
212 applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class ).values() );
216 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
217 * available and present in the classpath and as components in the IoC.
219 * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath
220 * and as a component in the IoC.
222 public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
224 return new ArrayList<InvalidRepositoryContentConsumer>(
225 applicationContext.getBeansOfType( InvalidRepositoryContentConsumer.class ).values() );
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 ).
235 * @param repository the repository configuration to use.
236 * @param localFile the local file to execute the consumers against.
237 * @param updateRelatedArtifacts TODO
239 public void executeConsumers( ManagedRepositoryConfiguration repository, File localFile,
240 boolean updateRelatedArtifacts )
242 // Run the repository consumers
245 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime(), false );
247 List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
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 )
253 List<KnownRepositoryContentConsumer> clone = new ArrayList<KnownRepositoryContentConsumer>();
254 clone.addAll( selectedKnownConsumers );
256 for ( KnownRepositoryContentConsumer consumer : clone )
258 if ( consumer.getId().equals( "create-missing-checksums" ) || consumer.getId().equals(
259 "metadata-updater" ) )
261 selectedKnownConsumers.remove( consumer );
266 List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
267 CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
268 CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
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 );
277 ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
278 closure.setBasefile( baseFile );
279 closure.setExecuteOnEntireRepo( false );
281 Closure processIfWanted = IfClosure.getInstance( predicate, closure );
283 CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
285 if ( predicate.getWantedFileCount() <= 0 )
287 // Nothing known processed this file. It is invalid!
288 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
291 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, false );
293 CollectionUtils.forAllDo( selectedKnownConsumers, scanCompletedClosure );
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 );
304 public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
306 this.selectedKnownConsumers = selectedKnownConsumers;
309 public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
311 this.selectedInvalidConsumers = selectedInvalidConsumers;
314 protected Date getStartTime()
316 return new Date( System.currentTimeMillis() );
319 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
321 this.archivaConfiguration = archivaConfiguration;