KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > dii > Request


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

22 import org.omg.CORBA.Any JavaDoc;
23 import org.omg.CORBA.TypeCode JavaDoc;
24 import org.omg.CORBA.NVList JavaDoc;
25 import org.omg.CORBA.NamedValue JavaDoc;
26 import org.omg.CORBA.portable.*;
27
28 import org.jacorb.orb.portableInterceptor.*;
29 import org.jacorb.orb.ParsedIOR;
30 import org.jacorb.orb.giop.*;
31 import org.jacorb.orb.*;
32
33 import java.util.Enumeration JavaDoc;
34
35 /**
36  * DII requests
37  *
38  * @author Gerald Brose, FU Berlin
39  * @version $Id: Request.java,v 1.16 2004/06/09 05:34:28 andre.spiegel Exp $
40  */

41
42 public class Request
43     extends org.omg.CORBA.Request JavaDoc
44 {
45     private org.jacorb.orb.NamedValue result_value;
46     private org.omg.CORBA.ExceptionList JavaDoc exceptions;
47     private org.omg.CORBA.ContextList JavaDoc contexts;
48     private org.omg.CORBA.Context JavaDoc ctx;
49     private Thread JavaDoc deferred_caller;
50     private org.jacorb.orb.ORB orb;
51     private org.omg.CORBA.portable.InputStream JavaDoc reply;
52
53     /* state of request object */
54     private boolean immediate = false;
55     private boolean deferred = false;
56     private boolean finished = false;
57
58     public org.omg.CORBA.Object JavaDoc target;
59     public ClientConnection connection;
60     public byte[] object_key;
61     public NVList JavaDoc arguments;
62     public String JavaDoc operation;
63     public org.omg.CORBA.Environment JavaDoc env = new Environment();
64
65     private ClientRequestInfoImpl info = null;
66
67     public Request( org.omg.CORBA.Object JavaDoc t,
68                     org.omg.CORBA.ORB JavaDoc _orb,
69                     ClientConnection e,
70                     byte[] obj_key,
71                     String JavaDoc op)
72     {
73         target = t;
74         orb = (org.jacorb.orb.ORB)_orb;
75         connection = e;
76         object_key = obj_key;
77         operation = op;
78         exceptions = new ExceptionList();
79         arguments = (NVList JavaDoc)orb.create_list(10);
80         Any JavaDoc a = orb.create_any();
81
82         /* default return type is void */
83         a.type( orb.get_primitive_tc( org.omg.CORBA.TCKind.tk_void ) );
84         result_value = new org.jacorb.orb.NamedValue(1);
85         result_value.set_value(a);
86     }
87
88     public Request( org.omg.CORBA.Object JavaDoc t,
89                     org.omg.CORBA.ORB JavaDoc _orb,
90                     ClientConnection e,
91                     byte[] obj_key,
92                     String JavaDoc op,
93                     org.omg.CORBA.NVList JavaDoc args,
94                     org.omg.CORBA.Context JavaDoc c,
95                     org.omg.CORBA.NamedValue JavaDoc result)
96     {
97         target = t;
98         orb = (org.jacorb.orb.ORB)_orb;
99         connection = e;
100         object_key = obj_key;
101         operation = op;
102         exceptions = new ExceptionList();
103         arguments = (NVList JavaDoc)args;
104         ctx = c;
105         result_value = (org.jacorb.orb.NamedValue)result;
106     }
107
108     public org.omg.CORBA.Object JavaDoc target()
109     {
110         return target;
111     }
112
113     public java.lang.String JavaDoc operation()
114     {
115         return operation;
116     }
117
118     public org.omg.CORBA.NVList JavaDoc arguments()
119     {
120         return arguments;
121     }
122
123     public org.omg.CORBA.NamedValue JavaDoc result()
124     {
125         return result_value;
126     }
127
128     public org.omg.CORBA.Environment JavaDoc env()
129     {
130         return env;
131     }
132
133     public org.omg.CORBA.ExceptionList JavaDoc exceptions()
134     {
135         return exceptions;
136     }
137
138     public org.omg.CORBA.ContextList JavaDoc contexts()
139     {
140         return contexts;
141     }
142
143     public org.omg.CORBA.Context JavaDoc ctx()
144     {
145         return ctx;
146     }
147
148     public void ctx( org.omg.CORBA.Context JavaDoc a)
149     {
150         ctx = a;
151     }
152
153     public Any JavaDoc add_in_arg()
154     {
155         NamedValue JavaDoc nv = arguments.add(org.omg.CORBA.ARG_IN.value);
156         ((org.jacorb.orb.NamedValue)nv).set_value( orb.create_any());
157         return nv.value();
158     }
159
160     public Any JavaDoc add_named_in_arg(java.lang.String JavaDoc name)
161     {
162         NamedValue JavaDoc nv = arguments.add_item(name,org.omg.CORBA.ARG_IN.value);
163         ((org.jacorb.orb.NamedValue)nv).set_value( orb.create_any());
164         return nv.value();
165     }
166
167     public Any JavaDoc add_inout_arg()
168     {
169         NamedValue JavaDoc nv = arguments.add(org.omg.CORBA.ARG_INOUT.value);
170         ((org.jacorb.orb.NamedValue)nv).set_value( orb.create_any());
171         return nv.value();
172     }
173
174     public Any JavaDoc add_named_inout_arg(java.lang.String JavaDoc name)
175     {
176         NamedValue JavaDoc nv = arguments.add_item(name,org.omg.CORBA.ARG_INOUT.value);
177         ((org.jacorb.orb.NamedValue)nv).set_value( orb.create_any());
178         return nv.value();
179     }
180
181     public Any JavaDoc add_out_arg()
182     {
183         NamedValue JavaDoc nv = arguments.add(org.omg.CORBA.ARG_OUT.value);
184         ((org.jacorb.orb.NamedValue)nv).set_value( orb.create_any());
185         return nv.value();
186     }
187
188     public Any JavaDoc add_named_out_arg(java.lang.String JavaDoc name)
189     {
190         NamedValue JavaDoc nv = arguments.add_item( name, org.omg.CORBA.ARG_OUT.value );
191         ((org.jacorb.orb.NamedValue)nv).set_value( orb.create_any());
192         return nv.value();
193     }
194
195     /**
196      * default return type is void
197      */

198
199     public void set_return_type( org.omg.CORBA.TypeCode JavaDoc tc)
200     {
201         result_value.value().type(tc);
202     }
203
204     public Any JavaDoc return_value()
205     {
206         return result_value.value();
207     }
208
209     private void _read_result()
210     {
211         if( result_value.value().type().kind() != org.omg.CORBA.TCKind.tk_void )
212             result_value.value().read_value( reply, result_value.value().type() );
213
214         /** get out/inout parameters if any */
215         for( Enumeration JavaDoc e = ((org.jacorb.orb.NVList)arguments).enumerate(); e.hasMoreElements();)
216         {
217             org.jacorb.orb.NamedValue nv =
218                 (org.jacorb.orb.NamedValue)e.nextElement();
219             if( nv.flags() != org.omg.CORBA.ARG_IN.value )
220                 nv.receive(reply);
221         }
222     }
223
224     private void _invoke( boolean response_expected )
225     {
226         while (true)
227         {
228             org.jacorb.orb.Delegate deleg =
229                 (org.jacorb.orb.Delegate)((org.omg.CORBA.portable.ObjectImpl JavaDoc)target)._get_delegate();
230
231             RequestOutputStream ros = (RequestOutputStream)
232                 deleg.request(target, operation, response_expected);
233
234             ros.setRequest(this);
235
236             for( Enumeration JavaDoc e = ((org.jacorb.orb.NVList)arguments).enumerate(); e.hasMoreElements();)
237             {
238                 org.jacorb.orb.NamedValue nv = (org.jacorb.orb.NamedValue)e.nextElement();
239                 if( nv.flags() != org.omg.CORBA.ARG_OUT.value )
240                     nv.send(ros);
241             }
242
243             try
244             {
245                 reply = deleg.invoke(target, ros);
246
247                 if( response_expected )
248                 {
249                     _read_result();
250
251                     if (info != null)
252                     {
253                         info.setResult (result_value.value());
254                         InterceptorManager manager = orb.getInterceptorManager();
255                         info.setCurrent (manager.getCurrent());
256
257                         try{
258                             deleg.invokeInterceptors(info,
259                                                      ClientInterceptorIterator.RECEIVE_REPLY);
260                         }catch(RemarshalException rem){
261                             //not allowed to happen here anyway
262
}
263                         info = null;
264                     }
265                 }
266             }
267             catch (RemarshalException rem)
268             {
269                 // Try again
270
continue;
271             }
272             catch (ApplicationException ae)
273             {
274                 org.omg.CORBA.Any JavaDoc any;
275                 org.omg.CORBA.TypeCode JavaDoc tc;
276                 String JavaDoc id = ae.getId ();
277                 int count = (exceptions == null) ? 0 : exceptions.count ();
278
279                 for (int i = 0; i < count; i++)
280                 {
281                     try
282                     {
283                         tc = exceptions.item (i);
284                         if (id.equals (tc.id ()))
285                         {
286                             any = orb.create_any ();
287                             any.read_value (ae.getInputStream (), tc);
288                             env.exception (new org.omg.CORBA.UnknownUserException JavaDoc (any));
289                             break;
290                         }
291                     }
292                     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
293                     {
294                     }
295                     catch (org.omg.CORBA.Bounds JavaDoc ex)
296                     {
297                        break;
298                     }
299                 }
300
301                 break;
302             }
303             catch (Exception JavaDoc e)
304             {
305                 env.exception (e);
306                 break;
307             }
308
309             break;
310         }
311     }
312
313     public void setInfo(ClientRequestInfoImpl info)
314     {
315         this.info = info;
316     }
317
318     public void invoke()
319     {
320         start();
321         _invoke(true);
322         finish();
323     }
324
325     public void send_oneway()
326     {
327         start();
328         _invoke(false);
329         finish();
330     }
331
332
333     // Made static as does not depend upon Request instance information.
334
static class Caller extends Thread JavaDoc
335     {
336         private Request r;
337
338         public Caller( Request client )
339         {
340             r = client;
341         }
342
343         public void run()
344         {
345             r._invoke(true);
346             r.finish();
347         }
348     }
349
350
351     public synchronized void send_deferred()
352     {
353         defer();
354         orb.addRequest( this );
355         deferred_caller = new Caller( this );
356         deferred_caller.start();
357     }
358
359     public synchronized void get_response()
360     {
361         if( ! immediate && ! deferred )
362         {
363             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
364                 ( 11, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
365         }
366         if ( immediate )
367         {
368             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
369                 ( 13, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
370         }
371
372         if( deferred_caller != null )
373         {
374             if ( deferred_caller.isAlive() )
375             {
376                 try
377                 {
378                     deferred_caller.join();
379                 }
380                 catch ( InterruptedException JavaDoc i ){}
381             }
382             deferred_caller = null;
383             orb.removeRequest( this );
384         }
385     }
386
387     public boolean poll_response()
388     {
389         Thread.yield();
390         if( ! immediate && ! deferred )
391         {
392             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
393                 ( 11, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
394         }
395         if ( immediate )
396         {
397             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
398                 ( 13, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
399         }
400         if ( deferred_caller == null )
401         {
402             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
403                 ( 12, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
404         }
405         return finished;
406     }
407
408     private synchronized void start()
409         throws org.omg.CORBA.BAD_INV_ORDER JavaDoc
410     {
411         if( immediate || deferred )
412         {
413             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
414                 ( 10, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
415         }
416         immediate = true;
417     }
418
419     private synchronized void defer()
420         throws org.omg.CORBA.BAD_INV_ORDER JavaDoc
421     {
422         if( immediate || deferred )
423         {
424             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc
425                 ( 10, org.omg.CORBA.CompletionStatus.COMPLETED_NO );
426         }
427         deferred = true;
428     }
429
430     private void finish()
431     {
432         finished = true;
433     }
434 }
435
Popular Tags