001: //========================================================================
002: //$Id: WaitingContinuation.java,v 1.1 2005/11/14 17:45:56 gregwilkins Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.util.ajax;
017:
018: public class WaitingContinuation implements
019: org.mortbay.util.ajax.Continuation {
020: Object _mutex;
021: Object _object;
022: boolean _new = true;
023: boolean _resumed = false;
024: boolean _pending = false;
025:
026: public WaitingContinuation() {
027: _mutex = this ;
028: }
029:
030: public WaitingContinuation(Object mutex) {
031: _mutex = mutex == null ? this : mutex;
032: }
033:
034: public void resume() {
035: synchronized (_mutex) {
036: _resumed = true;
037: _mutex.notify();
038: }
039: }
040:
041: public void reset() {
042: synchronized (_mutex) {
043: _resumed = false;
044: _pending = false;
045: _mutex.notify();
046: }
047: }
048:
049: public boolean isNew() {
050: return _new;
051: }
052:
053: public boolean suspend(long timeout) {
054: synchronized (_mutex) {
055: _new = false;
056: _pending = true;
057: boolean result;
058: try {
059: if (!_resumed && timeout >= 0) {
060: _mutex.wait(timeout);
061: }
062: } catch (InterruptedException e) {
063: e.printStackTrace();
064: } finally {
065: result = _resumed;
066: _resumed = false;
067: _pending = false;
068: }
069:
070: return result;
071: }
072: }
073:
074: public boolean isPending() {
075: return _pending;
076: }
077:
078: public boolean isResumed() {
079: return _resumed;
080: }
081:
082: public Object getObject() {
083: return _object;
084: }
085:
086: public void setObject(Object object) {
087: _object = object;
088: }
089:
090: public Object getMutex() {
091: return _mutex;
092: }
093:
094: public void setMutex(Object mutex) {
095: if (!_new && _mutex != this )
096: throw new IllegalStateException();
097: _mutex = mutex == null ? this : mutex;
098: }
099:
100: }
|