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