Coverage Report - org.apache.maven.archiva.scheduled.executors.ArchivaIndexingTaskExecutor
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchivaIndexingTaskExecutor
0%
0/71
0%
0/20
4.8
 
 1  
 package org.apache.maven.archiva.scheduled.executors;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.lucene.index.Term;
 23  
 import org.apache.lucene.search.IndexSearcher;
 24  
 import org.apache.lucene.search.TermQuery;
 25  
 import org.apache.lucene.search.TopDocs;
 26  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 27  
 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
 28  
 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
 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.context.UnsupportedExistingLuceneIndexException;
 43  
 import org.sonatype.nexus.index.packer.IndexPacker;
 44  
 import org.sonatype.nexus.index.packer.IndexPackingRequest;
 45  
 
 46  
 import java.io.File;
 47  
 import java.io.IOException;
 48  
 
 49  
 /**
 50  
  * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are
 51  
  * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the
 52  
  * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
 53  
  * 
 54  
  * @todo Nexus specifics shouldn't be in the archiva-scheduled module
 55  
  * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
 56  
  *                   instantiation-strategy="singleton"
 57  
  */
 58  0
 public class ArchivaIndexingTaskExecutor
 59  
     implements TaskExecutor, Initializable
 60  
 {
 61  0
     private Logger log = LoggerFactory.getLogger( ArchivaIndexingTaskExecutor.class );
 62  
 
 63  
     /**
 64  
      * @plexus.requirement
 65  
      */
 66  
     private IndexerEngine indexerEngine;
 67  
 
 68  
     /**
 69  
      * @plexus.requirement
 70  
      */
 71  
     private IndexPacker indexPacker;
 72  
 
 73  
     private ArtifactContextProducer artifactContextProducer;
 74  
 
 75  
     public void executeTask( Task task )
 76  
         throws TaskExecutionException
 77  
     {
 78  0
         synchronized ( indexerEngine )
 79  
         {
 80  0
             ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
 81  
 
 82  0
             ManagedRepositoryConfiguration repository = indexingTask.getRepository();
 83  0
             IndexingContext context = indexingTask.getContext();
 84  
 
 85  0
             if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
 86  
                 && indexingTask.isExecuteOnEntireRepo() )
 87  
             {
 88  0
                 log.debug( "Finishing indexing task on repo: " + repository.getId() );
 89  0
                 finishIndexingTask( indexingTask, repository, context );
 90  
             }
 91  
             else
 92  
             {
 93  
                 // create context if not a repo scan request
 94  0
                 if( !indexingTask.isExecuteOnEntireRepo() )
 95  
                 {
 96  
                     try
 97  
                     {
 98  0
                         log.debug( "Creating indexing context on resource: " + indexingTask.getResourceFile().getPath() );
 99  0
                         context = TaskCreator.createContext( repository );
 100  
                     }
 101  0
                     catch( IOException e )
 102  
                     {
 103  0
                         log.error( "Error occurred while creating context: " + e.getMessage() );
 104  0
                         throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage() );
 105  
                     }
 106  0
                     catch( UnsupportedExistingLuceneIndexException e )
 107  
                     {
 108  0
                         log.error( "Error occurred while creating context: " + e.getMessage() );
 109  0
                         throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage() );    
 110  0
                     }
 111  
                 }
 112  
 
 113  0
                 if ( context == null || context.getIndexDirectory() == null )
 114  
                 {
 115  0
                     throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
 116  
                 }
 117  
                 
 118  
                 try
 119  
                 {
 120  0
                     File artifactFile = indexingTask.getResourceFile();
 121  0
                     ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
 122  
 
 123  0
                     if ( ac != null )
 124  
                     {
 125  0
                         if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
 126  
                         {
 127  0
                             IndexSearcher s = context.getIndexSearcher();
 128  0
                             String uinfo = ac.getArtifactInfo().getUinfo();
 129  0
                             TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
 130  0
                             if ( d.totalHits == 0 )
 131  
                             {
 132  0
                                 log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
 133  0
                                 indexerEngine.index( context, ac );
 134  0
                                 context.getIndexWriter().commit();
 135  
                             }
 136  
                             else
 137  
                             {
 138  0
                                 log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
 139  0
                                 indexerEngine.update( context, ac );
 140  0
                                 context.getIndexWriter().commit();
 141  
                             }
 142  
 
 143  
                             // close the context if not a repo scan request
 144  0
                             if( !indexingTask.isExecuteOnEntireRepo() )
 145  
                             {
 146  0
                                 log.debug( "Finishing indexing task on resource file : " + indexingTask.getResourceFile().getPath() );
 147  0
                                 finishIndexingTask( indexingTask, repository, context );   
 148  
                             }
 149  0
                         }
 150  
                         else
 151  
                         {
 152  0
                             log.debug( "Removing artifact '" + ac.getArtifactInfo() + "' from index.." );
 153  0
                             indexerEngine.remove( context, ac );
 154  0
                             context.getIndexWriter().commit();
 155  
                         }
 156  
                     }
 157  
                 }
 158  0
                 catch ( IOException e )
 159  
                 {
 160  0
                     log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
 161  0
                     throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask
 162  
                         + "'", e );
 163  0
                 }
 164  
             }
 165  0
         }
 166  0
     }
 167  
 
 168  
     private void finishIndexingTask( ArtifactIndexingTask indexingTask, ManagedRepositoryConfiguration repository,
 169  
                                      IndexingContext context )
 170  
         throws TaskExecutionException
 171  
     {
 172  
         try
 173  
         {
 174  0
             context.optimize();
 175  
 
 176  0
             File managedRepository = new File( repository.getLocation() );
 177  0
             final File indexLocation = new File( managedRepository, ".index" );
 178  0
             IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
 179  0
             indexPacker.packIndex( request );
 180  
 
 181  0
             log.debug( "Index file packaged at '" + indexLocation.getPath() + "'." );
 182  
         }
 183  0
         catch ( IOException e )
 184  
         {
 185  0
             log.error( "Error occurred while executing indexing task '" + indexingTask + "': " + e.getMessage() );
 186  0
             throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask
 187  
                 + "'", e );
 188  
         }
 189  
         finally
 190  
         {
 191  0
             if ( context != null )
 192  
             {
 193  
                 try
 194  
                 {
 195  0
                     context.close( false );
 196  
                 }
 197  0
                 catch ( IOException e )
 198  
                 {
 199  0
                     log.error( "Error occurred while closing context: " + e.getMessage() );
 200  0
                     throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
 201  0
                 }
 202  
             }
 203  
         }
 204  0
     }
 205  
 
 206  
     public void initialize()
 207  
         throws InitializationException
 208  
     {
 209  0
         log.info( "Initialized " + this.getClass().getName() );
 210  
 
 211  0
         artifactContextProducer = new DefaultArtifactContextProducer();
 212  0
     }
 213  
 
 214  
     public void setIndexerEngine( IndexerEngine indexerEngine )
 215  
     {
 216  0
         this.indexerEngine = indexerEngine;
 217  0
     }
 218  
 
 219  
     public void setIndexPacker( IndexPacker indexPacker )
 220  
     {
 221  0
         this.indexPacker = indexPacker;
 222  0
     }
 223  
 }