]> source.dussan.org Git - archiva.git/blob
b2be61184893b2ebb44d5282a315c9edf24af4ce
[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.repository.ManagedRepository;
23 import org.apache.archiva.repository.RepositoryGroup;
24 import org.apache.archiva.repository.features.IndexCreationFeature;
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.nio.file.Path;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.concurrent.ScheduledFuture;
39 import java.util.stream.Collectors;
40
41 /**
42  * @author Olivier Lamy
43  * @since 2.0.0
44  */
45 @Service( "mergedRemoteIndexesScheduler#default" )
46 public class DefaultMergedRemoteIndexesScheduler
47     implements MergedRemoteIndexesScheduler
48 {
49
50     private Logger logger = LoggerFactory.getLogger( getClass() );
51
52     @Inject
53     @Named( value = "taskScheduler#mergeRemoteIndexes" )
54     private TaskScheduler taskScheduler;
55
56     @Inject
57     private IndexMerger indexMerger;
58
59     private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<>();
60
61     @Override
62     public void schedule(RepositoryGroup repositoryGroup, Path directory )
63     {
64         if ( StringUtils.isEmpty( repositoryGroup.getSchedulingDefinition() ) )
65         {
66             return;
67         }
68         CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getSchedulingDefinition() );
69
70         List<ManagedRepository> repositories = repositoryGroup.getRepositories();
71
72         if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
73         {
74
75             IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
76             Path indexPath = indexCreationFeature.getLocalIndexPath().getFilePath();
77             if (indexPath!=null)
78             {
79                 IndexMergerRequest indexMergerRequest =
80                     new IndexMergerRequest( repositories.stream( ).map( r -> r.getId( ) ).collect( Collectors.toList( ) ), true, repositoryGroup.getId( ),
81                         indexPath.toString( ),
82                         repositoryGroup.getMergedIndexTTL( ) ).mergedIndexDirectory( directory );
83
84                 MergedRemoteIndexesTaskRequest taskRequest =
85                     new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger );
86
87                 logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId( ),
88                     repositoryGroup.getSchedulingDefinition( ) );
89
90                 ScheduledFuture scheduledFuture =
91                     taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger );
92                 scheduledFutureMap.put( repositoryGroup.getId( ), scheduledFuture );
93             } else {
94                 logger.error("Requested index merger for repository group {} with non local index path {}", repositoryGroup.getId(), indexCreationFeature.getLocalIndexPath());
95             }
96         } else {
97             logger.error("Scheduling merged index for repository group {}, but it does not support IndexCreationFeature.", repositoryGroup.getId());
98         }
99     }
100
101     @Override
102     public void unschedule( RepositoryGroup repositoryGroup )
103     {
104         ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() );
105         if ( scheduledFuture != null )
106         {
107             scheduledFuture.cancel( true );
108         }
109     }
110 }