001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.soap.jaxws;
031:
032: import java.util.Map;
033:
034: import java.util.concurrent.CancellationException;
035: import java.util.concurrent.ExecutionException;
036: import java.util.concurrent.TimeUnit;
037:
038: import java.util.logging.Logger;
039: import java.util.logging.Level;
040:
041: import javax.xml.ws.Response;
042:
043: import com.caucho.util.Alarm;
044:
045: public class ResponseImpl<T> implements Response<T> {
046: private final static Logger log = Logger
047: .getLogger(ResponseImpl.class.getName());
048:
049: private T _value;
050: private Map<String, Object> _context;
051: private boolean _done = false;
052: private boolean _cancelled = false;
053: private Exception _exception = null;
054:
055: public Map<String, Object> getContext() {
056: return _context;
057: }
058:
059: public void setContext(Map<String, ? extends Object> context) {
060: _context = (Map<String, Object>) context;
061: }
062:
063: public T get() throws InterruptedException, CancellationException,
064: ExecutionException {
065: synchronized (this ) {
066: while (!_done && !_cancelled)
067: wait();
068:
069: if (_cancelled)
070: throw new CancellationException();
071:
072: if (_exception != null)
073: throw new ExecutionException(_exception);
074:
075: return _value;
076: }
077: }
078:
079: public T get(long timeout, TimeUnit unit)
080: throws InterruptedException, CancellationException,
081: ExecutionException {
082: long expire = unit.toMillis(timeout) + Alarm.getExactTime();
083:
084: synchronized (this ) {
085: if (Alarm.isTest()) {
086: // When Alarm.isTest(), getExactTime returns the test time
087: while (!_done && !_cancelled
088: && Alarm.getExactTime() < expire)
089: wait(expire - Alarm.getExactTime());
090: } else {
091: while (!_done && !_cancelled
092: && Alarm.getCurrentTime() < expire)
093: wait(expire - Alarm.getCurrentTime());
094: }
095:
096: if (_cancelled)
097: throw new CancellationException();
098:
099: if (_exception != null)
100: throw new ExecutionException(_exception);
101:
102: return _value;
103: }
104: }
105:
106: public void set(T value) {
107: synchronized (this ) {
108: _done = true;
109: _value = value;
110:
111: notifyAll();
112: }
113: }
114:
115: public void setException(Exception e) {
116: synchronized (this ) {
117: _done = true;
118: _exception = e;
119:
120: notifyAll();
121: }
122: }
123:
124: public boolean cancel(boolean mayInterruptIfRunning) {
125: synchronized (this ) {
126: _cancelled = true;
127:
128: notifyAll();
129:
130: return true;
131: }
132: }
133:
134: public boolean isDone() {
135: return _done;
136: }
137:
138: public boolean isCancelled() {
139: return _cancelled;
140: }
141: }
|