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