1 package org.apache.maven.archiva.scheduled.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.commons.collections.CollectionUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.database.ArchivaDAO;
27 import org.apache.maven.archiva.database.constraints.MostRecentRepositoryScanStatistics;
28 import org.apache.maven.archiva.model.RepositoryContentStatistics;
29 import org.apache.maven.archiva.repository.RepositoryException;
30 import org.apache.maven.archiva.repository.scanner.RepositoryScanStatistics;
31 import org.apache.maven.archiva.repository.scanner.RepositoryScanner;
32 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
35 import org.codehaus.plexus.taskqueue.Task;
36 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
37 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import java.util.List;
44 * ArchivaRepositoryScanningTaskExecutor
46 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
50 * role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
51 * role-hint="repository-scanning"
53 public class ArchivaRepositoryScanningTaskExecutor
54 implements TaskExecutor, Initializable
56 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
59 * @plexus.requirement role-hint="jdo"
61 private ArchivaDAO dao;
66 private ArchivaConfiguration archivaConfiguration;
69 * The repository scanner component.
73 private RepositoryScanner repoScanner;
75 public void initialize()
76 throws InitializationException
78 log.info( "Initialized " + this.getClass().getName() );
81 public void executeTask( Task task )
82 throws TaskExecutionException
84 RepositoryTask repoTask = (RepositoryTask) task;
86 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
88 throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
91 log.info( "Executing task from queue with job name: " + repoTask.getName() );
95 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
98 throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
101 long sinceWhen = RepositoryScanner.FRESH_SCAN;
103 List<RepositoryContentStatistics> results = dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
105 if ( CollectionUtils.isNotEmpty( results ) )
107 RepositoryContentStatistics lastStats = results.get( 0 );
108 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
111 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
113 log.info( "Finished repository task: " + stats.toDump( arepo ) );
115 // I hate jpox and modello
116 RepositoryContentStatistics dbstats = new RepositoryContentStatistics();
117 dbstats.setDuration( stats.getDuration() );
118 dbstats.setNewFileCount( stats.getNewFileCount() );
119 dbstats.setRepositoryId( stats.getRepositoryId() );
120 dbstats.setTotalFileCount( stats.getTotalFileCount() );
121 dbstats.setWhenGathered( stats.getWhenGathered() );
123 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
125 catch ( RepositoryException e )
127 throw new TaskExecutionException( "Repository error when executing repository job.", e );