]> source.dussan.org Git - archiva.git/blob
631e6a03f6a122646f1664bf3c0342042e933541
[archiva.git] /
1 package org.apache.maven.archiva.scheduled.executors;
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.commons.collections.CollectionUtils;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.database.ArchivaDAO;
28 import org.apache.maven.archiva.database.ArchivaDatabaseException;
29 import org.apache.maven.archiva.database.ObjectNotFoundException;
30 import org.apache.maven.archiva.database.constraints.ArtifactsByRepositoryConstraint;
31 import org.apache.maven.archiva.database.constraints.MostRecentRepositoryScanStatistics;
32 import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint;
33 import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint;
34 import org.apache.maven.archiva.model.ArchivaArtifact;
35 import org.apache.maven.archiva.model.RepositoryContentStatistics;
36 import org.apache.maven.archiva.repository.RepositoryException;
37 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
38 import org.apache.maven.archiva.repository.scanner.RepositoryScanStatistics;
39 import org.apache.maven.archiva.repository.scanner.RepositoryScanner;
40 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
42 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
43 import org.codehaus.plexus.taskqueue.Task;
44 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
45 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import java.io.File;
50 import java.util.ArrayList;
51 import java.util.List;
52
53 /**
54  * ArchivaRepositoryScanningTaskExecutor 
55  *
56  * @version $Id$
57  * 
58  * @plexus.component
59  *   role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
60  *   role-hint="repository-scanning"
61  */
62 public class ArchivaRepositoryScanningTaskExecutor
63     implements TaskExecutor, Initializable
64 {
65     private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
66     
67     /**
68      * @plexus.requirement role-hint="jdo"
69      */
70     private ArchivaDAO dao;
71     
72     /**
73      * @plexus.requirement
74      */
75     private ArchivaConfiguration archivaConfiguration;
76
77     /**
78      * The repository scanner component.
79      * 
80      * @plexus.requirement
81      */
82     private RepositoryScanner repoScanner;
83     
84     /**
85      * @plexus.requirement
86      */
87     private RepositoryContentConsumers consumers;
88     
89     private Task task;
90
91     public void initialize()
92         throws InitializationException
93     {
94         log.info( "Initialized " + this.getClass().getName() );
95     }
96
97     @SuppressWarnings("unchecked")
98     public void executeTask( Task task )
99         throws TaskExecutionException
100     {
101         this.task = task;
102         
103         RepositoryTask repoTask = (RepositoryTask) task;
104         
105         if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
106         {
107             throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
108         }
109
110         ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
111         
112         // execute consumers on resource file if set
113         if( repoTask.getResourceFile() != null )
114         {
115             log.debug( "Executing task from queue with job name: " + repoTask );
116             consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
117         }
118         else
119         {
120             log.info( "Executing task from queue with job name: " + repoTask );
121             
122             // otherwise, execute consumers on whole repository
123             try
124             {   
125                 if ( arepo == null )
126                 {
127                     throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
128                 }
129     
130                 long sinceWhen = RepositoryScanner.FRESH_SCAN;
131     
132                 List<RepositoryContentStatistics> results = (List<RepositoryContentStatistics>) dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
133                
134                 if ( CollectionUtils.isNotEmpty( results ) )
135                 {
136                     RepositoryContentStatistics lastStats = results.get( 0 );
137                     if( !repoTask.isScanAll() )
138                     {
139                         sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
140                     }
141                 }
142     
143                 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
144     
145                 log.info( "Finished repository task: " + stats.toDump( arepo ) );
146                 
147                 RepositoryContentStatistics dbstats = constructRepositoryStatistics( arepo, sinceWhen, results, stats );
148                 
149                 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );   
150                 
151                 this.task = null;
152             }
153             catch ( RepositoryException e )
154             {   
155                 throw new TaskExecutionException( "Repository error when executing repository job.", e );
156             }    
157         }
158     }
159
160     @SuppressWarnings("unchecked")
161     private RepositoryContentStatistics constructRepositoryStatistics( ManagedRepositoryConfiguration arepo,
162                                                                        long sinceWhen,
163                                                                        List<RepositoryContentStatistics> results,
164                                                                        RepositoryScanStatistics stats )        
165     {
166         // I hate jpox and modello <-- and so do I
167         RepositoryContentStatistics dbstats = new RepositoryContentStatistics();
168         dbstats.setDuration( stats.getDuration() );
169         dbstats.setNewFileCount( stats.getNewFileCount() );
170         dbstats.setRepositoryId( stats.getRepositoryId() );
171         dbstats.setTotalFileCount( stats.getTotalFileCount() );
172         dbstats.setWhenGathered( stats.getWhenGathered() );
173                 
174         // total artifact count
175         try
176         {
177             List<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts( 
178                       new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) );            
179             dbstats.setTotalArtifactCount( artifacts.size() );
180         }
181         catch ( ObjectNotFoundException oe )
182         {
183             log.error( "Object not found in the database : " + oe.getMessage() );
184         }
185         catch ( ArchivaDatabaseException ae )
186         {   
187             log.error( "Error occurred while querying artifacts for artifact count : " + ae.getMessage() );
188         }
189         
190         // total repo size
191         long size = FileUtils.sizeOfDirectory( new File( arepo.getLocation() ) );
192         dbstats.setTotalSize( size );
193           
194           // total unique groups
195         List<String> repos = new ArrayList<String>();
196         repos.add( arepo.getId() ); 
197         
198         List<String> groupIds = (List<String>) dao.query( new UniqueGroupIdConstraint( repos ) );
199         dbstats.setTotalGroupCount( groupIds.size() );
200                 
201         List<Object[]> artifactIds = (List<Object[]>) dao.query( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
202         dbstats.setTotalProjectCount( artifactIds.size() );
203                         
204         return dbstats;
205     }   
206     
207     public Task getCurrentTaskInExecution()
208     {
209         return task;
210     }
211 }