add listener for downloading

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1539523 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2013-11-07 05:22:45 +00:00
parent 99aa5b7535
commit db38728d12
2 changed files with 127 additions and 32 deletions

View File

@ -26,6 +26,7 @@ import org.apache.archiva.proxy.common.WagonFactory;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.StopWatch; import org.apache.commons.lang.time.StopWatch;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException; import org.apache.http.HttpException;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
@ -39,6 +40,7 @@ import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.codecs.LengthDelimitedDecoder;
import org.apache.http.nio.ContentDecoder; import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.ContentEncoder; import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.IOControl; import org.apache.http.nio.IOControl;
@ -49,8 +51,6 @@ import org.apache.maven.index.context.IndexingContext;
import org.apache.maven.index.updater.IndexUpdateRequest; import org.apache.maven.index.updater.IndexUpdateRequest;
import org.apache.maven.index.updater.IndexUpdater; import org.apache.maven.index.updater.IndexUpdater;
import org.apache.maven.index.updater.ResourceFetcher; import org.apache.maven.index.updater.ResourceFetcher;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -59,6 +59,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
@ -138,6 +139,9 @@ public class DownloadRemoteIndexTask
tempIndexDirectory.mkdirs(); tempIndexDirectory.mkdirs();
tempIndexDirectory.deleteOnExit(); tempIndexDirectory.deleteOnExit();
String baseIndexUrl = indexingContext.getIndexUpdateUrl(); String baseIndexUrl = indexingContext.getIndexUpdateUrl();
URL indexUrl = new URL( baseIndexUrl );
/* /*
String wagonProtocol = new URL( this.remoteRepository.getUrl() ).getProtocol(); String wagonProtocol = new URL( this.remoteRepository.getUrl() ).getProtocol();
@ -183,19 +187,34 @@ public class DownloadRemoteIndexTask
HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create(); HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create();
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
if ( this.networkProxy != null ) if ( this.networkProxy != null )
{ {
HttpHost httpHost = new HttpHost( this.networkProxy.getHost(), this.networkProxy.getPort() ); HttpHost httpHost = new HttpHost( this.networkProxy.getHost(), this.networkProxy.getPort() );
builder = builder.setProxy( httpHost ); builder = builder.setProxy( httpHost );
if ( this.networkProxy.getUsername() != null )
{
basicCredentialsProvider.setCredentials(
new AuthScope( this.networkProxy.getHost(), this.networkProxy.getPort(), null, null ),
new UsernamePasswordCredentials( this.networkProxy.getUsername(),
this.networkProxy.getPassword() ) );
}
} }
if ( this.remoteRepository.getUserName() != null ) if ( this.remoteRepository.getUserName() != null )
{ {
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); basicCredentialsProvider.setCredentials(
basicCredentialsProvider.setCredentials( AuthScope.ANY, new UsernamePasswordCredentials( new AuthScope( indexUrl.getHost(), indexUrl.getPort(), null, null ),
this.remoteRepository.getUserName(), this.remoteRepository.getPassword() ) ); new UsernamePasswordCredentials( this.remoteRepository.getUserName(),
this.remoteRepository.getPassword() ) );
} }
builder = builder.setDefaultCredentialsProvider( basicCredentialsProvider );
File indexDirectory = indexingContext.getIndexDirectoryFile(); File indexDirectory = indexingContext.getIndexDirectoryFile();
if ( !indexDirectory.exists() ) if ( !indexDirectory.exists() )
{ {
@ -259,7 +278,7 @@ public class DownloadRemoteIndexTask
} }
} }
/*
private static final class DownloadListener private static final class DownloadListener
implements TransferListener implements TransferListener
{ {
@ -310,6 +329,96 @@ public class DownloadRemoteIndexTask
log.debug( "transfer debug {}", message ); log.debug( "transfer debug {}", message );
} }
} }
*/
private static class ZeroCopyConsumerListener
extends ZeroCopyConsumer
{
private Logger log = LoggerFactory.getLogger( getClass() );
private String resourceName;
private long startTime;
private long totalLength = 0;
//private long currentLength = 0;
private ZeroCopyConsumerListener( File file, String resourceName )
throws FileNotFoundException
{
super( file );
this.resourceName = resourceName;
}
@Override
protected File process( final HttpResponse response, final File file, final ContentType contentType )
throws Exception
{
if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_OK )
{
throw new ClientProtocolException( "Upload failed: " + response.getStatusLine() );
}
long endTime = System.currentTimeMillis();
log.info( "end of transfer file {} {} kb: {}s", resourceName, this.totalLength / 1024,
( endTime - startTime ) / 1000 );
return file;
}
@Override
protected void onContentReceived( ContentDecoder decoder, IOControl ioControl )
throws IOException
{
if ( decoder instanceof LengthDelimitedDecoder )
{
LengthDelimitedDecoder ldl = LengthDelimitedDecoder.class.cast( decoder );
long len = getLen( ldl );
if ( len > -1 )
{
log.debug( "transfer of {} : {}/{}", resourceName, len / 1024, this.totalLength / 1024 );
}
}
super.onContentReceived( decoder, ioControl );
}
@Override
protected void onResponseReceived( HttpResponse response )
{
this.startTime = System.currentTimeMillis();
super.onResponseReceived( response );
this.totalLength = response.getEntity().getContentLength();
log.info( "start transfer of {}, contentLength: {}", resourceName, this.totalLength );
}
@Override
protected void onEntityEnclosed( HttpEntity entity, ContentType contentType )
throws IOException
{
super.onEntityEnclosed( entity, contentType );
}
private long getLen( LengthDelimitedDecoder ldl )
{
try
{
Field lenField = LengthDelimitedDecoder.class.getDeclaredField( "len" );
lenField.setAccessible( true );
long len = (Long) lenField.get( ldl );
return len;
}
catch ( NoSuchFieldException e )
{
log.debug( e.getMessage(), e );
return -1;
}
catch ( IllegalAccessException e )
{
log.debug( e.getMessage(), e );
return -1;
}
}
}
private static class ZeroCopyResourceFetcher private static class ZeroCopyResourceFetcher
implements ResourceFetcher implements ResourceFetcher
@ -348,7 +457,7 @@ public class DownloadRemoteIndexTask
} }
public InputStream retrieve( final String name ) public InputStream retrieve( final String name )
throws IOException, FileNotFoundException throws IOException
{ {
log.info( "index update retrieve file, name:{}", name ); log.info( "index update retrieve file, name:{}", name );
@ -359,29 +468,9 @@ public class DownloadRemoteIndexTask
} }
file.deleteOnExit(); file.deleteOnExit();
ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>( file ) ZeroCopyConsumer<File> consumer = new ZeroCopyConsumerListener( file, name );
{
@Override URL targetUrl = new URL( this.baseIndexUrl );
protected File process( final HttpResponse response, final File file, final ContentType contentType )
throws Exception
{
if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_OK )
{
throw new ClientProtocolException( "Upload failed: " + response.getStatusLine() );
}
return file;
}
@Override
protected void onContentReceived( ContentDecoder decoder, IOControl ioctrl )
throws IOException
{
log.debug( "onContentReceived" );
super.onContentReceived( decoder, ioctrl );
}
};
URL targetUrl = new URL( this.remoteRepository.getUrl() );
final HttpHost targetHost = new HttpHost( targetUrl.getHost(), targetUrl.getPort() ); final HttpHost targetHost = new HttpHost( targetUrl.getHost(), targetUrl.getPort() );
Future<File> httpResponseFuture = httpclient.execute( new HttpAsyncRequestProducer() Future<File> httpResponseFuture = httpclient.execute( new HttpAsyncRequestProducer()
@ -415,7 +504,7 @@ public class DownloadRemoteIndexTask
@Override @Override
public void requestCompleted( HttpContext context ) public void requestCompleted( HttpContext context )
{ {
// no op log.debug( "requestCompleted" );
} }
@Override @Override
@ -427,6 +516,7 @@ public class DownloadRemoteIndexTask
@Override @Override
public boolean isRepeatable() public boolean isRepeatable()
{ {
log.debug( "isRepeatable" );
return true; return true;
} }
@ -434,15 +524,16 @@ public class DownloadRemoteIndexTask
public void resetRequest() public void resetRequest()
throws IOException throws IOException
{ {
// no op log.debug( "resetRequest" );
} }
@Override @Override
public void close() public void close()
throws IOException throws IOException
{ {
// no op log.debug( "close" );
} }
}, consumer, null ); }, consumer, null );
try try
{ {

View File

@ -82,6 +82,10 @@
<asyncLogger name="org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry" level="debug"/> <asyncLogger name="org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry" level="debug"/>
<!--
<logger name="org.apache.archiva.scheduler.indexing" level="debug"/>
-->
<asyncRoot level="info" includeLocation="true"> <asyncRoot level="info" includeLocation="true">
<appender-ref ref="console"/> <appender-ref ref="console"/>
<appender-ref ref="rolling"/> <appender-ref ref="rolling"/>