This project has retired. For details please refer to its
Attic page.
DefaultMergedRemoteIndexesScheduler xref
1 package org.apache.archiva.indexer.merger.base;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
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 }