]> source.dussan.org Git - archiva.git/blob
527ee34fc7aca27c540aa4037fb56f344df0983a
[archiva.git] /
1 package org.apache.maven.archiva.scheduled.executors;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
40
41 import java.util.List;
42
43 /**
44  * ArchivaRepositoryScanningTaskExecutor 
45  *
46  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47  * @version $Id$
48  * 
49  * @plexus.component
50  *   role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
51  *   role-hint="repository-scanning"
52  */
53 public class ArchivaRepositoryScanningTaskExecutor
54     implements TaskExecutor, Initializable
55 {
56     private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
57     
58     /**
59      * @plexus.requirement role-hint="jdo"
60      */
61     private ArchivaDAO dao;
62     
63     /**
64      * @plexus.requirement
65      */
66     private ArchivaConfiguration archivaConfiguration;
67
68     /**
69      * The repository scanner component.
70      * 
71      * @plexus.requirement
72      */
73     private RepositoryScanner repoScanner;
74
75     public void initialize()
76         throws InitializationException
77     {
78         log.info( "Initialized " + this.getClass().getName() );
79     }
80
81     public void executeTask( Task task )
82         throws TaskExecutionException
83     {
84         RepositoryTask repoTask = (RepositoryTask) task;
85         
86         if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
87         {
88             throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
89         }
90
91         log.info( "Executing task from queue with job name: " + repoTask.getName() );
92         
93         try
94         {
95             ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
96             if ( arepo == null )
97             {
98                 throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
99             }
100
101             long sinceWhen = RepositoryScanner.FRESH_SCAN;
102
103             List<RepositoryContentStatistics> results = dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
104
105             if ( CollectionUtils.isNotEmpty( results ) )
106             {
107                 RepositoryContentStatistics lastStats = results.get( 0 );
108                 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
109             }
110
111             RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
112
113             log.info( "Finished repository task: " + stats.toDump( arepo ) );
114             
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() );
122             
123             dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
124         }
125         catch ( RepositoryException e )
126         {
127             throw new TaskExecutionException( "Repository error when executing repository job.", e );
128         }        
129     }
130 }