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