]> source.dussan.org Git - archiva.git/blob
3c729c552a9f25954c2f6e5658f0b86fd7c6955a
[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
43 import java.util.Date;
44
45 /**
46  * ArchivaRepositoryScanningTaskExecutor
47  *
48  * @version $Id$
49  * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
50  * role-hint="repository-scanning"
51  */
52 public class ArchivaRepositoryScanningTaskExecutor
53     implements TaskExecutor, Initializable
54 {
55     private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
56
57     /**
58      * @plexus.requirement
59      */
60     private ArchivaConfiguration archivaConfiguration;
61
62     /**
63      * The repository scanner component.
64      *
65      * @plexus.requirement
66      */
67     private RepositoryScanner repoScanner;
68
69     /**
70      * @plexus.requirement
71      */
72     private RepositoryContentConsumers consumers;
73
74     private Task task;
75
76     /**
77      * @plexus.requirement
78      */
79     private RepositoryStatisticsManager repositoryStatisticsManager;
80
81     /**
82      * TODO: may be different implementations
83      *
84      * @plexus.requirement
85      */
86     private RepositorySessionFactory repositorySessionFactory;
87
88     public void initialize()
89         throws InitializationException
90     {
91         log.info( "Initialized " + this.getClass().getName() );
92     }
93
94     @SuppressWarnings( "unchecked" )
95     public void executeTask( Task task )
96         throws TaskExecutionException
97     {
98
99         // TODO: replace this whole class with the prescribed content scanning service/action
100         // - scan repository for artifacts that do not have corresponding metadata or have been updated and
101         // send events for each
102         // - scan metadata for artifacts that have been removed and send events for each
103         // - scan metadata for missing plugin data
104         // - store information so that it can restart upon failure (publish event on the server recovery
105         // queue, remove it on successful completion)
106
107         this.task = task;
108
109         RepositoryTask repoTask = (RepositoryTask) task;
110
111         String repoId = repoTask.getRepositoryId();
112         if ( StringUtils.isBlank( repoId ) )
113         {
114             throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
115         }
116
117         ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
118             repoId );
119
120         // execute consumers on resource file if set
121         if ( repoTask.getResourceFile() != null )
122         {
123             log.debug( "Executing task from queue with job name: " + repoTask );
124             consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
125         }
126         else
127         {
128             log.info( "Executing task from queue with job name: " + repoTask );
129
130             // otherwise, execute consumers on whole repository
131             if ( arepo == null )
132             {
133                 throw new TaskExecutionException(
134                     "Unable to execute RepositoryTask with invalid repository id: " + repoId );
135             }
136
137             long sinceWhen = RepositoryScanner.FRESH_SCAN;
138             long previousFileCount = 0;
139
140             RepositorySession repositorySession = repositorySessionFactory.createSession();
141             MetadataRepository metadataRepository = repositorySession.getRepository();
142             try
143             {
144                 if ( !repoTask.isScanAll() )
145                 {
146                     RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics(
147                         metadataRepository, repoId );
148                     if ( previousStats != null )
149                     {
150                         sinceWhen = previousStats.getScanStartTime().getTime();
151                         previousFileCount = previousStats.getTotalFileCount();
152                     }
153                 }
154
155                 RepositoryScanStatistics stats;
156                 try
157                 {
158                     stats = repoScanner.scan( arepo, sinceWhen );
159                 }
160                 catch ( RepositoryScannerException e )
161                 {
162                     throw new TaskExecutionException( "Repository error when executing repository job.", e );
163                 }
164
165                 log.info( "Finished first scan: " + stats.toDump( arepo ) );
166
167                 // further statistics will be populated by the following method
168                 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
169                 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId, stats.getWhenGathered(),
170                                                                     endTime, 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: " + repoTask );
189
190             this.task = null;
191         }
192     }
193
194     public Task getCurrentTaskInExecution()
195     {
196         return task;
197     }
198 }