]> source.dussan.org Git - archiva.git/blob
c4a8b8ec5c3e24316568c328f69db9a231baee35
[archiva.git] /
1 package org.apache.archiva.scheduler.repository;
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.archiva.metadata.repository.MetadataRepositoryException;
23 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
24 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
25 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
26 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
27 import org.apache.archiva.repository.scanner.RepositoryScanner;
28 import org.apache.archiva.repository.scanner.RepositoryScannerException;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
34 import org.codehaus.plexus.taskqueue.Task;
35 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
36 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import java.util.Date;
41
42 /**
43  * ArchivaRepositoryScanningTaskExecutor
44  *
45  * @version $Id$
46  * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
47  * role-hint="repository-scanning"
48  */
49 public class ArchivaRepositoryScanningTaskExecutor
50     implements TaskExecutor, Initializable
51 {
52     private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
53
54     /**
55      * @plexus.requirement
56      */
57     private ArchivaConfiguration archivaConfiguration;
58
59     /**
60      * The repository scanner component.
61      *
62      * @plexus.requirement
63      */
64     private RepositoryScanner repoScanner;
65
66     /**
67      * @plexus.requirement
68      */
69     private RepositoryContentConsumers consumers;
70
71     private Task task;
72
73     /**
74      * @plexus.requirement
75      */
76     private RepositoryStatisticsManager repositoryStatisticsManager;
77
78     public void initialize()
79         throws InitializationException
80     {
81         log.info( "Initialized " + this.getClass().getName() );
82     }
83
84     @SuppressWarnings( "unchecked" )
85     public void executeTask( Task task )
86         throws TaskExecutionException
87     {
88
89         // TODO: replace this whole class with the prescribed content scanning service/action
90         // - scan repository for artifacts that do not have corresponding metadata or have been updated and
91         // send events for each
92         // - scan metadata for artifacts that have been removed and send events for each
93         // - scan metadata for missing plugin data
94         // - store information so that it can restart upon failure (publish event on the server recovery
95         // queue, remove it on successful completion)
96
97         this.task = task;
98
99         RepositoryTask repoTask = (RepositoryTask) task;
100
101         String repoId = repoTask.getRepositoryId();
102         if ( StringUtils.isBlank( repoId ) )
103         {
104             throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
105         }
106
107         ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
108             repoId );
109
110         // execute consumers on resource file if set
111         if ( repoTask.getResourceFile() != null )
112         {
113             log.debug( "Executing task from queue with job name: " + repoTask );
114             consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
115         }
116         else
117         {
118             log.info( "Executing task from queue with job name: " + repoTask );
119
120             // otherwise, execute consumers on whole repository
121             if ( arepo == null )
122             {
123                 throw new TaskExecutionException(
124                     "Unable to execute RepositoryTask with invalid repository id: " + repoId );
125             }
126
127             long sinceWhen = RepositoryScanner.FRESH_SCAN;
128             long previousFileCount = 0;
129
130             if ( !repoTask.isScanAll() )
131             {
132                 RepositoryStatistics previousStats;
133                 try
134                 {
135                     previousStats = repositoryStatisticsManager.getLastStatistics( repoId );
136                 }
137                 catch ( MetadataRepositoryException e )
138                 {
139                     throw new TaskExecutionException( "Unable to get previous statistics: " + e.getMessage(), e );
140                 }
141                 if ( previousStats != null )
142                 {
143                     sinceWhen = previousStats.getScanStartTime().getTime();
144                     previousFileCount = previousStats.getTotalFileCount();
145                 }
146             }
147
148             RepositoryScanStatistics stats;
149             try
150             {
151                 stats = repoScanner.scan( arepo, sinceWhen );
152             }
153             catch ( RepositoryScannerException e )
154             {
155                 throw new TaskExecutionException( "Repository error when executing repository job.", e );
156             }
157
158             log.info( "Finished first scan: " + stats.toDump( arepo ) );
159
160             // further statistics will be populated by the following method
161             Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
162             try
163             {
164                 repositoryStatisticsManager.addStatisticsAfterScan( repoId, stats.getWhenGathered(), endTime,
165                                                                     stats.getTotalFileCount(),
166                                                                     stats.getTotalFileCount() - previousFileCount );
167             }
168             catch ( MetadataRepositoryException e )
169             {
170                 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
171             }
172
173 //                log.info( "Scanning for removed repository content" );
174
175 //                metadataRepository.findAllProjects();
176             // FIXME: do something
177
178             log.info( "Finished repository task: " + repoTask );
179
180             this.task = null;
181         }
182     }
183
184     public Task getCurrentTaskInExecution()
185     {
186         return task;
187     }
188 }