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