View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.scheduler;
18  
19  import java.util.List;
20  import java.util.Vector;
21  import java.util.Collections;
22  import java.util.Comparator;
23  
24  /***
25   * Queue for the scheduler.
26   *
27   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
28   * @version $Id: JobQueue.java 516448 2007-03-09 16:25:47Z ate $
29   */
30  public class JobQueue
31  {
32      /***
33       * The queue of <code>JobEntry</code> objects.
34       */
35      private Vector queue = null;
36  
37      /***
38       * Creates a new instance.
39       *
40       * @exception Exception A generic exception.
41       */
42      public JobQueue()
43          throws Exception
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     {
153         sortQueue();
154     }
155 
156     /***
157      * Update the job for its next run time.
158      *
159      * @param je A JobEntry to be updated.
160      * @exception Exception, a generic exception.
161      */
162     public synchronized void updateQueue(JobEntry je)
163         throws Exception
164     {
165         je.calcRunTime();
166         sortQueue();
167     }
168 
169     /***
170      * Re-sort the existing queue.  Consumers of this method should be
171      * <code>synchronized</code>.
172      */
173     private void sortQueue()
174     {
175         Comparator aComparator = new Comparator () 
176             {
177                 public int compare(Object o1, Object o2) 
178                 {
179                     Long time1 = new Long (((JobEntry)o1).getNextRuntime());
180                     Long time2 = new Long (((JobEntry)o2).getNextRuntime());
181                     return (time1.compareTo(time2));
182                 }
183             };
184                                                
185         Collections.sort(queue,aComparator);
186     }
187 }