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>();
69 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
70 throws PlexusSisuBridgeException
72 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
73 this.mavenIndexerUtils = mavenIndexerUtils;
74 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
77 public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
78 throws IndexMergerException
80 StopWatch stopWatch = new StopWatch();
84 File tempRepoFile = indexMergerRequest.getMergedIndexDirectory();
86 String tempRepoId = tempRepoFile.getName();
90 File indexLocation = new File( tempRepoFile, indexMergerRequest.getMergedIndexPath() );
91 IndexingContext indexingContext =
92 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
93 mavenIndexerUtils.getAllIndexCreators() );
95 for ( String repoId : indexMergerRequest.getRepositoriesIds() )
97 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
98 if ( idxToMerge != null )
100 indexingContext.merge( idxToMerge.getIndexDirectory() );
104 indexingContext.optimize();
106 if ( indexMergerRequest.isPackIndex() )
108 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
109 indexPacker.packIndex( request );
111 temporaryGroupIndexes.add(
112 new TemporaryGroupIndex( tempRepoFile, tempRepoId, indexMergerRequest.getGroupId(), indexMergerRequest.getMergedIndexTtl() ) );
114 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
115 stopWatch.getTime() );
116 return indexingContext;
118 catch ( IOException e )
120 throw new IndexMergerException( e.getMessage(), e );
122 catch ( UnsupportedExistingLuceneIndexException e )
124 throw new IndexMergerException( e.getMessage(), e );
129 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
131 if ( temporaryGroupIndex == null )
138 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
139 if ( indexingContext != null )
141 indexer.removeIndexingContext( indexingContext, true );
143 File directory = temporaryGroupIndex.getDirectory();
144 if ( directory != null && directory.exists() )
146 FileUtils.deleteDirectory( directory );
148 temporaryGroupIndexes.remove( temporaryGroupIndex );
150 catch ( IOException e )
152 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
156 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
158 return this.temporaryGroupIndexes;