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 com.google.common.io.Files;
23 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.time.StopWatch;
29 import org.apache.maven.index.NexusIndexer;
30 import org.apache.maven.index.context.IndexingContext;
31 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
32 import org.apache.maven.index.packer.IndexPacker;
33 import org.apache.maven.index.packer.IndexPackingRequest;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.scheduling.annotation.Async;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
41 import java.io.IOException;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.concurrent.CopyOnWriteArrayList;
47 * @author Olivier Lamy
50 @Service("indexMerger#default")
51 public class DefaultIndexMerger
52 implements IndexMerger
55 private Logger log = LoggerFactory.getLogger( getClass() );
58 private ManagedRepositoryAdmin managedRepositoryAdmin;
60 private MavenIndexerUtils mavenIndexerUtils;
62 private NexusIndexer indexer;
64 private IndexPacker indexPacker;
66 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<TemporaryGroupIndex>();
68 private List<String> runningGroups = new CopyOnWriteArrayList<String>();
71 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
72 throws PlexusSisuBridgeException
74 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
75 this.mavenIndexerUtils = mavenIndexerUtils;
76 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
79 public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
80 throws IndexMergerException
82 String groupId = indexMergerRequest.getGroupId();
84 if ( runningGroups.contains( groupId ) )
86 log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
90 runningGroups.add( groupId );
92 StopWatch stopWatch = new StopWatch();
96 File mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
98 String tempRepoId = mergedIndexDirectory.getName();
102 File indexLocation = new File( mergedIndexDirectory, indexMergerRequest.getMergedIndexPath() );
103 IndexingContext indexingContext =
104 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory, indexLocation, null, null,
105 mavenIndexerUtils.getAllIndexCreators() );
107 for ( String repoId : indexMergerRequest.getRepositoriesIds() )
109 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
110 if ( idxToMerge != null )
112 indexingContext.merge( idxToMerge.getIndexDirectory() );
116 indexingContext.optimize();
118 if ( indexMergerRequest.isPackIndex() )
120 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
121 indexPacker.packIndex( request );
123 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
124 indexMergerRequest.getMergedIndexTtl() ) );
126 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
127 stopWatch.getTime() );
128 return indexingContext;
130 catch ( IOException e )
132 throw new IndexMergerException( e.getMessage(), e );
134 catch ( UnsupportedExistingLuceneIndexException e )
136 throw new IndexMergerException( e.getMessage(), e );
140 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 );
172 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
174 return this.temporaryGroupIndexes;