From c215c386165bc32df81033508598a56032cf90aa Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 3 Nov 2011 14:30:03 +0000 Subject: [PATCH] fix unit tests in archiva-scheduler-indexing git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1197140 13f79535-47bb-0310-9956-ffa450edef68 --- .../DefaultDownloadRemoteIndexScheduler.java | 4 +- .../indexing/DownloadRemoteIndexTask.java | 259 ++++++++++-------- .../DownloadRemoteIndexTaskRequest.java | 24 +- 3 files changed, 155 insertions(+), 132 deletions(-) diff --git a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DefaultDownloadRemoteIndexScheduler.java b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DefaultDownloadRemoteIndexScheduler.java index 83439050b..b00332171 100644 --- a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DefaultDownloadRemoteIndexScheduler.java +++ b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DefaultDownloadRemoteIndexScheduler.java @@ -175,8 +175,8 @@ public class DefaultDownloadRemoteIndexScheduler DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest = new DownloadRemoteIndexTaskRequest().setRemoteRepository( remoteRepository ).setNetworkProxy( - networkProxy ).setFullDownload( fullDownload ).setWagonFactory( wagonFactory ).setNexusIndexer( - nexusIndexer ).setIndexUpdater( indexUpdater ); + networkProxy ).setFullDownload( fullDownload ).setWagonFactory( + wagonFactory ).setRemoteRepositoryAdmin( remoteRepositoryAdmin ).setIndexUpdater( indexUpdater ); if ( now ) { diff --git a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTask.java b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTask.java index 2c25468fd..e41caa6bc 100644 --- a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTask.java +++ b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTask.java @@ -18,13 +18,14 @@ package org.apache.archiva.scheduler.indexing; * under the License. */ +import org.apache.archiva.admin.model.RepositoryAdminException; import org.apache.archiva.admin.model.beans.NetworkProxy; import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin; import org.apache.archiva.proxy.common.WagonFactory; import org.apache.archiva.proxy.common.WagonFactoryException; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.time.StopWatch; -import org.apache.maven.index.NexusIndexer; import org.apache.maven.index.context.IndexingContext; import org.apache.maven.index.updater.IndexUpdateRequest; import org.apache.maven.index.updater.IndexUpdater; @@ -63,11 +64,11 @@ import java.util.List; public class DownloadRemoteIndexTask implements Runnable { - private Logger log = LoggerFactory.getLogger( getClass( ) ); + private Logger log = LoggerFactory.getLogger( getClass() ); private RemoteRepository remoteRepository; - private NexusIndexer nexusIndexer; + private RemoteRepositoryAdmin remoteRepositoryAdmin; private WagonFactory wagonFactory; @@ -82,166 +83,126 @@ public class DownloadRemoteIndexTask public DownloadRemoteIndexTask( DownloadRemoteIndexTaskRequest downloadRemoteIndexTaskRequest, List runningRemoteDownloadIds ) { - this.remoteRepository = downloadRemoteIndexTaskRequest.getRemoteRepository( ); - this.nexusIndexer = downloadRemoteIndexTaskRequest.getNexusIndexer( ); - this.wagonFactory = downloadRemoteIndexTaskRequest.getWagonFactory( ); - this.networkProxy = downloadRemoteIndexTaskRequest.getNetworkProxy( ); - this.fullDownload = downloadRemoteIndexTaskRequest.isFullDownload( ); + this.remoteRepository = downloadRemoteIndexTaskRequest.getRemoteRepository(); + this.wagonFactory = downloadRemoteIndexTaskRequest.getWagonFactory(); + this.networkProxy = downloadRemoteIndexTaskRequest.getNetworkProxy(); + this.fullDownload = downloadRemoteIndexTaskRequest.isFullDownload(); this.runningRemoteDownloadIds = runningRemoteDownloadIds; - this.indexUpdater = downloadRemoteIndexTaskRequest.getIndexUpdater( ); + this.indexUpdater = downloadRemoteIndexTaskRequest.getIndexUpdater(); + this.remoteRepositoryAdmin = downloadRemoteIndexTaskRequest.getRemoteRepositoryAdmin(); } - public void run( ) + public void run() { // so short lock : not sure we need it synchronized ( this.runningRemoteDownloadIds ) { - if ( this.runningRemoteDownloadIds.contains( this.remoteRepository.getId( ) ) ) + if ( this.runningRemoteDownloadIds.contains( this.remoteRepository.getId() ) ) { // skip it as it's running log.info( "skip download index remote for repo {} it's already running", - this.remoteRepository.getId( ) ); + this.remoteRepository.getId() ); return; } - this.runningRemoteDownloadIds.add( this.remoteRepository.getId( ) ); + this.runningRemoteDownloadIds.add( this.remoteRepository.getId() ); } - StopWatch stopWatch = new StopWatch( ); - stopWatch.start( ); - log.info( "start download remote index for remote repository " + this.remoteRepository.getId( ) ); - IndexingContext indexingContext = - nexusIndexer.getIndexingContexts( ).get( "remote-" + remoteRepository.getId( ) ); - - // TODO check if null ? normally not as created by DefaultDownloadRemoteIndexScheduler#startup - - // create a temp directory to download files - final File tempIndexDirectory = new File( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" ); - File indexCacheDirectory = new File( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" ); - indexCacheDirectory.mkdirs(); + File tempIndexDirectory = null; + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); try { - if ( tempIndexDirectory.exists( ) ) + log.info( "start download remote index for remote repository " + this.remoteRepository.getId() ); + IndexingContext indexingContext = remoteRepositoryAdmin.createIndexContext( this.remoteRepository ); + + // create a temp directory to download files + tempIndexDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex" ); + File indexCacheDirectory = new File( indexingContext.getIndexDirectoryFile().getParent(), ".indexCache" ); + indexCacheDirectory.mkdirs(); + if ( tempIndexDirectory.exists() ) { FileUtils.deleteDirectory( tempIndexDirectory ); } - tempIndexDirectory.mkdirs( ); + tempIndexDirectory.mkdirs(); tempIndexDirectory.deleteOnExit(); - String baseIndexUrl = indexingContext.getIndexUpdateUrl( ); + String baseIndexUrl = indexingContext.getIndexUpdateUrl(); - final Wagon wagon = wagonFactory.getWagon( new URL( this.remoteRepository.getUrl( ) ).getProtocol( ) ); + final Wagon wagon = wagonFactory.getWagon( new URL( this.remoteRepository.getUrl() ).getProtocol() ); setupWagonReadTimeout( wagon ); // TODO transferListener - wagon.addTransferListener( new DownloadListener( ) ); + wagon.addTransferListener( new DownloadListener() ); ProxyInfo proxyInfo = null; if ( this.networkProxy != null ) { - proxyInfo = new ProxyInfo( ); - proxyInfo.setHost( this.networkProxy.getHost( ) ); - proxyInfo.setPort( this.networkProxy.getPort( ) ); - proxyInfo.setUserName( this.networkProxy.getUsername( ) ); - proxyInfo.setPassword( this.networkProxy.getPassword( ) ); + proxyInfo = new ProxyInfo(); + proxyInfo.setHost( this.networkProxy.getHost() ); + proxyInfo.setPort( this.networkProxy.getPort() ); + proxyInfo.setUserName( this.networkProxy.getUsername() ); + proxyInfo.setPassword( this.networkProxy.getPassword() ); } AuthenticationInfo authenticationInfo = null; - if ( this.remoteRepository.getUserName( ) != null ) + if ( this.remoteRepository.getUserName() != null ) { - authenticationInfo = new AuthenticationInfo( ); - authenticationInfo.setUserName( this.remoteRepository.getUserName( ) ); - authenticationInfo.setPassword( this.remoteRepository.getPassword( ) ); + authenticationInfo = new AuthenticationInfo(); + authenticationInfo.setUserName( this.remoteRepository.getUserName() ); + authenticationInfo.setPassword( this.remoteRepository.getPassword() ); } - wagon.connect( new Repository( this.remoteRepository.getId( ), baseIndexUrl ), authenticationInfo, + wagon.connect( new Repository( this.remoteRepository.getId(), baseIndexUrl ), authenticationInfo, proxyInfo ); - File indexDirectory = indexingContext.getIndexDirectoryFile( ); - if ( !indexDirectory.exists( ) ) + File indexDirectory = indexingContext.getIndexDirectoryFile(); + if ( !indexDirectory.exists() ) { - indexDirectory.mkdirs( ); + indexDirectory.mkdirs(); } - ResourceFetcher resourceFetcher = new ResourceFetcher( ) - { - public void connect( String id, String url ) - throws IOException - { - //no op - } - - public void disconnect( ) - throws IOException - { - // no op - } - - public InputStream retrieve( String name ) - throws IOException, FileNotFoundException - { - try - { - log.info( "index update retrieve file, name:{}", name ); - File file = new File( tempIndexDirectory, name ); - if ( file.exists( ) ) - { - file.delete( ); - } - file.deleteOnExit(); - wagon.get( name, file ); - return new FileInputStream( file ); - } - catch ( AuthorizationException e ) - { - throw new IOException( e.getMessage( ) ); - } - catch ( TransferFailedException e ) - { - throw new IOException( e.getMessage( ) ); - } - catch ( ResourceDoesNotExistException e ) - { - throw new FileNotFoundException( e.getMessage( ) ); - } - } - }; - + ResourceFetcher resourceFetcher = new WagonResourceFetcher( log, tempIndexDirectory, wagon ); IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher ); request.setForceFullUpdate( this.fullDownload ); request.setLocalIndexCacheDir( indexCacheDirectory ); this.indexUpdater.fetchAndUpdateIndex( request ); - stopWatch.stop( ); - log.info( "time to download remote repository index for repository {}: {} s", - this.remoteRepository.getId( ), ( stopWatch.getTime( ) / 1000 ) ); + stopWatch.stop(); + log.info( "time to download remote repository index for repository {}: {} s", this.remoteRepository.getId(), + ( stopWatch.getTime() / 1000 ) ); } catch ( MalformedURLException e ) { - log.error( e.getMessage( ), e ); - throw new RuntimeException( e.getMessage( ), e ); + log.error( e.getMessage(), e ); + throw new RuntimeException( e.getMessage(), e ); } catch ( WagonFactoryException e ) { - log.error( e.getMessage( ), e ); - throw new RuntimeException( e.getMessage( ), e ); + log.error( e.getMessage(), e ); + throw new RuntimeException( e.getMessage(), e ); } catch ( ConnectionException e ) { - log.error( e.getMessage( ), e ); - throw new RuntimeException( e.getMessage( ), e ); + log.error( e.getMessage(), e ); + throw new RuntimeException( e.getMessage(), e ); } catch ( AuthenticationException e ) { - log.error( e.getMessage( ), e ); - throw new RuntimeException( e.getMessage( ), e ); + log.error( e.getMessage(), e ); + throw new RuntimeException( e.getMessage(), e ); } catch ( IOException e ) { - log.error( e.getMessage( ), e ); - throw new RuntimeException( e.getMessage( ), e ); + log.error( e.getMessage(), e ); + throw new RuntimeException( e.getMessage(), e ); + } + catch ( RepositoryAdminException e ) + { + log.error( e.getMessage(), e ); + throw new RuntimeException( e.getMessage(), e ); } finally { deleteDirectoryQuiet( tempIndexDirectory ); - this.runningRemoteDownloadIds.remove( this.remoteRepository.getId( ) ); + this.runningRemoteDownloadIds.remove( this.remoteRepository.getId() ); } - log.info( "end download remote index for remote repository " + this.remoteRepository.getId( ) ); + log.info( "end download remote index for remote repository " + this.remoteRepository.getId() ); } private void deleteDirectoryQuiet( File f ) @@ -252,7 +213,7 @@ public class DownloadRemoteIndexTask } catch ( IOException e ) { - log.warn( "skip error delete " + f + ": " + e.getMessage( ) ); + log.warn( "skip error delete " + f + ": " + e.getMessage() ); } } @@ -260,15 +221,15 @@ public class DownloadRemoteIndexTask { try { - HttpConfiguration httpConfiguration = new HttpConfiguration( ).setAll( - new HttpMethodConfiguration( ).setReadTimeout( remoteRepository.getRemoteDownloadTimeout( ) * 1000 ) ); + HttpConfiguration httpConfiguration = new HttpConfiguration().setAll( + new HttpMethodConfiguration().setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 ) ); Method setHttpConfigurationMethod = - wagon.getClass( ).getMethod( "setHttpConfiguration", HttpConfiguration.class ); + wagon.getClass().getMethod( "setHttpConfiguration", HttpConfiguration.class ); setHttpConfigurationMethod.invoke( wagon, httpConfiguration ); } catch ( Exception e ) { - log.debug( "unable to set download remote time out for index {}", e.getMessage( ), e ); + log.debug( "unable to set download remote time out for index {}", e.getMessage(), e ); } } @@ -276,7 +237,7 @@ public class DownloadRemoteIndexTask public static class DownloadListener implements TransferListener { - private Logger log = LoggerFactory.getLogger( getClass( ) ); + private Logger log = LoggerFactory.getLogger( getClass() ); String reourceName; @@ -284,36 +245,36 @@ public class DownloadRemoteIndexTask public void transferInitiated( TransferEvent transferEvent ) { - reourceName = transferEvent.getResource( ).getName( ); + reourceName = transferEvent.getResource().getName(); log.debug( "initiate transfer of {}", reourceName ); } public void transferStarted( TransferEvent transferEvent ) { - reourceName = transferEvent.getResource( ).getName( ); - startTime = System.currentTimeMillis( ); - log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) ); + reourceName = transferEvent.getResource().getName(); + startTime = System.currentTimeMillis(); + log.info( "start transfer of {}", transferEvent.getResource().getName() ); } public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length ) { log.debug( "transfer of {} : {}/{}", - Arrays.asList( transferEvent.getResource( ).getName( ), buffer.length, length ).toArray( ) ); + Arrays.asList( transferEvent.getResource().getName(), buffer.length, length ).toArray() ); } public void transferCompleted( TransferEvent transferEvent ) { - reourceName = transferEvent.getResource( ).getName( ); - long endTime = System.currentTimeMillis( ); - log.info( "end of transfer file {}: {}s", transferEvent.getResource( ).getName( ), + reourceName = transferEvent.getResource().getName(); + long endTime = System.currentTimeMillis(); + log.info( "end of transfer file {}: {}s", transferEvent.getResource().getName(), ( endTime - startTime ) / 1000 ); } public void transferError( TransferEvent transferEvent ) { - log.info( "error of transfer file {}: {}", Arrays.asList( transferEvent.getResource( ).getName( ), - transferEvent.getException( ).getMessage( ) ).toArray( - new Object[2] ), transferEvent.getException( ) ); + log.info( "error of transfer file {}: {}", Arrays.asList( transferEvent.getResource().getName(), + transferEvent.getException().getMessage() ).toArray( + new Object[2] ), transferEvent.getException() ); } public void debug( String message ) @@ -322,4 +283,64 @@ public class DownloadRemoteIndexTask } } + private static class WagonResourceFetcher + implements ResourceFetcher + { + + Logger log; + + File tempIndexDirectory; + + Wagon wagon; + + private WagonResourceFetcher( Logger log, File tempIndexDirectory, Wagon wagon ) + { + this.log = log; + this.tempIndexDirectory = tempIndexDirectory; + this.wagon = wagon; + } + + public void connect( String id, String url ) + throws IOException + { + //no op + } + + public void disconnect() + throws IOException + { + // no op + } + + public InputStream retrieve( String name ) + throws IOException, FileNotFoundException + { + try + { + log.info( "index update retrieve file, name:{}", name ); + File file = new File( tempIndexDirectory, name ); + if ( file.exists() ) + { + file.delete(); + } + file.deleteOnExit(); + wagon.get( name, file ); + return new FileInputStream( file ); + } + catch ( AuthorizationException e ) + { + throw new IOException( e.getMessage() ); + } + catch ( TransferFailedException e ) + { + throw new IOException( e.getMessage() ); + } + catch ( ResourceDoesNotExistException e ) + { + throw new FileNotFoundException( e.getMessage() ); + } + } + } } + + diff --git a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTaskRequest.java b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTaskRequest.java index 46a7e638e..91efc1477 100644 --- a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTaskRequest.java +++ b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTaskRequest.java @@ -20,6 +20,7 @@ package org.apache.archiva.scheduler.indexing; import org.apache.archiva.admin.model.beans.NetworkProxy; import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin; import org.apache.archiva.proxy.common.WagonFactory; import org.apache.maven.index.NexusIndexer; import org.apache.maven.index.updater.IndexUpdater; @@ -32,7 +33,7 @@ public class DownloadRemoteIndexTaskRequest { private RemoteRepository remoteRepository; - private NexusIndexer nexusIndexer; + private RemoteRepositoryAdmin remoteRepositoryAdmin; private WagonFactory wagonFactory; @@ -58,16 +59,6 @@ public class DownloadRemoteIndexTaskRequest return this; } - public NexusIndexer getNexusIndexer() - { - return nexusIndexer; - } - - public DownloadRemoteIndexTaskRequest setNexusIndexer( NexusIndexer nexusIndexer ) - { - this.nexusIndexer = nexusIndexer; - return this; - } public WagonFactory getWagonFactory() { @@ -112,4 +103,15 @@ public class DownloadRemoteIndexTaskRequest this.indexUpdater = indexUpdater; return this; } + + public RemoteRepositoryAdmin getRemoteRepositoryAdmin() + { + return remoteRepositoryAdmin; + } + + public DownloadRemoteIndexTaskRequest setRemoteRepositoryAdmin( RemoteRepositoryAdmin remoteRepositoryAdmin ) + { + this.remoteRepositoryAdmin = remoteRepositoryAdmin; + return this; + } } -- 2.39.5