1 package org.apache.archiva.scheduler.indexing.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 Li
16 * cense is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.UnsupportedBaseContextException;
25 import org.apache.archiva.redback.components.taskqueue.Task;
26 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
27 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
28 import org.apache.archiva.repository.ManagedRepository;
29 import org.apache.archiva.repository.features.IndexCreationFeature;
30 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
31 import org.apache.maven.index.ArtifactContext;
32 import org.apache.maven.index.ArtifactContextProducer;
33 import org.apache.maven.index.DefaultScannerListener;
34 import org.apache.maven.index.FlatSearchRequest;
35 import org.apache.maven.index.FlatSearchResponse;
36 import org.apache.maven.index.Indexer;
37 import org.apache.maven.index.IndexerEngine;
38 import org.apache.maven.index.MAVEN;
39 import org.apache.maven.index.Scanner;
40 import org.apache.maven.index.ScanningRequest;
41 import org.apache.maven.index.ScanningResult;
42 import org.apache.maven.index.context.IndexingContext;
43 import org.apache.maven.index.expr.SourcedSearchExpression;
44 import org.apache.maven.index.packer.IndexPacker;
45 import org.apache.maven.index.packer.IndexPackingRequest;
46 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
47 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.stereotype.Service;
52 import javax.inject.Inject;
53 import java.io.IOException;
54 import java.nio.file.Path;
57 * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
58 * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
59 * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
61 @Service( "taskExecutor#indexing" )
62 public class ArchivaIndexingTaskExecutor
63 implements TaskExecutor
65 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
68 private IndexPacker indexPacker;
71 private ArtifactContextProducer artifactContextProducer;
74 private Indexer indexer;
77 private Scanner scanner;
80 IndexerEngine indexerEngine;
83 * depending on current {@link Task} you have.
84 * If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action#FINISH} && isExecuteOnEntireRepo:
85 * repository will be scanned.
88 * @throws TaskExecutionException
91 public void executeTask( Task task )
92 throws TaskExecutionException
94 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
96 ManagedRepository repository = indexingTask.getRepository( );
97 ArchivaIndexingContext archivaContext = indexingTask.getContext( );
98 IndexingContext context = null;
101 context = archivaContext.getBaseContext( IndexingContext.class );
103 catch ( UnsupportedBaseContextException e )
105 throw new TaskExecutionException( "Bad repository type.", e );
108 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction( ) )
109 && indexingTask.isExecuteOnEntireRepo( ) )
111 long start = System.currentTimeMillis( );
114 context.updateTimestamp( );
115 DefaultScannerListener listener = new DefaultScannerListener( context, indexerEngine, true, null );
116 ScanningRequest request = new ScanningRequest( context, listener );
117 ScanningResult result = scanner.scan( request );
118 if ( result.hasExceptions( ) )
120 log.error( "Exceptions occured during index scan of " + context.getId( ) );
121 result.getExceptions( ).stream( ).map( e -> e.getMessage( ) ).distinct( ).limit( 5 ).forEach(
122 s -> log.error( "Message: " + s )
126 catch ( IOException e )
128 log.error( "Error during context scan {}: {}", context.getId( ), context.getIndexDirectory( ) );
130 long end = System.currentTimeMillis( );
131 log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId( ),
132 indexingTask.isOnlyUpdate( ), ( end - start ) );
133 log.debug( "Finishing indexing task on repo: {}", repository.getId( ) );
134 finishIndexingTask( indexingTask, repository, context );
138 // create context if not a repo scan request
139 if ( !indexingTask.isExecuteOnEntireRepo( ) )
143 log.debug( "Creating indexing context on resource: {}", //
144 ( indexingTask.getResourceFile( ) == null
146 : indexingTask.getResourceFile( ) ) );
147 archivaContext = repository.getIndexingContext( );
148 context = archivaContext.getBaseContext( IndexingContext.class );
150 catch ( UnsupportedBaseContextException e )
152 log.error( "Error occurred while creating context: {}", e.getMessage( ) );
153 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage( ), e );
157 if ( context == null || context.getIndexDirectory( ) == null )
159 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
164 Path artifactFile = indexingTask.getResourceFile( );
165 if ( artifactFile == null )
167 log.debug( "no artifact pass in indexing task so skip it" );
171 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile.toFile( ) );
175 // MRM-1779 pom must be indexed too
176 // TODO make that configurable?
177 if ( artifactFile.getFileName( ).toString( ).endsWith( ".pom" ) )
179 ac.getArtifactInfo( ).setFileExtension( "pom" );
180 ac.getArtifactInfo( ).setPackaging( "pom" );
181 ac.getArtifactInfo( ).setClassifier( "pom" );
183 if ( indexingTask.getAction( ).equals( ArtifactIndexingTask.Action.ADD ) )
185 //IndexSearcher s = context.getIndexSearcher();
186 //String uinfo = ac.getArtifactInfo().getUinfo();
187 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
189 BooleanQuery.Builder qb = new BooleanQuery.Builder();
190 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
191 ac.getArtifactInfo( ).getGroupId( ) ) ), BooleanClause.Occur.MUST );
192 qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
193 ac.getArtifactInfo( ).getArtifactId( ) ) ), BooleanClause.Occur.MUST );
194 qb.add( indexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
195 ac.getArtifactInfo( ).getVersion( ) ) ), BooleanClause.Occur.MUST );
196 if ( ac.getArtifactInfo( ).getClassifier( ) != null )
198 qb.add( indexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
199 ac.getArtifactInfo( ).getClassifier( ) ) ), BooleanClause.Occur.MUST );
201 if ( ac.getArtifactInfo( ).getPackaging( ) != null )
203 qb.add( indexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
204 ac.getArtifactInfo( ).getPackaging( ) ) ), BooleanClause.Occur.MUST );
206 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( qb.build(), context );
207 FlatSearchResponse flatSearchResponse = indexer.searchFlat( flatSearchRequest );
208 if ( flatSearchResponse.getResults( ).isEmpty( ) )
210 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo( ) );
211 indexerEngine.index( context, ac );
215 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo( ) );
216 // TODO check if update exists !!
217 indexerEngine.update( context, ac );
220 context.updateTimestamp( );
227 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo( ) );
228 indexerEngine.remove( context, ac );
232 // close the context if not a repo scan request
233 if ( !indexingTask.isExecuteOnEntireRepo( ) )
235 log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile( ) != null
236 ? indexingTask.getResourceFile( )
238 finishIndexingTask( indexingTask, repository, context );
241 catch ( IOException e )
243 log.error( "Error occurred while executing indexing task '{}': {}", indexingTask, e.getMessage( ),
245 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
252 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
253 IndexingContext context )
254 throws TaskExecutionException
259 log.debug( "Finishing indexing" );
262 if ( repository.supportsFeature( IndexCreationFeature.class ) )
264 IndexCreationFeature icf = repository.getFeature( IndexCreationFeature.class ).get( );
265 if ( !icf.isSkipPackedIndexCreation( ) && icf.getLocalPackedIndexPath( ) != null && icf.getLocalIndexPath().getFilePath()!=null )
268 log.debug( "Creating packed index from {} on {}", context.getIndexDirectoryFile( ), icf.getLocalPackedIndexPath( ) );
269 IndexPackingRequest request = new IndexPackingRequest( context, //
270 context.acquireIndexSearcher( ).getIndexReader( ),
272 icf.getLocalPackedIndexPath( ).getFilePath().toFile( ) );
274 indexPacker.packIndex( request );
275 context.updateTimestamp( true );
277 log.debug( "Index file packed at '{}'.", icf.getLocalPackedIndexPath( ) );
281 log.debug( "skip packed index creation" );
286 log.debug( "skip packed index creation" );
289 catch ( IOException e )
291 log.error( "Error occurred while executing indexing task '{}': {}", indexingTask, e.getMessage( ) );
292 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
297 public void setIndexPacker( IndexPacker indexPacker )
299 this.indexPacker = indexPacker;