001package org.apache.archiva.indexer.merger.base; 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.indexer.merger.IndexMerger; 023import org.apache.archiva.indexer.merger.IndexMergerRequest; 024import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler; 025import org.apache.archiva.repository.ManagedRepository; 026import org.apache.archiva.repository.RepositoryGroup; 027import org.apache.archiva.repository.features.IndexCreationFeature; 028import org.apache.archiva.repository.storage.StorageAsset; 029import org.apache.commons.lang3.StringUtils; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.springframework.scheduling.TaskScheduler; 033import org.springframework.scheduling.support.CronTrigger; 034import org.springframework.stereotype.Service; 035 036import javax.inject.Inject; 037import javax.inject.Named; 038import java.nio.file.Path; 039import java.util.List; 040import java.util.Map; 041import java.util.concurrent.ConcurrentHashMap; 042import java.util.concurrent.ScheduledFuture; 043import java.util.stream.Collectors; 044 045/** 046 * @author Olivier Lamy 047 * @since 2.0.0 048 */ 049@Service( "mergedRemoteIndexesScheduler#default" ) 050public class DefaultMergedRemoteIndexesScheduler 051 implements MergedRemoteIndexesScheduler 052{ 053 054 private Logger logger = LoggerFactory.getLogger( getClass() ); 055 056 @Inject 057 @Named( value = "taskScheduler#mergeRemoteIndexes" ) 058 private TaskScheduler taskScheduler; 059 060 @Inject 061 private IndexMerger indexMerger; 062 063 private Map<String, ScheduledFuture> scheduledFutureMap = new ConcurrentHashMap<>(); 064 065 @Override 066 public void schedule(RepositoryGroup repositoryGroup, StorageAsset directory ) 067 { 068 if ( StringUtils.isEmpty( repositoryGroup.getSchedulingDefinition() ) ) 069 { 070 return; 071 } 072 CronTrigger cronTrigger = new CronTrigger( repositoryGroup.getSchedulingDefinition() ); 073 074 List<ManagedRepository> repositories = repositoryGroup.getRepositories(); 075 076 if (repositoryGroup.supportsFeature( IndexCreationFeature.class )) 077 { 078 079 IndexCreationFeature indexCreationFeature = repositoryGroup.getFeature( IndexCreationFeature.class ).get(); 080 Path indexPath = indexCreationFeature.getLocalIndexPath().getFilePath(); 081 if (indexPath!=null) 082 { 083 IndexMergerRequest indexMergerRequest = 084 new IndexMergerRequest( repositories.stream( ).map( r -> r.getId( ) ).collect( Collectors.toList( ) ), true, repositoryGroup.getId( ), 085 indexPath.toString( ), 086 repositoryGroup.getMergedIndexTTL( ) ).mergedIndexDirectory( directory ); 087 088 MergedRemoteIndexesTaskRequest taskRequest = 089 new MergedRemoteIndexesTaskRequest( indexMergerRequest, indexMerger ); 090 091 logger.info( "schedule merge remote index for group {} with cron {}", repositoryGroup.getId( ), 092 repositoryGroup.getSchedulingDefinition( ) ); 093 094 ScheduledFuture scheduledFuture = 095 taskScheduler.schedule( new MergedRemoteIndexesTask( taskRequest ), cronTrigger ); 096 scheduledFutureMap.put( repositoryGroup.getId( ), scheduledFuture ); 097 } else { 098 logger.error("Requested index merger for repository group {} with non local index path {}", repositoryGroup.getId(), indexCreationFeature.getLocalIndexPath()); 099 } 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}