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.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.common.utils.BaseFile;
26 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
29 import org.apache.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
30 import org.apache.archiva.repository.scanner.functors.TriggerBeginScanClosure;
31 import org.apache.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
32 import org.apache.commons.collections.Closure;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.collections.functors.IfClosure;
35 import org.springframework.beans.BeansException;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.context.ApplicationContextAware;
38 import org.springframework.stereotype.Service;
40 import javax.inject.Inject;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.HashMap;
45 import java.util.List;
49 * RepositoryContentConsumerUtil
51 @Service ("repositoryContentConsumers")
52 public class RepositoryContentConsumers
53 implements ApplicationContextAware
57 private ApplicationContext applicationContext;
59 private ArchivaAdministration archivaAdministration;
61 private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
63 private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
66 public RepositoryContentConsumers( ArchivaAdministration archivaAdministration )
68 this.archivaAdministration = archivaAdministration;
71 public void setApplicationContext( ApplicationContext applicationContext )
74 this.applicationContext = applicationContext;
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()
91 throws RepositoryAdminException
93 return archivaAdministration.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()
110 throws RepositoryAdminException
112 return archivaAdministration.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()
122 throws RepositoryAdminException
124 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
126 for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
128 consumerMap.put( consumer.getId(), consumer );
135 * Get the map of {@link String} ids to {@link InvalidRepositoryContentConsumer} implementations,
136 * for those consumers that have been selected according to the active configuration.
138 * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
140 public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
141 throws RepositoryAdminException
143 Map<String, InvalidRepositoryContentConsumer> consumerMap =
144 new HashMap<String, InvalidRepositoryContentConsumer>();
146 for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
148 consumerMap.put( consumer.getId(), consumer );
155 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
156 * selected according to the active configuration.
158 * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
159 * by the active configuration.
161 public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
162 throws RepositoryAdminException
164 // FIXME only for testing
165 if ( selectedKnownConsumers != null )
167 return selectedKnownConsumers;
169 List<KnownRepositoryContentConsumer> ret = new ArrayList<KnownRepositoryContentConsumer>();
171 List<String> knownSelected = getSelectedKnownConsumerIds();
173 for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
175 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
184 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
185 * selected according to the active configuration.
187 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
188 * by the active configuration.
190 public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
191 throws RepositoryAdminException
194 // FIXME only for testing
195 if ( selectedInvalidConsumers != null )
197 return selectedInvalidConsumers;
200 List<InvalidRepositoryContentConsumer> ret = new ArrayList<InvalidRepositoryContentConsumer>();
202 List<String> invalidSelected = getSelectedInvalidConsumerIds();
204 for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
206 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
216 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
217 * available and present in the classpath and as components in the IoC.
219 * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath
220 * and as a component in the IoC.
222 public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
224 return new ArrayList<KnownRepositoryContentConsumer>(
225 applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class ).values() );
229 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
230 * available and present in the classpath and as components in the IoC.
232 * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath
233 * and as a component in the IoC.
235 public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
237 return new ArrayList<InvalidRepositoryContentConsumer>(
238 applicationContext.getBeansOfType( InvalidRepositoryContentConsumer.class ).values() );
242 * A convienence method to execute all of the active selected consumers for a
243 * particular arbitrary file.
244 * NOTE: Make sure that there is no repository scanning task executing before invoking this so as to prevent
245 * the index writer/reader of the current index-content consumer executing from getting closed. For an example,
246 * see ArchivaDavResource#executeConsumers( File ).
248 * @param repository the repository configuration to use.
249 * @param localFile the local file to execute the consumers against.
250 * @param updateRelatedArtifacts TODO
252 public void executeConsumers( ManagedRepository repository, File localFile, boolean updateRelatedArtifacts )
253 throws RepositoryAdminException
255 // Run the repository consumers
258 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime(), false );
260 List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
263 // - do not create missing/fix invalid checksums and update metadata when deploying from webdav since these are uploaded by maven
264 if ( updateRelatedArtifacts == false )
266 List<KnownRepositoryContentConsumer> clone = new ArrayList<KnownRepositoryContentConsumer>();
267 clone.addAll( selectedKnownConsumers );
269 for ( KnownRepositoryContentConsumer consumer : clone )
271 if ( consumer.getId().equals( "create-missing-checksums" ) || consumer.getId().equals(
272 "metadata-updater" ) )
274 selectedKnownConsumers.remove( consumer );
279 List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
280 CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
281 CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
283 // yuck. In case you can't read this, it says
284 // "process the file if the consumer has it in the includes list, and not in the excludes list"
285 BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
286 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
287 predicate.setBasefile( baseFile );
288 predicate.setCaseSensitive( false );
290 ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
291 closure.setBasefile( baseFile );
292 closure.setExecuteOnEntireRepo( false );
294 Closure processIfWanted = IfClosure.getInstance( predicate, closure );
296 CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
298 if ( predicate.getWantedFileCount() <= 0 )
300 // Nothing known processed this file. It is invalid!
301 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
304 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, false );
306 CollectionUtils.forAllDo( selectedKnownConsumers, scanCompletedClosure );
310 /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
311 CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
312 CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
317 public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
319 this.selectedKnownConsumers = selectedKnownConsumers;
322 public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
324 this.selectedInvalidConsumers = selectedInvalidConsumers;
327 protected Date getStartTime()
329 return new Date( System.currentTimeMillis() );
332 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
334 this.archivaAdministration = archivaAdministration;