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.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
31 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
32 import org.codehaus.plexus.taskqueue.Task;
33 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
34 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.sonatype.nexus.index.ArtifactContext;
38 import org.sonatype.nexus.index.ArtifactContextProducer;
39 import org.sonatype.nexus.index.ArtifactInfo;
40 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
41 import org.sonatype.nexus.index.IndexerEngine;
42 import org.sonatype.nexus.index.NexusIndexer;
43 import org.sonatype.nexus.index.context.DefaultIndexingContext;
44 import org.sonatype.nexus.index.context.IndexingContext;
45 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
46 import org.sonatype.nexus.index.packer.IndexPacker;
47 import org.sonatype.nexus.index.packer.IndexPackingRequest;
50 * ArchivaIndexingTaskExecutor
52 * Executes all indexing tasks. Adding, updating and removing artifacts from the index are all performed by
53 * this executor. Add and update artifact in index tasks are added in the indexing task queue by the NexusIndexerConsumer while
54 * remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
56 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
58 public class ArchivaIndexingTaskExecutor
59 implements TaskExecutor, Initializable
61 private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
66 private IndexerEngine indexerEngine;
71 private ArchivaConfiguration archivaConfiguration;
76 private IndexPacker indexPacker;
78 private ArtifactContextProducer artifactContextProducer;
80 public void executeTask( Task task )
81 throws TaskExecutionException
83 synchronized( indexerEngine )
85 ArtifactIndexingTask indexingTask = ( ArtifactIndexingTask ) task;
87 ManagedRepositoryConfiguration repository =
88 archivaConfiguration.getConfiguration().findManagedRepositoryById( indexingTask.getRepositoryId() );
90 String indexDir = repository.getIndexDir();
91 File managedRepository = new File( repository.getLocation() );
93 File indexDirectory = null;
94 if( indexDir != null && !"".equals( indexDir ) )
96 indexDirectory = new File( repository.getIndexDir() );
100 indexDirectory = new File( managedRepository, ".indexer" );
103 IndexingContext context = null;
107 new DefaultIndexingContext( repository.getId(), repository.getId(), managedRepository,
108 indexDirectory, null, null, NexusIndexer.FULL_INDEX, false );
109 context.setSearchable( repository.isScanned() );
111 File artifactFile = indexingTask.getResourceFile();
112 ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
116 if( indexingTask.getAction().equals( ArtifactIndexingTask.ADD ) )
119 IndexReader r = context.getIndexReader();
120 for ( int i = 0; i < r.numDocs(); i++ )
122 if ( !r.isDeleted( i ) )
124 Document d = r.document( i );
125 String uinfo = d.get( ArtifactInfo.UINFO );
126 if( ac.getArtifactInfo().getUinfo().equals( uinfo ) )
136 log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
137 indexerEngine.index( context, ac );
141 log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
142 indexerEngine.update( context, ac );
147 log.debug( "removing artifact '" + ac.getArtifactInfo() + "' from index.." );
148 indexerEngine.remove( context, ac );
151 final File indexLocation = new File( managedRepository, ".index" );
152 IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
153 indexPacker.packIndex( request );
156 catch ( IOException e )
158 throw new TaskExecutionException( "Error occurred while executing indexing task '" +
159 indexingTask.getName() + "'" );
161 catch ( UnsupportedExistingLuceneIndexException e )
163 throw new TaskExecutionException( "Unsupported Lucene index format: " + e.getMessage() );
167 if( context != null )
171 context.close( false );
173 catch ( IOException e )
175 throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
182 public void initialize()
183 throws InitializationException
185 log.info( "Initialized " + this.getClass().getName() );
187 artifactContextProducer = new DefaultArtifactContextProducer();
190 public void setIndexerEngine( IndexerEngine indexerEngine )
192 this.indexerEngine = indexerEngine;
195 public void setIndexPacker( IndexPacker indexPacker )
197 this.indexPacker = indexPacker;
200 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
202 this.archivaConfiguration = archivaConfiguration;