Coverage Report - org.apache.maven.archiva.database.updater.JdoDatabaseUpdater
 
Classes in this File Line Coverage Branch Coverage Complexity
JdoDatabaseUpdater
0%
0/50
0%
0/12
0
 
 1  
 package org.apache.maven.archiva.database.updater;
 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.util.Date;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.commons.collections.CollectionUtils;
 27  
 import org.apache.commons.collections.IteratorUtils;
 28  
 import org.apache.commons.collections.Predicate;
 29  
 import org.apache.commons.collections.functors.NotPredicate;
 30  
 import org.apache.maven.archiva.database.ArchivaDAO;
 31  
 import org.apache.maven.archiva.database.ArchivaDatabaseException;
 32  
 import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint;
 33  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 34  
 import org.apache.maven.archiva.model.functors.UnprocessedArtifactPredicate;
 35  
 import org.slf4j.Logger;
 36  
 import org.slf4j.LoggerFactory;
 37  
 
 38  
 /**
 39  
  * JdoDatabaseUpdater
 40  
  *
 41  
  * @version $Id: JdoDatabaseUpdater.java 755266 2009-03-17 14:28:40Z brett $
 42  
  * 
 43  
  * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseUpdater" 
 44  
  *   role-hint="jdo" 
 45  
  */
 46  0
 public class JdoDatabaseUpdater
 47  
     implements DatabaseUpdater
 48  
 {
 49  0
     private Logger log = LoggerFactory.getLogger( JdoDatabaseUpdater.class );
 50  
     
 51  
     /**
 52  
      * @plexus.requirement role-hint="jdo"
 53  
      */
 54  
     private ArchivaDAO dao;
 55  
 
 56  
     /**
 57  
      * @plexus.requirement
 58  
      */
 59  
     private DatabaseConsumers dbConsumers;
 60  
 
 61  0
     private ProcessArchivaArtifactClosure processArtifactClosure = new ProcessArchivaArtifactClosure();
 62  
 
 63  
     public void update()
 64  
         throws ArchivaDatabaseException
 65  
     {
 66  0
         updateAllUnprocessed();
 67  0
         updateAllProcessed();
 68  0
     }
 69  
 
 70  
     @SuppressWarnings("unchecked")
 71  
     public void updateAllUnprocessed()
 72  
         throws ArchivaDatabaseException
 73  
     {
 74  0
         List<ArchivaArtifact> unprocessedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( false ) );
 75  
 
 76  0
         beginConsumerLifecycle( dbConsumers.getSelectedUnprocessedConsumers() );
 77  
 
 78  
         try
 79  
         {
 80  
             // Process each consumer.
 81  0
             Predicate predicate = UnprocessedArtifactPredicate.getInstance();
 82  
 
 83  0
             Iterator<ArchivaArtifact> it = IteratorUtils.filteredIterator( unprocessedArtifacts.iterator(), predicate );
 84  0
             while ( it.hasNext() )
 85  
             {
 86  0
                 ArchivaArtifact artifact = it.next();
 87  0
                 updateUnprocessed( artifact );
 88  0
             }
 89  
         }
 90  
         finally
 91  
         {
 92  0
             endConsumerLifecycle( dbConsumers.getSelectedUnprocessedConsumers() );
 93  0
         }
 94  0
     }
 95  
 
 96  
     @SuppressWarnings("unchecked")
 97  
     public void updateAllProcessed()
 98  
         throws ArchivaDatabaseException
 99  
     {
 100  0
         List<ArchivaArtifact> processedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( true ) );
 101  
 
 102  0
         beginConsumerLifecycle( dbConsumers.getSelectedCleanupConsumers() );
 103  
 
 104  
         try
 105  
         {
 106  
             // Process each consumer.
 107  0
             Predicate predicate = NotPredicate.getInstance( UnprocessedArtifactPredicate.getInstance() );
 108  
 
 109  0
             Iterator<ArchivaArtifact> it = IteratorUtils.filteredIterator( processedArtifacts.iterator(), predicate );
 110  0
             while ( it.hasNext() )
 111  
             {
 112  0
                 ArchivaArtifact artifact = it.next();
 113  0
                 updateProcessed( artifact );
 114  0
             }
 115  
         }
 116  
         finally
 117  
         {
 118  0
             endConsumerLifecycle( dbConsumers.getSelectedCleanupConsumers() );
 119  0
         }
 120  0
     }
 121  
 
 122  
     private void endConsumerLifecycle( List<ArchivaArtifactConsumer> consumers )
 123  
     {
 124  0
         for ( ArchivaArtifactConsumer consumer : consumers )
 125  
         {
 126  0
             consumer.completeScan();
 127  
         }
 128  0
     }
 129  
 
 130  
     private void beginConsumerLifecycle( List<ArchivaArtifactConsumer> consumers )
 131  
     {
 132  0
         for ( ArchivaArtifactConsumer consumer : consumers )
 133  
         {
 134  0
             consumer.beginScan();
 135  
         }
 136  0
     }
 137  
 
 138  
     public void updateUnprocessed( ArchivaArtifact artifact )
 139  
         throws ArchivaDatabaseException
 140  
     {
 141  0
         List<ArchivaArtifactConsumer> consumers = dbConsumers.getSelectedUnprocessedConsumers();
 142  
 
 143  0
         if ( CollectionUtils.isEmpty( consumers ) )
 144  
         {
 145  0
             log.warn( "There are no selected consumers for unprocessed artifacts." );
 146  0
             return;
 147  
         }
 148  
         
 149  0
         this.processArtifactClosure.setArtifact( artifact );
 150  0
         CollectionUtils.forAllDo( consumers, this.processArtifactClosure );
 151  
 
 152  0
         artifact.getModel().setWhenProcessed( new Date() );
 153  0
         dao.getArtifactDAO().saveArtifact( artifact );
 154  0
     }
 155  
 
 156  
     public void updateProcessed( ArchivaArtifact artifact )
 157  
         throws ArchivaDatabaseException
 158  
     {
 159  0
         List<ArchivaArtifactConsumer> consumers = dbConsumers.getSelectedCleanupConsumers();
 160  
 
 161  0
         if ( CollectionUtils.isEmpty( consumers ) )
 162  
         {
 163  0
             log.warn( "There are no selected consumers for artifact cleanup." );
 164  0
             return;
 165  
         }
 166  
         
 167  0
         this.processArtifactClosure.setArtifact( artifact );
 168  0
         CollectionUtils.forAllDo( consumers, this.processArtifactClosure );
 169  0
     }
 170  
 }