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.plexusbridge.MavenIndexerUtils;
22 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.lang.time.StopWatch;
26 import org.apache.maven.index.NexusIndexer;
27 import org.apache.maven.index.context.IndexingContext;
28 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
29 import org.apache.maven.index.packer.IndexPacker;
30 import org.apache.maven.index.packer.IndexPackingRequest;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.scheduling.annotation.Async;
34 import org.springframework.stereotype.Service;
36 import javax.inject.Inject;
38 import java.io.IOException;
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.concurrent.CopyOnWriteArrayList;
44 * @author Olivier Lamy
47 @Service("indexMerger#default")
48 public class DefaultIndexMerger
49 implements IndexMerger
52 private Logger log = LoggerFactory.getLogger( getClass() );
54 private MavenIndexerUtils mavenIndexerUtils;
56 private NexusIndexer indexer;
58 private IndexPacker indexPacker;
60 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<>();
62 private List<String> runningGroups = new CopyOnWriteArrayList<String>();
65 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
66 throws PlexusSisuBridgeException
68 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
69 this.mavenIndexerUtils = mavenIndexerUtils;
70 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
74 public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
75 throws IndexMergerException
77 String groupId = indexMergerRequest.getGroupId();
79 if ( runningGroups.contains( groupId ) )
81 log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
85 runningGroups.add( groupId );
87 StopWatch stopWatch = new StopWatch();
91 File mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
93 String tempRepoId = mergedIndexDirectory.getName();
97 File indexLocation = new File( mergedIndexDirectory, indexMergerRequest.getMergedIndexPath() );
98 IndexingContext indexingContext =
99 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory, indexLocation, null, null,
100 mavenIndexerUtils.getAllIndexCreators() );
102 for ( String repoId : indexMergerRequest.getRepositoriesIds() )
104 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
105 if ( idxToMerge != null )
107 indexingContext.merge( idxToMerge.getIndexDirectory() );
111 indexingContext.optimize();
113 if ( indexMergerRequest.isPackIndex() )
115 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
116 indexPacker.packIndex( request );
119 if ( indexMergerRequest.isTemporary() )
121 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
122 indexMergerRequest.getMergedIndexTtl() ) );
125 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
126 stopWatch.getTime() );
127 return indexingContext;
129 catch ( IOException e )
131 throw new IndexMergerException( e.getMessage(), e );
133 catch ( UnsupportedExistingLuceneIndexException e )
135 throw new IndexMergerException( e.getMessage(), e );
139 runningGroups.remove( groupId );
145 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
147 if ( temporaryGroupIndex == null )
154 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
155 if ( indexingContext != null )
157 indexer.removeIndexingContext( indexingContext, true );
159 File directory = temporaryGroupIndex.getDirectory();
160 if ( directory != null && directory.exists() )
162 FileUtils.deleteDirectory( directory );
164 temporaryGroupIndexes.remove( temporaryGroupIndex );
166 catch ( IOException e )
168 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
173 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
175 return this.temporaryGroupIndexes;