1 package org.apache.maven.archiva.scheduled.executors;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
50 import java.util.ArrayList;
51 import java.util.List;
54 * ArchivaRepositoryScanningTaskExecutor
59 * role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
60 * role-hint="repository-scanning"
62 public class ArchivaRepositoryScanningTaskExecutor
63 implements TaskExecutor, Initializable
65 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
68 * @plexus.requirement role-hint="jdo"
70 private ArchivaDAO dao;
75 private ArchivaConfiguration archivaConfiguration;
78 * The repository scanner component.
82 private RepositoryScanner repoScanner;
87 private RepositoryContentConsumers consumers;
91 public void initialize()
92 throws InitializationException
94 log.info( "Initialized " + this.getClass().getName() );
97 @SuppressWarnings("unchecked")
98 public void executeTask( Task task )
99 throws TaskExecutionException
103 RepositoryTask repoTask = (RepositoryTask) task;
105 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
107 throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
110 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
112 // execute consumers on resource file if set
113 if( repoTask.getResourceFile() != null )
115 log.debug( "Executing task from queue with job name: " + repoTask );
116 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
120 log.info( "Executing task from queue with job name: " + repoTask );
122 // otherwise, execute consumers on whole repository
127 throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
130 long sinceWhen = RepositoryScanner.FRESH_SCAN;
132 List<RepositoryContentStatistics> results = (List<RepositoryContentStatistics>) dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
134 if ( CollectionUtils.isNotEmpty( results ) )
136 RepositoryContentStatistics lastStats = results.get( 0 );
137 if( !repoTask.isScanAll() )
139 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
143 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
145 log.info( "Finished repository task: " + stats.toDump( arepo ) );
147 RepositoryContentStatistics dbstats = constructRepositoryStatistics( arepo, sinceWhen, results, stats );
149 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
153 catch ( RepositoryException e )
155 throw new TaskExecutionException( "Repository error when executing repository job.", e );
160 @SuppressWarnings("unchecked")
161 private RepositoryContentStatistics constructRepositoryStatistics( ManagedRepositoryConfiguration arepo,
163 List<RepositoryContentStatistics> results,
164 RepositoryScanStatistics stats )
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() );
174 // total artifact count
177 List<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts(
178 new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) );
179 dbstats.setTotalArtifactCount( artifacts.size() );
181 catch ( ObjectNotFoundException oe )
183 log.error( "Object not found in the database : " + oe.getMessage() );
185 catch ( ArchivaDatabaseException ae )
187 log.error( "Error occurred while querying artifacts for artifact count : " + ae.getMessage() );
191 long size = FileUtils.sizeOfDirectory( new File( arepo.getLocation() ) );
192 dbstats.setTotalSize( size );
194 // total unique groups
195 List<String> repos = new ArrayList<String>();
196 repos.add( arepo.getId() );
198 List<String> groupIds = (List<String>) dao.query( new UniqueGroupIdConstraint( repos ) );
199 dbstats.setTotalGroupCount( groupIds.size() );
201 List<Object[]> artifactIds = (List<Object[]>) dao.query( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
202 dbstats.setTotalProjectCount( artifactIds.size() );
207 public Task getCurrentTaskInExecution()