View Javadoc

1   package org.apache.turbine.services.schedule;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  import java.util.Vector;
23  
24  import org.apache.turbine.util.TurbineException;
25  
26  /***
27   * Queue for the scheduler.
28   *
29   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
30   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
31   * @version $Id: JobQueue.java 264148 2005-08-29 14:21:04Z henning $
32   */
33  public class JobQueue
34  {
35      /***
36       * The queue of <code>JobEntry</code> objects.
37       */
38      private Vector queue = null;
39  
40      /***
41       * Creates a new instance.
42       */
43      public JobQueue()
44      {
45          queue = new Vector(10);
46      }
47  
48      /***
49       * Return the next job off the top of the queue, or <code>null</code> if
50       * there are no jobs in the queue.
51       *
52       * @return The next job in the queue.
53       */
54      public JobEntry getNext()
55      {
56          if (queue.size() > 0)
57          {
58              return (JobEntry) queue.elementAt(0);
59          }
60          else
61          {
62              return null;
63          }
64      }
65  
66      /***
67       * Return a specific job.
68       *
69       * @param je The JobEntry we are looking for.
70       * @return A JobEntry.
71       */
72      public JobEntry getJob(JobEntry je)
73      {
74          int index = -1;
75  
76          if (je != null)
77          {
78              index = queue.indexOf(je);
79          }
80  
81          if (index < 0)
82          {
83              return null;
84          }
85          else
86          {
87              return (JobEntry) queue.elementAt(index);
88          }
89      }
90  
91      /***
92       * List jobs in the queue.  This is used by the scheduler UI.
93       *
94       * @return A Vector of <code>JobEntry</code> objects.
95       */
96      public Vector list()
97      {
98          if (queue != null && queue.size() > 0)
99          {
100             return (Vector) queue.clone();
101         }
102         else
103         {
104             return null;
105         }
106     }
107 
108     /***
109      * Add a job to the queue.
110      *
111      * @param je A JobEntry job.
112      */
113     public synchronized void add(JobEntry je)
114     {
115         queue.addElement(je);
116         sortQueue();
117     }
118 
119     /***
120      * Batch load jobs.  Retains any already enqueued jobs.  Called on
121      * <code>SchedulerService</code> start-up.
122      *
123      * @param jobEntries A list of the <code>JobEntry</code> objects to load.
124      */
125     public synchronized void batchLoad(List jobEntries)
126     {
127         if (jobEntries != null)
128         {
129             queue.addAll(jobEntries);
130             sortQueue();
131         }
132 
133     }
134 
135     /***
136      * Remove a job from the queue.
137      *
138      * @param je A JobEntry with the job to remove.
139      */
140     public synchronized void remove(JobEntry je)
141     {
142         queue.removeElement(je);
143         sortQueue();
144     }
145 
146     /***
147      * Modify a job on the queue.
148      *
149      * @param je A JobEntry with the job to modify
150      */
151     public synchronized void modify(JobEntry je)
152             throws TurbineException
153     {
154         remove(je);
155         je.calcRunTime();
156         this.add(je);
157         sortQueue();
158     }
159 
160     /***
161      * Update the job for its next run time.
162      *
163      * @param je A JobEntry to be updated.
164      * @exception TurbineException a generic exception.
165      */
166     public synchronized void updateQueue(JobEntry je)
167             throws TurbineException
168     {
169         je.calcRunTime();
170         sortQueue();
171     }
172 
173     /***
174      * Re-sort the existing queue.  Consumers of this method should be
175      * <code>synchronized</code>.
176      */
177     private void sortQueue()
178     {
179         Comparator aComparator = new Comparator()
180         {
181             public int compare(Object o1, Object o2)
182             {
183                 Long time1 = new Long(((JobEntry) o1).getNextRuntime());
184                 Long time2 = new Long(((JobEntry) o2).getNextRuntime());
185                 return (time1.compareTo(time2));
186             }
187         };
188 
189         Collections.sort(queue, aComparator);
190     }
191 }