1 package org.apache.archiva.indexer.maven;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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.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.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.FilesystemStorage;
43 import org.apache.archiva.repository.storage.RepositoryStorage;
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.ContextMemberProvider;
58 import org.apache.maven.index.context.IndexCreator;
59 import org.apache.maven.index.context.IndexingContext;
60 import org.apache.maven.index.context.StaticContextMemberProvider;
61 import org.apache.maven.index.packer.IndexPacker;
62 import org.apache.maven.index.packer.IndexPackingRequest;
63 import org.apache.maven.index.updater.IndexUpdateRequest;
64 import org.apache.maven.index.updater.IndexUpdater;
65 import org.apache.maven.index.updater.ResourceFetcher;
66 import org.apache.maven.index_shaded.lucene.index.IndexFormatTooOldException;
67 import org.apache.maven.wagon.ConnectionException;
68 import org.apache.maven.wagon.ResourceDoesNotExistException;
69 import org.apache.maven.wagon.StreamWagon;
70 import org.apache.maven.wagon.TransferFailedException;
71 import org.apache.maven.wagon.Wagon;
72 import org.apache.maven.wagon.authentication.AuthenticationException;
73 import org.apache.maven.wagon.authentication.AuthenticationInfo;
74 import org.apache.maven.wagon.authorization.AuthorizationException;
75 import org.apache.maven.wagon.events.TransferEvent;
76 import org.apache.maven.wagon.events.TransferListener;
77 import org.apache.maven.wagon.proxy.ProxyInfo;
78 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
79 import org.apache.maven.wagon.shared.http.HttpConfiguration;
80 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
81 import org.slf4j.Logger;
82 import org.slf4j.LoggerFactory;
83 import org.springframework.stereotype.Service;
85 import javax.inject.Inject;
86 import java.io.FileNotFoundException;
87 import java.io.IOException;
88 import java.io.InputStream;
89 import java.net.MalformedURLException;
91 import java.nio.file.Files;
92 import java.nio.file.Path;
93 import java.nio.file.Paths;
94 import java.util.Collection;
95 import java.util.List;
97 import java.util.Objects;
98 import java.util.concurrent.ConcurrentSkipListSet;
99 import java.util.stream.Collectors;
102 * Maven implementation of index manager.
103 * The index manager is a singleton, so we try to make sure, that index operations are not running
104 * parallel by synchronizing on the index path.
105 * A update operation waits for parallel running methods to finish before starting, but after a certain
106 * time of retries a IndexUpdateFailedException is thrown.
108 @Service( "archivaIndexManager#maven" )
109 public class MavenIndexManager implements ArchivaIndexManager {
111 private static final Logger log = LoggerFactory.getLogger( MavenIndexManager.class );
114 private Indexer indexer;
117 private IndexerEngine indexerEngine;
120 private List<? extends IndexCreator> indexCreators;
123 private IndexPacker indexPacker;
126 private Scanner scanner;
129 private ArchivaConfiguration archivaConfiguration;
132 private WagonFactory wagonFactory;
135 private IndexUpdater indexUpdater;
138 private ArtifactContextProducer artifactContextProducer;
141 private ProxyRegistry proxyRegistry;
144 private ConcurrentSkipListSet<StorageAsset> activeContexts = new ConcurrentSkipListSet<>( );
146 private static final int WAIT_TIME = 100;
147 private static final int MAX_WAIT = 10;
150 public static IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
152 if ( !context.supports( IndexingContext.class ) )
154 log.error( "The provided archiva index context does not support the maven IndexingContext" );
155 throw new UnsupportedBaseContextException( "The context does not support the Maven IndexingContext" );
157 return context.getBaseContext( IndexingContext.class );
160 private StorageAsset getIndexPath( ArchivaIndexingContext ctx )
162 return ctx.getPath( );
166 interface IndexUpdateConsumer
169 void accept( IndexingContext indexingContext ) throws IndexUpdateFailedException;
173 * This method is used to do some actions around the update execution code. And to make sure, that no other
174 * method is running on the same index.
176 private void executeUpdateFunction( ArchivaIndexingContext context, IndexUpdateConsumer function ) throws IndexUpdateFailedException
178 IndexingContext indexingContext = null;
181 indexingContext = getMvnContext( context );
183 catch ( UnsupportedBaseContextException e )
185 throw new IndexUpdateFailedException( "Maven index is not supported by this context", e );
187 final StorageAsset ctxPath = getIndexPath( context );
189 boolean active = false;
190 while ( loop-- > 0 && !active )
192 active = activeContexts.add( ctxPath );
195 Thread.currentThread( ).sleep( WAIT_TIME );
197 catch ( InterruptedException e )
206 function.accept( indexingContext );
210 activeContexts.remove( ctxPath );
215 throw new IndexUpdateFailedException( "Timeout while waiting for index release on context " + context.getId( ) );
220 public void pack( final ArchivaIndexingContext context ) throws IndexUpdateFailedException
222 executeUpdateFunction( context, indexingContext -> {
225 IndexPackingRequest request = new IndexPackingRequest( indexingContext,
226 indexingContext.acquireIndexSearcher( ).getIndexReader( ),
227 indexingContext.getIndexDirectoryFile( ) );
228 indexPacker.packIndex( request );
229 indexingContext.updateTimestamp( true );
231 catch ( IOException e )
233 log.error( "IOException while packing index of context " + context.getId( ) + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ) );
234 throw new IndexUpdateFailedException( "IOException during update of " + context.getId( ), e );
242 public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
244 executeUpdateFunction( context, indexingContext -> {
245 DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
246 ScanningRequest request = new ScanningRequest( indexingContext, listener );
247 ScanningResult result = scanner.scan( request );
248 if ( result.hasExceptions( ) )
250 log.error( "Exceptions occured during index scan of " + context.getId( ) );
251 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
252 s -> log.error( "Message: " + s )
260 public void update(final ArchivaIndexingContext context, final boolean fullUpdate) throws IndexUpdateFailedException
262 log.info( "start download remote index for remote repository {}", context.getRepository( ).getId( ) );
264 if ( !( context.getRepository( ) instanceof RemoteRepository ) || !(context.getRepository().supportsFeature(RemoteIndexFeature.class)) )
266 throw new IndexUpdateFailedException( "The context is not associated to a remote repository with remote index " + context.getId( ) );
268 RemoteIndexFeature rif = context.getRepository().getFeature(RemoteIndexFeature.class).get();
269 remoteUpdateUri = context.getRepository().getLocation().resolve(rif.getIndexUri());
271 final RemoteRepository remoteRepository = (RemoteRepository) context.getRepository( );
273 executeUpdateFunction( context,
277 // create a temp directory to download files
278 Path tempIndexDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".tmpIndex" );
279 Path indexCacheDirectory = Paths.get( indexingContext.getIndexDirectoryFile( ).getParent( ), ".indexCache" );
280 Files.createDirectories( indexCacheDirectory );
281 if ( Files.exists( tempIndexDirectory ) )
283 org.apache.archiva.common.utils.FileUtils.deleteDirectory( tempIndexDirectory );
285 Files.createDirectories( tempIndexDirectory );
286 tempIndexDirectory.toFile( ).deleteOnExit( );
287 String baseIndexUrl = indexingContext.getIndexUpdateUrl( );
289 String wagonProtocol = remoteUpdateUri.toURL( ).getProtocol( );
291 NetworkProxy networkProxy = null;
292 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
294 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
295 if ( StringUtils.isNotBlank( rif.getProxyId( ) ) )
297 networkProxy = proxyRegistry.getNetworkProxy( rif.getProxyId( ) );
298 if ( networkProxy == null )
301 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
306 final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
307 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders( ) ).networkProxy(
310 int readTimeout = (int) rif.getDownloadTimeout( ).toMillis( ) * 1000;
311 wagon.setReadTimeout( readTimeout );
312 wagon.setTimeout( (int) remoteRepository.getTimeout( ).toMillis( ) * 1000 );
314 if ( wagon instanceof AbstractHttpClientWagon )
316 HttpConfiguration httpConfiguration = new HttpConfiguration( );
317 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration( );
318 httpMethodConfiguration.setUsePreemptive( true );
319 httpMethodConfiguration.setReadTimeout( readTimeout );
320 httpConfiguration.setGet( httpMethodConfiguration );
321 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
324 wagon.addTransferListener( new DownloadListener( ) );
325 ProxyInfo proxyInfo = null;
326 if ( networkProxy != null )
328 proxyInfo = new ProxyInfo( );
329 proxyInfo.setType( networkProxy.getProtocol( ) );
330 proxyInfo.setHost( networkProxy.getHost( ) );
331 proxyInfo.setPort( networkProxy.getPort( ) );
332 proxyInfo.setUserName( networkProxy.getUsername( ) );
333 proxyInfo.setPassword( new String(networkProxy.getPassword( )) );
335 AuthenticationInfo authenticationInfo = null;
336 if ( remoteRepository.getLoginCredentials( ) != null && ( remoteRepository.getLoginCredentials( ) instanceof PasswordCredentials ) )
338 PasswordCredentials creds = (PasswordCredentials) remoteRepository.getLoginCredentials( );
339 authenticationInfo = new AuthenticationInfo( );
340 authenticationInfo.setUserName( creds.getUsername( ) );
341 authenticationInfo.setPassword( new String( creds.getPassword( ) ) );
343 wagon.connect( new org.apache.maven.wagon.repository.Repository( remoteRepository.getId( ), baseIndexUrl ), authenticationInfo,
346 Path indexDirectory = indexingContext.getIndexDirectoryFile( ).toPath( );
347 if ( !Files.exists( indexDirectory ) )
349 Files.createDirectories( indexDirectory );
352 ResourceFetcher resourceFetcher =
353 new WagonResourceFetcher( log, tempIndexDirectory, wagon, remoteRepository );
354 IndexUpdateRequest request = new IndexUpdateRequest( indexingContext, resourceFetcher );
355 request.setForceFullUpdate( fullUpdate );
356 request.setLocalIndexCacheDir( indexCacheDirectory.toFile( ) );
358 indexUpdater.fetchAndUpdateIndex( request );
360 indexingContext.updateTimestamp( true );
364 catch ( AuthenticationException e )
366 log.error( "Could not login to the remote proxy for updating index of {}", remoteRepository.getId( ), e );
367 throw new IndexUpdateFailedException( "Login in to proxy failed while updating remote repository " + remoteRepository.getId( ), e );
369 catch ( ConnectionException e )
371 log.error( "Connection error during index update for remote repository {}", remoteRepository.getId( ), e );
372 throw new IndexUpdateFailedException( "Connection error during index update for remote repository " + remoteRepository.getId( ), e );
374 catch ( MalformedURLException e )
376 log.error( "URL for remote index update of remote repository {} is not correct {}", remoteRepository.getId( ), remoteUpdateUri, e );
377 throw new IndexUpdateFailedException( "URL for remote index update of repository is not correct " + remoteUpdateUri, e );
379 catch ( IOException e )
381 log.error( "IOException during index update of remote repository {}: {}", remoteRepository.getId( ), e.getMessage( ), e );
382 throw new IndexUpdateFailedException( "IOException during index update of remote repository " + remoteRepository.getId( )
383 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
385 catch ( WagonFactoryException e )
387 log.error( "Wagon for remote index download of {} could not be created: {}", remoteRepository.getId( ), e.getMessage( ), e );
388 throw new IndexUpdateFailedException( "Error while updating the remote index of " + remoteRepository.getId( ), e );
395 public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
397 final StorageAsset ctxUri = context.getPath();
398 executeUpdateFunction(context, indexingContext -> {
399 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
401 indexer.addArtifactsToIndex(artifacts, indexingContext);
402 } catch (IOException e) {
403 log.error("IOException while adding artifact {}", e.getMessage(), e);
404 throw new IndexUpdateFailedException("Error occured while adding artifact to index of "+context.getId()
405 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
411 public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
413 final StorageAsset ctxUri = context.getPath();
414 executeUpdateFunction(context, indexingContext -> {
415 Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.getFilePath().toUri().resolve(r)).toFile())).collect(Collectors.toList());
417 indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
418 } catch (IOException e) {
419 log.error("IOException while removing artifact {}", e.getMessage(), e);
420 throw new IndexUpdateFailedException("Error occured while removing artifact from index of "+context.getId()
421 + (StringUtils.isNotEmpty(e.getMessage()) ? ": "+e.getMessage() : ""));
428 public boolean supportsRepository( RepositoryType type )
430 return type == RepositoryType.MAVEN;
434 public ArchivaIndexingContext createContext( Repository repository ) throws IndexCreationFailedException
436 log.debug("Creating context for repo {}, type: {}", repository.getId(), repository.getType());
437 if ( repository.getType( ) != RepositoryType.MAVEN )
439 throw new UnsupportedRepositoryTypeException( repository.getType( ) );
441 IndexingContext mvnCtx = null;
444 if ( repository instanceof RemoteRepository )
446 mvnCtx = createRemoteContext( (RemoteRepository) repository );
448 else if ( repository instanceof ManagedRepository )
450 mvnCtx = createManagedContext( (ManagedRepository) repository );
453 catch ( IOException e )
455 log.error( "IOException during context creation " + e.getMessage( ), e );
456 throw new IndexCreationFailedException( "Could not create index context for repository " + repository.getId( )
457 + ( StringUtils.isNotEmpty( e.getMessage( ) ) ? ": " + e.getMessage( ) : "" ), e );
460 return new MavenIndexContext( repository, mvnCtx );
464 public ArchivaIndexingContext reset(ArchivaIndexingContext context) throws IndexUpdateFailedException {
465 ArchivaIndexingContext ctx;
466 executeUpdateFunction(context, indexingContext -> {
468 indexingContext.close(true);
469 } catch (IOException e) {
470 log.warn("Index close failed");
473 StorageUtil.deleteRecursively(context.getPath());
474 } catch (IOException e) {
475 throw new IndexUpdateFailedException("Could not delete index files");
479 Repository repo = context.getRepository();
480 ctx = createContext(context.getRepository());
481 if (repo instanceof EditableRepository) {
482 ((EditableRepository)repo).setIndexingContext(ctx);
484 } catch (IndexCreationFailedException e) {
485 throw new IndexUpdateFailedException("Could not create index");
491 public ArchivaIndexingContext move(ArchivaIndexingContext context, Repository repo) throws IndexCreationFailedException {
495 if (context.supports(IndexingContext.class)) {
497 StorageAsset newPath = getIndexPath(repo);
498 IndexingContext ctx = context.getBaseContext(IndexingContext.class);
499 Path oldPath = ctx.getIndexDirectoryFile().toPath();
500 if (oldPath.equals(newPath)) {
501 // Nothing to do, if path does not change
504 if (!Files.exists(oldPath)) {
505 return createContext(repo);
506 } else if (context.isEmpty()) {
508 return createContext(repo);
510 context.close(false);
511 Files.move(oldPath, newPath.getFilePath());
512 return createContext(repo);
514 } catch (IOException e) {
515 log.error("IOException while moving index directory {}", e.getMessage(), e);
516 throw new IndexCreationFailedException("Could not recreated the index.", e);
517 } catch (UnsupportedBaseContextException e) {
518 throw new IndexCreationFailedException("The given context, is not a maven context.");
521 throw new IndexCreationFailedException("Bad context type. This is not a maven context.");
526 public void updateLocalIndexPath(Repository repo) {
527 if (repo.supportsFeature(IndexCreationFeature.class)) {
528 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
530 icf.setLocalIndexPath(getIndexPath(repo));
531 icf.setLocalPackedIndexPath(getPackedIndexPath(repo));
532 } catch (IOException e) {
533 log.error("Could not set local index path for {}. New URI: {}", repo.getId(), icf.getIndexPath());
539 public ArchivaIndexingContext mergeContexts(Repository destinationRepo, List<ArchivaIndexingContext> contexts,
540 boolean packIndex) throws UnsupportedOperationException,
541 IndexCreationFailedException, IllegalArgumentException {
542 if (!destinationRepo.supportsFeature(IndexCreationFeature.class)) {
543 throw new IllegalArgumentException("The given repository does not support the indexcreation feature");
545 Path mergedIndexDirectory = null;
547 mergedIndexDirectory = Files.createTempDirectory("archivaMergedIndex");
548 } catch (IOException e) {
549 log.error("Could not create temporary directory for merged index: {}", e.getMessage(), e);
550 throw new IndexCreationFailedException("IO error while creating temporary directory for merged index: "+e.getMessage(), e);
552 IndexCreationFeature indexCreationFeature = destinationRepo.getFeature(IndexCreationFeature.class).get();
553 if (indexCreationFeature.getLocalIndexPath()== null) {
554 throw new IllegalArgumentException("The given repository does not have a local index path");
556 StorageAsset destinationPath = indexCreationFeature.getLocalIndexPath();
558 String tempRepoId = mergedIndexDirectory.getFileName().toString();
562 Path indexLocation = destinationPath.getFilePath();
564 List<IndexingContext> members = contexts.stream( ).filter(ctx -> ctx.supports(IndexingContext.class)).map( ctx ->
567 return ctx.getBaseContext(IndexingContext.class);
568 } catch (UnsupportedBaseContextException e) {
569 // does not happen here
572 }).filter( Objects::nonNull ).collect( Collectors.toList() );
573 ContextMemberProvider memberProvider = new StaticContextMemberProvider(members);
574 IndexingContext mergedCtx = indexer.createMergedIndexingContext( tempRepoId, tempRepoId, mergedIndexDirectory.toFile(),
575 indexLocation.toFile(), true, memberProvider);
576 mergedCtx.optimize();
580 IndexPackingRequest request = new IndexPackingRequest( mergedCtx, //
581 mergedCtx.acquireIndexSearcher().getIndexReader(), //
582 indexLocation.toFile() );
583 indexPacker.packIndex( request );
586 return new MavenIndexContext(destinationRepo, mergedCtx);
588 catch ( IOException e)
590 throw new IndexCreationFailedException( "IO Error during index merge: "+ e.getMessage(), e );
594 private StorageAsset getIndexPath(URI indexDirUri, RepositoryStorage repoStorage, String defaultDir) throws IOException
596 StorageAsset rootAsset = repoStorage.getAsset("");
597 RepositoryStorage storage = rootAsset.getStorage();
599 Path repositoryPath = rootAsset.getFilePath().toAbsolutePath();
600 StorageAsset indexDir;
601 if ( ! StringUtils.isEmpty(indexDirUri.toString( ) ) )
604 indexDirectory = PathUtil.getPathFromUri( indexDirUri );
605 // not absolute so create it in repository directory
606 if ( indexDirectory.isAbsolute( ) && !indexDirectory.startsWith(repositoryPath))
608 if (storage instanceof FilesystemStorage) {
609 FilesystemStorage fsStorage = (FilesystemStorage) storage;
610 FilesystemStorage indexStorage = new FilesystemStorage(indexDirectory.getParent(), fsStorage.getFileLockManager());
611 indexDir = indexStorage.getAsset(indexDirectory.getFileName().toString());
613 throw new IOException("The given storage is not file based.");
615 } else if (indexDirectory.isAbsolute()) {
616 indexDir = storage.getAsset(repositoryPath.relativize(indexDirectory).toString());
620 indexDir = storage.getAsset(indexDirectory.toString());
625 indexDir = storage.getAsset( defaultDir );
628 if ( !indexDir.exists() )
630 indexDir = storage.addAsset(indexDir.getPath(), true);
635 private StorageAsset getIndexPath( Repository repo) throws IOException {
636 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
637 return getIndexPath( icf.getIndexPath(), repo, DEFAULT_INDEX_PATH);
640 private StorageAsset getPackedIndexPath(Repository repo) throws IOException {
641 IndexCreationFeature icf = repo.getFeature(IndexCreationFeature.class).get();
642 return getIndexPath(icf.getPackedIndexPath(), repo, DEFAULT_PACKED_INDEX_PATH);
645 private IndexingContext createRemoteContext(RemoteRepository remoteRepository ) throws IOException
647 String contextKey = "remote-" + remoteRepository.getId( );
650 // create remote repository path
651 Path repoDir = remoteRepository.getAsset( "" ).getFilePath();
652 if ( !Files.exists( repoDir ) )
654 Files.createDirectories( repoDir );
657 StorageAsset indexDirectory;
659 // is there configured indexDirectory ?
660 if ( remoteRepository.supportsFeature( RemoteIndexFeature.class ) )
662 RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
663 indexDirectory = getIndexPath(remoteRepository);
664 String remoteIndexUrl = calculateIndexRemoteUrl( remoteRepository.getLocation( ), rif );
668 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
670 catch ( IndexFormatTooOldException e )
672 // existing index with an old lucene format so we need to delete it!!!
673 // delete it first then recreate it.
674 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
675 remoteRepository.getId( ) );
676 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory.getFilePath() );
677 return getIndexingContext( remoteRepository, contextKey, repoDir, indexDirectory, remoteIndexUrl );
683 throw new IOException( "No remote index defined" );
687 private IndexingContext getIndexingContext( Repository repository, String contextKey, Path repoDir, StorageAsset indexDirectory, String indexUrl ) throws IOException
691 if (!Files.exists(indexDirectory.getFilePath())) {
692 Files.createDirectories(indexDirectory.getFilePath());
694 return indexer.createIndexingContext( contextKey, repository.getId( ), repoDir.toFile( ), indexDirectory.getFilePath( ).toFile( ),
695 repository.getLocation( ) == null ? null : repository.getLocation( ).toString( ),
699 } catch (Exception e) {
700 log.error("Could not create index for asset {}", indexDirectory);
701 throw new IOException(e);
705 private IndexingContext createManagedContext( ManagedRepository repository ) throws IOException
708 IndexingContext context;
709 // take care first about repository location as can be relative
710 Path repositoryDirectory = repository.getAsset( "" ).getFilePath();
712 if ( !Files.exists( repositoryDirectory ) )
716 Files.createDirectories( repositoryDirectory );
718 catch ( IOException e )
720 log.error( "Could not create directory {}", repositoryDirectory );
724 StorageAsset indexDirectory;
726 if ( repository.supportsFeature( IndexCreationFeature.class ) )
728 indexDirectory = getIndexPath(repository);
729 log.debug( "Preparing index at {}", indexDirectory );
731 String indexUrl = repositoryDirectory.toUri( ).toURL( ).toExternalForm( );
734 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
735 context.setSearchable( repository.isScanned( ) );
737 catch ( IndexFormatTooOldException e )
739 // existing index with an old lucene format so we need to delete it!!!
740 // delete it first then recreate it.
741 log.warn( "the index of repository {} is too old we have to delete and recreate it", //
742 repository.getId( ) );
743 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDirectory.getFilePath() );
744 context = getIndexingContext( repository, repository.getId( ), repositoryDirectory, indexDirectory, indexUrl );
745 context.setSearchable( repository.isScanned( ) );
751 throw new IOException( "No repository index defined" );
755 private String calculateIndexRemoteUrl( URI baseUri, RemoteIndexFeature rif )
757 if ( rif.getIndexUri( ) == null )
759 return baseUri.resolve( DEFAULT_INDEX_PATH ).toString( );
763 return baseUri.resolve( rif.getIndexUri( ) ).toString( );
767 private static final class DownloadListener
768 implements TransferListener
770 private Logger log = LoggerFactory.getLogger( getClass( ) );
772 private String resourceName;
774 private long startTime;
776 private int totalLength = 0;
779 public void transferInitiated( TransferEvent transferEvent )
781 startTime = System.currentTimeMillis( );
782 resourceName = transferEvent.getResource( ).getName( );
783 log.debug( "initiate transfer of {}", resourceName );
787 public void transferStarted( TransferEvent transferEvent )
789 this.totalLength = 0;
790 resourceName = transferEvent.getResource( ).getName( );
791 log.info( "start transfer of {}", transferEvent.getResource( ).getName( ) );
795 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
797 log.debug( "transfer of {} : {}/{}", transferEvent.getResource( ).getName( ), buffer.length, length );
798 this.totalLength += length;
802 public void transferCompleted( TransferEvent transferEvent )
804 resourceName = transferEvent.getResource( ).getName( );
805 long endTime = System.currentTimeMillis( );
806 log.info( "end of transfer file {} {} kb: {}s", transferEvent.getResource( ).getName( ),
807 this.totalLength / 1024, ( endTime - startTime ) / 1000 );
811 public void transferError( TransferEvent transferEvent )
813 log.info( "error of transfer file {}: {}", transferEvent.getResource( ).getName( ),
814 transferEvent.getException( ).getMessage( ), transferEvent.getException( ) );
818 public void debug( String message )
820 log.debug( "transfer debug {}", message );
824 private static class WagonResourceFetcher
825 implements ResourceFetcher
830 Path tempIndexDirectory;
834 RemoteRepository remoteRepository;
836 private WagonResourceFetcher( Logger log, Path tempIndexDirectory, Wagon wagon,
837 RemoteRepository remoteRepository )
840 this.tempIndexDirectory = tempIndexDirectory;
842 this.remoteRepository = remoteRepository;
846 public void connect( String id, String url ) {
851 public void disconnect( ) {
856 public InputStream retrieve( String name )
860 log.info( "index update retrieve file, name:{}", name );
861 Path file = tempIndexDirectory.resolve( name );
862 Files.deleteIfExists( file );
863 file.toFile( ).deleteOnExit( );
864 wagon.get( addParameters( name, remoteRepository ), file.toFile( ) );
865 return Files.newInputStream( file );
867 catch ( AuthorizationException | TransferFailedException e )
869 throw new IOException( e.getMessage( ), e );
871 catch ( ResourceDoesNotExistException e )
873 FileNotFoundException fnfe = new FileNotFoundException( e.getMessage( ) );
879 // FIXME remove crappy copy/paste
880 protected String addParameters( String path, RemoteRepository remoteRepository )
882 if ( remoteRepository.getExtraParameters( ).isEmpty( ) )
887 boolean question = false;
889 StringBuilder res = new StringBuilder( path == null ? "" : path );
891 for ( Map.Entry<String, String> entry : remoteRepository.getExtraParameters( ).entrySet( ) )
895 res.append( '?' ).append( entry.getKey( ) ).append( '=' ).append( entry.getValue( ) );
899 return res.toString( );