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.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.RepositoryGroup;
25 import org.apache.archiva.admin.model.group.RepositoryGroupAdmin;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.commons.lang.time.StopWatch;
33 import org.apache.maven.index.NexusIndexer;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
36 import org.apache.maven.index.packer.IndexPacker;
37 import org.apache.maven.index.packer.IndexPackingRequest;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.scheduling.annotation.Async;
41 import org.springframework.stereotype.Service;
43 import javax.inject.Inject;
45 import java.io.IOException;
46 import java.util.Collection;
47 import java.util.List;
48 import java.util.concurrent.CopyOnWriteArrayList;
51 * @author Olivier Lamy
54 @Service("indexMerger#default")
55 public class DefaultIndexMerger
56 implements IndexMerger
59 private Logger log = LoggerFactory.getLogger( getClass() );
61 private MavenIndexerUtils mavenIndexerUtils;
63 private NexusIndexer indexer;
65 private IndexPacker indexPacker;
67 private List<TemporaryGroupIndex> temporaryGroupIndexes = new CopyOnWriteArrayList<TemporaryGroupIndex>();
69 private List<String> runningGroups = new CopyOnWriteArrayList<String>();
72 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
73 throws PlexusSisuBridgeException
75 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
76 this.mavenIndexerUtils = mavenIndexerUtils;
77 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
80 public IndexingContext buildMergedIndex( IndexMergerRequest indexMergerRequest )
81 throws IndexMergerException
83 String groupId = indexMergerRequest.getGroupId();
85 if ( runningGroups.contains( groupId ) )
87 log.info( "skip build merge remote indexes for id: '{}' as already running", groupId );
91 runningGroups.add( groupId );
93 StopWatch stopWatch = new StopWatch();
97 File mergedIndexDirectory = indexMergerRequest.getMergedIndexDirectory();
99 String tempRepoId = mergedIndexDirectory.getName();
103 File indexLocation = new File( mergedIndexDirectory, indexMergerRequest.getMergedIndexPath() );
104 IndexingContext indexingContext =
105 indexer.addIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory, indexLocation, null, null,
106 mavenIndexerUtils.getAllIndexCreators() );
108 for ( String repoId : indexMergerRequest.getRepositoriesIds() )
110 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
111 if ( idxToMerge != null )
113 indexingContext.merge( idxToMerge.getIndexDirectory() );
117 indexingContext.optimize();
119 if ( indexMergerRequest.isPackIndex() )
121 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
122 indexPacker.packIndex( request );
125 if ( indexMergerRequest.isTemporary() )
127 temporaryGroupIndexes.add( new TemporaryGroupIndex( mergedIndexDirectory, tempRepoId, groupId,
128 indexMergerRequest.getMergedIndexTtl() ) );
131 log.info( "merged index for repos {} in {} s", indexMergerRequest.getRepositoriesIds(),
132 stopWatch.getTime() );
133 return indexingContext;
135 catch ( IOException e )
137 throw new IndexMergerException( e.getMessage(), e );
139 catch ( UnsupportedExistingLuceneIndexException e )
141 throw new IndexMergerException( e.getMessage(), e );
145 runningGroups.remove( groupId );
150 public void cleanTemporaryGroupIndex( TemporaryGroupIndex temporaryGroupIndex )
152 if ( temporaryGroupIndex == null )
159 IndexingContext indexingContext = indexer.getIndexingContexts().get( temporaryGroupIndex.getIndexId() );
160 if ( indexingContext != null )
162 indexer.removeIndexingContext( indexingContext, true );
164 File directory = temporaryGroupIndex.getDirectory();
165 if ( directory != null && directory.exists() )
167 FileUtils.deleteDirectory( directory );
169 temporaryGroupIndexes.remove( temporaryGroupIndex );
171 catch ( IOException e )
173 log.warn( "fail to delete temporary group index {}", temporaryGroupIndex.getIndexId(), e );
177 public Collection<TemporaryGroupIndex> getTemporaryGroupIndexes()
179 return this.temporaryGroupIndexes;