]> source.dussan.org Git - archiva.git/blob
72114947da2bf8b7a32b0f5cf9fa6768acab26e9
[archiva.git] /
1 package org.apache.archiva.repository.index.mock;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.NetworkProxy;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.common.utils.FileUtils;
26 import org.apache.archiva.common.utils.PathUtil;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.indexer.*;
29 import org.apache.archiva.proxy.common.WagonFactory;
30 import org.apache.archiva.proxy.common.WagonFactoryException;
31 import org.apache.archiva.proxy.common.WagonFactoryRequest;
32 import org.apache.archiva.repository.*;
33 import org.apache.archiva.repository.features.IndexCreationFeature;
34 import org.apache.archiva.repository.features.RemoteIndexFeature;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.maven.index.*;
37 import org.apache.maven.index.context.IndexCreator;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.apache.maven.index.packer.IndexPacker;
40 import org.apache.maven.index.packer.IndexPackingRequest;
41 import org.apache.maven.index.updater.IndexUpdateRequest;
42 import org.apache.maven.index.updater.IndexUpdater;
43 import org.apache.maven.index.updater.ResourceFetcher;
44 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
45 import org.apache.maven.wagon.*;
46 import org.apache.maven.wagon.authentication.AuthenticationException;
47 import org.apache.maven.wagon.authentication.AuthenticationInfo;
48 import org.apache.maven.wagon.authorization.AuthorizationException;
49 import org.apache.maven.wagon.events.TransferEvent;
50 import org.apache.maven.wagon.events.TransferListener;
51 import org.apache.maven.wagon.proxy.ProxyInfo;
52 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
53 import org.apache.maven.wagon.shared.http.HttpConfiguration;
54 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.stereotype.Service;
58
59 import javax.inject.Inject;
60 import java.io.FileNotFoundException;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.net.MalformedURLException;
64 import java.net.URI;
65 import java.nio.file.Files;
66 import java.nio.file.Path;
67 import java.nio.file.Paths;
68 import java.util.Collection;
69 import java.util.List;
70 import java.util.Map;
71 import java.util.concurrent.ConcurrentSkipListSet;
72 import java.util.stream.Collectors;
73
74 /**
75  * @author Martin Stockhammer <martin_s@apache.org>
76  */
77 @Service("archivaIndexManager#maven")
78 public class ArchivaIndexManagerMock implements ArchivaIndexManager {
79     private static final Logger log = LoggerFactory.getLogger( ArchivaIndexManagerMock.class );
80
81     @Inject
82     private Indexer indexer;
83
84     @Inject
85     private IndexerEngine indexerEngine;
86
87     @Inject
88     private List<? extends IndexCreator> indexCreators;
89
90     @Inject
91     private IndexPacker indexPacker;
92
93     @Inject
94     private Scanner scanner;
95
96     @Inject
97     private ArchivaConfiguration archivaConfiguration;
98
99     @Inject
100     private WagonFactory wagonFactory;
101
102     @Inject
103     private NetworkProxyAdmin networkProxyAdmin;
104
105
106     @Inject
107     private ArtifactContextProducer artifactContextProducer;
108
109     private ConcurrentSkipListSet<Path> activeContexts = new ConcurrentSkipListSet<>( );
110
111     private static final int WAIT_TIME = 100;
112     private static final int MAX_WAIT = 10;
113
114
115     public static IndexingContext getMvnContext(ArchivaIndexingContext context ) throws UnsupportedBaseContextException
116     {
117         if ( !context.supports( IndexingContext.class ) )
118         {
119             log.error( "The provided archiva index context does not support the maven IndexingContext" );
120             throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
121         }
122         return context.getBaseContext( IndexingContext.class );
123     }
124
125     private Path getIndexPath( ArchivaIndexingContext ctx )
126     {
127         return PathUtil.getPathFromUri( ctx.getPath( ) );
128     }
129
130     @FunctionalInterface
131     interface IndexUpdateConsumer
132     {
133
134         void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
135     }
136
137     /*
138      * This method is used to do some actions around the update execution code. And to make sure, that no other
139      * method is running on the same index.
140      */
141     private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
142     {
143         IndexingContext indexingContext = null;
144         try
145         {
146             indexingContext = getMvnContext( context );
147         }
148         catch ( UnsupportedBaseContextException e )
149         {
150             throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
151         }
152         final Path ctxPath = getIndexPath( context );
153         int loop = MAX_WAIT;
154         boolean active = false;
155         while ( loop-- > 0 && !active )
156         {
157             active = activeContexts.add( ctxPath );
158             try
159             {
160                 Thread.currentThread( ).sleep( WAIT_TIME );
161             }
162             catch ( InterruptedException e )
163             {
164                 // Ignore this
165             }
166         }
167         if ( active )
168         {
169             try
170             {
171                 function.accept( indexingContext );
172             }
173             finally
174             {
175                 activeContexts.remove( ctxPath );
176             }
177         }
178         else
179         {
180             throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
181         }
182     }
183
184     @Override
185     public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
186     {
187         executeUpdateFunction( context, indexingContext -> {
188                     try
189                     {
190                         IndexPackingRequest request = new IndexPackingRequest( indexingContext,
191                                 indexingContext.acquireIndexSearcher( ).getIndexReader( ),
192                                 indexingContext.getIndexDirectoryFile( ) );
193                         indexPacker.packIndex( request );
194                         indexingContext.updateTimestamp( true );
195                     }
196                     catch ( IOException e )
197                     {
198                         log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
199                         throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
200                     }
201                 }
202         );
203
204     }
205
206     @Override
207     public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
208     {
209         executeUpdateFunction( context, indexingContext -> {
210             DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
211             ScanningRequest request = new ScanningRequest( indexingContext, listener );
212             ScanningResult result = scanner.scan( request );
213             if ( result.hasExceptions( ) )
214             {
215                 log.error( "Exceptions occured during index scan of " + context.getId( ) );
216                 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
217                         s -> log.error( "Message: " + s )
218                 );
219             }
220
221         } );
222     }
223
224     @Override
225     public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException
226     {
227         log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
228         URI remoteUpdateUri;
229         if ( !( context.getRepository( ) instanceof RemoteRepository) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class)) )
230         {
231             throw new IndexUpdateFailedException( "The context is not associated to a remote repository with remote index " + context.getId( ) );
232         } else {
233             RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class).get();
234             remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
235         }
236         final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
237
238         executeUpdateFunction( context,
239                 indexingContext -> {
240                     try
241                     {
242                         // create a temp directory to download files
243                         Path tempIndexDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" );
244                         Path indexCacheDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" );
245                         Files.createDirectories( indexCacheDirectory );
246                         if ( Files.exists( tempIndexDirectory ) )
247                         {
248                             org.apache.archiva.common.utils.FileUtils.deleteDirectory( tempIndexDirectory );
249                         }
250                         Files.createDirectories( tempIndexDirectory );
251                         tempIndexDirectory.toFile( ).deleteOnExit( );
252                         String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
253
254                         String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
255
256                         NetworkProxy networkProxy = null;
257                         if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
258                         {
259                             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
260                             if ( StringUtils.isNotBlank( rif.getProxyId( ) ) )
261                             {
262                                 try
263                                 {
264                                     networkProxy = networkProxyAdmin.getNetworkProxy( rif.getProxyId( ) );
265                                 }
266                                 catch ( RepositoryAdminException e )
267                                 {
268                                     log.error( "Error occured while retrieving proxy {}", e.getMessage( ) );
269                                 }
270                                 if ( networkProxy == null )
271                                 {
272                                     log.warn(
273                                             "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
274                                             rif.getProxyId( ) );
275                                 }
276                             }
277
278                             final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
279                                     new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
280                                             networkProxy )
281                             );
282                             int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
283                             wagon.setReadTimeout( readTimeout );
284                             wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
285
286                             if ( wagon instanceof AbstractHttpClientWagon)
287                             {
288                                 HttpConfiguration httpConfiguration = new HttpConfiguration( );
289                                 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
290                                 httpMethodConfiguration.setUsePreemptive( true );
291                                 httpMethodConfiguration.setReadTimeout( readTimeout );
292                                 httpConfiguration.setGet( httpMethodConfiguration );
293                                 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
294                             }
295
296                             wagon.addTransferListener( new DownloadListener( ) );
297                             ProxyInfo proxyInfo = null;
298                             if ( networkProxy != null )
299                             {
300                                 proxyInfo = new ProxyInfo( );
301                                 proxyInfo.setType( networkProxy.getProtocol( ) );
302                                 proxyInfo.setHost( networkProxy.getHost( ) );
303                                 proxyInfo.setPort( networkProxy.getPort( ) );
304                                 proxyInfo.setUserName( networkProxy.getUsername( ) );
305                                 proxyInfo.setPassword( networkProxy.getPassword( ) );
306                             }
307                             AuthenticationInfo authenticationInfo = null;
308                             if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials) )
309                             {
310                                 PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
311                                 authenticationInfo = new AuthenticationInfo( );
312                                 authenticationInfo.setUserName( creds.getUsername( ) );
313                                 authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
314                             }
315                             wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
316                                     proxyInfo );
317
318                             Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
319                             if ( !Files.exists( indexDirectory ) )
320                             {
321                                 Files.createDirectories( indexDirectory );
322                             }
323
324                             ResourceFetcher resourceFetcher =
325                                     new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
326                             IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
327                             request.setForceFullUpdate( fullUpdate );
328                             request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
329
330                             // indexUpdater.fetchAndUpdateIndex( request );
331
332                             indexingContext.updateTimestamp( true );
333                         }
334
335                     }
336                     catch ( AuthenticationException e )
337                     {
338                         log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
339                         throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
340                     }
341                     catch ( ConnectionException e )
342                     {
343                         log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
344                         throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
345                     }
346                     catch ( MalformedURLException e )
347                     {
348                         log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
349                         throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
350                     }
351                     catch ( IOException e )
352                     {
353                         log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
354                         throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
355                                 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
356                     }
357                     catch ( WagonFactoryException e )
358                     {
359                         log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
360                         throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
361                     }
362                 } );
363
364     }
365
366     @Override
367     public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
368     {
369         final URI ctxUri = context.getPath();
370         executeUpdateFunction(context, indexingContext -> {
371             Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
372             try {
373                 indexer.addArtifactsToIndex(artifacts, indexingContext);
374             } catch (IOException e) {
375                 log.error("IOException while adding artifact {}", e.getMessage(), e);
376                 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
377                         + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
378             }
379         });
380     }
381
382     @Override
383     public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
384     {
385         final URI ctxUri = context.getPath();
386         executeUpdateFunction(context, indexingContext -> {
387             Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
388             try {
389                 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
390             } catch (IOException e) {
391                 log.error("IOException while removing artifact {}", e.getMessage(), e);
392                 throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
393                         + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
394             }
395         });
396
397     }
398
399     @Override
400     public boolean supportsRepository( RepositoryType type )
401     {
402         return type == RepositoryType.MAVEN;
403     }
404
405     @Override
406     public ArchivaIndexingContext createContext( Repository repository ) throws IndexCreationFailedException
407     {
408         log.debug("Creating context for repo {}, type: {}", repository.getId(), repository.getType());
409         if ( repository.getType( ) != RepositoryType.MAVEN )
410         {
411             throw new UnsupportedRepositoryTypeException( repository.getType( ) );
412         }
413         IndexingContext mvnCtx = null;
414         try
415         {
416             if ( repository instanceof RemoteRepository )
417             {
418                 mvnCtx = createRemoteContext( (RemoteRepository) repository );
419             }
420             else if ( repository instanceof ManagedRepository )
421             {
422                 mvnCtx = createManagedContext( (ManagedRepository) repository );
423             }
424         }
425         catch ( IOException e )
426         {
427             log.error( "IOException during context creation " + e.getMessage( ), e );
428             throw new IndexCreationFailedException( "Could not create index context for repository " + repository.getId( )
429                     + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
430         }
431         MavenIndexContextMock context = new MavenIndexContextMock( repository, mvnCtx );
432
433         return context;
434     }
435
436     @Override
437     public ArchivaIndexingContext reset(ArchivaIndexingContext context) throws IndexUpdateFailedException {
438         ArchivaIndexingContext ctx;
439         executeUpdateFunction(context, indexingContext -> {
440             try {
441                 indexingContext.close(true);
442             } catch (IOException e) {
443                 log.warn("Index close failed");
444             }
445             try {
446                 FileUtils.deleteDirectory(Paths.get(context.getPath()));
447             } catch (IOException e) {
448                 throw new IndexUpdateFailedException("Could not delete index files");
449             }
450         });
451         try {
452             Repository repo = context.getRepository();
453             ctx = createContext(context.getRepository());
454             if (repo instanceof EditableRepository) {
455                 ((EditableRepository)repo).setIndexingContext(ctx);
456             }
457         } catch (IndexCreationFailedException e) {
458             throw new IndexUpdateFailedException("Could not create index");
459         }
460         return ctx;
461     }
462
463     @Override
464     public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
465         if (context==null) {
466             return null;
467         }
468         if (context.supports(IndexingContext.class)) {
469             try {
470                 Path newPath = getIndexPath(repo);
471                 IndexingContext ctx = context.getBaseContext(IndexingContext.class);
472                 Path oldPath = ctx.getIndexDirectoryFile().toPath();
473                 if (oldPath.equals(newPath)) {
474                     // Nothing to do, if path does not change
475                     return context;
476                 }
477                 if (!Files.exists(oldPath)) {
478                     return createContext(repo);
479                 } else if (context.isEmpty()) {
480                     context.close();
481                     return createContext(repo);
482                 } else {
483                     context.close(false);
484                     Files.move(oldPath, newPath);
485                     return createContext(repo);
486                 }
487             } catch (IOException e) {
488                 log.error("IOException while moving index directory {}", e.getMessage(), e);
489                 throw new IndexCreationFailedException("Could not recreated the index.", e);
490             } catch (UnsupportedBaseContextException e) {
491                 throw new IndexCreationFailedException("The given context, is not a maven context.");
492             }
493         } else {
494             throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
495         }
496     }
497
498     private Path getIndexPath(Repository repo) throws IOException {
499         IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
500         Path repoDir = repo.getLocalPath();
501         URI indexDir = icf.getIndexPath();
502         Path indexDirectory = null;
503         if ( ! StringUtils.isEmpty(indexDir.toString( ) ) )
504         {
505
506             indexDirectory = PathUtil.getPathFromUri( indexDir );
507             // not absolute so create it in repository directory
508             if ( !indexDirectory.isAbsolute( ) )
509             {
510                 indexDirectory = repoDir.resolve( indexDirectory );
511             }
512         }
513         else
514         {
515             indexDirectory = repoDir.resolve( ".index" );
516         }
517
518         if ( !Files.exists( indexDirectory ) )
519         {
520             Files.createDirectories( indexDirectory );
521         }
522         return indexDirectory;
523     }
524
525     private IndexingContext createRemoteContext(RemoteRepository remoteRepository ) throws IOException
526     {
527         Path appServerBase = archivaConfiguration.getAppServerBaseDir( );
528
529         String contextKey = "remote-" + remoteRepository.getId( );
530
531
532         // create remote repository path
533         Path repoDir = remoteRepository.getLocalPath();
534         if ( !Files.exists( repoDir ) )
535         {
536             Files.createDirectories( repoDir );
537         }
538
539         Path indexDirectory = null;
540
541         // is there configured indexDirectory ?
542         if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
543         {
544             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
545             indexDirectory = getIndexPath(remoteRepository);
546             String remoteIndexUrl = calculateIndexRemoteUrl( remoteRepository.getLocation( ), rif );
547             try
548             {
549
550                 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
551             }
552             catch ( IndexFormatTooOldException e )
553             {
554                 // existing index with an old lucene format so we need to delete it!!!
555                 // delete it first then recreate it.
556                 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
557                         remoteRepository.getId( ) );
558                 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
559                 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
560
561             }
562         }
563         else
564         {
565             throw new IOException( "No remote index defined" );
566         }
567     }
568
569     private IndexingContext getIndexingContext( Repository repository, String contextKey, Path repoDir, Path indexDirectory, String indexUrl ) throws IOException
570     {
571         return indexer.createIndexingContext( contextKey, repository.getId( ), repoDir.toFile( ), indexDirectory.toFile( ),
572                 repository.getLocation( ) == null ? null : repository.getLocation( ).toString( ),
573                 indexUrl,
574                 true, false,
575                 indexCreators );
576     }
577
578     private IndexingContext createManagedContext( ManagedRepository repository ) throws IOException
579     {
580
581         IndexingContext context;
582         // take care first about repository location as can be relative
583         Path repositoryDirectory = repository.getLocalPath();
584
585         if ( !Files.exists( repositoryDirectory ) )
586         {
587             try
588             {
589                 Files.createDirectories( repositoryDirectory );
590             }
591             catch ( IOException e )
592             {
593                 log.error( "Could not create directory {}", repositoryDirectory );
594             }
595         }
596
597         Path indexDirectory = null;
598
599         if ( repository.supportsFeature( IndexCreationFeature.class ) )
600         {
601             indexDirectory = getIndexPath(repository);
602
603             String indexUrl = repositoryDirectory.toUri( ).toURL( ).toExternalForm( );
604             try
605             {
606                 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
607                 context.setSearchable( repository.isScanned( ) );
608             }
609             catch ( IndexFormatTooOldException e )
610             {
611                 // existing index with an old lucene format so we need to delete it!!!
612                 // delete it first then recreate it.
613                 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
614                         repository.getId( ) );
615                 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory );
616                 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
617                 context.setSearchable( repository.isScanned( ) );
618             }
619             return context;
620         }
621         else
622         {
623             throw new IOException( "No repository index defined" );
624         }
625     }
626
627     private String calculateIndexRemoteUrl( URI baseUri, RemoteIndexFeature rif )
628     {
629         if ( rif.getIndexUri( ) == null )
630         {
631             return baseUri.resolve( ".index" ).toString( );
632         }
633         else
634         {
635             return baseUri.resolve( rif.getIndexUri( ) ).toString( );
636         }
637     }
638
639     private static final class DownloadListener
640             implements TransferListener
641     {
642         private Logger log = LoggerFactory.getLogger( getClass( ) );
643
644         private String resourceName;
645
646         private long startTime;
647
648         private int totalLength = 0;
649
650         @Override
651         public void transferInitiated( TransferEvent transferEvent )
652         {
653             startTime = System.currentTimeMillis( );
654             resourceName = transferEvent.getResource( ).getName( );
655             log.debug( "initiate transfer of {}", resourceName );
656         }
657
658         @Override
659         public void transferStarted( TransferEvent transferEvent )
660         {
661             this.totalLength = 0;
662             resourceName = transferEvent.getResource( ).getName( );
663             log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) );
664         }
665
666         @Override
667         public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
668         {
669             log.debug( "transfer of {} : {}/{}", transferEvent.getResource( ).getName( ), buffer.length, length );
670             this.totalLength += length;
671         }
672
673         @Override
674         public void transferCompleted( TransferEvent transferEvent )
675         {
676             resourceName = transferEvent.getResource( ).getName( );
677             long endTime = System.currentTimeMillis( );
678             log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource( ).getName( ),
679                     this.totalLength / 1024, ( endTime - startTime ) / 1000 );
680         }
681
682         @Override
683         public void transferError( TransferEvent transferEvent )
684         {
685             log.info( "error of transfer file {}: {}", transferEvent.getResource( ).getName( ),
686                     transferEvent.getException( ).getMessage( ), transferEvent.getException( ) );
687         }
688
689         @Override
690         public void debug( String message )
691         {
692             log.debug( "transfer debug {}", message );
693         }
694     }
695
696     private static class WagonResourceFetcher
697             implements ResourceFetcher
698     {
699
700         Logger log;
701
702         Path tempIndexDirectory;
703
704         Wagon wagon;
705
706         RemoteRepository remoteRepository;
707
708         private WagonResourceFetcher( Logger log, Path tempIndexDirectory, Wagon wagon,
709                                       RemoteRepository remoteRepository )
710         {
711             this.log = log;
712             this.tempIndexDirectory = tempIndexDirectory;
713             this.wagon = wagon;
714             this.remoteRepository = remoteRepository;
715         }
716
717         @Override
718         public void connect( String id, String url )
719                 throws IOException
720         {
721             //no op
722         }
723
724         @Override
725         public void disconnect( )
726                 throws IOException
727         {
728             // no op
729         }
730
731         @Override
732         public InputStream retrieve(String name )
733                 throws IOException, FileNotFoundException
734         {
735             try
736             {
737                 log.info( "index update retrieve file, name:{}", name );
738                 Path file = tempIndexDirectory.resolve( name );
739                 Files.deleteIfExists( file );
740                 file.toFile( ).deleteOnExit( );
741                 wagon.get( addParameters( name, remoteRepository ), file.toFile( ) );
742                 return Files.newInputStream( file );
743             }
744             catch ( AuthorizationException | TransferFailedException e )
745             {
746                 throw new IOException( e.getMessage( ), e );
747             }
748             catch ( ResourceDoesNotExistException e )
749             {
750                 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage( ) );
751                 fnfe.initCause( e );
752                 throw fnfe;
753             }
754         }
755
756         // FIXME remove crappy copy/paste
757         protected String addParameters( String path, RemoteRepository remoteRepository )
758         {
759             if ( remoteRepository.getExtraParameters( ).isEmpty( ) )
760             {
761                 return path;
762             }
763
764             boolean question = false;
765
766             StringBuilder res = new StringBuilder( path == null ? "" : path );
767
768             for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters( ).entrySet( ) )
769             {
770                 if ( !question )
771                 {
772                     res.append( '?' ).append( entry.getKey( ) ).append( '=' ).append( entry.getValue( ) );
773                 }
774             }
775
776             return res.toString( );
777         }
778
779     }
780 }