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
97 public void executeTask( Task task )
98 throws TaskExecutionException
100 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
102 ManagedRepository repository = indexingTask.getRepository();
103 IndexingContext context = indexingTask.getContext();
105 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
106 && indexingTask.isExecuteOnEntireRepo() )
110 long start = System.currentTimeMillis();
111 nexusIndexer.scan( context, null, indexingTask.isOnlyUpdate() );
112 long end = System.currentTimeMillis();
113 log.info( "indexed maven repository: {}, onlyUpdate: {}, time {} ms", repository.getId(),
114 indexingTask.isOnlyUpdate(), ( end - start ) );
116 catch ( IOException e )
118 throw new TaskExecutionException( "Error scan repository " + repository, e );
120 log.debug( "Finishing indexing task on repo: {}", repository.getId() );
121 finishIndexingTask( indexingTask, repository, context );
125 // create context if not a repo scan request
126 if ( !indexingTask.isExecuteOnEntireRepo() )
130 log.debug( "Creating indexing context on resource: {}", ( indexingTask.getResourceFile() == null
132 : indexingTask.getResourceFile().getPath() ) );
133 context = managedRepositoryAdmin.createIndexContext( repository );
135 catch ( RepositoryAdminException e )
137 log.error( "Error occurred while creating context: " + e.getMessage() );
138 throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage(), e );
142 if ( context == null || context.getIndexDirectory() == null )
144 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
149 File artifactFile = indexingTask.getResourceFile();
150 if ( artifactFile == null )
152 log.debug( "no artifact pass in indexing task so skip it" );
156 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
160 // MRM-1779 pom must be indexed too
161 // TODO make that configurable?
162 if ( artifactFile.getPath().endsWith( ".pom" ) )
164 ac.getArtifactInfo().fextension = "pom";
165 ac.getArtifactInfo().packaging = "pom";
166 ac.getArtifactInfo().classifier = "pom";
168 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
170 //IndexSearcher s = context.getIndexSearcher();
171 //String uinfo = ac.getArtifactInfo().getUinfo();
172 //TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
174 BooleanQuery q = new BooleanQuery();
175 q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression(
176 ac.getArtifactInfo().groupId ) ), BooleanClause.Occur.MUST );
177 q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression(
178 ac.getArtifactInfo().artifactId ) ), BooleanClause.Occur.MUST );
179 q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new SourcedSearchExpression(
180 ac.getArtifactInfo().version ) ), BooleanClause.Occur.MUST );
181 if ( ac.getArtifactInfo().classifier != null )
183 q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new SourcedSearchExpression(
184 ac.getArtifactInfo().classifier ) ), BooleanClause.Occur.MUST );
186 if ( ac.getArtifactInfo().packaging != null )
188 q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new SourcedSearchExpression(
189 ac.getArtifactInfo().packaging ) ), BooleanClause.Occur.MUST );
191 FlatSearchRequest flatSearchRequest = new FlatSearchRequest( q, context );
192 FlatSearchResponse flatSearchResponse = nexusIndexer.searchFlat( flatSearchRequest );
193 if ( flatSearchResponse.getResults().isEmpty() )
195 log.debug( "Adding artifact '{}' to index..", ac.getArtifactInfo() );
196 nexusIndexer.addArtifactToIndex( ac, context );
200 log.debug( "Updating artifact '{}' in index..", ac.getArtifactInfo() );
201 // TODO check if update exists !!
202 nexusIndexer.deleteArtifactFromIndex( ac, context );
203 nexusIndexer.addArtifactToIndex( ac, context );
206 context.updateTimestamp();
213 log.debug( "Removing artifact '{}' from index..", ac.getArtifactInfo() );
214 nexusIndexer.deleteArtifactFromIndex( ac, context );
218 // close the context if not a repo scan request
219 if ( !indexingTask.isExecuteOnEntireRepo() )
221 log.debug( "Finishing indexing task on resource file : {}", indexingTask.getResourceFile() != null
222 ? indexingTask.getResourceFile().getPath()
224 finishIndexingTask( indexingTask, repository, context );
227 catch ( IOException e )
229 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage(),
231 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
238 private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepository repository,
239 IndexingContext context )
240 throws TaskExecutionException
247 if ( !repository.isSkipPackedIndexCreation() )
250 IndexPackingRequest request = new IndexPackingRequest( context, context.getIndexDirectoryFile() );
251 indexPacker.packIndex( request );
252 context.updateTimestamp( true );
254 log.debug( "Index file packaged at '{}'.", context.getIndexDirectoryFile() );
258 log.debug( "skip packed index creation" );
261 catch ( IOException e )
263 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
264 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask + "'",
269 public void setIndexPacker( IndexPacker indexPacker )
271 this.indexPacker = indexPacker;
274 public PlexusSisuBridge getPlexusSisuBridge()
276 return plexusSisuBridge;
279 public void setPlexusSisuBridge( PlexusSisuBridge plexusSisuBridge )
281 this.plexusSisuBridge = plexusSisuBridge;