]> source.dussan.org Git - archiva.git/blob
787c1c69e4419408068033f47709ee6ad02a1d30
[archiva.git] /
1 package org.apache.archiva.indexer.merger;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.admin.model.beans.RepositoryGroup;
23 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
24 import org.apache.archiva.scheduler.MergedRemoteIndexesScheduler;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.scheduling.TaskScheduler;
28 import org.springframework.scheduling.support.CronTrigger;
29 import org.springframework.stereotype.Service;
30
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import java.io.File;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.concurrent.ScheduledFuture;
38
39 /**
40  * @author Olivier Lamy
41  * @since 2.0.0
42  */
43 @Service( "mergedRemoteIndexesScheduler#default" )
44 public class DefaultMergedRemoteIndexesScheduler
45     implements MergedRemoteIndexesScheduler
46 {
47
48     private Logger logger = LoggerFactory.getLogger( getClass() );
49
50     @Inject
51     @Named( value = "taskScheduler#mergeRemoteIndexes" )
52     private TaskScheduler taskScheduler;
53
54     @Inject
55     private RepositoryGroupAdmin repositoryGroupAdmin;
56
57     @Inject
58     private IndexMerger indexMerger;
59
60     private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<String, ScheduledFuture>();
61
62     @Override
63     public void schedule( RepositoryGroup repositoryGroup )
64     {
65         if ( repositoryGroup.getCronExpression() == null )
66         {
67             return;
68         }
69         CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getCronExpression() );
70
71         File directory = repositoryGroupAdmin.getMergedIndexDirectory( repositoryGroup.getId() );
72
73         List<String> repositories = repositoryGroup.getRepositories();
74
75         IndexMergerRequest indexMergerRequest =
76             new IndexMergerRequest( repositories, true, repositoryGroup.getId(), repositoryGroup.getMergedIndexPath(),
77                                     repositoryGroup.getMergedIndexTtl() ).mergedIndexDirectory( directory );
78
79         MergedRemoteIndexesTask.MergedRemoteIndexesTaskRequest taskRequest =
80             new MergedRemoteIndexesTask.MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger );
81
82         ScheduledFuture scheduledFuture =
83             taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger );
84         scheduledFutureMap.put( repositoryGroup.getId(), scheduledFuture );
85     }
86
87     @Override
88     public void unschedule( RepositoryGroup repositoryGroup )
89     {
90         ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() );
91         if ( scheduledFuture != null )
92         {
93             scheduledFuture.cancel( true );
94         }
95     }
96 }