1 package org.apache.archiva.scheduler.repository;
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.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.collections.CollectionUtils;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.database.ArchivaDAO;
36 import org.apache.maven.archiva.database.ArchivaDatabaseException;
37 import org.apache.maven.archiva.database.ObjectNotFoundException;
38 import org.apache.maven.archiva.database.constraints.ArtifactsByRepositoryConstraint;
39 import org.apache.maven.archiva.database.constraints.MostRecentRepositoryScanStatistics;
40 import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint;
41 import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint;
42 import org.apache.maven.archiva.model.ArchivaArtifact;
43 import org.apache.maven.archiva.model.RepositoryContentStatistics;
44 import org.apache.maven.archiva.repository.events.RepositoryListener;
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
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 * TODO: just for stats, remove this and use the main stats module
70 * @plexus.requirement role-hint="jdo"
72 private ArchivaDAO dao;
77 private ArchivaConfiguration archivaConfiguration;
80 * The repository scanner component.
84 private RepositoryScanner repoScanner;
87 * @plexus.requirement role="org.apache.maven.archiva.repository.events.RepositoryListener"
89 private List<RepositoryListener> repositoryListeners;
94 private RepositoryContentConsumers consumers;
98 public void initialize()
99 throws InitializationException
101 log.info( "Initialized " + this.getClass().getName() );
104 @SuppressWarnings("unchecked")
105 public void executeTask( Task task )
106 throws TaskExecutionException
109 // TODO: replace this whole class with the prescribed content scanning service/action
110 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
111 // send events for each
112 // - scan metadata for artifacts that have been removed and send events for each
113 // - scan metadata for missing plugin data
114 // - store information so that it can restart upon failure (publish event on the server recovery
115 // queue, remove it on successful completion)
119 RepositoryTask repoTask = (RepositoryTask) task;
121 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
123 throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
126 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
128 // execute consumers on resource file if set
129 if( repoTask.getResourceFile() != null )
131 log.debug( "Executing task from queue with job name: " + repoTask );
132 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
136 log.info( "Executing task from queue with job name: " + repoTask );
138 // otherwise, execute consumers on whole repository
143 throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
146 long sinceWhen = RepositoryScanner.FRESH_SCAN;
148 List<RepositoryContentStatistics> results = (List<RepositoryContentStatistics>) dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
150 if ( CollectionUtils.isNotEmpty( results ) )
152 RepositoryContentStatistics lastStats = results.get( 0 );
153 if( !repoTask.isScanAll() )
155 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
159 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
161 log.info( "Finished first scan: " + stats.toDump( arepo ) );
163 RepositoryContentStatistics dbstats = constructRepositoryStatistics( arepo, sinceWhen, results, stats );
165 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
167 // log.info( "Scanning for removed repository content" );
169 // FIXME: remove hardcoding
170 // MetadataRepository metadataRepository =
171 // new FileMetadataRepository( new File( arepo.getLocation(), ".metadata" ) );
173 // metadataRepository.findAllProjects();
174 // FIXME: do something
176 log.info( "Finished repository task: " + repoTask );
180 catch ( RepositoryScannerException e )
182 throw new TaskExecutionException( "Repository error when executing repository job.", e );
187 @SuppressWarnings("unchecked")
188 private RepositoryContentStatistics constructRepositoryStatistics( ManagedRepositoryConfiguration arepo,
190 List<RepositoryContentStatistics> results,
191 RepositoryScanStatistics stats )
193 // I hate jpox and modello <-- and so do I
194 RepositoryContentStatistics dbstats = new RepositoryContentStatistics();
195 dbstats.setDuration( stats.getDuration() );
196 dbstats.setNewFileCount( stats.getNewFileCount() );
197 dbstats.setRepositoryId( stats.getRepositoryId() );
198 dbstats.setTotalFileCount( stats.getTotalFileCount() );
199 dbstats.setWhenGathered( stats.getWhenGathered() );
201 // total artifact count
204 List<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts(
205 new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) );
206 dbstats.setTotalArtifactCount( artifacts.size() );
208 catch ( ObjectNotFoundException oe )
210 log.error( "Object not found in the database : " + oe.getMessage() );
212 catch ( ArchivaDatabaseException ae )
214 log.error( "Error occurred while querying artifacts for artifact count : " + ae.getMessage() );
218 long size = FileUtils.sizeOfDirectory( new File( arepo.getLocation() ) );
219 dbstats.setTotalSize( size );
221 // total unique groups
222 List<String> repos = new ArrayList<String>();
223 repos.add( arepo.getId() );
225 List<String> groupIds = (List<String>) dao.query( new UniqueGroupIdConstraint( repos ) );
226 dbstats.setTotalGroupCount( groupIds.size() );
228 List<Object[]> artifactIds = (List<Object[]>) dao.query( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
229 dbstats.setTotalProjectCount( artifactIds.size() );
234 public Task getCurrentTaskInExecution()