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.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.lucene.search.BooleanClause;
31 import org.apache.lucene.search.BooleanQuery;
32 import org.apache.maven.index.ArtifactContext;
33 import org.apache.maven.index.ArtifactContextProducer;
34 import org.apache.maven.index.FlatSearchRequest;
35 import org.apache.maven.index.FlatSearchResponse;
36 import org.apache.maven.index.MAVEN;
37 import org.apache.maven.index.NexusIndexer;
38 import org.apache.maven.index.artifact.IllegalArtifactCoordinateException;
39 import org.apache.maven.index.context.IndexCreator;
40 import org.apache.maven.index.context.IndexingContext;
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;
82 private ManagedRepositoryAdmin managedRepositoryAdmin;
84 private NexusIndexer nexusIndexer;
86 private List<? extends IndexCreator> allIndexCreators;
89 public void initialize()
90 throws PlexusSisuBridgeException
92 log.info( "Initialized {}", this.getClass().getName() );
94 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
96 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
98 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
100 allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
103 public void executeTask( Task task )
104 throws TaskExecutionException
106 synchronized ( nexusIndexer )
108 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
110 ManagedRepository repository = indexingTask.getRepository();
111 IndexingContext context = indexingTask.getContext();
113 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
114 && indexingTask.isExecuteOnEntireRepo() )
118 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
120 catch ( IOException e )
122 throw new TaskExecutionException( "Error scan repository " + repository, e );
124 log.debug( "Finishing indexing task on repo: {}", repository.getId() );
125 finishIndexingTask( indexingTask, repository, context );
129 // create context if not a repo scan request
130 if ( !indexingTask.isExecuteOnEntireRepo() )
134 log.debug( "Creating indexing context on resource: {}",
135 indexingTask.getResourceFile().getPath() );
136 context = managedRepositoryAdmin.createIndexContext( repository );
138 catch ( RepositoryAdminException e )
140 log.error( "Error occurred while creating context: " + e.getMessage() );
141 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(),
146 if ( context == null || context.getIndexDirectory() == null )
148 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
153 File artifactFile = indexingTask.getResourceFile();
154 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
158 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
160 //IndexSearcher s = context.getIndexSearcher();
161 //String uinfo = ac.getArtifactInfo().getUinfo();
162 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
164 BooleanQuery q = new BooleanQuery();
165 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
166 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
167 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
168 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
169 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
170 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
171 if ( ac.getArtifactInfo().classifier != null )
173 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
174 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
176 if ( ac.getArtifactInfo().packaging != null )
178 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
179 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
181 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
182 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
183 if ( flatSearchResponse.getResults().isEmpty() )
185 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
186 nexusIndexer.addArtifactToIndex( ac, context );
190 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
191 // TODO check if update exists !!
192 nexusIndexer.deleteArtifactFromIndex( ac, context );
193 nexusIndexer.addArtifactToIndex( ac, context );
196 context.updateTimestamp();
198 // close the context if not a repo scan request
199 if ( !indexingTask.isExecuteOnEntireRepo() )
201 log.debug( "Finishing indexing task on resource file : {}",
202 indexingTask.getResourceFile().getPath() );
203 finishIndexingTask( indexingTask, repository, context );
208 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
209 nexusIndexer.deleteArtifactFromIndex( ac, context );
213 catch ( IOException e )
215 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
217 throw new TaskExecutionException(
218 "Error occurred while executing indexing task '" + indexingTask + "'", e );
220 catch ( IllegalArtifactCoordinateException e )
222 log.error( "Error occurred while getting artifact context: " + e.getMessage() );
223 throw new TaskExecutionException( "Error occurred while getting artifact context.", e );
229 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
230 IndexingContext context )
231 throws TaskExecutionException
238 File managedRepository = new File( repository.getLocation() );
239 String indexDirectory = repository.getIndexDirectory();
240 final File indexLocation = StringUtils.isBlank( indexDirectory )
241 ? new File( managedRepository, ".indexer" )
242 : new File( indexDirectory );
243 IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
244 indexPacker.packIndex( request );
245 context.updateTimestamp( true );
247 log.debug( "Index file packaged at '{}'.", indexLocation.getPath() );
250 catch ( IOException e )
252 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
253 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
258 public void setIndexPacker( IndexPacker indexPacker )
260 this.indexPacker = indexPacker;
263 public PlexusSisuBridge getPlexusSisuBridge()
265 return plexusSisuBridge;
268 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
270 this.plexusSisuBridge = plexusSisuBridge;