Coverage Report - org.apache.archiva.consumers.lucene.LuceneCleanupRemoveIndexedConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
LuceneCleanupRemoveIndexedConsumer
0%
0/37
0%
0/2
0
 
 1  
 package org.apache.archiva.consumers.lucene;
 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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 27  
 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
 28  
 import org.apache.maven.archiva.consumers.ConsumerException;
 29  
 import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer;
 30  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 31  
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 32  
 import org.apache.maven.archiva.repository.RepositoryContentFactory;
 33  
 import org.apache.maven.archiva.repository.RepositoryException;
 34  
 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
 35  
 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
 36  
 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
 37  
 import org.codehaus.plexus.taskqueue.TaskQueueException;
 38  
 import org.slf4j.Logger;
 39  
 import org.slf4j.LoggerFactory;
 40  
 import org.sonatype.nexus.index.context.IndexingContext;
 41  
 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
 42  
 
 43  
 /**
 44  
  * LuceneCleanupRemoveIndexedConsumer Clean up the index of artifacts that are no longer existing in the file system
 45  
  * (managed repositories).
 46  
  * 
 47  
  * @version $Id$
 48  
  */
 49  
 public class LuceneCleanupRemoveIndexedConsumer
 50  
     extends AbstractMonitoredConsumer
 51  
     implements DatabaseCleanupConsumer
 52  
 {
 53  0
     private static final Logger log = LoggerFactory.getLogger( LuceneCleanupRemoveIndexedConsumer.class );
 54  
 
 55  
     private RepositoryContentFactory repoFactory;
 56  
 
 57  
     private ArchivaTaskScheduler scheduler;
 58  
 
 59  
     public LuceneCleanupRemoveIndexedConsumer( RepositoryContentFactory repoFactory, ArchivaTaskScheduler scheduler )
 60  0
     {
 61  0
         this.repoFactory = repoFactory;
 62  0
         this.scheduler = scheduler;
 63  0
     }
 64  
 
 65  
     public void beginScan()
 66  
     {
 67  0
     }
 68  
 
 69  
     public void completeScan()
 70  
     {
 71  0
     }
 72  
 
 73  
     public List<String> getIncludedTypes()
 74  
     {
 75  0
         return null;
 76  
     }
 77  
 
 78  
     public void processArchivaArtifact( ArchivaArtifact artifact )
 79  
         throws ConsumerException
 80  
     {
 81  0
         ManagedRepositoryContent repoContent = null;
 82  
 
 83  
         try
 84  
         {
 85  0
             repoContent = repoFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() );
 86  
         }
 87  0
         catch ( RepositoryException e )
 88  
         {
 89  0
             throw new ConsumerException( "Can't run index cleanup consumer: " + e.getMessage() );
 90  0
         }
 91  
 
 92  0
         ManagedRepositoryConfiguration repository = repoContent.getRepository();
 93  
 
 94  0
         IndexingContext context = null;
 95  
         try
 96  
         {
 97  0
             File artifactFile = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) );
 98  
 
 99  0
             if ( !artifactFile.exists() )
 100  
             {
 101  0
                 context = TaskCreator.createContext( repository );
 102  
 
 103  0
                 ArtifactIndexingTask task =
 104  
                     TaskCreator.createIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.DELETE,
 105  
                                                     context );
 106  
 
 107  0
                 log.debug( "Queueing indexing task '" + task + "' to remove the artifact from the index." );
 108  0
                 scheduler.queueIndexingTask( task );
 109  
 
 110  
                 // note we finish immediately here since it isn't done repo-by-repo. It might be nice to ensure that is
 111  
                 // the case for optimisation though
 112  0
                 task =
 113  
                     TaskCreator.createIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.FINISH,
 114  
                                                     context );
 115  0
                 log.debug( "Queueing indexing task + '" + task + "' to finish indexing." );
 116  0
                 scheduler.queueIndexingTask( task );
 117  
             }
 118  
 
 119  
         }
 120  0
         catch ( TaskQueueException e )
 121  
         {
 122  0
             throw new ConsumerException( e.getMessage() );
 123  
         }
 124  0
         catch ( IOException e )
 125  
         {
 126  0
             throw new ConsumerException( e.getMessage(), e );
 127  
         }
 128  0
         catch ( UnsupportedExistingLuceneIndexException e )
 129  
         {
 130  0
             throw new ConsumerException( e.getMessage(), e );
 131  0
         }
 132  0
     }
 133  
 
 134  
     public String getDescription()
 135  
     {
 136  0
         return "Remove indexed content if not present on filesystem.";
 137  
     }
 138  
 
 139  
     public String getId()
 140  
     {
 141  0
         return "not-present-remove-indexed";
 142  
     }
 143  
 
 144  
     public boolean isPermanent()
 145  
     {
 146  0
         return false;
 147  
     }
 148  
 
 149  
     public void setRepositoryContentFactory( RepositoryContentFactory repoFactory )
 150  
     {
 151  0
         this.repoFactory = repoFactory;
 152  0
     }
 153  
 }