001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.orb.giop;
022:
023: import org.jacorb.orb.*;
024:
025: import org.omg.CORBA.portable.RemarshalException;
026:
027: /**
028: * Connections deliver replies to instances of this class.
029: * The mechanism by which the ORB can retrieve the replies is
030: * implemented in subclasses.
031: *
032: * @author Nicolas Noffke
033: * @version $Id: ReplyPlaceholder.java,v 1.23 2006/08/29 15:03:00 alphonse.bendt Exp $
034: */
035: public abstract class ReplyPlaceholder {
036: protected final Object lock = new Object();
037: protected boolean ready = false;
038: protected boolean communicationException = false;
039: protected boolean remarshalException = false;
040: protected boolean timeoutException = false;
041:
042: protected MessageInputStream in = null;
043:
044: protected final int timeout;
045:
046: /**
047: * self-configuring c'tor
048: */
049:
050: public ReplyPlaceholder(ORB orb) {
051: timeout = orb.getConfiguration().getAttributeAsInteger(
052: "jacorb.connection.client.pending_reply_timeout", 0);
053: }
054:
055: public void replyReceived(MessageInputStream in) {
056: synchronized (lock) {
057: if (!timeoutException) {
058: this .in = in;
059: ready = true;
060: lock.notifyAll();
061: }
062: }
063: }
064:
065: public void cancel() {
066: synchronized (lock) {
067: if (in == null) {
068: communicationException = true;
069: ready = true;
070: lock.notify();
071: }
072: }
073: }
074:
075: public void retry() {
076: synchronized (lock) {
077: remarshalException = true;
078: ready = true;
079: lock.notify();
080: }
081: }
082:
083: /**
084: * Non-public implementation of the blocking method that
085: * returns a reply when it becomes available. Subclasses
086: * should specify a different method, under a different
087: * name, that does any specific processing of the reply before
088: * returning it to the caller.
089: */
090: protected MessageInputStream getInputStream(boolean hasTimeoutPolicy)
091: throws RemarshalException {
092: final boolean _shouldUseTimeout = !hasTimeoutPolicy
093: && timeout > 0;
094: final long _maxWait = _shouldUseTimeout ? System
095: .currentTimeMillis()
096: + timeout : Long.MAX_VALUE;
097: final long _timeout = _shouldUseTimeout ? timeout : 0;
098:
099: synchronized (lock) {
100: try {
101: while (!ready && System.currentTimeMillis() < _maxWait) {
102: lock.wait(_timeout);
103: }
104: } catch (InterruptedException e) {
105: // ignored
106: }
107:
108: if (!ready && _shouldUseTimeout) {
109: timeoutException = true;
110: }
111:
112: if (remarshalException) {
113: throw new org.omg.CORBA.portable.RemarshalException();
114: }
115:
116: if (communicationException) {
117: throw new org.omg.CORBA.COMM_FAILURE(0,
118: org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
119: }
120:
121: if (timeoutException) {
122: throw new org.omg.CORBA.TIMEOUT(
123: "client timeout reached");
124: }
125:
126: return in;
127: }
128: }
129: }
|