]> source.dussan.org Git - archiva.git/blob
776253d99b04b71fc7fd1ab3fd6fb52fc235aaa1
[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.apache.commons.lang.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.scheduling.TaskScheduler;
29 import org.springframework.scheduling.support.CronTrigger;
30 import org.springframework.stereotype.Service;
31
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import java.io.File;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.concurrent.ScheduledFuture;
39
40 /**
41  * @author Olivier Lamy
42  * @since 2.0.0
43  */
44 @Service( "mergedRemoteIndexesScheduler#default" )
45 public class DefaultMergedRemoteIndexesScheduler
46     implements MergedRemoteIndexesScheduler
47 {
48
49     private Logger logger = LoggerFactory.getLogger( getClass() );
50
51     @Inject
52     @Named( value = "taskScheduler#mergeRemoteIndexes" )
53     private TaskScheduler taskScheduler;
54
55     @Inject
56     private IndexMerger indexMerger;
57
58     private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<String, ScheduledFuture>();
59
60     @Override
61     public void schedule( RepositoryGroup repositoryGroup, File directory )
62     {
63         if ( StringUtils.isEmpty( repositoryGroup.getCronExpression() ) )
64         {
65             return;
66         }
67         CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getCronExpression() );
68
69         List<String> repositories = repositoryGroup.getRepositories();
70
71         IndexMergerRequest indexMergerRequest =
72             new IndexMergerRequest( repositories, true, repositoryGroup.getId(), repositoryGroup.getMergedIndexPath(),
73                                     repositoryGroup.getMergedIndexTtl() ).mergedIndexDirectory( directory );
74
75         MergedRemoteIndexesTaskRequest taskRequest =
76             new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger );
77
78         logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId(),
79                      repositoryGroup.getCronExpression() );
80
81         ScheduledFuture scheduledFuture =
82             taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger );
83         scheduledFutureMap.put( repositoryGroup.getId(), scheduledFuture );
84     }
85
86     @Override
87     public void unschedule( RepositoryGroup repositoryGroup )
88     {
89         ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() );
90         if ( scheduledFuture != null )
91         {
92             scheduledFuture.cancel( true );
93         }
94     }
95 }