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