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.Scheduled;
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.Date;
42 import java.util.List;
43 import java.util.concurrent.CopyOnWriteArrayList;
46 * @author Olivier Lamy
49 @Service( "indexMerger#default" )
50 public class DefaultIndexMerger
51 implements IndexMerger
54 private Logger log = LoggerFactory.getLogger( getClass() );
57 private ManagedRepositoryAdmin managedRepositoryAdmin;
59 private MavenIndexerUtils mavenIndexerUtils;
61 private NexusIndexer indexer;
63 private IndexPacker indexPacker;
65 private List<TemporaryIndex> temporaryIndexes = new CopyOnWriteArrayList<TemporaryIndex>();
68 public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
69 throws PlexusSisuBridgeException
71 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
72 this.mavenIndexerUtils = mavenIndexerUtils;
73 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
76 public File buildMergedIndex( Collection<String> repositoriesIds, boolean packIndex )
77 throws IndexMergerException
79 File tempRepoFile = Files.createTempDir();
80 tempRepoFile.deleteOnExit();
82 String tempRepoId = tempRepoFile.getName();
86 File indexLocation = new File( tempRepoFile, ".indexer" );
87 IndexingContext indexingContext =
88 indexer.addIndexingContext( tempRepoId, tempRepoId, tempRepoFile, indexLocation, null, null,
89 mavenIndexerUtils.getAllIndexCreators() );
91 for ( String repoId : repositoriesIds )
93 IndexingContext idxToMerge = indexer.getIndexingContexts().get( repoId );
94 if ( idxToMerge != null )
96 indexingContext.merge( idxToMerge.getIndexDirectory() );
100 indexingContext.optimize();
104 IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
105 indexPacker.packIndex( request );
107 temporaryIndexes.add( new TemporaryIndex( tempRepoFile ) );
108 return indexingContext.getIndexDirectoryFile();
110 catch ( IOException e )
112 throw new IndexMergerException( e.getMessage(), e );
114 catch ( UnsupportedExistingLuceneIndexException e )
116 throw new IndexMergerException( e.getMessage(), e );
121 @Scheduled( fixedDelay = 900000 )
122 public void cleanTemporaryIndex()
124 for ( TemporaryIndex temporaryIndex : temporaryIndexes )
126 // cleanup files older than 30 minutes
127 if ( new Date().getTime() - temporaryIndex.creationTime > 1800000 )
131 FileUtils.deleteDirectory( temporaryIndex.directory );
132 temporaryIndexes.remove( temporaryIndex );
133 log.debug( "remove directory {}", temporaryIndex.directory );
135 catch ( IOException e )
137 log.warn( "failed to remove directory:" + temporaryIndex.directory, e );
140 temporaryIndexes.remove( temporaryIndex );
144 private static class TemporaryIndex
146 private long creationTime = new Date().getTime();
148 private File directory;
150 TemporaryIndex( File directory )
152 this.directory = directory;
156 public int hashCode()
158 return Long.toString( creationTime ).hashCode();
162 public boolean equals( Object o )
168 if ( !( o instanceof TemporaryIndex ) )
172 return this.creationTime == ( (TemporaryIndex) o ).creationTime;