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