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:
203: boolean s_intercept = false;
204: ServerRequestInterceptorOperations s_interceptor = null;
205: gnuServerRequestInfo s_info = null;
206:
207: boolean c_intercept = false;
208: ClientRequestInterceptorOperations c_interceptor = null;
209: gnuClientRequestInfo c_info = null;
210:
211: try
212: {
213: if (poa.m_orb.iServer != null || poa.m_orb.iClient != null)
214: {
215: setORB(poa.m_orb);
216:
217:
218: m_rqh = new RequestHeader();
219: m_rqh.operation = m_operation;
220: m_rph = new ReplyHeader();
221:
222: m_rqh.object_key = object.Id;
223: m_rph.request_id = m_rqh.request_id;
224: }
225:
226: if (poa.m_orb.iClient != null)
227: {
228: c_interceptor = poa.m_orb.iClient;
229:
230: c_info = new gnuClientRequestInfo(this);
231: c_intercept = true;
232:
233: c_interceptor.send_request(c_info);
234:
235: m_target = object;
236: }
237:
238: if (poa.m_orb.iServer != null)
239: {
240: s_interceptor = poa.m_orb.iServer;
241:
242: s_info = new gnuServerRequestInfo(object, m_rqh, m_rph);
243: s_info.m_request = this;
244:
245: s_intercept = true;
246:
247: s_interceptor.receive_request_service_contexts(s_info);
248: }
249:
250: if (handler == null)
251: {
252: handler = object.getHandler(operation(), cookie, false);
253: }
254:
255: BufferedCdrOutput request_part = new BufferedCdrOutput();
256:
257: request_part.setOrb(orb());
258:
259: if (m_args != null && m_args.count() > 0)
260: {
261: write_parameters(header, request_part);
262:
263: if (m_parameter_buffer != null)
264: {
265: throw new BAD_INV_ORDER("Please either add parameters or " +
266: "write them into stream, but not both " + "at once."
267: );
268: }
269: }
270:
271: if (m_parameter_buffer != null)
272: {
273: write_parameter_buffer(header, request_part);
274: }
275:
276: Servant servant;
277:
278: if (handler instanceof Servant)
279: {
280: servant = (Servant) handler;
281: }
282: else
283: {
284: throw new BAD_OPERATION("Unexpected handler type " + handler);
285: }
286:
287: org.omg.CORBA.portable.InputStream input =
288: request_part.create_input_stream();
289:
290:
291: ServantDelegateImpl sd = null;
292:
293: Delegate d = null;
294:
295: try
296: {
297: d = servant._get_delegate();
298: }
299: catch (Exception ex)
300: {
301:
302: }
303: if (d instanceof ServantDelegateImpl)
304: {
305:
306:
307: sd = (ServantDelegateImpl) d;
308: if (sd.object != object)
309: {
310: sd = new ServantDelegateImpl(servant, poa, Id);
311: }
312: }
313: else
314: {
315: sd = new ServantDelegateImpl(servant, poa, Id);
316: }
317: servant._set_delegate(sd);
318:
319: try
320: {
321: ORB o = orb();
322: if (o instanceof ORB_1_4)
323: {
324: ((ORB_1_4) o).currents.put(Thread.currentThread(), this);
325: }
326:
327: try
328: {
329: if (s_intercept)
330: {
331: s_interceptor.receive_request(s_info);
332: }
333: handler._invoke(m_operation, input, this);
334:
335:
336: if ((s_intercept || c_intercept) && isExceptionReply())
337: {
338: s_info.m_reply_header.reply_status =
339: ReplyHeader.USER_EXCEPTION;
340: m_rph.reply_status = ReplyHeader.USER_EXCEPTION;
341:
342:
343: Any a = new gnuAny();
344: OutputStream buf = getBuffer();
345: InputStream in = buf.create_input_stream();
346: String uex_idl = "unknown";
347: try
348: {
349: in.mark(Integer.MAX_VALUE);
350: uex_idl = in.read_string();
351: m_exception_id = uex_idl;
352: in.reset();
353: }
354: catch (IOException e)
355: {
356: throw new Unexpected(e);
357: }
358:
359: try
360: {
361: UserException exception =
362: ObjectCreator.readUserException(uex_idl, in);
363:
364: m_environment.exception(exception);
365: ObjectCreator.insertWithHelper(a, exception);
366: }
367: catch (Exception e)
368: {
369:
370:
371: a.insert_Streamable(new StreamHolder(
372: buf.create_input_stream()
373: )
374: );
375:
376: RecordTypeCode r =
377: new RecordTypeCode(TCKind.tk_except);
378: r.setId(uex_idl);
379: r.setName(ObjectCreator.getDefaultName(uex_idl));
380: }
381:
382: s_info.m_usr_exception = a;
383: c_info.m_wrapped_exception = a;
384: s_interceptor.send_exception(s_info);
385: c_interceptor.receive_exception(c_info);
386: }
387: else
388: {
389: if (s_intercept)
390: {
391: s_info.m_reply_header.reply_status =
392: ReplyHeader.NO_EXCEPTION;
393: s_interceptor.send_reply(s_info);
394: }
395: if (c_intercept)
396: {
397: m_rph.reply_status = ReplyHeader.NO_EXCEPTION;
398: c_interceptor.receive_reply(c_info);
399: }
400: }
401: }
402: catch (SystemException sys_ex)
403: {
404: if (s_intercept)
405: {
406: s_info.m_reply_header.reply_status =
407: ReplyHeader.SYSTEM_EXCEPTION;
408: s_info.m_sys_exception = sys_ex;
409: s_interceptor.send_exception(s_info);
410: }
411:
412: if (c_intercept)
413: {
414: m_rph.reply_status = ReplyHeader.SYSTEM_EXCEPTION;
415:
416: Any a = new gnuAny();
417: if (ObjectCreator.insertSysException(a, sys_ex))
418: {
419: c_info.m_wrapped_exception = a;
420: }
421: c_interceptor.receive_exception(c_info);
422: }
423:
424: throw sys_ex;
425: }
426: }
427: finally
428: {
429: ORB o = orb();
430: if (o instanceof ORB_1_4)
431: {
432: ((ORB_1_4) o).currents.remove(Thread.currentThread());
433: }
434: }
435:
436: if (poa.servant_locator != null)
437: {
438: poa.servant_locator.postinvoke(object.Id, poa, operation(),
439: cookie.value, object.getServant()
440: );
441: }
442: return buffer.create_input_stream();
443: }
444:
445: catch (ForwardRequest fex)
446: {
447:
448: if (s_intercept)
449: {
450: Forwarding:
451: while (true)
452: {
453: s_info.m_reply_header.reply_status =
454: ReplyHeader.LOCATION_FORWARD;
455: s_info.m_forward_reference = fex.forward;
456: try
457: {
458: s_interceptor.send_other(s_info);
459: break Forwarding;
460: }
461: catch (ForwardRequest fex2)
462: {
463: s_info.m_forward_reference = fex2.forward;
464: fex.forward = s_info.m_forward_reference;
465: }
466: }
467: }
468:
469: if (c_intercept)
470: {
471: this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
472: this.m_forwarding_target = fex.forward;
473: try
474: {
475: c_interceptor.receive_other(c_info);
476: }
477: catch (ForwardRequest fex2)
478: {
479: fex.forward = fex2.forward;
480: }
481: }
482: throw new gnuForwardRequest(fex.forward);
483: }
484: catch (gnuForwardRequest fex)
485: {
486:
487:
488: if (s_intercept)
489: {
490: Forwarding:
491: while (true)
492: {
493: s_info.m_reply_header.reply_status =
494: ReplyHeader.LOCATION_FORWARD;
495: s_info.m_forward_reference = fex.forward_reference;
496: try
497: {
498: s_interceptor.send_other(s_info);
499: break Forwarding;
500: }
501: catch (ForwardRequest fex2)
502: {
503: s_info.m_forward_reference = fex2.forward;
504: fex.forward_reference = (ObjectImpl) fex2.forward;
505: }
506: }
507: }
508:
509: if (c_intercept)
510: {
511: this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;
512: this.m_forwarding_target = fex.forward_reference;
513: try
514: {
515: c_interceptor.receive_other(c_info);
516: }
517: catch (ForwardRequest fex2)
518: {
519: fex.forward_reference = (ObjectImpl) fex2.forward;
520: }
521: }
522: throw fex;
523: }
524: }
525:
526:
530: public void invoke()
531: {
532: InvokeHandler handler = object.getHandler(operation(), cookie, false);
533:
534: if (handler instanceof DynamicImpHandler)
535: {
536: DynamicImplementation dyn = ((DynamicImpHandler) handler).servant;
537: if (serverRequest == null)
538: {
539: serverRequest = new LocalServerRequest(this);
540: }
541: try
542: {
543: poa.m_orb.currents.put(Thread.currentThread(), this);
544: dyn.invoke(serverRequest);
545: }
546: finally
547: {
548: poa.m_orb.currents.remove(Thread.currentThread());
549: }
550: }
551: else
552: {
553: org.omg.CORBA.portable.InputStream input = v_invoke(handler);
554:
555: if (!exceptionReply)
556: {
557: NamedValue arg;
558:
559:
560: if (m_result != null)
561: {
562: m_result.value().read_value(input, m_result.value().type());
563: }
564:
565:
566: if (m_args != null)
567: {
568: for (int i = 0; i < m_args.count(); i++)
569: {
570: try
571: {
572: arg = m_args.item(i);
573:
574:
575: if ((arg.flags() & ARG_OUT.value) != 0)
576: {
577: arg.value().read_value(input, arg.value().type());
578: }
579: }
580: catch (Bounds ex)
581: {
582: Unexpected.error(ex);
583: }
584: }
585: }
586: }
587: else
588: {
589:
590: gnuAny exc = new gnuAny();
591:
592: exc.insert_Streamable(new StreamHolder(input));
593:
594: UnknownUserException unuex = new UnknownUserException(exc);
595: m_environment.exception(unuex);
596: }
597: }
598: }
599:
600:
607: public OutputStream createExceptionReply()
608: {
609: exceptionReply = true;
610: prepareStream();
611: return buffer;
612: }
613:
614:
622: public OutputStream createReply()
623: {
624: exceptionReply = false;
625: prepareStream();
626: return buffer;
627: }
628:
629:
640: BufferedCdrOutput getBuffer()
641: {
642: return buffer;
643: }
644:
645:
649: boolean isExceptionReply()
650: {
651: return exceptionReply;
652: }
653:
654:
657: private void prepareStream()
658: {
659: buffer = new BufferedCdrOutput();
660: buffer.setOrb(orb());
661: }
662:
663:
667: public StreamBasedRequest getParameterStream()
668: {
669: m_parameter_buffer = new StreamBasedRequest();
670: m_parameter_buffer.request = this;
671: m_parameter_buffer.setOrb(poa.orb());
672: return m_parameter_buffer;
673: }
674:
675: public byte[] get_object_id() throws NoContext
676: {
677: return Id;
678: }
679:
680: public POA get_POA() throws NoContext
681: {
682: return poa;
683: }