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 com.google.inject.Inject;
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.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.springframework.stereotype.Service;
35 import java.io.IOException;
36 import java.util.Collection;
39 * @author Olivier Lamy
42 @Service( "indexMerger#default" )
43 public class DefaultIndexMerger
44 implements IndexMerger
48 private ManagedRepositoryAdmin managedRepositoryAdmin;
50 private MavenIndexerUtils mavenIndexerUtils;
52 private NexusIndexer indexer;
54 private IndexPacker indexPacker;
57 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
58 throws PlexusSisuBridgeException
60 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
61 this.mavenIndexerUtils = mavenIndexerUtils;
62 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
65 public File buildMergedIndex( Collection<String> repositoriesIds, boolean packIndex )
66 throws IndexMergerException
68 File tempRepoFile = Files.createTempDir();
69 tempRepoFile.deleteOnExit();
71 String tempRepoId = tempRepoFile.getName();
75 File indexLocation = new File( tempRepoFile, ".indexer" );
76 IndexingContext indexingContext =
77 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
78 mavenIndexerUtils.getAllIndexCreators() );
80 for ( String repoId : repositoriesIds )
82 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
83 if ( idxToMerge != null )
85 indexingContext.merge( idxToMerge.getIndexDirectory() );
89 indexingContext.optimize();
93 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
94 indexPacker.packIndex( request );
96 return indexingContext.getIndexDirectoryFile();
98 catch ( IOException e )
100 throw new IndexMergerException( e.getMessage(), e );
102 catch ( UnsupportedExistingLuceneIndexException e )
104 throw new IndexMergerException( e.getMessage(), e );