Coverage Report - org.apache.turbine.services.schedule.JobQueue
 
Classes in this File Line Coverage Branch Coverage Complexity
JobQueue
83%
31/37
50%
6/12
1,818
JobQueue$1
100%
4/4
N/A
1,818
 
 1  
 package org.apache.turbine.services.schedule;
 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.Collections;
 23  
 import java.util.Comparator;
 24  
 import java.util.List;
 25  
 import java.util.Vector;
 26  
 
 27  
 import org.apache.turbine.util.TurbineException;
 28  
 
 29  
 /**
 30  
  * Queue for the scheduler.
 31  
  *
 32  
  * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
 33  
  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
 34  
  * @version $Id: JobQueue.java 615328 2008-01-25 20:25:05Z tv $
 35  
  */
 36  
 public class JobQueue
 37  
 {
 38  
     /**
 39  
      * The queue of <code>JobEntry</code> objects.
 40  
      */
 41  2
     private Vector<JobEntry> queue = null;
 42  
 
 43  
     /**
 44  
      * Creates a new instance.
 45  
      */
 46  
     public JobQueue()
 47  2
     {
 48  2
         queue = new Vector<JobEntry>(10);
 49  2
     }
 50  
 
 51  
     /**
 52  
      * Return the next job off the top of the queue, or <code>null</code> if
 53  
      * there are no jobs in the queue.
 54  
      *
 55  
      * @return The next job in the queue.
 56  
      */
 57  
     public JobEntry getNext()
 58  
     {
 59  3
         if (queue.size() > 0)
 60  
         {
 61  3
             return queue.elementAt(0);
 62  
         }
 63  
         else
 64  
         {
 65  0
             return null;
 66  
         }
 67  
     }
 68  
 
 69  
     /**
 70  
      * Return a specific job.
 71  
      *
 72  
      * @param je The JobEntry we are looking for.
 73  
      * @return A JobEntry.
 74  
      */
 75  
     public JobEntry getJob(JobEntry je)
 76  
     {
 77  2
         int index = -1;
 78  
 
 79  2
         if (je != null)
 80  
         {
 81  2
             index = queue.indexOf(je);
 82  
         }
 83  
 
 84  2
         if (index < 0)
 85  
         {
 86  0
             return null;
 87  
         }
 88  
         else
 89  
         {
 90  2
             return queue.elementAt(index);
 91  
         }
 92  
     }
 93  
 
 94  
     /**
 95  
      * List jobs in the queue.  This is used by the scheduler UI.
 96  
      *
 97  
      * @return A Vector of <code>JobEntry</code> objects.
 98  
      */
 99  
     @SuppressWarnings("unchecked")
 100  
     public Vector<JobEntry> list()
 101  
     {
 102  6
         if (queue != null && queue.size() > 0)
 103  
         {
 104  6
             return (Vector<JobEntry>) queue.clone();
 105  
         }
 106  
         else
 107  
         {
 108  0
             return null;
 109  
         }
 110  
     }
 111  
 
 112  
     /**
 113  
      * Add a job to the queue.
 114  
      *
 115  
      * @param je A JobEntry job.
 116  
      */
 117  
     public synchronized void add(JobEntry je)
 118  
     {
 119  2
         queue.addElement(je);
 120  2
         sortQueue();
 121  2
     }
 122  
 
 123  
     /**
 124  
      * Batch load jobs.  Retains any already enqueued jobs.  Called on
 125  
      * <code>SchedulerService</code> start-up.
 126  
      *
 127  
      * @param jobEntries A list of the <code>JobEntry</code> objects to load.
 128  
      */
 129  
     public synchronized void batchLoad(List<JobEntry> jobEntries)
 130  
     {
 131  2
         if (jobEntries != null)
 132  
         {
 133  2
             queue.addAll(jobEntries);
 134  2
             sortQueue();
 135  
         }
 136  
 
 137  2
     }
 138  
 
 139  
     /**
 140  
      * Remove a job from the queue.
 141  
      *
 142  
      * @param je A JobEntry with the job to remove.
 143  
      */
 144  
     public synchronized void remove(JobEntry je)
 145  
     {
 146  4
         queue.removeElement(je);
 147  4
         sortQueue();
 148  4
     }
 149  
 
 150  
     /**
 151  
      * Modify a job on the queue.
 152  
      *
 153  
      * @param je A JobEntry with the job to modify
 154  
      */
 155  
     public synchronized void modify(JobEntry je)
 156  
             throws TurbineException
 157  
     {
 158  2
         remove(je);
 159  2
         je.calcRunTime();
 160  2
         this.add(je);
 161  2
         sortQueue();
 162  2
     }
 163  
 
 164  
     /**
 165  
      * Update the job for its next run time.
 166  
      *
 167  
      * @param je A JobEntry to be updated.
 168  
      * @exception TurbineException a generic exception.
 169  
      */
 170  
     public synchronized void updateQueue(JobEntry je)
 171  
             throws TurbineException
 172  
     {
 173  0
         je.calcRunTime();
 174  0
         sortQueue();
 175  0
     }
 176  
 
 177  
     /**
 178  
      * Re-sort the existing queue.  Consumers of this method should be
 179  
      * <code>synchronized</code>.
 180  
      */
 181  
     private void sortQueue()
 182  
     {
 183  10
         Comparator<JobEntry> aComparator = new Comparator<JobEntry>()
 184  14
         {
 185  
             public int compare(JobEntry o1, JobEntry o2)
 186  
             {
 187  4
                 Long time1 = new Long(o1.getNextRuntime());
 188  4
                 Long time2 = new Long(o2.getNextRuntime());
 189  4
                 return (time1.compareTo(time2));
 190  
             }
 191  
         };
 192  
 
 193  10
         Collections.sort(queue, aComparator);
 194  10
     }
 195  
 }