1 package org.apache.archiva.scheduler.repository.mock;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.filelock.DefaultFileLockManager;
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.common.utils.PathUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.indexer.ArchivaIndexManager;
26 import org.apache.archiva.indexer.ArchivaIndexingContext;
27 import org.apache.archiva.indexer.IndexCreationFailedException;
28 import org.apache.archiva.indexer.IndexUpdateFailedException;
29 import org.apache.archiva.indexer.UnsupportedBaseContextException;
30 import org.apache.archiva.maven.common.proxy.WagonFactory;
31 import org.apache.archiva.maven.common.proxy.WagonFactoryException;
32 import org.apache.archiva.maven.common.proxy.WagonFactoryRequest;
33 import org.apache.archiva.proxy.model.NetworkProxy;
34 import org.apache.archiva.repository.EditableRepository;
35 import org.apache.archiva.repository.ManagedRepository;
36 import org.apache.archiva.repository.base.PasswordCredentials;
37 import org.apache.archiva.repository.RemoteRepository;
38 import org.apache.archiva.repository.Repository;
39 import org.apache.archiva.repository.RepositoryType;
40 import org.apache.archiva.repository.UnsupportedRepositoryTypeException;
41 import org.apache.archiva.repository.storage.fs.FilesystemAsset;
42 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
43 import org.apache.archiva.repository.storage.StorageAsset;
44 import org.apache.archiva.repository.features.IndexCreationFeature;
45 import org.apache.archiva.repository.features.RemoteIndexFeature;
46 import org.apache.commons.lang3.StringUtils;
47 import org.apache.maven.index.ArtifactContext;
48 import org.apache.maven.index.ArtifactContextProducer;
49 import org.apache.maven.index.DefaultScannerListener;
50 import org.apache.maven.index.Indexer;
51 import org.apache.maven.index.IndexerEngine;
52 import org.apache.maven.index.Scanner;
53 import org.apache.maven.index.ScanningRequest;
54 import org.apache.maven.index.ScanningResult;
55 import org.apache.maven.index.context.IndexCreator;
56 import org.apache.maven.index.context.IndexingContext;
57 import org.apache.maven.index.packer.IndexPacker;
58 import org.apache.maven.index.packer.IndexPackingRequest;
59 import org.apache.maven.index.updater.IndexUpdateRequest;
60 import org.apache.maven.index.updater.ResourceFetcher;
61 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
62 import org.apache.maven.wagon.ConnectionException;
63 import org.apache.maven.wagon.ResourceDoesNotExistException;
64 import org.apache.maven.wagon.StreamWagon;
65 import org.apache.maven.wagon.TransferFailedException;
66 import org.apache.maven.wagon.Wagon;
67 import org.apache.maven.wagon.authentication.AuthenticationException;
68 import org.apache.maven.wagon.authentication.AuthenticationInfo;
69 import org.apache.maven.wagon.authorization.AuthorizationException;
70 import org.apache.maven.wagon.events.TransferEvent;
71 import org.apache.maven.wagon.events.TransferListener;
72 import org.apache.maven.wagon.proxy.ProxyInfo;
73 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
74 import org.apache.maven.wagon.shared.http.HttpConfiguration;
75 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.springframework.stereotype.Service;
80 import javax.inject.Inject;
81 import java.io.FileNotFoundException;
82 import java.io.IOException;
83 import java.io.InputStream;
84 import java.net.MalformedURLException;
86 import java.nio.file.Files;
87 import java.nio.file.Path;
88 import java.nio.file.Paths;
89 import java.util.Collection;
90 import java.util.List;
92 import java.util.concurrent.ConcurrentSkipListSet;
93 import java.util.stream.Collectors;
95 @Service("archivaIndexManager#maven")
96 public class ArchivaIndexManagerMock implements ArchivaIndexManager {
98 private static final Logger log = LoggerFactory.getLogger( ArchivaIndexManagerMock.class );
101 private Indexer indexer;
104 private IndexerEngine indexerEngine;
107 private List<? extends IndexCreator> indexCreators;
110 private IndexPacker indexPacker;
113 private Scanner scanner;
116 private ArchivaConfiguration archivaConfiguration;
119 private WagonFactory wagonFactory;
123 private ArtifactContextProducer artifactContextProducer;
125 private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
127 private static final int WAIT_TIME = 100;
128 private static final int MAX_WAIT = 10;
131 public static IndexingContext getMvnContext(ArchivaIndexingContext context ) throws UnsupportedBaseContextException
133 if ( !context.supports( IndexingContext.class ) )
135 log.error( "The provided archiva index context does not support the maven IndexingContext" );
136 throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
138 return context.getBaseContext( IndexingContext.class );
141 private Path getIndexPath( ArchivaIndexingContext ctx )
143 return ctx.getPath().getFilePath();
147 interface IndexUpdateConsumer
150 void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
154 * This method is used to do some actions around the update execution code. And to make sure, that no other
155 * method is running on the same index.
157 private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
159 IndexingContext indexingContext = null;
162 indexingContext = getMvnContext( context );
164 catch ( UnsupportedBaseContextException e )
166 throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
168 final Path ctxPath = getIndexPath( context );
170 boolean active = false;
171 while ( loop-- > 0 && !active )
173 active = activeContexts.add( ctxPath );
176 Thread.currentThread( ).sleep( WAIT_TIME );
178 catch ( InterruptedException e )
187 function.accept( indexingContext );
191 activeContexts.remove( ctxPath );
196 throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
201 public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
203 executeUpdateFunction( context, indexingContext -> {
206 IndexPackingRequest request = new IndexPackingRequest( indexingContext,
207 indexingContext.acquireIndexSearcher( ).getIndexReader( ),
208 indexingContext.getIndexDirectoryFile( ) );
209 indexPacker.packIndex( request );
210 indexingContext.updateTimestamp( true );
212 catch ( IOException e )
214 log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
215 throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
223 public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
225 executeUpdateFunction( context, indexingContext -> {
226 DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
227 ScanningRequest request = new ScanningRequest( indexingContext, listener );
228 ScanningResult result = scanner.scan( request );
229 if ( result.hasExceptions( ) )
231 log.error( "Exceptions occured during index scan of " + context.getId( ) );
232 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
233 s -> log.error( "Message: " + s )
241 public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException
243 log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
245 if ( !( context.getRepository( ) instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class)) )
247 throw new IndexUpdateFailedException( "The context is not associated to a remote repository with remote index " + context.getId( ) );
249 RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class).get();
250 remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
252 final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
254 executeUpdateFunction( context,
258 // create a temp directory to download files
259 Path tempIndexDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" );
260 Path indexCacheDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" );
261 Files.createDirectories( indexCacheDirectory );
262 if ( Files.exists( tempIndexDirectory ) )
264 FileUtils.deleteDirectory( tempIndexDirectory );
266 Files.createDirectories( tempIndexDirectory );
267 tempIndexDirectory.toFile( ).deleteOnExit( );
268 String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
270 String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
272 NetworkProxy networkProxy = null;
273 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
275 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
277 final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
278 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
281 int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
282 wagon.setReadTimeout( readTimeout );
283 wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
285 if ( wagon instanceof AbstractHttpClientWagon)
287 HttpConfiguration httpConfiguration = new HttpConfiguration( );
288 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
289 httpMethodConfiguration.setUsePreemptive( true );
290 httpMethodConfiguration.setReadTimeout( readTimeout );
291 httpConfiguration.setGet( httpMethodConfiguration );
292 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
295 wagon.addTransferListener( new DownloadListener( ) );
296 ProxyInfo proxyInfo = null;
297 if ( networkProxy != null )
299 proxyInfo = new ProxyInfo( );
300 proxyInfo.setType( networkProxy.getProtocol( ) );
301 proxyInfo.setHost( networkProxy.getHost( ) );
302 proxyInfo.setPort( networkProxy.getPort( ) );
303 proxyInfo.setUserName( networkProxy.getUsername( ) );
304 proxyInfo.setPassword(new String(networkProxy.getPassword()));
306 AuthenticationInfo authenticationInfo = null;
307 if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials) )
309 PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
310 authenticationInfo = new AuthenticationInfo( );
311 authenticationInfo.setUserName( creds.getUsername( ) );
312 authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
314 wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
317 Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
318 if ( !Files.exists( indexDirectory ) )
320 Files.createDirectories( indexDirectory );
323 ResourceFetcher resourceFetcher =
324 new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
325 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
326 request.setForceFullUpdate( fullUpdate );
327 request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
329 // indexUpdater.fetchAndUpdateIndex( request );
331 indexingContext.updateTimestamp( true );
335 catch ( AuthenticationException e )
337 log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
338 throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
340 catch ( ConnectionException e )
342 log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
343 throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
345 catch ( MalformedURLException e )
347 log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
348 throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
350 catch ( IOException e )
352 log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
353 throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
354 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
356 catch ( WagonFactoryException e )
358 log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
359 throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
366 public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
368 StorageAsset ctxUri = context.getPath();
369 executeUpdateFunction(context, indexingContext -> {
370 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
372 indexer.addArtifactsToIndex(artifacts, indexingContext);
373 } catch (IOException e) {
374 log.error("IOException while adding artifact {}", e.getMessage(), e);
375 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
376 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
382 public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
384 final StorageAsset ctxUri = context.getPath();
385 executeUpdateFunction(context, indexingContext -> {
386 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
388 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
389 } catch (IOException e) {
390 log.error("IOException while removing artifact {}", e.getMessage(), e);
391 throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
392 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
399 public boolean supportsRepository( RepositoryType type )
401 return type == RepositoryType.MAVEN;
405 public ArchivaIndexingContext createContext( Repository repository ) throws IndexCreationFailedException
407 log.debug("Creating context for repo {}, type: {}", repository.getId(), repository.getType());
408 if ( repository.getType( ) != RepositoryType.MAVEN )
410 throw new UnsupportedRepositoryTypeException( repository.getType( ) );
412 IndexingContext mvnCtx = null;
415 if ( repository instanceof RemoteRepository )
417 mvnCtx = createRemoteContext( (RemoteRepository) repository );
419 else if ( repository instanceof ManagedRepository )
421 mvnCtx = createManagedContext( (ManagedRepository) repository );
424 catch ( IOException e )
426 log.error( "IOException during context creation " + e.getMessage( ), e );
427 throw new IndexCreationFailedException( "Could not create index context for repository " + repository.getId( )
428 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
430 MavenIndexContextMock context = new MavenIndexContextMock( repository, mvnCtx );
436 public ArchivaIndexingContext reset(ArchivaIndexingContext context) throws IndexUpdateFailedException {
437 ArchivaIndexingContext ctx;
438 executeUpdateFunction(context, indexingContext -> {
440 indexingContext.close(true);
441 } catch (IOException e) {
442 log.warn("Index close failed");
444 org.apache.archiva.repository.storage.util.StorageUtil.deleteRecursively(context.getPath());
447 Repository repo = context.getRepository();
448 ctx = createContext(context.getRepository());
449 if (repo instanceof EditableRepository) {
450 ((EditableRepository)repo).setIndexingContext(ctx);
452 } catch (IndexCreationFailedException e) {
453 throw new IndexUpdateFailedException("Could not create index");
459 public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
463 if (context.supports(IndexingContext.class)) {
465 StorageAsset newPath = getIndexPath(repo);
466 IndexingContext ctx = context.getBaseContext(IndexingContext.class);
467 Path oldPath = ctx.getIndexDirectoryFile().toPath();
468 if (oldPath.equals(newPath)) {
469 // Nothing to do, if path does not change
472 if (!Files.exists(oldPath)) {
473 return createContext(repo);
474 } else if (context.isEmpty()) {
476 return createContext(repo);
478 context.close(false);
479 Files.move(oldPath, newPath.getFilePath());
480 return createContext(repo);
482 } catch (IOException e) {
483 log.error("IOException while moving index directory {}", e.getMessage(), e);
484 throw new IndexCreationFailedException("Could not recreated the index.", e);
485 } catch (UnsupportedBaseContextException e) {
486 throw new IndexCreationFailedException("The given context, is not a maven context.");
489 throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
494 public void updateLocalIndexPath(Repository repo) {
495 if (repo.supportsFeature(IndexCreationFeature.class)) {
496 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
498 icf.setLocalIndexPath(getIndexPath(repo));
499 } catch (IOException e) {
500 log.error("Could not set local index path for {}. New URI: {}", repo.getId(), icf.getIndexPath());
506 public ArchivaIndexingContext mergeContexts(Repository destinationRepo, List<ArchivaIndexingContext> contexts, boolean packIndex) throws UnsupportedOperationException, IndexCreationFailedException {
510 private StorageAsset getIndexPath( Repository repo) throws IOException {
511 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
512 Path repoDir = repo.getRoot().getFilePath();
513 URI indexDir = icf.getIndexPath();
514 String indexPath = indexDir.getPath();
515 Path indexDirectory = null;
516 FilesystemStorage filesystemStorage = (FilesystemStorage) repo.getRoot().getStorage();
517 if ( ! StringUtils.isEmpty(indexDir.toString( ) ) )
520 indexDirectory = PathUtil.getPathFromUri( indexDir );
521 // not absolute so create it in repository directory
522 if ( indexDirectory.isAbsolute( ) )
524 indexPath = indexDirectory.getFileName().toString();
525 filesystemStorage = new FilesystemStorage(indexDirectory, new DefaultFileLockManager());
529 indexDirectory = repoDir.resolve( indexDirectory );
534 indexDirectory = repoDir.resolve( ".index" );
535 indexPath = ".index";
538 if ( !Files.exists( indexDirectory ) )
540 Files.createDirectories( indexDirectory );
542 return new FilesystemAsset( filesystemStorage, indexPath, indexDirectory);
545 private IndexingContext createRemoteContext(RemoteRepository remoteRepository ) throws IOException
547 Path appServerBase = archivaConfiguration.getAppServerBaseDir( );
549 String contextKey = "remote-" + remoteRepository.getId( );
552 // create remote repository path
553 Path repoDir = remoteRepository.getRoot().getFilePath();
554 if ( !Files.exists( repoDir ) )
556 Files.createDirectories( repoDir );
559 StorageAsset indexDirectory = null;
561 // is there configured indexDirectory ?
562 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
564 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
565 indexDirectory = getIndexPath(remoteRepository);
566 String remoteIndexUrl = calculateIndexRemoteUrl( remoteRepository.getLocation( ), rif );
570 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
572 catch ( IndexFormatTooOldException e )
574 // existing index with an old lucene format so we need to delete it!!!
575 // delete it first then recreate it.
576 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
577 remoteRepository.getId( ) );
578 FileUtils.deleteDirectory( indexDirectory.getFilePath() );
579 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
585 throw new IOException( "No remote index defined" );
589 private IndexingContext getIndexingContext( Repository repository, String contextKey, Path repoDir, StorageAsset indexDirectory, String indexUrl ) throws IOException
591 return indexer.createIndexingContext( contextKey, repository.getId( ), repoDir.toFile( ), indexDirectory.getFilePath().toFile( ),
592 repository.getLocation( ) == null ? null : repository.getLocation( ).toString( ),
598 private IndexingContext createManagedContext( ManagedRepository repository ) throws IOException
601 IndexingContext context;
602 // take care first about repository location as can be relative
603 Path repositoryDirectory = repository.getRoot().getFilePath();
605 if ( !Files.exists( repositoryDirectory ) )
609 Files.createDirectories( repositoryDirectory );
611 catch ( IOException e )
613 log.error( "Could not create directory {}", repositoryDirectory );
617 StorageAsset indexDirectory = null;
619 if ( repository.supportsFeature( IndexCreationFeature.class ) )
621 indexDirectory = getIndexPath(repository);
623 String indexUrl = repositoryDirectory.toUri( ).toURL( ).toExternalForm( );
626 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
627 context.setSearchable( repository.isScanned( ) );
629 catch ( IndexFormatTooOldException e )
631 // existing index with an old lucene format so we need to delete it!!!
632 // delete it first then recreate it.
633 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
634 repository.getId( ) );
635 FileUtils.deleteDirectory( indexDirectory.getFilePath() );
636 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
637 context.setSearchable( repository.isScanned( ) );
643 throw new IOException( "No repository index defined" );
647 private String calculateIndexRemoteUrl( URI baseUri, RemoteIndexFeature rif )
649 if ( rif.getIndexUri( ) == null )
651 return baseUri.resolve( ".index" ).toString( );
655 return baseUri.resolve( rif.getIndexUri( ) ).toString( );
659 private static final class DownloadListener
660 implements TransferListener
662 private Logger log = LoggerFactory.getLogger( getClass( ) );
664 private String resourceName;
666 private long startTime;
668 private int totalLength = 0;
671 public void transferInitiated( TransferEvent transferEvent )
673 startTime = System.currentTimeMillis( );
674 resourceName = transferEvent.getResource( ).getName( );
675 log.debug( "initiate transfer of {}", resourceName );
679 public void transferStarted( TransferEvent transferEvent )
681 this.totalLength = 0;
682 resourceName = transferEvent.getResource( ).getName( );
683 log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) );
687 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
689 log.debug( "transfer of {} : {}/{}", transferEvent.getResource( ).getName( ), buffer.length, length );
690 this.totalLength += length;
694 public void transferCompleted( TransferEvent transferEvent )
696 resourceName = transferEvent.getResource( ).getName( );
697 long endTime = System.currentTimeMillis( );
698 log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource( ).getName( ),
699 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
703 public void transferError( TransferEvent transferEvent )
705 log.info( "error of transfer file {}: {}", transferEvent.getResource( ).getName( ),
706 transferEvent.getException( ).getMessage( ), transferEvent.getException( ) );
710 public void debug( String message )
712 log.debug( "transfer debug {}", message );
716 private static class WagonResourceFetcher
717 implements ResourceFetcher
722 Path tempIndexDirectory;
726 RemoteRepository remoteRepository;
728 private WagonResourceFetcher( Logger log, Path tempIndexDirectory, Wagon wagon,
729 RemoteRepository remoteRepository )
732 this.tempIndexDirectory = tempIndexDirectory;
734 this.remoteRepository = remoteRepository;
738 public void connect( String id, String url )
745 public void disconnect( )
752 public InputStream retrieve(String name )
753 throws IOException, FileNotFoundException
757 log.info( "index update retrieve file, name:{}", name );
758 Path file = tempIndexDirectory.resolve( name );
759 Files.deleteIfExists( file );
760 file.toFile( ).deleteOnExit( );
761 wagon.get( addParameters( name, remoteRepository ), file.toFile( ) );
762 return Files.newInputStream( file );
764 catch ( AuthorizationException | TransferFailedException e )
766 throw new IOException( e.getMessage( ), e );
768 catch ( ResourceDoesNotExistException e )
770 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage( ) );
776 // FIXME remove crappy copy/paste
777 protected String addParameters( String path, RemoteRepository remoteRepository )
779 if ( remoteRepository.getExtraParameters( ).isEmpty( ) )
784 boolean question = false;
786 StringBuilder res = new StringBuilder( path == null ? "" : path );
788 for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters( ).entrySet( ) )
792 res.append( '?' ).append( entry.getKey( ) ).append( '=' ).append( entry.getValue( ) );
796 return res.toString( );