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.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;
49 import java.util.ArrayList;
50 import java.util.List;
53 * ArchivaRepositoryScanningTaskExecutor
58 * role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
59 * role-hint="repository-scanning"
61 public class ArchivaRepositoryScanningTaskExecutor
62 implements TaskExecutor, Initializable
64 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
67 * @plexus.requirement role-hint="jdo"
69 private ArchivaDAO dao;
74 private ArchivaConfiguration archivaConfiguration;
77 * The repository scanner component.
81 private RepositoryScanner repoScanner;
83 public void initialize()
84 throws InitializationException
86 log.info( "Initialized " + this.getClass().getName() );
89 @SuppressWarnings("unchecked")
90 public void executeTask( Task task )
91 throws TaskExecutionException
93 RepositoryTask repoTask = (RepositoryTask) task;
95 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
97 throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
100 log.info( "Executing task from queue with job name: " + repoTask.getName() );
104 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
107 throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
110 long sinceWhen = RepositoryScanner.FRESH_SCAN;
112 List<RepositoryContentStatistics> results = (List<RepositoryContentStatistics>) dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
114 if ( CollectionUtils.isNotEmpty( results ) )
116 RepositoryContentStatistics lastStats = results.get( 0 );
117 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
120 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
122 log.info( "Finished repository task: " + stats.toDump( arepo ) );
124 RepositoryContentStatistics dbstats = constructRepositoryStatistics( arepo, sinceWhen, results, stats );
126 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
128 catch ( RepositoryException e )
130 throw new TaskExecutionException( "Repository error when executing repository job.", e );
134 @SuppressWarnings("unchecked")
135 private RepositoryContentStatistics constructRepositoryStatistics( ManagedRepositoryConfiguration arepo,
137 List<RepositoryContentStatistics> results,
138 RepositoryScanStatistics stats )
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() );
148 // total artifact count
151 List<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts(
152 new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) );
153 dbstats.setTotalArtifactCount( artifacts.size() );
155 catch ( ObjectNotFoundException oe )
157 log.error( "Object not found in the database : " + oe.getMessage() );
159 catch ( ArchivaDatabaseException ae )
161 log.error( "Error occurred while querying artifacts for artifact count : " + ae.getMessage() );
165 long size = FileUtils.sizeOfDirectory( new File( arepo.getLocation() ) );
166 dbstats.setTotalSize( size );
168 // total unique groups
169 List<String> repos = new ArrayList<String>();
170 repos.add( arepo.getId() );
172 List<String> groupIds = (List<String>) dao.query( new UniqueGroupIdConstraint( repos ) );
173 dbstats.setTotalGroupCount( groupIds.size() );
175 List<Object[]> artifactIds = (List<Object[]>) dao.query( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
176 dbstats.setTotalProjectCount( artifactIds.size() );