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