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.
62 @Service( "taskExecutor#indexing" )
63 public class ArchivaIndexingTaskExecutor
64 implements TaskExecutor
66 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
71 private IndexPacker indexPacker;
73 private ArtifactContextProducer artifactContextProducer;
76 private PlexusSisuBridge plexusSisuBridge;
79 private MavenIndexerUtils mavenIndexerUtils;
81 private NexusIndexer nexusIndexer;
83 private List<? extends IndexCreator> allIndexCreators;
86 public void initialize()
87 throws PlexusSisuBridgeException
89 log.info( "Initialized {}", this.getClass().getName() );
91 artifactContextProducer = new DefaultArtifactContextProducer();
93 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
95 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
97 allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
100 public void executeTask( Task task )
101 throws TaskExecutionException
103 synchronized ( nexusIndexer )
105 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
107 ManagedRepositoryConfiguration repository = indexingTask.getRepository();
108 IndexingContext context = indexingTask.getContext();
110 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
111 && indexingTask.isExecuteOnEntireRepo() )
115 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
117 catch ( IOException e )
119 throw new TaskExecutionException( "Error scan repository " + repository, e );
121 log.debug( "Finishing indexing task on repo: {}", repository.getId() );
122 finishIndexingTask( indexingTask, repository, context );
126 // create context if not a repo scan request
127 if ( !indexingTask.isExecuteOnEntireRepo() )
131 log.debug( "Creating indexing context on resource: {}",
132 indexingTask.getResourceFile().getPath() );
133 context = ArtifactIndexingTask.createContext( repository, nexusIndexer, allIndexCreators );
135 catch ( IOException e )
137 log.error( "Error occurred while creating context: " + e.getMessage() );
138 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
141 catch ( UnsupportedExistingLuceneIndexException e )
143 log.error( "Error occurred while creating context: " + e.getMessage() );
144 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
149 if ( context == null || context.getIndexDirectory() == null )
151 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
156 File artifactFile = indexingTask.getResourceFile();
157 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
161 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
163 //IndexSearcher s = context.getIndexSearcher();
164 //String uinfo = ac.getArtifactInfo().getUinfo();
165 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
167 BooleanQuery q = new BooleanQuery();
168 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
169 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
170 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
171 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
172 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
173 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
174 if ( ac.getArtifactInfo().classifier != null )
176 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
177 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
179 if ( ac.getArtifactInfo().packaging != null )
181 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
182 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
184 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
185 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
186 if ( flatSearchResponse.getResults().isEmpty() )
188 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
189 nexusIndexer.addArtifactToIndex( ac, context );
193 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
194 // TODO check if update exists !!
195 nexusIndexer.deleteArtifactFromIndex( ac, context );
196 nexusIndexer.addArtifactToIndex( ac, context );
199 //nexusIndexer.scan( context, true );
201 context.updateTimestamp();
203 // close the context if not a repo scan request
204 if ( !indexingTask.isExecuteOnEntireRepo() )
206 log.debug( "Finishing indexing task on resource file : {}",
207 indexingTask.getResourceFile().getPath() );
208 finishIndexingTask( indexingTask, repository, context );
213 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
214 nexusIndexer.deleteArtifactFromIndex( ac, context );
218 catch ( IOException e )
220 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
222 throw new TaskExecutionException(
223 "Error occurred while executing indexing task '" + indexingTask + "'", e );
225 catch ( IllegalArtifactCoordinateException e )
227 log.error( "Error occurred while getting artifact context: " + e.getMessage() );
228 throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
234 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepositoryConfiguration repository,
235 IndexingContext context )
236 throws TaskExecutionException
244 File managedRepository = new File( repository.getLocation() );
245 final File indexLocation = new File( managedRepository, ".index" );
246 IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
247 indexPacker.packIndex( request );
249 log.debug( "Index file packaged at '{}'.", indexLocation.getPath() );
252 catch ( IOException e )
254 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
255 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
261 olamy don't close it anymore as it nullify IndexSearcher
262 if ( context != null )
266 context.close( false );
268 catch ( IOException e )
270 log.error( "Error occurred while closing context: " + e.getMessage() );
271 throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
278 public void setIndexPacker( IndexPacker indexPacker )
280 this.indexPacker = indexPacker;
283 public PlexusSisuBridge getPlexusSisuBridge()
285 return plexusSisuBridge;
288 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
290 this.plexusSisuBridge = plexusSisuBridge;