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.model.RepositoryContentStatistics;
33 import org.apache.maven.archiva.repository.RepositoryException;
34 import org.apache.maven.archiva.repository.scanner.RepositoryScanStatistics;
35 import org.apache.maven.archiva.repository.scanner.RepositoryScanner;
36 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
38 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
39 import org.codehaus.plexus.taskqueue.Task;
40 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
41 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 import java.util.List;
49 * ArchivaRepositoryScanningTaskExecutor
51 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
55 * role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
56 * role-hint="repository-scanning"
58 public class ArchivaRepositoryScanningTaskExecutor
59 implements TaskExecutor, Initializable
61 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
64 * @plexus.requirement role-hint="jdo"
66 private ArchivaDAO dao;
71 private ArchivaConfiguration archivaConfiguration;
74 * The repository scanner component.
78 private RepositoryScanner repoScanner;
80 public void initialize()
81 throws InitializationException
83 log.info( "Initialized " + this.getClass().getName() );
86 public void executeTask( Task task )
87 throws TaskExecutionException
89 RepositoryTask repoTask = (RepositoryTask) task;
91 if ( StringUtils.isBlank( repoTask.getRepositoryId() ) )
93 throw new TaskExecutionException("Unable to execute RepositoryTask with blank repository Id.");
96 log.info( "Executing task from queue with job name: " + repoTask.getName() );
100 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById( repoTask.getRepositoryId() );
103 throw new TaskExecutionException( "Unable to execute RepositoryTask with invalid repository id: " + repoTask.getRepositoryId() );
106 long sinceWhen = RepositoryScanner.FRESH_SCAN;
108 List<RepositoryContentStatistics> results = dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
110 if ( CollectionUtils.isNotEmpty( results ) )
112 RepositoryContentStatistics lastStats = results.get( 0 );
113 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
116 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
118 log.info( "Finished repository task: " + stats.toDump( arepo ) );
120 RepositoryContentStatistics dbstats = constructRepositoryStatistics( arepo, sinceWhen, results, stats );
122 dao.getRepositoryContentStatisticsDAO().saveRepositoryContentStatistics( dbstats );
124 catch ( RepositoryException e )
126 throw new TaskExecutionException( "Repository error when executing repository job.", e );
130 private RepositoryContentStatistics constructRepositoryStatistics( ManagedRepositoryConfiguration arepo,
132 List<RepositoryContentStatistics> results,
133 RepositoryScanStatistics stats )
135 // I hate jpox and modello <-- and so do I
136 RepositoryContentStatistics dbstats = new RepositoryContentStatistics();
137 dbstats.setDuration( stats.getDuration() );
138 dbstats.setNewFileCount( stats.getNewFileCount() );
139 dbstats.setRepositoryId( stats.getRepositoryId() );
140 dbstats.setTotalFileCount( stats.getTotalFileCount() );
141 dbstats.setWhenGathered( stats.getWhenGathered() );
145 List<RepositoryContentStatistics> secondResults = dao.query( new MostRecentRepositoryScanStatistics( arepo.getId() ) );
146 if ( CollectionUtils.isNotEmpty( results ) )
148 RepositoryContentStatistics lastStats = secondResults.get( 0 );
149 sinceWhen = lastStats.getWhenGathered().getTime() + lastStats.getDuration();
153 // total artifact count
156 List artifacts = dao.getArtifactDAO().queryArtifacts(
157 new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) );
158 dbstats.setTotalArtifactCount( artifacts.size() );
160 catch ( ObjectNotFoundException oe )
162 log.error( "Object not found in the database : " + oe.getMessage() );
164 catch ( ArchivaDatabaseException ae )
166 log.error( "Error occurred while querying artifacts for artifact count : " + ae.getMessage() );
171 long size = FileUtils.sizeOfDirectory( new File( arepo.getLocation() ) );
172 dbstats.setTotalSize( size );
177 // total unique groups
178 List<String> repos = new ArrayList<String>();
179 repos.add( arepo.getId() );
182 List<String> groupIds = dao.getArtifactDAO().queryArtifacts( new UniqueGroupIdConstraint( repos ) );
183 dbstats.setTotalGroupCount( groupIds.size() );
185 catch ( ObjectNotFoundException oe )
189 catch ( ArchivaDatabaseException ae )
194 // total unique projects
197 List<Object[]> artifactIds = dao.getArtifactDAO().queryArtifacts( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
198 dbstats.setTotalProjectCount( artifactIds.size() );
200 catch ( ObjectNotFoundException oe )
204 catch ( ArchivaDatabaseException ae )