1 package org.apache.archiva.indexer.merger.base;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.nio.file.Path;
39 import java.util.List;
41 import java.util.concurrent.ConcurrentHashMap;
42 import java.util.concurrent.ScheduledFuture;
43 import java.util.stream.Collectors;
46 * @author Olivier Lamy
49 @Service( "mergedRemoteIndexesScheduler#default" )
50 public class DefaultMergedRemoteIndexesScheduler
51 implements MergedRemoteIndexesScheduler
54 private Logger logger = LoggerFactory.getLogger( getClass() );
57 @Named( value = "taskScheduler#mergeRemoteIndexes" )
58 private TaskScheduler taskScheduler;
61 private IndexMerger indexMerger;
63 private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<>();
66 public void schedule(RepositoryGroup repositoryGroup, StorageAsset directory )
68 if ( StringUtils.isEmpty( repositoryGroup.getSchedulingDefinition() ) )
72 CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getSchedulingDefinition() );
74 List<ManagedRepository> repositories = repositoryGroup.getRepositories();
76 if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
79 IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
80 Path indexPath = indexCreationFeature.getLocalIndexPath().getFilePath();
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 );
88 MergedRemoteIndexesTaskRequest taskRequest =
89 new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger );
91 logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId( ),
92 repositoryGroup.getSchedulingDefinition( ) );
94 ScheduledFuture scheduledFuture =
95 taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger );
96 scheduledFutureMap.put( repositoryGroup.getId( ), scheduledFuture );
98 logger.error("Requested index merger for repository group {} with non local index path {}", repositoryGroup.getId(), indexCreationFeature.getLocalIndexPath());
101 logger.error("Scheduling merged index for repository group {}, but it does not support IndexCreationFeature.", repositoryGroup.getId());
106 public void unschedule( RepositoryGroup repositoryGroup )
108 ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() );
109 if ( scheduledFuture != null )
111 scheduledFuture.cancel( true );