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.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() );
55 private ManagedRepositoryAdmin managedRepositoryAdmin;
57 private MavenIndexerUtils mavenIndexerUtils;
59 private NexusIndexer indexer;
61 private IndexPacker indexPacker;
63 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<TemporaryGroupIndex>();
66 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
67 throws PlexusSisuBridgeException
69 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
70 this.mavenIndexerUtils = mavenIndexerUtils;
71 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
74 public IndexingContext buildMergedIndex( Collection<String> repositoriesIds, boolean packIndex )
75 throws IndexMergerException
77 File tempRepoFile = Files.createTempDir();
78 tempRepoFile.deleteOnExit();
80 String tempRepoId = tempRepoFile.getName();
84 File indexLocation = new File( tempRepoFile, ".indexer" );
85 IndexingContext indexingContext =
86 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
87 mavenIndexerUtils.getAllIndexCreators() );
89 for ( String repoId : repositoriesIds )
91 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
92 if ( idxToMerge != null )
94 indexingContext.merge( idxToMerge.getIndexDirectory() );
98 indexingContext.optimize();
102 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
103 indexPacker.packIndex( request );
105 temporaryGroupIndexes.add( new TemporaryGroupIndex( tempRepoFile, tempRepoId ) );
106 return indexingContext;
108 catch ( IOException e )
110 throw new IndexMergerException( e.getMessage(), e );
112 catch ( UnsupportedExistingLuceneIndexException e )
114 throw new IndexMergerException( e.getMessage(), e );
119 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
121 if ( temporaryGroupIndex == null )
128 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
129 if ( indexingContext != null )
131 indexer.removeIndexingContext( indexingContext, true );
134 catch ( IOException e )
136 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
140 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
142 return this.temporaryGroupIndexes;