1 package org.apache.maven.archiva.database.updater;
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.CollectionUtils;
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.collections.functors.OrPredicate;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
27 import org.apache.maven.archiva.consumers.functors.PermanentConsumerPredicate;
28 import org.apache.maven.archiva.model.ArchivaArtifact;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.BeansException;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.context.ApplicationContextAware;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
44 public class DatabaseConsumers
45 implements ApplicationContextAware
47 private Logger log = LoggerFactory.getLogger( DatabaseConsumers.class );
49 private ArchivaConfiguration archivaConfiguration;
51 private Predicate selectedCleanupConsumers;
53 private Predicate selectedUnprocessedConsumers;
55 private ApplicationContext applicationContext;
57 public DatabaseConsumers( ArchivaConfiguration archivaConfiguration )
59 this.archivaConfiguration = archivaConfiguration;
61 Predicate permanentConsumers = new PermanentConsumerPredicate();
63 selectedCleanupConsumers = new OrPredicate( permanentConsumers, new SelectedCleanupConsumersPredicate() );
64 selectedUnprocessedConsumers = new OrPredicate( permanentConsumers, new SelectedUnprocessedConsumersPredicate() );
67 class SelectedUnprocessedConsumersPredicate
70 public boolean evaluate( Object object )
72 boolean satisfies = false;
74 if ( object instanceof DatabaseUnprocessedArtifactConsumer )
76 DatabaseUnprocessedArtifactConsumer consumer = (DatabaseUnprocessedArtifactConsumer) object;
77 DatabaseScanningConfiguration config = archivaConfiguration.getConfiguration().getDatabaseScanning();
79 return config.getUnprocessedConsumers().contains( consumer.getId() );
86 class SelectedCleanupConsumersPredicate
89 public boolean evaluate( Object object )
91 boolean satisfies = false;
93 if ( object instanceof DatabaseCleanupConsumer )
95 DatabaseCleanupConsumer consumer = (DatabaseCleanupConsumer) object;
96 DatabaseScanningConfiguration config = archivaConfiguration.getConfiguration().getDatabaseScanning();
98 return config.getCleanupConsumers().contains( consumer.getId() );
105 public void setApplicationContext( ApplicationContext applicationContext )
106 throws BeansException
108 this.applicationContext = applicationContext;
112 * Get the {@link List} of {@link DatabaseUnprocessedArtifactConsumer} objects
113 * for those consumers selected due to the configuration.
115 * @return the list of selected {@link DatabaseUnprocessedArtifactConsumer} objects.
117 public List getSelectedUnprocessedConsumers()
119 List ret = new ArrayList();
120 ret.addAll( CollectionUtils.select( getAvailableUnprocessedConsumers(), selectedUnprocessedConsumers ) );
125 * Get the {@link List} of {@link DatabaseCleanupConsumer} objects for those
126 * consumers selected due to the configuration.
128 * @return the list of selected {@link DatabaseCleanupConsumer} objects.
130 public List getSelectedCleanupConsumers()
132 List ret = new ArrayList();
133 ret.addAll( CollectionUtils.select( getAvailableCleanupConsumers(), selectedCleanupConsumers ) );
138 * Get the complete {@link List} of {@link DatabaseUnprocessedArtifactConsumer} objects
139 * that are available in the system, regardless of configuration.
141 * @return the list of all available {@link DatabaseUnprocessedArtifactConsumer} objects.
143 public List getAvailableUnprocessedConsumers()
145 return new ArrayList( applicationContext.getBeansOfType( DatabaseUnprocessedArtifactConsumer.class ).values() );
149 * Get the complete {@link List} of {@link DatabaseCleanupConsumer} objects
150 * that are available in the system, regardless of configuration.
152 * @return the list of all available {@link DatabaseCleanupConsumer} objects.
154 public List getAvailableCleanupConsumers()
156 return new ArrayList( applicationContext.getBeansOfType( DatabaseCleanupConsumer.class ).values() );
160 * Execute the cleanup consumers to cleanup the specified artifact from the database and index.
164 public void executeCleanupConsumer( ArchivaArtifact artifact )
166 List consumers = getSelectedCleanupConsumers();
167 Iterator it = consumers.iterator();
168 while ( it.hasNext() )
170 ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next();
171 consumer.beginScan();
174 if ( CollectionUtils.isEmpty( consumers ) )
176 log.warn( "There are no selected consumers for artifact cleanup." );
180 ProcessArchivaArtifactClosure processArtifactClosure = new ProcessArchivaArtifactClosure();
181 processArtifactClosure.setArtifact( artifact );
183 CollectionUtils.forAllDo( consumers, processArtifactClosure );
185 while ( it.hasNext() )
187 ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next();
188 consumer.completeScan();