1 package org.apache.maven.archiva.scheduler.executors;
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.maven.archiva.common.consumers.Consumer;
23 import org.apache.maven.archiva.common.consumers.ConsumerException;
24 import org.apache.maven.archiva.common.consumers.ConsumerFactory;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory;
28 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
29 import org.apache.maven.archiva.discoverer.Discoverer;
30 import org.apache.maven.archiva.discoverer.DiscovererException;
31 import org.apache.maven.archiva.discoverer.DiscovererStatistics;
32 import org.apache.maven.archiva.scheduler.task.DataRefreshTask;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.codehaus.plexus.logging.AbstractLogEnabled;
35 import org.codehaus.plexus.taskqueue.Task;
36 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
37 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Iterator;
42 import java.util.List;
47 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
50 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
51 * role-hint="data-refresh"
53 public class DataRefreshExecutor
54 extends AbstractLogEnabled
55 implements TaskExecutor
57 public static final String DATAREFRESH_FILE = ".datarefresh";
60 * Configuration store.
64 private ArchivaConfiguration archivaConfiguration;
69 private ConfiguredRepositoryFactory repoFactory;
74 private DataRefreshConsumers consumerNames;
79 private Discoverer discoverer;
84 private ConsumerFactory consumerFactory;
86 public void executeTask( Task task )
87 throws TaskExecutionException
89 DataRefreshTask indexerTask = (DataRefreshTask) task;
91 getLogger().info( "Executing task from queue with job name: " + indexerTask.getJobName() );
97 throws TaskExecutionException
99 Configuration configuration = archivaConfiguration.getConfiguration();
101 List consumers = new ArrayList();
103 for ( Iterator it = consumerNames.iterator(); it.hasNext(); )
105 String name = (String) it.next();
108 Consumer consumer = consumerFactory.createConsumer( name );
109 consumers.add( consumer );
111 catch ( ConsumerException e )
113 getLogger().warn( e.getMessage(), e );
114 throw new TaskExecutionException( e.getMessage(), e );
118 long time = System.currentTimeMillis();
120 for ( Iterator i = configuration.getRepositories().iterator(); i.hasNext(); )
122 RepositoryConfiguration repositoryConfiguration = (RepositoryConfiguration) i.next();
124 if ( !repositoryConfiguration.isIndexed() )
129 ArtifactRepository repository = repoFactory.createRepository( repositoryConfiguration );
131 List filteredConsumers = filterConsumers( consumers, repository );
133 DiscovererStatistics lastRunStats = new DiscovererStatistics( repository );
136 lastRunStats.load( DATAREFRESH_FILE );
138 catch ( IOException e )
141 "Unable to load last run statistics for repository [" + repository.getId() + "]: "
147 DiscovererStatistics stats = discoverer
148 .walkRepository( repository, filteredConsumers, repositoryConfiguration.isIncludeSnapshots(),
149 lastRunStats.getTimestampFinished(), null, null );
151 stats.dump( getLogger() );
152 stats.save( DATAREFRESH_FILE );
154 catch ( DiscovererException e )
157 "Unable to run data refresh against repository [" + repository.getId() + "]: "
158 + e.getMessage(), e );
160 catch ( IOException e )
163 "Unable to save last run statistics for repository [" + repository.getId() + "]: "
168 time = System.currentTimeMillis() - time;
170 getLogger().info( "Finished data refresh process in " + time + "ms." );
174 * Not all consumers work with all repositories.
175 * This will filter out those incompatible consumers based on the provided repository.
177 * @param consumers the initial list of consumers.
178 * @param repository the repository to test consumer against.
179 * @return the filtered list of consumers.
181 private List filterConsumers( List consumers, ArtifactRepository repository )
183 List filtered = new ArrayList();
185 for ( Iterator it = consumers.iterator(); it.hasNext(); )
187 Consumer consumer = (Consumer) it.next();
188 if ( consumer.init( repository ) )
191 filtered.add( consumer );
195 getLogger().info( "Disabling consumer [" + consumer.getName() + "] for repository " + repository );