1 package org.apache.maven.archiva.scheduled.executors;
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 License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.io.IOException;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
29 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
31 import org.codehaus.plexus.taskqueue.Task;
32 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
33 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.sonatype.nexus.index.ArtifactContext;
37 import org.sonatype.nexus.index.ArtifactContextProducer;
38 import org.sonatype.nexus.index.ArtifactInfo;
39 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
40 import org.sonatype.nexus.index.IndexerEngine;
41 import org.sonatype.nexus.index.context.IndexingContext;
42 import org.sonatype.nexus.index.packer.IndexPacker;
43 import org.sonatype.nexus.index.packer.IndexPackingRequest;
46 * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
47 * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
48 * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
50 * @todo Nexus specifics shouldn't be in the archiva-scheduled module
51 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
52 * instantiation-strategy="singleton"
54 public class ArchivaIndexingTaskExecutor
55 implements TaskExecutor, Initializable
57 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
62 private IndexerEngine indexerEngine;
67 private IndexPacker indexPacker;
69 private ArtifactContextProducer artifactContextProducer;
71 public void executeTask( Task task )
72 throws TaskExecutionException
74 synchronized ( indexerEngine )
76 ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
78 ManagedRepositoryConfiguration repository = indexingTask.getRepository();
79 IndexingContext context = indexingTask.getContext();
81 if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() ) )
87 File managedRepository = new File( repository.getLocation() );
88 final File indexLocation = new File( managedRepository, ".index" );
89 IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
90 indexPacker.packIndex( request );
92 log.debug( "Index file packaged at '" + indexLocation.getPath() + "'." );
94 catch ( IOException e )
96 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
97 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask
102 if ( context != null )
106 context.close( false );
108 catch ( IOException e )
110 log.error( "Error occurred while closing context: " + e.getMessage() );
111 throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
118 if ( context.getIndexDirectory() == null )
120 throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
125 File artifactFile = indexingTask.getResourceFile();
126 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
130 if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
133 IndexReader r = context.getIndexReader();
134 for ( int i = 0; i < r.numDocs(); i++ )
136 if ( !r.isDeleted( i ) )
138 Document d = r.document( i );
139 String uinfo = d.get( ArtifactInfo.UINFO );
140 if ( ac.getArtifactInfo().getUinfo().equals( uinfo ) )
150 log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
151 indexerEngine.index( context, ac );
152 context.getIndexWriter().commit();
156 log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
157 indexerEngine.update( context, ac );
158 context.getIndexWriter().commit();
163 log.debug( "Removing artifact '" + ac.getArtifactInfo() + "' from index.." );
164 indexerEngine.remove( context, ac );
165 context.getIndexWriter().commit();
169 catch ( IOException e )
171 log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
172 throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask
179 public void initialize()
180 throws InitializationException
182 log.info( "Initialized " + this.getClass().getName() );
184 artifactContextProducer = new DefaultArtifactContextProducer();
187 public void setIndexerEngine( IndexerEngine indexerEngine )
189 this.indexerEngine = indexerEngine;
192 public void setIndexPacker( IndexPacker indexPacker )
194 this.indexPacker = indexPacker;