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