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;
72 public void setApplicationContext( ApplicationContext applicationContext )
75 this.applicationContext = applicationContext;
80 * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
81 * 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.
102 * NOTE: This list can be larger and contain entries that might not exist or be available
103 * in the classpath, or as a component.
106 * @return the list of consumer ids that have been selected by the configuration.
108 public List<String> getSelectedInvalidConsumerIds()
109 throws RepositoryAdminException
111 return archivaAdministration.getInvalidContentConsumers();
115 * Get the map of {@link String} ids to {@link KnownRepositoryContentConsumer} implementations,
116 * for those consumers that have been selected according to the active configuration.
118 * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
120 public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
121 throws RepositoryAdminException
123 Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<>();
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()
140 throws RepositoryAdminException
142 Map<String, InvalidRepositoryContentConsumer> consumerMap = new HashMap<>();
144 for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
146 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 synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
160 throws RepositoryAdminException
162 // FIXME only for testing
163 if ( selectedKnownConsumers != null )
165 return selectedKnownConsumers;
167 List<KnownRepositoryContentConsumer> ret = new ArrayList<>();
169 List<String> knownSelected = getSelectedKnownConsumerIds();
171 for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
173 if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
182 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
183 * selected according to the active configuration.
185 * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
186 * by the active configuration.
188 public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
189 throws RepositoryAdminException
192 // FIXME only for testing
193 if ( selectedInvalidConsumers != null )
195 return selectedInvalidConsumers;
198 List<InvalidRepositoryContentConsumer> ret = new ArrayList<>();
200 List<String> invalidSelected = getSelectedInvalidConsumerIds();
202 for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
204 if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
214 * Get the list of {@link KnownRepositoryContentConsumer} objects that are
215 * available and present in the classpath and as components in the IoC.
217 * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath
218 * and as a component in the IoC.
220 public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
222 return new ArrayList<>( applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class ).values() );
226 * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
227 * available and present in the classpath and as components in the IoC.
229 * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath
230 * and as a component in the IoC.
232 public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
234 return new ArrayList<>( applicationContext.getBeansOfType( InvalidRepositoryContentConsumer.class ).values() );
238 * A convienence method to execute all of the active selected consumers for a
239 * particular arbitrary file.
240 * NOTE: Make sure that there is no repository scanning task executing before invoking this so as to prevent
241 * the index writer/reader of the current index-content consumer executing from getting closed. For an example,
242 * see ArchivaDavResource#executeConsumers( File ).
244 * @param repository the repository configuration to use.
245 * @param localFile the local file to execute the consumers against.
246 * @param updateRelatedArtifacts TODO
248 public void executeConsumers( ManagedRepository repository, File localFile, boolean updateRelatedArtifacts )
249 throws RepositoryAdminException
251 // Run the repository consumers
254 Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime(), false );
256 List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
259 // - do not create missing/fix invalid checksums and update metadata when deploying from webdav since these are uploaded by maven
260 if ( !updateRelatedArtifacts )
262 List<KnownRepositoryContentConsumer> clone = new ArrayList<>();
263 clone.addAll( selectedKnownConsumers );
265 for ( KnownRepositoryContentConsumer consumer : clone )
267 if ( consumer.getId().equals( "create-missing-checksums" ) || consumer.getId().equals(
268 "metadata-updater" ) )
270 selectedKnownConsumers.remove( consumer );
275 List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
276 CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
277 CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
279 // yuck. In case you can't read this, it says
280 // "process the file if the consumer has it in the includes list, and not in the excludes list"
281 BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
282 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate( repository );
283 predicate.setBasefile( baseFile );
284 predicate.setCaseSensitive( false );
286 ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
287 closure.setBasefile( baseFile );
288 closure.setExecuteOnEntireRepo( false );
290 Closure processIfWanted = IfClosure.getInstance( predicate, closure );
292 CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
294 if ( predicate.getWantedFileCount() <= 0 )
296 // Nothing known processed this file. It is invalid!
297 CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
300 TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, false );
302 CollectionUtils.forAllDo( selectedKnownConsumers, scanCompletedClosure );
306 /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
307 CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
308 CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
313 public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
315 this.selectedKnownConsumers = selectedKnownConsumers;
318 public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
320 this.selectedInvalidConsumers = selectedInvalidConsumers;
323 protected Date getStartTime()
325 return new Date( System.currentTimeMillis() );
328 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
330 this.archivaAdministration = archivaAdministration;