1 package org.jacorb.orb.dii; 2 3 22 import org.omg.CORBA.Any ; 23 import org.omg.CORBA.TypeCode ; 24 import org.omg.CORBA.NVList ; 25 import org.omg.CORBA.NamedValue ; 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 ; 34 35 41 42 public class Request 43 extends org.omg.CORBA.Request 44 { 45 private org.jacorb.orb.NamedValue result_value; 46 private org.omg.CORBA.ExceptionList exceptions; 47 private org.omg.CORBA.ContextList contexts; 48 private org.omg.CORBA.Context ctx; 49 private Thread deferred_caller; 50 private org.jacorb.orb.ORB orb; 51 private org.omg.CORBA.portable.InputStream reply; 52 53 54 private boolean immediate = false; 55 private boolean deferred = false; 56 private boolean finished = false; 57 58 public org.omg.CORBA.Object target; 59 public ClientConnection connection; 60 public byte[] object_key; 61 public NVList arguments; 62 public String operation; 63 public org.omg.CORBA.Environment env = new Environment(); 64 65 private ClientRequestInfoImpl info = null; 66 67 public Request( org.omg.CORBA.Object t, 68 org.omg.CORBA.ORB _orb, 69 ClientConnection e, 70 byte[] obj_key, 71 String 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 )orb.create_list(10); 80 Any a = orb.create_any(); 81 82 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 t, 89 org.omg.CORBA.ORB _orb, 90 ClientConnection e, 91 byte[] obj_key, 92 String op, 93 org.omg.CORBA.NVList args, 94 org.omg.CORBA.Context c, 95 org.omg.CORBA.NamedValue 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 )args; 104 ctx = c; 105 result_value = (org.jacorb.orb.NamedValue)result; 106 } 107 108 public org.omg.CORBA.Object target() 109 { 110 return target; 111 } 112 113 public java.lang.String operation() 114 { 115 return operation; 116 } 117 118 public org.omg.CORBA.NVList arguments() 119 { 120 return arguments; 121 } 122 123 public org.omg.CORBA.NamedValue result() 124 { 125 return result_value; 126 } 127 128 public org.omg.CORBA.Environment env() 129 { 130 return env; 131 } 132 133 public org.omg.CORBA.ExceptionList exceptions() 134 { 135 return exceptions; 136 } 137 138 public org.omg.CORBA.ContextList contexts() 139 { 140 return contexts; 141 } 142 143 public org.omg.CORBA.Context ctx() 144 { 145 return ctx; 146 } 147 148 public void ctx( org.omg.CORBA.Context a) 149 { 150 ctx = a; 151 } 152 153 public Any add_in_arg() 154 { 155 NamedValue 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 add_named_in_arg(java.lang.String name) 161 { 162 NamedValue 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 add_inout_arg() 168 { 169 NamedValue 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 add_named_inout_arg(java.lang.String name) 175 { 176 NamedValue 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 add_out_arg() 182 { 183 NamedValue 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 add_named_out_arg(java.lang.String name) 189 { 190 NamedValue 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 198 199 public void set_return_type( org.omg.CORBA.TypeCode tc) 200 { 201 result_value.value().type(tc); 202 } 203 204 public Any 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 215 for( Enumeration 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 )target)._get_delegate(); 230 231 RequestOutputStream ros = (RequestOutputStream) 232 deleg.request(target, operation, response_expected); 233 234 ros.setRequest(this); 235 236 for( Enumeration 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 } 263 info = null; 264 } 265 } 266 } 267 catch (RemarshalException rem) 268 { 269 continue; 271 } 272 catch (ApplicationException ae) 273 { 274 org.omg.CORBA.Any any; 275 org.omg.CORBA.TypeCode tc; 276 String 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 (any)); 289 break; 290 } 291 } 292 catch (org.omg.CORBA.TypeCodePackage.BadKind ex) 293 { 294 } 295 catch (org.omg.CORBA.Bounds ex) 296 { 297 break; 298 } 299 } 300 301 break; 302 } 303 catch (Exception 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 static class Caller extends Thread 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 364 ( 11, org.omg.CORBA.CompletionStatus.COMPLETED_NO ); 365 } 366 if ( immediate ) 367 { 368 throw new org.omg.CORBA.BAD_INV_ORDER 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 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 393 ( 11, org.omg.CORBA.CompletionStatus.COMPLETED_NO ); 394 } 395 if ( immediate ) 396 { 397 throw new org.omg.CORBA.BAD_INV_ORDER 398 ( 13, org.omg.CORBA.CompletionStatus.COMPLETED_NO ); 399 } 400 if ( deferred_caller == null ) 401 { 402 throw new org.omg.CORBA.BAD_INV_ORDER 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 410 { 411 if( immediate || deferred ) 412 { 413 throw new org.omg.CORBA.BAD_INV_ORDER 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 421 { 422 if( immediate || deferred ) 423 { 424 throw new org.omg.CORBA.BAD_INV_ORDER 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 |