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.admin.model.beans.RepositoryGroup;
23 import org.apache.archiva.scheduler.MergedRemoteIndexesScheduler;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.scheduling.TaskScheduler;
28 import org.springframework.scheduling.support.CronTrigger;
29 import org.springframework.stereotype.Service;
31 import javax.inject.Inject;
32 import javax.inject.Named;
34 import java.util.List;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.concurrent.ScheduledFuture;
40 * @author Olivier Lamy
43 @Service( "mergedRemoteIndexesScheduler#default" )
44 public class DefaultMergedRemoteIndexesScheduler
45 implements MergedRemoteIndexesScheduler
48 private Logger logger = LoggerFactory.getLogger( getClass() );
51 @Named( value = "taskScheduler#mergeRemoteIndexes" )
52 private TaskScheduler taskScheduler;
55 private IndexMerger indexMerger;
57 private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<>();
60 public void schedule( RepositoryGroup repositoryGroup, File directory )
62 if ( StringUtils.isEmpty( repositoryGroup.getCronExpression() ) )
66 CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getCronExpression() );
68 List<String> repositories = repositoryGroup.getRepositories();
70 IndexMergerRequest indexMergerRequest =
71 new IndexMergerRequest( repositories, true, repositoryGroup.getId(), repositoryGroup.getMergedIndexPath(),
72 repositoryGroup.getMergedIndexTtl() ).mergedIndexDirectory( directory );
74 MergedRemoteIndexesTaskRequest taskRequest =
75 new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger );
77 logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId(),
78 repositoryGroup.getCronExpression() );
80 ScheduledFuture scheduledFuture =
81 taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger );
82 scheduledFutureMap.put( repositoryGroup.getId(), scheduledFuture );
86 public void unschedule( RepositoryGroup repositoryGroup )
88 ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() );
89 if ( scheduledFuture != null )
91 scheduledFuture.cancel( true );