This project has retired. For details please refer to its
Attic page.
ArchivaRepositoryScanningTaskExecutor xref
1 package org.apache.archiva.scheduler.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.archiva.scheduler.repository.model.RepositoryTask;
36 import org.apache.commons.lang.StringUtils;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
38 import org.apache.archiva.redback.components.taskqueue.Task;
39 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
40 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
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
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 private ManagedRepositoryAdmin managedRepositoryAdmin;
62
63 @Inject
64 private RepositoryScanner repoScanner;
65
66 @Inject
67 private RepositoryContentConsumers consumers;
68
69 private Task task;
70
71 @Inject
72 private RepositoryStatisticsManager repositoryStatisticsManager;
73
74
75
76
77 @Inject
78 private RepositorySessionFactory repositorySessionFactory;
79
80 @PostConstruct
81 public void initialize()
82 throws InitializationException
83 {
84 log.info( "Initialized {}", this.getClass().getName() );
85 }
86
87 @SuppressWarnings( "unchecked" )
88 @Override
89 public void executeTask( RepositoryTask task )
90 throws TaskExecutionException
91 {
92 try
93 {
94
95
96
97
98
99
100
101
102 this.task = task;
103
104 String repoId = task.getRepositoryId();
105 if ( StringUtils.isBlank( repoId ) )
106 {
107 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
108 }
109
110 ManagedRepository arepo = managedRepositoryAdmin.getManagedRepository( repoId );
111
112
113 if ( task.getResourceFile() != null )
114 {
115 log.debug( "Executing task from queue with job name: {}", task );
116 consumers.executeConsumers( arepo, task.getResourceFile(), task.isUpdateRelatedArtifacts() );
117 }
118 else
119 {
120 log.info( "Executing task from queue with job name: {}", task );
121
122
123 if ( arepo == null )
124 {
125 throw new TaskExecutionException(
126 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
127 }
128
129 long sinceWhen = RepositoryScanner.FRESH_SCAN;
130 long previousFileCount = 0;
131
132 RepositorySession repositorySession = repositorySessionFactory.createSession();
133 MetadataRepository metadataRepository = repositorySession.getRepository();
134 try
135 {
136 if ( !task.isScanAll() )
137 {
138 RepositoryStatistics previousStats =
139 repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
140 if ( previousStats != null )
141 {
142 sinceWhen = previousStats.getScanStartTime().getTime();
143 previousFileCount = previousStats.getTotalFileCount();
144 }
145 }
146
147 RepositoryScanStatistics stats;
148 try
149 {
150 stats = repoScanner.scan( arepo, sinceWhen );
151 }
152 catch ( RepositoryScannerException e )
153 {
154 throw new TaskExecutionException( "Repository error when executing repository job.", e );
155 }
156
157 log.info( "Finished first scan: {}", stats.toDump( arepo ) );
158
159
160 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
161
162 log.info( "Gathering repository statistics" );
163
164 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
165 stats.getWhenGathered(), endTime,
166 stats.getTotalFileCount(),
167 stats.getTotalFileCount() - previousFileCount );
168 repositorySession.save();
169 }
170 catch ( MetadataRepositoryException e )
171 {
172 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
173 }
174 finally
175 {
176 repositorySession.close();
177 }
178
179
180
181
182
183
184 log.info( "Finished repository task: {}", task );
185
186 this.task = null;
187 }
188 }
189 catch ( RepositoryAdminException e )
190 {
191 log.error( e.getMessage(), e );
192 throw new TaskExecutionException( e.getMessage(), e );
193 }
194 }
195
196 public Task getCurrentTaskInExecution()
197 {
198 return task;
199 }
200
201 public RepositoryScanner getRepoScanner()
202 {
203 return repoScanner;
204 }
205
206 public void setRepoScanner( RepositoryScanner repoScanner )
207 {
208 this.repoScanner = repoScanner;
209 }
210
211 public RepositoryContentConsumers getConsumers()
212 {
213 return consumers;
214 }
215
216 public void setConsumers( RepositoryContentConsumers consumers )
217 {
218 this.consumers = consumers;
219 }
220
221 public RepositorySessionFactory getRepositorySessionFactory()
222 {
223 return repositorySessionFactory;
224 }
225
226 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
227 {
228 this.repositorySessionFactory = repositorySessionFactory;
229 }
230
231 public RepositoryStatisticsManager getRepositoryStatisticsManager()
232 {
233 return repositoryStatisticsManager;
234 }
235
236 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
237 {
238 this.repositoryStatisticsManager = repositoryStatisticsManager;
239 }
240
241 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
242 {
243 return managedRepositoryAdmin;
244 }
245
246 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
247 {
248 this.managedRepositoryAdmin = managedRepositoryAdmin;
249 }
250 }