1 package org.apache.archiva.scheduler.indexing;
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.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26 import org.apache.lucene.search.BooleanClause;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.index.ArtifactContext;
30 import org.apache.maven.index.ArtifactContextProducer;
31 import org.apache.maven.index.DefaultArtifactContextProducer;
32 import org.apache.maven.index.FlatSearchRequest;
33 import org.apache.maven.index.FlatSearchResponse;
34 import org.apache.maven.index.MAVEN;
35 import org.apache.maven.index.NexusIndexer;
36 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
37 import org.apache.maven.index.context.IndexCreator;
38 import org.apache.maven.index.context.IndexingContext;
39 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
40 import org.apache.maven.index.expr.SourcedSearchExpression;
41 import org.apache.maven.index.packer.IndexPacker;
42 import org.apache.maven.index.packer.IndexPackingRequest;
43 import org.codehaus.plexus.taskqueue.Task;
44 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
45 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.stereotype.Service;
50 import javax.annotation.PostConstruct;
51 import javax.inject.Inject;
53 import java.io.IOException;
54 import java.util.List;
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 * plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
62 * instantiation-strategy="singleton"
64 @Service( "taskExecutor#indexing" )
65 public class ArchivaIndexingTaskExecutor
66 implements TaskExecutor
68 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
73 private IndexPacker indexPacker;
75 private ArtifactContextProducer artifactContextProducer;
78 private PlexusSisuBridge plexusSisuBridge;
81 private MavenIndexerUtils mavenIndexerUtils;
83 private NexusIndexer nexusIndexer;
85 private List<? extends IndexCreator> allIndexCreators;
88 public void initialize()
89 throws PlexusSisuBridgeException
91 log.info( "Initialized {}", this.getClass().getName() );
93 artifactContextProducer = new DefaultArtifactContextProducer();
95 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
97 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
99 allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
102 public void executeTask( Task task )
103 throws TaskExecutionException
105 synchronized ( nexusIndexer )
107 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
109 ManagedRepositoryConfiguration repository = indexingTask.getRepository();
110 IndexingContext context = indexingTask.getContext();
112 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
113 && indexingTask.isExecuteOnEntireRepo() )
117 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
119 catch ( IOException e )
121 throw new TaskExecutionException( "Error scan repository " + repository, e );
123 log.debug( "Finishing indexing task on repo: {}", repository.getId() );
124 finishIndexingTask( indexingTask, repository, context );
128 // create context if not a repo scan request
129 if ( !indexingTask.isExecuteOnEntireRepo() )
133 log.debug( "Creating indexing context on resource: {}",
134 indexingTask.getResourceFile().getPath() );
135 context = ArtifactIndexingTask.createContext( repository, nexusIndexer, allIndexCreators );
137 catch ( IOException e )
139 log.error( "Error occurred while creating context: " + e.getMessage() );
140 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
143 catch ( UnsupportedExistingLuceneIndexException e )
145 log.error( "Error occurred while creating context: " + e.getMessage() );
146 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
151 if ( context == null || context.getIndexDirectory() == null )
153 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
158 File artifactFile = indexingTask.getResourceFile();
159 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
163 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
165 //IndexSearcher s = context.getIndexSearcher();
166 //String uinfo = ac.getArtifactInfo().getUinfo();
167 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
169 BooleanQuery q = new BooleanQuery();
170 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
171 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
172 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
173 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
174 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
175 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
176 if ( ac.getArtifactInfo().classifier != null )
178 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
179 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
181 if ( ac.getArtifactInfo().packaging != null )
183 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
184 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
186 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
187 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
188 if ( flatSearchResponse.getResults().isEmpty() )
190 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
191 nexusIndexer.addArtifactToIndex( ac, context );
195 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
196 // TODO check if update exists !!
197 nexusIndexer.deleteArtifactFromIndex( ac, context );
198 nexusIndexer.addArtifactToIndex( ac, context );
201 //nexusIndexer.scan( context, true );
203 context.updateTimestamp();
205 // close the context if not a repo scan request
206 if ( !indexingTask.isExecuteOnEntireRepo() )
208 log.debug( "Finishing indexing task on resource file : {}",
209 indexingTask.getResourceFile().getPath() );
210 finishIndexingTask( indexingTask, repository, context );
215 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
216 nexusIndexer.deleteArtifactFromIndex( ac, context );
220 catch ( IOException e )
222 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
224 throw new TaskExecutionException(
225 "Error occurred while executing indexing task '" + indexingTask + "'", e );
227 catch ( IllegalArtifactCoordinateException e )
229 log.error( "Error occurred while getting artifact context: " + e.getMessage() );
230 throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
236 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepositoryConfiguration repository,
237 IndexingContext context )
238 throws TaskExecutionException
246 File managedRepository = new File( repository.getLocation() );
247 final File indexLocation = new File( managedRepository, ".index" );
248 IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
249 indexPacker.packIndex( request );
251 log.debug( "Index file packaged at '{}'.", indexLocation.getPath() );
254 catch ( IOException e )
256 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
257 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
263 olamy don't close it anymore as it nullify IndexSearcher
264 if ( context != null )
268 context.close( false );
270 catch ( IOException e )
272 log.error( "Error occurred while closing context: " + e.getMessage() );
273 throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
280 public void setIndexPacker( IndexPacker indexPacker )
282 this.indexPacker = indexPacker;
285 public PlexusSisuBridge getPlexusSisuBridge()
287 return plexusSisuBridge;
290 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
292 this.plexusSisuBridge = plexusSisuBridge;