]> source.dussan.org Git - archiva.git/blob
481615a0003fe5b3d94b61c97f1ec47f15db396a
[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.MetadataRepository;
23 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
27 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
28 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
29 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
30 import org.apache.archiva.repository.scanner.RepositoryScanner;
31 import org.apache.archiva.repository.scanner.RepositoryScannerException;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
37 import org.codehaus.plexus.taskqueue.Task;
38 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
39 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.scheduling.TaskScheduler;
43 import org.springframework.stereotype.Service;
44
45 import javax.annotation.PostConstruct;
46 import javax.inject.Inject;
47 import javax.inject.Named;
48 import java.util.Date;
49
50 /**
51  * ArchivaRepositoryScanningTaskExecutor
52  *
53  * @version $Id$
54  * plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
55  * role-hint="repository-scanning"
56  */
57 @Service("taskExecutor#repository-scanning")
58 public class ArchivaRepositoryScanningTaskExecutor
59     implements TaskExecutor, Initializable
60 {
61     private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
62
63     /**
64      * plexus.requirement
65      */
66     @Inject
67     @Named(value="archivaConfiguration#default")
68     private ArchivaConfiguration archivaConfiguration;
69
70     /**
71      * The repository scanner component.
72      *
73      * plexus.requirement
74      */
75     @Inject
76     private RepositoryScanner repoScanner;
77
78     /**
79      * plexus.requirement
80      */
81     @Inject
82     private RepositoryContentConsumers consumers;
83
84     private Task task;
85
86     /**
87      * plexus.requirement
88      */
89     @Inject
90     private RepositoryStatisticsManager repositoryStatisticsManager;
91
92     /**
93      * TODO: may be different implementations
94      *
95      * plexus.requirement
96      */
97     @Inject
98     private RepositorySessionFactory repositorySessionFactory;
99
100     @PostConstruct
101     public void initialize()
102         throws InitializationException
103     {
104         log.info( "Initialized {}", this.getClass().getName() );
105     }
106
107     @SuppressWarnings( "unchecked" )
108     public void executeTask( Task task )
109         throws TaskExecutionException
110     {
111
112         // TODO: replace this whole class with the prescribed content scanning service/action
113         // - scan repository for artifacts that do not have corresponding metadata or have been updated and
114         // send events for each
115         // - scan metadata for artifacts that have been removed and send events for each
116         // - scan metadata for missing plugin data
117         // - store information so that it can restart upon failure (publish event on the server recovery
118         // queue, remove it on successful completion)
119
120         this.task = task;
121
122         RepositoryTask repoTask = (RepositoryTask) task;
123
124         String repoId = repoTask.getRepositoryId();
125         if ( StringUtils.isBlank( repoId ) )
126         {
127             throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
128         }
129
130         ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
131             repoId );
132
133         // execute consumers on resource file if set
134         if ( repoTask.getResourceFile() != null )
135         {
136             log.debug( "Executing task from queue with job name: {}", repoTask );
137             consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
138         }
139         else
140         {
141             log.info( "Executing task from queue with job name: {}", repoTask );
142
143             // otherwise, execute consumers on whole repository
144             if ( arepo == null )
145             {
146                 throw new TaskExecutionException(
147                     "Unable to execute RepositoryTask with invalid repository id: " + repoId );
148             }
149
150             long sinceWhen = RepositoryScanner.FRESH_SCAN;
151             long previousFileCount = 0;
152
153             RepositorySession repositorySession = repositorySessionFactory.createSession();
154             MetadataRepository metadataRepository = repositorySession.getRepository();
155             try
156             {
157                 if ( !repoTask.isScanAll() )
158                 {
159                     RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics(
160                         metadataRepository, repoId );
161                     if ( previousStats != null )
162                     {
163                         sinceWhen = previousStats.getScanStartTime().getTime();
164                         previousFileCount = previousStats.getTotalFileCount();
165                     }
166                 }
167
168                 RepositoryScanStatistics stats;
169                 try
170                 {
171                     stats = repoScanner.scan( arepo, sinceWhen );
172                 }
173                 catch ( RepositoryScannerException e )
174                 {
175                     throw new TaskExecutionException( "Repository error when executing repository job.", e );
176                 }
177
178                 log.info( "Finished first scan: " + stats.toDump( arepo ) );
179
180                 // further statistics will be populated by the following method
181                 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
182
183                 log.info( "Gathering repository statistics" );
184
185                 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId, stats.getWhenGathered(),
186                                                                     endTime, stats.getTotalFileCount(),
187                                                                     stats.getTotalFileCount() - previousFileCount );
188                 repositorySession.save();
189             }
190             catch ( MetadataRepositoryException e )
191             {
192                 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
193             }
194             finally
195             {
196                 repositorySession.close();
197             }
198
199 //                log.info( "Scanning for removed repository content" );
200
201 //                metadataRepository.findAllProjects();
202             // FIXME: do something
203
204             log.info( "Finished repository task: {}", repoTask );
205
206             this.task = null;
207         }
208     }
209
210     public Task getCurrentTaskInExecution()
211     {
212         return task;
213     }
214
215     public ArchivaConfiguration getArchivaConfiguration()
216     {
217         return archivaConfiguration;
218     }
219
220     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
221     {
222         this.archivaConfiguration = archivaConfiguration;
223     }
224
225     public RepositoryScanner getRepoScanner()
226     {
227         return repoScanner;
228     }
229
230     public void setRepoScanner( RepositoryScanner repoScanner )
231     {
232         this.repoScanner = repoScanner;
233     }
234
235     public RepositoryContentConsumers getConsumers()
236     {
237         return consumers;
238     }
239
240     public void setConsumers( RepositoryContentConsumers consumers )
241     {
242         this.consumers = consumers;
243     }
244
245     public RepositorySessionFactory getRepositorySessionFactory()
246     {
247         return repositorySessionFactory;
248     }
249
250     public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
251     {
252         this.repositorySessionFactory = repositorySessionFactory;
253     }
254
255     public RepositoryStatisticsManager getRepositoryStatisticsManager()
256     {
257         return repositoryStatisticsManager;
258     }
259
260     public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
261     {
262         this.repositoryStatisticsManager = repositoryStatisticsManager;
263     }
264 }