KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > giop > ReplyPlaceholder


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.giop;
22
23 import org.jacorb.orb.*;
24
25 import org.omg.GIOP.*;
26 import org.omg.CORBA.portable.RemarshalException JavaDoc;
27
28 /**
29  * Connections deliver replies to instances of this class.
30  * The mechanism by which the ORB can retrieve the replies is
31  * implemented in subclasses.
32  *
33  * @author Nicolas Noffke
34  * @version $Id: ReplyPlaceholder.java,v 1.20 2004/05/19 11:25:47 andre.spiegel Exp $
35  */

36 public abstract class ReplyPlaceholder
37 {
38     protected boolean ready = false;
39     protected boolean communicationException = false;
40     protected boolean remarshalException = false;
41     protected boolean timeoutException = false;
42
43     protected MessageInputStream in = null;
44
45     protected int timeout ;
46
47     /**
48      * self-configuring c'tor
49      */

50
51     public ReplyPlaceholder(ORB orb)
52     {
53         timeout =
54             orb.getConfiguration().getAttributeAsInteger("jacorb.connection.client.pending_reply_timeout", 0);
55     }
56
57     public synchronized void replyReceived( MessageInputStream in )
58     {
59         if( ! timeoutException )
60         {
61             this.in = in;
62             ready = true;
63             notifyAll();
64         }
65     }
66
67     public synchronized void cancel()
68     {
69         if( in == null )
70         {
71             communicationException = true;
72             ready = true;
73             notify();
74         }
75     }
76
77
78     public synchronized void retry()
79     {
80         remarshalException = true;
81         ready = true;
82         notify();
83     }
84
85 // public synchronized void timeout()
86
// {
87
// timeoutException = true;
88
// ready = true;
89
// notify();
90
// }
91

92     /**
93      * Non-public implementation of the blocking method that
94      * returns a reply when it becomes available. Subclasses
95      * should specify a different method, under a different
96      * name, that does any specific processing of the reply before
97      * returning it to the caller.
98      */

99     protected synchronized MessageInputStream getInputStream()
100         throws RemarshalException JavaDoc
101     {
102         while( !ready )
103         {
104             try
105             {
106                 if( timeout > 0 )
107                 {
108                     wait( timeout ); //wait only "timeout" long
109

110                     //timeout
111
if( ! ready )
112                     {
113                         ready = true; //break loop
114
timeoutException = true;
115                     }
116                 }
117                 else
118                 {
119                     wait(); //wait infinitely
120
}
121             }
122             catch( InterruptedException JavaDoc e )
123             {}
124         }
125
126         if( remarshalException )
127         {
128             throw new org.omg.CORBA.portable.RemarshalException JavaDoc();
129         }
130
131         if( communicationException )
132         {
133             throw new org.omg.CORBA.COMM_FAILURE JavaDoc(
134                 0,
135                 org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE );
136         }
137
138         if( timeoutException )
139         {
140             throw new org.omg.CORBA.TIMEOUT JavaDoc ("client timeout reached");
141         }
142
143         return in;
144     }
145 }// ReplyPlaceholder
146
Popular Tags