001package org.apache.archiva.indexer.merger; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.admin.model.beans.RepositoryGroup; 023import org.apache.archiva.scheduler.MergedRemoteIndexesScheduler; 024import org.apache.commons.lang.StringUtils; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027import org.springframework.scheduling.TaskScheduler; 028import org.springframework.scheduling.support.CronTrigger; 029import org.springframework.stereotype.Service; 030 031import javax.inject.Inject; 032import javax.inject.Named; 033import java.io.File; 034import java.util.List; 035import java.util.Map; 036import java.util.concurrent.ConcurrentHashMap; 037import java.util.concurrent.ScheduledFuture; 038 039/** 040 * @author Olivier Lamy 041 * @since 2.0.0 042 */ 043@Service( "mergedRemoteIndexesScheduler#default" ) 044public class DefaultMergedRemoteIndexesScheduler 045 implements MergedRemoteIndexesScheduler 046{ 047 048 private Logger logger = LoggerFactory.getLogger( getClass() ); 049 050 @Inject 051 @Named( value = "taskScheduler#mergeRemoteIndexes" ) 052 private TaskScheduler taskScheduler; 053 054 @Inject 055 private IndexMerger indexMerger; 056 057 private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<>(); 058 059 @Override 060 public void schedule( RepositoryGroup repositoryGroup, File directory ) 061 { 062 if ( StringUtils.isEmpty( repositoryGroup.getCronExpression() ) ) 063 { 064 return; 065 } 066 CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getCronExpression() ); 067 068 List<String> repositories = repositoryGroup.getRepositories(); 069 070 IndexMergerRequest indexMergerRequest = 071 new IndexMergerRequest( repositories, true, repositoryGroup.getId(), repositoryGroup.getMergedIndexPath(), 072 repositoryGroup.getMergedIndexTtl() ).mergedIndexDirectory( directory ); 073 074 MergedRemoteIndexesTaskRequest taskRequest = 075 new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger ); 076 077 logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId(), 078 repositoryGroup.getCronExpression() ); 079 080 ScheduledFuture scheduledFuture = 081 taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger ); 082 scheduledFutureMap.put( repositoryGroup.getId(), scheduledFuture ); 083 } 084 085 @Override 086 public void unschedule( RepositoryGroup repositoryGroup ) 087 { 088 ScheduledFuture scheduledFuture = scheduledFutureMap.remove( repositoryGroup.getId() ); 089 if ( scheduledFuture != null ) 090 { 091 scheduledFuture.cancel( true ); 092 } 093 } 094}