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.logging.AbstractLogEnabled;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
36 import org.codehaus.plexus.taskqueue.Task;
37 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
38 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
40 import java.util.List;
43 * ArchivaRepositoryScanningTaskExecutor
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49 * role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
50 * role-hint="repository-scanning"
52 public class ArchivaRepositoryScanningTaskExecutor
53 extends AbstractLogEnabled
54 implements TaskExecutor, Initializable
57 * @plexus.requirement role-hint="jdo"
59 private ArchivaDAO dao;
64 private ArchivaConfiguration archivaConfiguration;
67 * The repository scanner component.
71 private RepositoryScanner repoScanner;
73 public void initialize()
74 throws InitializationException
76 getLogger().info( "Initialized " + this.getClass().getName() );
79 public void executeTask( Task task )
80 throws TaskExecutionException
82 RepositoryTask repoTask = (RepositoryTask) task;
84 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
86 throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
89 getLogger().info( "Executing task from queue with job name: " + repoTask.getName() );
93 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
95 long sinceWhen = RepositoryScanner.FRESH_SCAN;
97 List<RepositoryContentStatistics> results = dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
99 if ( CollectionUtils.isNotEmpty( results ) )
101 RepositoryContentStatistics lastStats = results.get( 0 );
102 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
105 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
107 getLogger().info( "Finished repository task: " + stats.toDump( arepo ) );
109 // I hate jpox and modello
110 RepositoryContentStatistics dbstats = new RepositoryContentStatistics();
111 dbstats.setDuration( stats.getDuration() );
112 dbstats.setNewFileCount( stats.getNewFileCount() );
113 dbstats.setRepositoryId( stats.getRepositoryId() );
114 dbstats.setTotalFileCount( stats.getTotalFileCount() );
115 dbstats.setWhenGathered( stats.getWhenGathered() );
119 catch ( RepositoryException e )
121 throw new TaskExecutionException( "Repository error when executing repository job.", e );