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