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 );
66 private IndexPacker indexPacker;
68 private ArtifactContextProducer artifactContextProducer;
71 private PlexusSisuBridge plexusSisuBridge;
74 private ManagedRepositoryAdmin managedRepositoryAdmin;
76 private NexusIndexer nexusIndexer;
79 public void initialize()
80 throws PlexusSisuBridgeException
82 log.info( "Initialized {}", this.getClass().getName() );
84 artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class );
86 indexPacker = plexusSisuBridge.lookup( IndexPacker.class, "default" );
88 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
93 * depending on current {@link Task} you have.
94 * If {@link org.apache.archiva.scheduler.indexing.ArtifactIndexingTask.Action.FINISH} && isExecuteOnEntireRepo:
95 * repository will be scanned.
98 * @throws TaskExecutionException
100 public void executeTask( Task task )
101 throws TaskExecutionException
103 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
105 ManagedRepository repository = indexingTask.getRepository();
106 IndexingContext context = indexingTask.getContext();
108 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
109 && indexingTask.isExecuteOnEntireRepo() )
113 long start = System.currentTimeMillis();
114 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
115 long end = System.currentTimeMillis();
116 log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(),
117 indexingTask.isOnlyUpdate(), ( end - start ) );
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: {}", ( indexingTask.getResourceFile() == null
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(), e );
145 if ( context == null || context.getIndexDirectory() == null )
147 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
152 File artifactFile = indexingTask.getResourceFile();
153 if ( artifactFile == null )
155 log.debug( "no artifact pass in indexing task so skip it" );
159 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
163 // MRM-1779 pom must be indexed too
164 // TODO make that configurable?
165 if ( artifactFile.getPath().endsWith( ".pom" ) )
167 ac.getArtifactInfo().fextension = "pom";
168 ac.getArtifactInfo().packaging = "pom";
169 ac.getArtifactInfo().classifier = "pom";
171 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
173 //IndexSearcher s = context.getIndexSearcher();
174 //String uinfo = ac.getArtifactInfo().getUinfo();
175 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
177 BooleanQuery q = new BooleanQuery();
178 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
179 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
180 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
181 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
182 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
183 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
184 if ( ac.getArtifactInfo().classifier != null )
186 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
187 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
189 if ( ac.getArtifactInfo().packaging != null )
191 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
192 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
194 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
195 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
196 if ( flatSearchResponse.getResults().isEmpty() )
198 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
199 nexusIndexer.addArtifactToIndex( ac, context );
203 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
204 // TODO check if update exists !!
205 nexusIndexer.deleteArtifactFromIndex( ac, context );
206 nexusIndexer.addArtifactToIndex( ac, context );
209 context.updateTimestamp();
216 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
217 nexusIndexer.deleteArtifactFromIndex( ac, context );
221 // close the context if not a repo scan request
222 if ( !indexingTask.isExecuteOnEntireRepo() )
224 log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null
225 ? indexingTask.getResourceFile().getPath()
227 finishIndexingTask( indexingTask, repository, context );
230 catch ( IOException e )
232 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
234 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
241 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
242 IndexingContext context )
243 throws TaskExecutionException
250 if ( !repository.isSkipPackedIndexCreation() )
253 IndexPackingRequest request = new IndexPackingRequest( context, context.getIndexDirectoryFile() );
254 indexPacker.packIndex( request );
255 context.updateTimestamp( true );
257 log.debug( "Index file packaged at '{}'.", context.getIndexDirectoryFile() );
261 log.debug( "skip packed index creation" );
264 catch ( IOException e )
266 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
267 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
272 public void setIndexPacker( IndexPacker indexPacker )
274 this.indexPacker = indexPacker;
277 public PlexusSisuBridge getPlexusSisuBridge()
279 return plexusSisuBridge;
282 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
284 this.plexusSisuBridge = plexusSisuBridge;