View Javadoc

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 534527 2007-05-02 16:10:59Z tv $
35   */
36  public class JobQueue
37  {
38      /***
39       * The queue of <code>JobEntry</code> objects.
40       */
41      private Vector queue = null;
42  
43      /***
44       * Creates a new instance.
45       */
46      public JobQueue()
47      {
48          queue = new Vector(10);
49      }
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          if (queue.size() > 0)
60          {
61              return (JobEntry) queue.elementAt(0);
62          }
63          else
64          {
65              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          int index = -1;
78  
79          if (je != null)
80          {
81              index = queue.indexOf(je);
82          }
83  
84          if (index < 0)
85          {
86              return null;
87          }
88          else
89          {
90              return (JobEntry) 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      public Vector list()
100     {
101         if (queue != null && queue.size() > 0)
102         {
103             return (Vector) queue.clone();
104         }
105         else
106         {
107             return null;
108         }
109     }
110 
111     /***
112      * Add a job to the queue.
113      *
114      * @param je A JobEntry job.
115      */
116     public synchronized void add(JobEntry je)
117     {
118         queue.addElement(je);
119         sortQueue();
120     }
121 
122     /***
123      * Batch load jobs.  Retains any already enqueued jobs.  Called on
124      * <code>SchedulerService</code> start-up.
125      *
126      * @param jobEntries A list of the <code>JobEntry</code> objects to load.
127      */
128     public synchronized void batchLoad(List jobEntries)
129     {
130         if (jobEntries != null)
131         {
132             queue.addAll(jobEntries);
133             sortQueue();
134         }
135 
136     }
137 
138     /***
139      * Remove a job from the queue.
140      *
141      * @param je A JobEntry with the job to remove.
142      */
143     public synchronized void remove(JobEntry je)
144     {
145         queue.removeElement(je);
146         sortQueue();
147     }
148 
149     /***
150      * Modify a job on the queue.
151      *
152      * @param je A JobEntry with the job to modify
153      */
154     public synchronized void modify(JobEntry je)
155             throws TurbineException
156     {
157         remove(je);
158         je.calcRunTime();
159         this.add(je);
160         sortQueue();
161     }
162 
163     /***
164      * Update the job for its next run time.
165      *
166      * @param je A JobEntry to be updated.
167      * @exception TurbineException a generic exception.
168      */
169     public synchronized void updateQueue(JobEntry je)
170             throws TurbineException
171     {
172         je.calcRunTime();
173         sortQueue();
174     }
175 
176     /***
177      * Re-sort the existing queue.  Consumers of this method should be
178      * <code>synchronized</code>.
179      */
180     private void sortQueue()
181     {
182         Comparator aComparator = new Comparator()
183         {
184             public int compare(Object o1, Object o2)
185             {
186                 Long time1 = new Long(((JobEntry) o1).getNextRuntime());
187                 Long time2 = new Long(((JobEntry) o2).getNextRuntime());
188                 return (time1.compareTo(time2));
189             }
190         };
191 
192         Collections.sort(queue, aComparator);
193     }
194 }