From 93e5fdfa2780f2790fb3f4cad3929d18754bf9db Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 2 Nov 2011 22:33:35 +0000 Subject: [PATCH] [MRM-815] aggregate indices for repository groups. cleanup created temp directories. git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196846 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva-base/archiva-indexer/pom.xml | 14 ++++ .../indexer/merger/DefaultIndexMerger.java | 72 ++++++++++++++++++- .../resources/META-INF/spring-context.xml | 16 ++++- .../archiva-webapp/src/test/tomcat/log4j.xml | 4 ++ pom.xml | 11 +++ 5 files changed, 112 insertions(+), 5 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-indexer/pom.xml b/archiva-modules/archiva-base/archiva-indexer/pom.xml index 0d5b87a4f..2aafd9402 100644 --- a/archiva-modules/archiva-base/archiva-indexer/pom.xml +++ b/archiva-modules/archiva-base/archiva-indexer/pom.xml @@ -33,6 +33,20 @@ org.apache.archiva archiva-repository-admin-api + + org.apache.archiva + archiva-common + + + + org.springframework + spring-context + + + org.springframework + spring-context-support + + org.apache.lucene lucene-core diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/merger/DefaultIndexMerger.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/merger/DefaultIndexMerger.java index 7a29fb76f..1943e6cc7 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/merger/DefaultIndexMerger.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/merger/DefaultIndexMerger.java @@ -19,21 +19,28 @@ package org.apache.archiva.indexer.merger; */ import com.google.common.io.Files; -import com.google.inject.Inject; import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; import org.apache.archiva.common.plexusbridge.MavenIndexerUtils; import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; +import org.apache.commons.io.FileUtils; import org.apache.maven.index.NexusIndexer; import org.apache.maven.index.context.IndexingContext; import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException; import org.apache.maven.index.packer.IndexPacker; import org.apache.maven.index.packer.IndexPackingRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import javax.inject.Inject; import java.io.File; import java.io.IOException; import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; /** * @author Olivier Lamy @@ -44,6 +51,8 @@ public class DefaultIndexMerger implements IndexMerger { + private Logger log = LoggerFactory.getLogger( getClass() ); + @Inject private ManagedRepositoryAdmin managedRepositoryAdmin; @@ -53,7 +62,9 @@ public class DefaultIndexMerger private IndexPacker indexPacker; - @javax.inject.Inject + private List temporaryIndexes = new CopyOnWriteArrayList(); + + @Inject public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils ) throws PlexusSisuBridgeException { @@ -93,6 +104,7 @@ public class DefaultIndexMerger IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation ); indexPacker.packIndex( request ); } + temporaryIndexes.add( new TemporaryIndex( tempRepoFile ) ); return indexingContext.getIndexDirectoryFile(); } catch ( IOException e ) @@ -104,4 +116,60 @@ public class DefaultIndexMerger throw new IndexMergerException( e.getMessage(), e ); } } + + + @Scheduled( fixedDelay = 900000 ) + public void cleanTemporaryIndex() + { + for ( TemporaryIndex temporaryIndex : temporaryIndexes ) + { + // cleanup files older than 30 minutes + if ( new Date().getTime() - temporaryIndex.creationTime > 1800000 ) + { + try + { + FileUtils.deleteDirectory( temporaryIndex.directory ); + temporaryIndexes.remove( temporaryIndex ); + log.debug( "remove directory {}", temporaryIndex.directory ); + } + catch ( IOException e ) + { + log.warn( "failed to remove directory:" + temporaryIndex.directory, e ); + } + } + temporaryIndexes.remove( temporaryIndex ); + } + } + + private static class TemporaryIndex + { + private long creationTime = new Date().getTime(); + + private File directory; + + TemporaryIndex( File directory ) + { + this.directory = directory; + } + + @Override + public int hashCode() + { + return Long.toString( creationTime ).hashCode(); + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof TemporaryIndex ) ) + { + return false; + } + return this.creationTime == ( (TemporaryIndex) o ).creationTime; + } + } } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml index e23b9807e..bf5c7ba89 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml @@ -22,17 +22,27 @@ + http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/task + http://www.springframework.org/schema/task/spring-task-3.0.xsd" + default-lazy-init="false"> - org.sonatype.nexus.index.DefaultNexusIndexer + + org.apache.maven.index.DefaultNexusIndexer + + + + + + \ No newline at end of file diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/tomcat/log4j.xml b/archiva-modules/archiva-web/archiva-webapp/src/test/tomcat/log4j.xml index 03f6171ff..11476e0f5 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/tomcat/log4j.xml +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/tomcat/log4j.xml @@ -73,6 +73,10 @@ + + + + diff --git a/pom.xml b/pom.xml index c1ebe3fe9..d2ad00349 100644 --- a/pom.xml +++ b/pom.xml @@ -1221,6 +1221,17 @@ + + org.springframework + spring-context-support + ${spring.version} + + + commons-logging + commons-logging + + + org.springframework spring-web -- 2.39.5