1 package org.apache.archiva.indexer.merger;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.FileUtils;
22 import org.apache.archiva.indexer.ArchivaIndexManager;
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.IndexCreationFailedException;
25 import org.apache.archiva.indexer.merger.IndexMerger;
26 import org.apache.archiva.indexer.merger.IndexMergerException;
27 import org.apache.archiva.indexer.merger.IndexMergerRequest;
28 import org.apache.archiva.indexer.merger.TemporaryGroupIndex;
29 import org.apache.archiva.repository.Repository;
30 import org.apache.archiva.repository.RepositoryRegistry;
31 import org.apache.commons.lang.time.StopWatch;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.scheduling.annotation.Async;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.util.Collection;
42 import java.util.List;
43 import java.util.Optional;
44 import java.util.concurrent.CopyOnWriteArrayList;
45 import java.util.stream.Collectors;
48 * @author Olivier Lamy
51 @Service("indexMerger#default")
52 public class DefaultIndexMerger
53 implements IndexMerger
57 RepositoryRegistry repositoryRegistry;
59 private Logger log = LoggerFactory.getLogger( getClass() );
61 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<>();
63 private List<ArchivaIndexingContext> temporaryContextes = new CopyOnWriteArrayList<>( );
65 private List<String> runningGroups = new CopyOnWriteArrayList<>();
68 public DefaultIndexMerger( )
73 public ArchivaIndexingContext buildMergedIndex(IndexMergerRequest indexMergerRequest )
74 throws IndexMergerException
76 String groupId = indexMergerRequest.getGroupId();
78 if ( runningGroups.contains( groupId ) )
80 log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
84 runningGroups.add( groupId );
85 StopWatch stopWatch = new StopWatch();
90 Path mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
91 Repository destinationRepository = repositoryRegistry.getRepository(indexMergerRequest.getGroupId());
93 ArchivaIndexManager idxManager = repositoryRegistry.getIndexManager(destinationRepository.getType());
94 List<ArchivaIndexingContext> sourceContexts = indexMergerRequest.getRepositoriesIds().stream().map(id -> repositoryRegistry.getRepository(id).getIndexingContext()).collect(Collectors.toList());
96 ArchivaIndexingContext result = idxManager.mergeContexts(destinationRepository, sourceContexts, indexMergerRequest.isPackIndex());
97 if ( indexMergerRequest.isTemporary() )
99 String tempRepoId = destinationRepository.getId()+System.currentTimeMillis();
100 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
101 indexMergerRequest.getMergedIndexTtl() ) );
102 temporaryContextes.add(result);
105 } catch (IndexCreationFailedException e) {
106 throw new IndexMergerException("Index merging failed " + e.getMessage(), e);
111 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
112 stopWatch.getTime() );
113 runningGroups.remove(groupId);
119 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
121 if ( temporaryGroupIndex == null )
128 Optional<ArchivaIndexingContext> ctxOpt = temporaryContextes.stream( ).filter( ctx -> ctx.getId( ).equals( temporaryGroupIndex.getIndexId( ) ) ).findFirst( );
129 if (ctxOpt.isPresent()) {
130 ArchivaIndexingContext ctx = ctxOpt.get();
132 temporaryGroupIndexes.remove( temporaryGroupIndex );
133 temporaryContextes.remove( ctx );
134 Path directory = temporaryGroupIndex.getDirectory();
135 if ( directory != null && Files.exists(directory) )
137 FileUtils.deleteDirectory( directory );
141 catch ( IOException e )
143 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
148 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
150 return this.temporaryGroupIndexes;