]> source.dussan.org Git - archiva.git/commitdiff
[MRM-815] aggregate indices for repository groups.
authorOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 22:33:35 +0000 (22:33 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 22:33:35 +0000 (22:33 +0000)
cleanup created temp directories.

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196846 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-indexer/pom.xml
archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/merger/DefaultIndexMerger.java
archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/spring-context.xml
archiva-modules/archiva-web/archiva-webapp/src/test/tomcat/log4j.xml
pom.xml

index 0d5b87a4fc9c924bce70ed680bb637b26c923b4b..2aafd9402b452c8ebc10a4132fa992df82bd068d 100644 (file)
       <groupId>org.apache.archiva</groupId>
       <artifactId>archiva-repository-admin-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.archiva</groupId>
+      <artifactId>archiva-common</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-context</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-context-support</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>org.apache.lucene</groupId>
       <artifactId>lucene-core</artifactId>
index 7a29fb76fb8c7a7cef011e94275e642580f82abf..1943e6cc7a54e86a4d1d3d4c697b1d29504d2952 100644 (file)
@@ -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<TemporaryIndex> temporaryIndexes = new CopyOnWriteArrayList<TemporaryIndex>();
+
+    @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;
+        }
+    }
 }
index e23b9807e03c5e94d79d6048c945140af7342fd3..bf5c7ba89c69699988ccf704538e6da135323217 100644 (file)
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
-           http://www.springframework.org/schema/context/spring-context-3.0.xsd"
-       default-lazy-init="true">
+           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">
 
   <context:annotation-config/>
   <context:component-scan base-package="org.apache.archiva.indexer.search,org.apache.archiva.indexer.merger"/>
 
   <bean id="logger" class="org.apache.archiva.common.utils.Slf4JPlexusLogger">
-    <constructor-arg type="java.lang.Class"><value>org.sonatype.nexus.index.DefaultNexusIndexer</value></constructor-arg>
+    <constructor-arg type="java.lang.Class">
+      <value>org.apache.maven.index.DefaultNexusIndexer</value>
+    </constructor-arg>
   </bean>
 
+
+  <task:executor id="springExecutor" pool-size="2"/>
+  <task:scheduler id="springScheduler" pool-size="2"/>
+  <task:annotation-driven executor="springExecutor" scheduler="springScheduler"/>
+
 </beans>
\ No newline at end of file
index 03f6171ffd3436305b01dc57055d51941df06f7e..11476e0f5fa854e2884c38561e675c8a5b2f87f0 100644 (file)
     <level value="info"/>
   </logger>
 
+  <logger name="org.apache.archiva.indexer.merger">
+    <level value="debug"/>
+  </logger>
+
   <root>
     <priority value ="info" />
     <appender-ref ref="console" />
diff --git a/pom.xml b/pom.xml
index c1ebe3fe9764ed789e76749fa6524d7d073feca2..d2ad003490bd419e04186131b20bb4b3a4af715e 100644 (file)
--- a/pom.xml
+++ b/pom.xml
           </exclusion>
         </exclusions>
       </dependency>
+      <dependency>
+        <groupId>org.springframework</groupId>
+        <artifactId>spring-context-support</artifactId>
+        <version>${spring.version}</version>
+        <exclusions>
+          <exclusion>
+            <groupId>commons-logging</groupId>
+            <artifactId>commons-logging</artifactId>
+          </exclusion>
+        </exclusions>
+      </dependency>
       <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>