KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > corba > RequestImpl


1 /*
2  * @(#)RequestImpl.java 1.90 04/04/07
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.corba;
17
18
19 import org.omg.CORBA.Any JavaDoc;
20 import org.omg.CORBA.ARG_IN JavaDoc;
21 import org.omg.CORBA.ARG_OUT JavaDoc;
22 import org.omg.CORBA.ARG_INOUT JavaDoc;
23 import org.omg.CORBA.Context JavaDoc;
24 import org.omg.CORBA.ContextList JavaDoc;
25 import org.omg.CORBA.Environment JavaDoc;
26 import org.omg.CORBA.ExceptionList JavaDoc;
27 import org.omg.CORBA.NVList JavaDoc;
28 import org.omg.CORBA.NamedValue JavaDoc;
29 import org.omg.CORBA.Request JavaDoc;
30 import org.omg.CORBA.SystemException JavaDoc;
31 import org.omg.CORBA.TCKind JavaDoc;
32 import org.omg.CORBA.TypeCode JavaDoc;
33 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
34 import org.omg.CORBA.UnknownUserException JavaDoc;
35 import org.omg.CORBA.Bounds JavaDoc;
36 import org.omg.CORBA.UNKNOWN JavaDoc;
37 import org.omg.CORBA.INTERNAL JavaDoc;
38 import org.omg.CORBA.NO_IMPLEMENT JavaDoc;
39 import org.omg.CORBA.CompletionStatus JavaDoc;
40 import org.omg.CORBA.WrongTransaction JavaDoc;
41
42 import org.omg.CORBA.portable.ApplicationException JavaDoc ;
43 import org.omg.CORBA.portable.RemarshalException JavaDoc ;
44 import org.omg.CORBA.portable.InputStream JavaDoc ;
45 import org.omg.CORBA.portable.OutputStream JavaDoc ;
46
47 import com.sun.corba.se.spi.orb.ORB;
48 import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
49 import com.sun.corba.se.spi.logging.CORBALogDomains;
50 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
51 import com.sun.corba.se.impl.corba.AsynchInvoke;
52
53 public class RequestImpl
54     extends Request JavaDoc
55 {
56     ///////////////////////////////////////////////////////////////////////////
57
// data members
58

59     protected org.omg.CORBA.Object JavaDoc _target;
60     protected String JavaDoc _opName;
61     protected NVList JavaDoc _arguments;
62     protected ExceptionList JavaDoc _exceptions;
63     private NamedValue JavaDoc _result;
64     protected Environment JavaDoc _env;
65     private Context JavaDoc _ctx;
66     private ContextList JavaDoc _ctxList;
67     protected ORB _orb;
68     private ORBUtilSystemException _wrapper;
69
70     // invocation-specific stuff
71
protected boolean _isOneWay = false;
72     private int[] _paramCodes;
73     private long[] _paramLongs;
74     private java.lang.Object JavaDoc[] _paramObjects;
75
76     // support for deferred invocations.
77
// protected instead of private since it needs to be set by the
78
// thread object doing the asynchronous invocation.
79
protected boolean gotResponse = false;
80
81     ///////////////////////////////////////////////////////////////////////////
82
// constructor
83

84     // REVISIT - used to be protected. Now public so it can be
85
// accessed from xgiop.
86
public RequestImpl (ORB orb,
87             org.omg.CORBA.Object JavaDoc targetObject,
88             Context JavaDoc ctx,
89             String JavaDoc operationName,
90             NVList JavaDoc argumentList,
91             NamedValue JavaDoc resultContainer,
92             ExceptionList JavaDoc exceptionList,
93             ContextList JavaDoc ctxList)
94     {
95
96         // initialize the orb
97
_orb = orb;
98     _wrapper = ORBUtilSystemException.get( orb,
99         CORBALogDomains.OA_INVOCATION ) ;
100
101         // initialize target, context and operation name
102
_target = targetObject;
103         _ctx = ctx;
104         _opName = operationName;
105
106         // initialize argument list if not passed in
107
if (argumentList == null)
108             _arguments = new NVListImpl(_orb);
109         else
110             _arguments = argumentList;
111
112         // set result container.
113
_result = resultContainer;
114
115         // initialize exception list if not passed in
116
if (exceptionList == null)
117             _exceptions = new ExceptionListImpl();
118         else
119             _exceptions = exceptionList;
120
121         // initialize context list if not passed in
122
if (ctxList == null)
123             _ctxList = new ContextListImpl(_orb);
124         else
125             _ctxList = ctxList;
126
127         // initialize environment
128
_env = new EnvironmentImpl();
129
130     }
131
132     public org.omg.CORBA.Object JavaDoc target()
133     {
134         return _target;
135     }
136
137     public String JavaDoc operation()
138     {
139         return _opName;
140     }
141
142     public NVList JavaDoc arguments()
143     {
144         return _arguments;
145     }
146     
147     public NamedValue JavaDoc result()
148     {
149         return _result;
150     }
151     
152     public Environment JavaDoc env()
153     {
154         return _env;
155     }
156     
157     public ExceptionList JavaDoc exceptions()
158     {
159         return _exceptions;
160     }
161     
162     public ContextList JavaDoc contexts()
163     {
164         return _ctxList;
165     }
166     
167     public synchronized Context JavaDoc ctx()
168     {
169         if (_ctx == null)
170             _ctx = new ContextImpl(_orb);
171         return _ctx;
172     }
173     
174     public synchronized void ctx(Context JavaDoc newCtx)
175     {
176         _ctx = newCtx;
177     }
178
179     public synchronized Any JavaDoc add_in_arg()
180     {
181         return _arguments.add(org.omg.CORBA.ARG_IN.value).value();
182     }
183
184     public synchronized Any JavaDoc add_named_in_arg(String JavaDoc name)
185     {
186         return _arguments.add_item(name, org.omg.CORBA.ARG_IN.value).value();
187     }
188
189     public synchronized Any JavaDoc add_inout_arg()
190     {
191         return _arguments.add(org.omg.CORBA.ARG_INOUT.value).value();
192     }
193
194     public synchronized Any JavaDoc add_named_inout_arg(String JavaDoc name)
195     {
196         return _arguments.add_item(name, org.omg.CORBA.ARG_INOUT.value).value();
197     }
198
199     public synchronized Any JavaDoc add_out_arg()
200     {
201         return _arguments.add(org.omg.CORBA.ARG_OUT.value).value();
202     }
203
204     public synchronized Any JavaDoc add_named_out_arg(String JavaDoc name)
205     {
206         return _arguments.add_item(name, org.omg.CORBA.ARG_OUT.value).value();
207     }
208
209     public synchronized void set_return_type(TypeCode JavaDoc tc)
210     {
211         if (_result == null)
212             _result = new NamedValueImpl(_orb);
213         _result.value().type(tc);
214     }
215
216     public synchronized Any JavaDoc return_value()
217     {
218         if (_result == null)
219             _result = new NamedValueImpl(_orb);
220         return _result.value();
221     }
222
223     public synchronized void add_exception(TypeCode JavaDoc exceptionType)
224     {
225         _exceptions.add(exceptionType);
226     }
227     
228     public synchronized void invoke()
229     {
230         doInvocation();
231     }
232
233     public synchronized void send_oneway()
234     {
235         _isOneWay = true;
236         doInvocation();
237     }
238     
239     public synchronized void send_deferred()
240     {
241         AsynchInvoke invokeObject = new AsynchInvoke(_orb, this, false);
242         new Thread JavaDoc(invokeObject).start();
243     }
244     
245     public synchronized boolean poll_response()
246     {
247         // this method has to be synchronized even though it seems
248
// "readonly" since the thread object doing the asynchronous
249
// invocation can potentially update this variable in parallel.
250
// updates are currently simply synchronized againt the request
251
// object.
252
return gotResponse;
253     }
254     
255     public synchronized void get_response()
256         throws org.omg.CORBA.WrongTransaction JavaDoc
257     {
258         while (gotResponse == false) {
259             // release the lock. wait to be notified by the thread that is
260
// doing the asynchronous invocation.
261
try {
262             wait();
263             }
264         catch (InterruptedException JavaDoc e) {}
265         }
266     }
267     
268     ///////////////////////////////////////////////////////////////////////////
269
// private helper methods
270

271     /*
272      * The doInvocation operation is where the real mechanics of
273      * performing the request invocation is done.
274      */

275     protected void doInvocation()
276     {
277         org.omg.CORBA.portable.Delegate JavaDoc delegate = StubAdapter.getDelegate(
278         _target ) ;
279
280     // Initiate Client Portable Interceptors. Inform the PIHandler that
281
// this is a DII request so that it knows to ignore the second
282
// inevitable call to initiateClientPIRequest in createRequest.
283
// Also, save the RequestImpl object for later use.
284
_orb.getPIHandler().initiateClientPIRequest( true );
285     _orb.getPIHandler().setClientPIInfo( this );
286
287     InputStream JavaDoc $in = null;
288     try {
289         OutputStream JavaDoc $out = delegate.request(null, _opName, !_isOneWay);
290         // Marshal args
291
try {
292         for (int i=0; i<_arguments.count() ; i++) {
293             NamedValue JavaDoc nv = _arguments.item(i);
294             switch (nv.flags()) {
295             case ARG_IN.value:
296             nv.value().write_value($out);
297             break;
298             case ARG_OUT.value:
299             break;
300             case ARG_INOUT.value:
301             nv.value().write_value($out);
302             break;
303                 }
304         }
305         } catch ( org.omg.CORBA.Bounds JavaDoc ex ) {
306         throw _wrapper.boundsErrorInDiiRequest( ex ) ;
307         }
308
309         $in = delegate.invoke(null, $out);
310     } catch (ApplicationException JavaDoc e) {
311         // REVISIT - minor code.
312
// This is already handled in subcontract.
313
// REVISIT - uncomment.
314
//throw new INTERNAL();
315
} catch (RemarshalException JavaDoc e) {
316         doInvocation();
317     } catch( SystemException JavaDoc ex ) {
318         _env.exception(ex);
319         // NOTE: The exception should not be thrown.
320
// However, JDK 1.4 and earlier threw the exception,
321
// so we keep the behavior to be compatible.
322
throw ex;
323     } finally {
324         delegate.releaseReply(null, $in);
325         }
326     }
327
328     // REVISIT - make protected after development - so xgiop can get it.
329
public void unmarshalReply(InputStream JavaDoc is)
330     {
331         // First unmarshal the return value if it is not void
332
if ( _result != null ) {
333             Any JavaDoc returnAny = _result.value();
334             TypeCode JavaDoc returnType = returnAny.type();
335             if ( returnType.kind().value() != TCKind._tk_void )
336                 returnAny.read_value(is, returnType);
337         }
338         
339         // Now unmarshal the out/inout args
340
try {
341             for ( int i=0; i<_arguments.count() ; i++) {
342                 NamedValue JavaDoc nv = _arguments.item(i);
343                 switch( nv.flags() ) {
344         case ARG_IN.value:
345             break;
346         case ARG_OUT.value:
347         case ARG_INOUT.value:
348             Any JavaDoc any = nv.value();
349             any.read_value(is, any.type());
350             break;
351                 }
352             }
353         }
354     catch ( org.omg.CORBA.Bounds JavaDoc ex ) {
355         // Cannot happen since we only iterate till _arguments.count()
356
}
357     }
358 }
359
Popular Tags