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.PlexusSisuBridge;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
28 import org.apache.archiva.redback.components.taskqueue.Task;
29 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
30 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.maven.index.ArtifactContext;
34 import org.apache.maven.index.ArtifactContextProducer;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.MAVEN;
38 import org.apache.maven.index.NexusIndexer;
39 import org.apache.maven.index.context.IndexingContext;
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.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Service;
47 import javax.annotation.PostConstruct;
48 import javax.inject.Inject;
50 import java.io.IOException;
53 * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
54 * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
55 * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
57 @Service ( "taskExecutor#indexing" )
58 public class ArchivaIndexingTaskExecutor
59 implements TaskExecutor
61 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
63 private IndexPacker indexPacker;
65 private ArtifactContextProducer artifactContextProducer;
68 private PlexusSisuBridge plexusSisuBridge;
71 private ManagedRepositoryAdmin managedRepositoryAdmin;
73 private NexusIndexer nexusIndexer;
76 public void initialize()
77 throws PlexusSisuBridgeException
79 log.info( "Initialized {}", this.getClass().getName() );
81 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
83 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
85 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
90 * depending on current {@link Task} you have.
91 * If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action#FINISH} && isExecuteOnEntireRepo:
92 * repository will be scanned.
95 * @throws TaskExecutionException
98 public void executeTask( Task task )
99 throws TaskExecutionException
101 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
103 ManagedRepository repository = indexingTask.getRepository();
104 IndexingContext context = indexingTask.getContext();
106 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
107 && indexingTask.isExecuteOnEntireRepo() )
111 long start = System.currentTimeMillis();
112 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
113 long end = System.currentTimeMillis();
114 log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(),
115 indexingTask.isOnlyUpdate(), ( end - start ) );
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: {}", ( indexingTask.getResourceFile() == null
133 : indexingTask.getResourceFile().getPath() ) );
134 context = managedRepositoryAdmin.createIndexContext( repository );
136 catch ( RepositoryAdminException e )
138 log.error( "Error occurred while creating context: " + e.getMessage() );
139 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(), e );
143 if ( context == null || context.getIndexDirectory() == null )
145 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
150 File artifactFile = indexingTask.getResourceFile();
151 if ( artifactFile == null )
153 log.debug( "no artifact pass in indexing task so skip it" );
157 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
161 // MRM-1779 pom must be indexed too
162 // TODO make that configurable?
163 if ( artifactFile.getPath().endsWith( ".pom" ) )
165 ac.getArtifactInfo().fextension = "pom";
166 ac.getArtifactInfo().packaging = "pom";
167 ac.getArtifactInfo().classifier = "pom";
169 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
171 //IndexSearcher s = context.getIndexSearcher();
172 //String uinfo = ac.getArtifactInfo().getUinfo();
173 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
175 BooleanQuery q = new BooleanQuery();
176 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
177 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
178 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
179 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
180 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
181 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
182 if ( ac.getArtifactInfo().classifier != null )
184 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
185 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
187 if ( ac.getArtifactInfo().packaging != null )
189 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
190 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
192 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
193 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
194 if ( flatSearchResponse.getResults().isEmpty() )
196 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
197 nexusIndexer.addArtifactToIndex( ac, context );
201 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
202 // TODO check if update exists !!
203 nexusIndexer.deleteArtifactFromIndex( ac, context );
204 nexusIndexer.addArtifactToIndex( ac, context );
207 context.updateTimestamp();
214 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
215 nexusIndexer.deleteArtifactFromIndex( ac, context );
219 // close the context if not a repo scan request
220 if ( !indexingTask.isExecuteOnEntireRepo() )
222 log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null
223 ? indexingTask.getResourceFile().getPath()
225 finishIndexingTask( indexingTask, repository, context );
228 catch ( IOException e )
230 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
232 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
239 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
240 IndexingContext context )
241 throws TaskExecutionException
248 if ( !repository.isSkipPackedIndexCreation() )
251 IndexPackingRequest request = new IndexPackingRequest( context, context.getIndexDirectoryFile() );
252 indexPacker.packIndex( request );
253 context.updateTimestamp( true );
255 log.debug( "Index file packaged at '{}'.", context.getIndexDirectoryFile() );
259 log.debug( "skip packed index creation" );
262 catch ( IOException e )
264 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
265 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
270 public void setIndexPacker( IndexPacker indexPacker )
272 this.indexPacker = indexPacker;
275 public PlexusSisuBridge getPlexusSisuBridge()
277 return plexusSisuBridge;
280 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
282 this.plexusSisuBridge = plexusSisuBridge;