001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.web.micro.mts;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.core.service.ThreadService;
034: import org.cougaar.core.thread.Schedulable;
035:
036: /**
037: * A simple "todo" queue for processing work in a pooled thread.
038: * <p>
039: * Should repackage into <code>org.cougaar.core.util</code>.<br>
040: * The blackboard equivalent should use a dummy subscription to hold
041: * onto queued items and present a "getAddedList()" interface.
042: */
043: public abstract class TodoQueue {
044:
045: private final LoggingService log;
046: private final Schedulable thread;
047:
048: private final List todo = new ArrayList();
049: private final List tmp = new ArrayList();
050:
051: /**
052: * @param lane ThreadService lane, e.g. WILL_BLOCK_LANE
053: */
054: public TodoQueue(LoggingService log, ThreadService threadService,
055: String threadName, int lane) {
056: this .log = log;
057:
058: Runnable r = new Runnable() {
059: public void run() {
060: doAllNow();
061: }
062: };
063: this .thread = threadService
064: .getThread(this , r, threadName, lane);
065: }
066:
067: /**
068: * Add work to do in our asynchronous "doNow" callback.
069: *
070: * @param o any object, such as a Runnable
071: */
072: public void add(Object o) {
073: synchronized (todo) {
074: todo.add(o);
075: }
076: thread.start();
077: }
078:
079: /**
080: * Do previously queued work in our single-threaded pooled thread.
081: *
082: * @param o an object passed into {@link #add(Object)}.
083: */
084: protected abstract void doNow(Object o);
085:
086: private void doAllNow() {
087: synchronized (todo) {
088: if (todo.isEmpty())
089: return;
090: tmp.addAll(todo);
091: todo.clear();
092: }
093: for (int i = 0, n = tmp.size(); i < n; i++) {
094: Object oi = tmp.get(i);
095: try {
096: doNow(oi);
097: } catch (Exception e) {
098: String si;
099: try {
100: si = oi.toString();
101: } catch (Exception e2) {
102: si = e2.toString();
103: }
104: if (log.isErrorEnabled()) {
105: log.error("doNow[" + i + "/" + n + "] failed for "
106: + si, e);
107: }
108: }
109: }
110: tmp.clear();
111: }
112: }
|