1 package org.apache.archiva.indexer.merger;
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.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;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import java.nio.file.Path;
35 import java.util.List;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.concurrent.ScheduledFuture;
39 import java.util.stream.Collectors;
42 * @author Olivier Lamy
45 @Service( "mergedRemoteIndexesScheduler#default" )
46 public class DefaultMergedRemoteIndexesScheduler
47 implements MergedRemoteIndexesScheduler
50 private Logger logger = LoggerFactory.getLogger( getClass() );
53 @Named( value = "taskScheduler#mergeRemoteIndexes" )
54 private TaskScheduler taskScheduler;
57 private IndexMerger indexMerger;
59 private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<>();
62 public void schedule(RepositoryGroup repositoryGroup, Path directory )
64 if ( StringUtils.isEmpty( repositoryGroup.getSchedulingDefinition() ) )
68 CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getSchedulingDefinition() );
70 List<ManagedRepository> repositories = repositoryGroup.getRepositories();
72 if (repositoryGroup.supportsFeature( IndexCreationFeature.class ))
75 IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get();
76 Path indexPath = indexCreationFeature.getLocalIndexPath().getFilePath();
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 );
84 MergedRemoteIndexesTaskRequest taskRequest =
85 new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger );
87 logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId( ),
88 repositoryGroup.getSchedulingDefinition( ) );
90 ScheduledFuture scheduledFuture =
91 taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger );
92 scheduledFutureMap.put( repositoryGroup.getId( ), scheduledFuture );
94 logger.error("Requested index merger for repository group {} with non local index path {}", repositoryGroup.getId(), indexCreationFeature.getLocalIndexPath());
97 logger.error("Scheduling merged index for repository group {}, but it does not support IndexCreationFeature.", repositoryGroup.getId());
102 public void unschedule( RepositoryGroup repositoryGroup )
104 ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() );
105 if ( scheduledFuture != null )
107 scheduledFuture.cancel( true );