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