001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.scheduler;
018:
019: import java.util.List;
020: import java.util.Vector;
021: import java.util.Collections;
022: import java.util.Comparator;
023:
024: /**
025: * Queue for the scheduler.
026: *
027: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
028: * @version $Id: JobQueue.java 516448 2007-03-09 16:25:47Z ate $
029: */
030: public class JobQueue {
031: /**
032: * The queue of <code>JobEntry</code> objects.
033: */
034: private Vector queue = null;
035:
036: /**
037: * Creates a new instance.
038: *
039: * @exception Exception A generic exception.
040: */
041: public JobQueue() throws Exception {
042: queue = new Vector(10);
043: }
044:
045: /**
046: * Return the next job off the top of the queue, or <code>null</code> if
047: * there are no jobs in the queue.
048: *
049: * @return The next job in the queue.
050: */
051: public JobEntry getNext() {
052: if (queue.size() > 0) {
053: return (JobEntry) queue.elementAt(0);
054: } else {
055: return null;
056: }
057: }
058:
059: /**
060: * Return a specific job.
061: *
062: * @param je The JobEntry we are looking for.
063: * @return A JobEntry.
064: */
065: public JobEntry getJob(JobEntry je) {
066: int index = -1;
067:
068: if (je != null) {
069: index = queue.indexOf(je);
070: }
071:
072: if (index < 0) {
073: return null;
074: } else {
075: return (JobEntry) queue.elementAt(index);
076: }
077: }
078:
079: /**
080: * List jobs in the queue. This is used by the scheduler UI.
081: *
082: * @return A Vector of <code>JobEntry</code> objects.
083: */
084: public Vector list() {
085: if (queue != null && queue.size() > 0) {
086: return (Vector) queue.clone();
087: } else {
088: return null;
089: }
090: }
091:
092: /**
093: * Add a job to the queue.
094: *
095: * @param je A JobEntry job.
096: */
097: public synchronized void add(JobEntry je) {
098: queue.addElement(je);
099: sortQueue();
100: }
101:
102: /**
103: * Batch load jobs. Retains any already enqueued jobs. Called on
104: * <code>SchedulerService</code> start-up.
105: *
106: * @param jobEntries A list of the <code>JobEntry</code> objects to load.
107: */
108: public synchronized void batchLoad(List jobEntries) {
109: if (jobEntries != null) {
110: queue.addAll(jobEntries);
111: sortQueue();
112: }
113:
114: }
115:
116: /**
117: * Remove a job from the queue.
118: *
119: * @param je A JobEntry with the job to remove.
120: */
121: public synchronized void remove(JobEntry je) {
122: queue.removeElement(je);
123: sortQueue();
124: }
125:
126: /**
127: * Modify a job on the queue.
128: *
129: * @param je A JobEntry with the job to modify
130: */
131: public synchronized void modify(JobEntry je) {
132: sortQueue();
133: }
134:
135: /**
136: * Update the job for its next run time.
137: *
138: * @param je A JobEntry to be updated.
139: * @exception Exception, a generic exception.
140: */
141: public synchronized void updateQueue(JobEntry je) throws Exception {
142: je.calcRunTime();
143: sortQueue();
144: }
145:
146: /**
147: * Re-sort the existing queue. Consumers of this method should be
148: * <code>synchronized</code>.
149: */
150: private void sortQueue() {
151: Comparator aComparator = new Comparator() {
152: public int compare(Object o1, Object o2) {
153: Long time1 = new Long(((JobEntry) o1).getNextRuntime());
154: Long time2 = new Long(((JobEntry) o2).getNextRuntime());
155: return (time1.compareTo(time2));
156: }
157: };
158:
159: Collections.sort(queue, aComparator);
160: }
161: }
|