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;
22 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
23 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.maven.index.NexusIndexer;
28 import org.apache.maven.index.context.IndexingContext;
29 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
30 import org.apache.maven.index.packer.IndexPacker;
31 import org.apache.maven.index.packer.IndexPackingRequest;
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;
39 import java.io.IOException;
40 import java.util.Collection;
41 import java.util.List;
42 import java.util.concurrent.CopyOnWriteArrayList;
45 * @author Olivier Lamy
48 @Service( "indexMerger#default" )
49 public class DefaultIndexMerger
50 implements IndexMerger
53 private Logger log = LoggerFactory.getLogger( getClass() );
56 private ManagedRepositoryAdmin managedRepositoryAdmin;
58 private MavenIndexerUtils mavenIndexerUtils;
60 private NexusIndexer indexer;
62 private IndexPacker indexPacker;
64 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<TemporaryGroupIndex>();
67 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
68 throws PlexusSisuBridgeException
70 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
71 this.mavenIndexerUtils = mavenIndexerUtils;
72 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
75 public IndexingContext buildMergedIndex( Collection<String> repositoriesIds, boolean packIndex )
76 throws IndexMergerException
78 File tempRepoFile = Files.createTempDir();
79 tempRepoFile.deleteOnExit();
81 String tempRepoId = tempRepoFile.getName();
85 File indexLocation = new File( tempRepoFile, ".indexer" );
86 IndexingContext indexingContext =
87 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
88 mavenIndexerUtils.getAllIndexCreators() );
90 for ( String repoId : repositoriesIds )
92 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
93 if ( idxToMerge != null )
95 indexingContext.merge( idxToMerge.getIndexDirectory() );
99 indexingContext.optimize();
103 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
104 indexPacker.packIndex( request );
106 temporaryGroupIndexes.add( new TemporaryGroupIndex( tempRepoFile, tempRepoId ) );
107 return indexingContext;
109 catch ( IOException e )
111 throw new IndexMergerException( e.getMessage(), e );
113 catch ( UnsupportedExistingLuceneIndexException e )
115 throw new IndexMergerException( e.getMessage(), e );
120 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
122 if ( temporaryGroupIndex == null )
129 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
130 if ( indexingContext != null )
132 indexer.removeIndexingContext( indexingContext, true );
134 File directory = temporaryGroupIndex.getDirectory();
135 if ( directory != null && directory.exists() )
137 FileUtils.deleteDirectory( directory );
139 temporaryGroupIndexes.remove( temporaryGroupIndex );
141 catch ( IOException e )
143 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
147 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
149 return this.temporaryGroupIndexes;