*/
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
implements IndexMerger
{
+ private Logger log = LoggerFactory.getLogger( getClass() );
+
@Inject
private ManagedRepositoryAdmin managedRepositoryAdmin;
private IndexPacker indexPacker;
- @javax.inject.Inject
+ private List<TemporaryIndex> temporaryIndexes = new CopyOnWriteArrayList<TemporaryIndex>();
+
+ @Inject
public DefaultIndexMerger( PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
throws PlexusSisuBridgeException
{
IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
indexPacker.packIndex( request );
}
+ temporaryIndexes.add( new TemporaryIndex( tempRepoFile ) );
return indexingContext.getIndexDirectoryFile();
}
catch ( IOException e )
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;
+ }
+ }
}
<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