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