1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82:
83: import ;
84:
85:
91: public class LocalRequest extends gnuRequest implements ResponseHandler,
92: CurrentOperations
93: {
94:
97: CookieHolder cookie;
98:
99:
102: final byte[] Id;
103:
104:
107: private static final MessageHeader header = new MessageHeader();
108:
109:
113: boolean exceptionReply;
114:
115:
118: BufferedCdrOutput buffer;
119:
120:
123: final gnuPOA poa;
124:
125:
128: gnuServantObject object;
129:
130:
133: LocalServerRequest serverRequest;
134:
135:
138: public LocalRequest(gnuServantObject local_object, gnuPOA a_poa, byte[] an_id)
139: {
140: Id = an_id;
141: poa = a_poa;
142:
143:
144: if (poa.servant_locator != null)
145: {
146: cookie = new CookieHolder();
147: }
148: object = local_object;
149: prepareStream();
150: }
151:
152:
156: org.omg.CORBA.portable.InputStream s_invoke(InvokeHandler handler)
157: throws ApplicationException
158: {
159: try
160: {
161: poa.m_orb.currents.put(Thread.currentThread(), this);
162:
163: org.omg.CORBA.portable.InputStream input = v_invoke(handler);
164:
165: if (!exceptionReply)
166: {
167: return input;
168: }
169: else
170: {
171: input.mark(500);
172:
173: String id = input.read_string();
174: try
175: {
176: input.reset();
177: }
178: catch (IOException ex)
179: {
180: InternalError ierr = new InternalError();
181: ierr.initCause(ex);
182: throw ierr;
183: }
184: throw new ApplicationException(id, input);
185: }
186: }
187: finally
188: {
189: poa.m_orb.currents.remove(Thread.currentThread());
190: }
191: }
192:
193:
199: public org.omg.CORBA.portable.InputStream v_invoke(InvokeHandler handler)
200: {
201:
202: poa.checkDiscarding();
203:
204:
205:
206: boolean s_intercept = false;
207: ServerRequestInterceptorOperations s_interceptor = null;
208: gnuServerRequestInfo s_info = null;
209:
210: boolean c_intercept = false;
211: ClientRequestInterceptorOperations c_interceptor = null;
212: gnuClientRequestInfo c_info = null;
213:
214: try
215: {
216: if (poa.m_orb.iServer != null || poa.m_orb.iClient != null)
217: {
218: setORB(poa.m_orb);
219:
220:
221: m_rqh = new RequestHeader();
222: m_rqh.operation = m_operation;
223: m_rph = new ReplyHeader();
224:
225: m_rqh.object_key = object.Id;
226: m_rph.request_id = m_rqh.request_id;
227: }
228:
229: if (poa.m_orb.iClient != null)
230: {
231: c_interceptor = poa.m_orb.iClient;
232:
233: c_info = new gnuClientRequestInfo(this);
234: c_intercept = true;
235:
236: c_interceptor.send_request(c_info);
237:
238: m_target = object;
239: }
240:
241: if (poa.m_orb.iServer != null)
242: {
243: s_interceptor = poa.m_orb.iServer;
244:
245: s_info = new gnuServerRequestInfo(object, m_rqh, m_rph);
246: s_info.m_request = this;
247:
248: s_intercept = true;
249:
250: s_interceptor.receive_request_service_contexts(s_info);
251: }
252:
253: if (handler == null)
254: {
255: handler = object.getHandler(operation(), cookie, false);
256: }
257:
258: BufferedCdrOutput request_part = new BufferedCdrOutput();
259:
260: request_part.setOrb(orb());
261:
262: if (m_args != null && m_args.count() > 0)
263: {
264: write_parameters(header, request_part);
265:
266: if (m_parameter_buffer != null)
267: {
268: throw new BAD_INV_ORDER("Please either add parameters or " +
269: "write them into stream, but not both " + "at once."
270: );
271: }
272: }
273:
274: if (m_parameter_buffer != null)
275: {
276: write_parameter_buffer(header, request_part);
277: }
278:
279: Servant servant;
280:
281: if (handler instanceof Servant)
282: {
283: servant = (Servant) handler;
284: }
285: else
286: {
287: throw new BAD_OPERATION("Unexpected handler type " + handler);
288: }
289:
290: org.omg.CORBA.portable.InputStream input =
291: request_part.create_input_stream();
292:
293:
294: ServantDelegateImpl sd = null;
295:
296: Delegate d = null;
297:
298: try
299: {
300: d = servant._get_delegate();
301: }
302: catch (Exception ex)
303: {
304:
305: }
306: if (d instanceof ServantDelegateImpl)
307: {
308:
309:
310: sd = (ServantDelegateImpl) d;
311: if (sd.object != object)
312: {
313: sd = new ServantDelegateImpl(servant, poa, Id);
314: }
315: }
316: else
317: {
318: sd = new ServantDelegateImpl(servant, poa, Id);
319: }
320: servant._set_delegate(sd);
321:
322: try
323: {
324: ORB o = orb();
325: if (o instanceof ORB_1_4)
326: {
327: ((ORB_1_4) o).currents.put(Thread.currentThread(), this);
328: }
329:
330: try
331: {
332: if (s_intercept)
333: {
334: s_interceptor.receive_request(s_info);
335: }
336: handler._invoke(m_operation, input, this);
337:
338:
339: if ((s_intercept || c_intercept) && isExceptionReply())
340: {
341: s_info.m_reply_header.reply_status =
342: ReplyHeader.USER_EXCEPTION;
343: m_rph.reply_status = ReplyHeader.USER_EXCEPTION;
344:
345:
346: Any a = new gnuAny();
347: OutputStream buf = getBuffer();
348: InputStream in = buf.create_input_stream();
349: String uex_idl = "unknown";
350: try
351: {
352: in.mark(Integer.MAX_VALUE);
353: uex_idl = in.read_string();
354: m_exception_id = uex_idl;
355: in.reset();
356: }
357: catch (IOException e)
358: {
359: throw new Unexpected(e);
360: }
361:
362: try
363: {
364: UserException exception =
365: ObjectCreator.readUserException(uex_idl, in);
366:
367: m_environment.exception(exception);
368: ObjectCreator.insertWithHelper(a, exception);
369: }
370: catch (Exception e)
371: {
372:
373:
374: a.insert_Streamable(new StreamHolder(
375: buf.create_input_stream()
376: )
377: );
378:
379: RecordTypeCode r =
380: new RecordTypeCode(TCKind.tk_except);
381: r.setId(uex_idl);
382: r.setName(ObjectCreator.getDefaultName(uex_idl));
383: }
384:
385: s_info.m_usr_exception = a;
386: c_info.m_wrapped_exception = a;
387: s_interceptor.send_exception(s_info);
388: c_interceptor.receive_exception(c_info);
389: }
390: else
391: {
392: if (s_intercept)
393: {
394: s_info.m_reply_header.reply_status =
395: ReplyHeader.NO_EXCEPTION;
396: s_interceptor.send_reply(s_info);
397: }
398: if (c_intercept)
399: {
400: m_rph.reply_status = ReplyHeader.NO_EXCEPTION;
401: c_interceptor.receive_reply(c_info);
402: }
403: }
404: }
405: catch (SystemException sys_ex)
406: {
407: if (s_intercept)
408: {
409: s_info.m_reply_header.reply_status =
410: ReplyHeader.SYSTEM_EXCEPTION;
411: s_info.m_sys_exception = sys_ex;
412: s_interceptor.send_exception(s_info);
413: }
414:
415: if (c_intercept)
416: {
417: m_rph.reply_status = ReplyHeader.SYSTEM_EXCEPTION;
418:
419: Any a = new gnuAny();
420: if (ObjectCreator.insertSysException(a, sys_ex))
421: {
422: c_info.m_wrapped_exception = a;
423: }
424: c_interceptor.receive_exception(c_info);
425: }
426:
427: throw sys_ex;
428: }
429: }
430: finally
431: {
432: ORB o = orb();
433: if (o instanceof ORB_1_4)
434: {
435: ((ORB_1_4) o).currents.remove(Thread.currentThread());
436: }
437: }
438:
439: if (poa.servant_locator != null)
440: {
441: poa.servant_locator.postinvoke(object.Id, poa, operation(),
442: cookie.value, object.getServant()
443: );
444: }
445: return buffer.create_input_stream();
446: }
447:
448: catch (ForwardRequest fex)
449: {
450:
451: if (s_intercept)
452: {
453: Forwarding:
454: while (true)
455: {
456: s_info.m_reply_header.reply_status =
457: ReplyHeader.LOCATION_FORWARD;
458: s_info.m_forward_reference = fex.forward;
459: try
460: {
461: s_interceptor.send_other(s_info);
462: break Forwarding;
463: }
464: catch (ForwardRequest fex2)
465: {
466: s_info.m_forward_reference = fex2.forward;
467: fex.forward = s_info.m_forward_reference;
468: }
469: }
470: }
471:
472: if (c_intercept)
473: {
474: this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
475: this.m_forwarding_target = fex.forward;
476: try
477: {
478: c_interceptor.receive_other(c_info);
479: }
480: catch (ForwardRequest fex2)
481: {
482: fex.forward = fex2.forward;
483: }
484: }
485: throw new gnuForwardRequest(fex.forward);
486: }
487: catch (gnuForwardRequest fex)
488: {
489:
490:
491: if (s_intercept)
492: {
493: Forwarding:
494: while (true)
495: {
496: s_info.m_reply_header.reply_status =
497: ReplyHeader.LOCATION_FORWARD;
498: s_info.m_forward_reference = fex.forward_reference;
499: try
500: {
501: s_interceptor.send_other(s_info);
502: break Forwarding;
503: }
504: catch (ForwardRequest fex2)
505: {
506: s_info.m_forward_reference = fex2.forward;
507: fex.forward_reference = (ObjectImpl) fex2.forward;
508: }
509: }
510: }
511:
512: if (c_intercept)
513: {
514: this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
515: this.m_forwarding_target = fex.forward_reference;
516: try
517: {
518: c_interceptor.receive_other(c_info);
519: }
520: catch (ForwardRequest fex2)
521: {
522: fex.forward_reference = (ObjectImpl) fex2.forward;
523: }
524: }
525: throw fex;
526: }
527: }
528:
529:
533: public void invoke()
534: {
535: InvokeHandler handler = object.getHandler(operation(), cookie, false);
536:
537: if (handler instanceof DynamicImpHandler)
538: {
539: DynamicImplementation dyn = ((DynamicImpHandler) handler).servant;
540: if (serverRequest == null)
541: {
542: serverRequest = new LocalServerRequest(this);
543: }
544: try
545: {
546: poa.m_orb.currents.put(Thread.currentThread(), this);
547: dyn.invoke(serverRequest);
548: }
549: finally
550: {
551: poa.m_orb.currents.remove(Thread.currentThread());
552: }
553: }
554: else
555: {
556: org.omg.CORBA.portable.InputStream input = v_invoke(handler);
557:
558: if (!exceptionReply)
559: {
560: NamedValue arg;
561:
562:
563: if (m_result != null)
564: {
565: m_result.value().read_value(input, m_result.value().type());
566: }
567:
568:
569: if (m_args != null)
570: {
571: for (int i = 0; i < m_args.count(); i++)
572: {
573: try
574: {
575: arg = m_args.item(i);
576:
577:
578: if ((arg.flags() & ARG_OUT.value) != 0)
579: {
580: arg.value().read_value(input, arg.value().type());
581: }
582: }
583: catch (Bounds ex)
584: {
585: Unexpected.error(ex);
586: }
587: }
588: }
589: }
590: else
591: {
592:
593: gnuAny exc = new gnuAny();
594:
595: exc.insert_Streamable(new StreamHolder(input));
596:
597: UnknownUserException unuex = new UnknownUserException(exc);
598: m_environment.exception(unuex);
599: }
600: }
601: }
602:
603:
610: public OutputStream createExceptionReply()
611: {
612: exceptionReply = true;
613: prepareStream();
614: return buffer;
615: }
616:
617:
625: public OutputStream createReply()
626: {
627: exceptionReply = false;
628: prepareStream();
629: return buffer;
630: }
631:
632:
643: BufferedCdrOutput getBuffer()
644: {
645: return buffer;
646: }
647:
648:
652: boolean isExceptionReply()
653: {
654: return exceptionReply;
655: }
656:
657:
660: private void prepareStream()
661: {
662: buffer = new BufferedCdrOutput();
663: buffer.setOrb(orb());
664: }
665:
666:
670: public StreamBasedRequest getParameterStream()
671: {
672: m_parameter_buffer = new StreamBasedRequest();
673: m_parameter_buffer.request = this;
674: m_parameter_buffer.setOrb(poa.orb());
675: return m_parameter_buffer;
676: }
677:
678: public byte[] get_object_id() throws NoContext
679: {
680: return Id;
681: }
682:
683: public POA get_POA() throws NoContext
684: {
685: return poa;
686: }
687: }