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.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.lucene.search.BooleanClause;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.maven.index.ArtifactContext;
31 import org.apache.maven.index.ArtifactContextProducer;
32 import org.apache.maven.index.DefaultArtifactContextProducer;
33 import org.apache.maven.index.FlatSearchRequest;
34 import org.apache.maven.index.FlatSearchResponse;
35 import org.apache.maven.index.MAVEN;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
38 import org.apache.maven.index.context.IndexCreator;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
41 import org.apache.maven.index.expr.SourcedSearchExpression;
42 import org.apache.maven.index.packer.IndexPacker;
43 import org.apache.maven.index.packer.IndexPackingRequest;
44 import org.codehaus.plexus.taskqueue.Task;
45 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
46 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Service;
51 import javax.annotation.PostConstruct;
52 import javax.inject.Inject;
54 import java.io.IOException;
55 import java.util.List;
58 * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
59 * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
60 * 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 = plexusSisuBridge.lookup( ArtifactContextProducer.class );
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 ManagedRepository 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 context.updateTimestamp();
201 // close the context if not a repo scan request
202 if ( !indexingTask.isExecuteOnEntireRepo() )
204 log.debug( "Finishing indexing task on resource file : {}",
205 indexingTask.getResourceFile().getPath() );
206 finishIndexingTask( indexingTask, repository, context );
211 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
212 nexusIndexer.deleteArtifactFromIndex( ac, context );
216 catch ( IOException e )
218 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
220 throw new TaskExecutionException(
221 "Error occurred while executing indexing task '" + indexingTask + "'", e );
223 catch ( IllegalArtifactCoordinateException e )
225 log.error( "Error occurred while getting artifact context: " + e.getMessage() );
226 throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
232 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
233 IndexingContext context )
234 throws TaskExecutionException
241 File managedRepository = new File( repository.getLocation() );
242 String indexDirectory = repository.getIndexDirectory();
243 final File indexLocation = StringUtils.isBlank( indexDirectory )
244 ? new File( managedRepository, ".indexer" )
245 : new File( indexDirectory );
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 + "'",
260 public void setIndexPacker( IndexPacker indexPacker )
262 this.indexPacker = indexPacker;
265 public PlexusSisuBridge getPlexusSisuBridge()
267 return plexusSisuBridge;
270 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
272 this.plexusSisuBridge = plexusSisuBridge;