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
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.database.ArchivaDAO;
32 import org.apache.maven.archiva.database.ArchivaDatabaseException;
33 import org.apache.maven.archiva.database.ObjectNotFoundException;
34 import org.apache.maven.archiva.database.constraints.ArtifactsByRepositoryConstraint;
35 import org.apache.maven.archiva.database.constraints.MostRecentRepositoryScanStatistics;
36 import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint;
37 import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint;
38 import org.apache.maven.archiva.model.ArchivaArtifact;
39 import org.apache.maven.archiva.model.RepositoryContentStatistics;
40 import org.apache.maven.archiva.repository.RepositoryException;
41 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
42 import org.apache.maven.archiva.repository.scanner.RepositoryScanStatistics;
43 import org.apache.maven.archiva.repository.scanner.RepositoryScanner;
44 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
45 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
46 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
47 import org.codehaus.plexus.taskqueue.Task;
48 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
49 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * ArchivaRepositoryScanningTaskExecutor
57 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
58 * role-hint="repository-scanning"
60 public class ArchivaRepositoryScanningTaskExecutor
61 implements TaskExecutor, Initializable
63 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
66 * @plexus.requirement role-hint="jdo"
68 private ArchivaDAO dao;
73 private ArchivaConfiguration archivaConfiguration;
76 * The repository scanner component.
80 private RepositoryScanner repoScanner;
85 private RepositoryContentConsumers consumers;
89 public void initialize()
90 throws InitializationException
92 log.info( "Initialized " + this.getClass().getName() );
95 @SuppressWarnings("unchecked")
96 public void executeTask( Task task )
97 throws TaskExecutionException
101 RepositoryTask repoTask = (RepositoryTask) task;
103 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
105 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
108 ManagedRepositoryConfiguration arepo =
109 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
111 // execute consumers on resource file if set
112 if ( repoTask.getResourceFile() != null )
114 log.debug( "Executing task from queue with job name: " + repoTask );
115 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
119 log.info( "Executing task from queue with job name: " + repoTask );
121 // otherwise, execute consumers on whole repository
126 throw new TaskExecutionException(
127 "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
130 long sinceWhen = RepositoryScanner.FRESH_SCAN;
132 List<RepositoryContentStatistics> results = (List<RepositoryContentStatistics>) dao.query(
133 new MostRecentRepositoryScanStatistics( arepo.getId() ) );
135 if ( CollectionUtils.isNotEmpty( results ) )
137 RepositoryContentStatistics lastStats = results.get( 0 );
138 if ( !repoTask.isScanAll() )
140 sinceWhen = lastStats.getWhenGathered().getTime() - lastStats.getDuration();
144 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
146 log.info( "Finished repository task: " + stats.toDump( arepo ) );
148 RepositoryContentStatistics dbstats = constructRepositoryStatistics( arepo, stats );
150 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
154 catch ( RepositoryException e )
156 throw new TaskExecutionException( "Repository error when executing repository job.", e );
161 @SuppressWarnings("unchecked")
162 private RepositoryContentStatistics constructRepositoryStatistics( ManagedRepositoryConfiguration arepo,
163 RepositoryScanStatistics stats )
165 // I hate jpox and modello <-- and so do I
166 RepositoryContentStatistics dbstats = new RepositoryContentStatistics();
167 dbstats.setDuration( stats.getDuration() );
168 dbstats.setNewFileCount( stats.getNewFileCount() );
169 dbstats.setRepositoryId( stats.getRepositoryId() );
170 dbstats.setTotalFileCount( stats.getTotalFileCount() );
171 dbstats.setWhenGathered( stats.getWhenGathered() );
173 // total artifact count
176 // note that when gathered is the end of the scan, so we look for all those before that time
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() );
190 // total repo size -- TODO: needs to exclude ignored files (eg .svn)
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 =
202 (List<Object[]>) dao.query( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
203 dbstats.setTotalProjectCount( artifactIds.size() );
208 public Task getCurrentTaskInExecution()