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:
54: import ;
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: import ;
86:
87:
93: public class gnuServantObject extends ObjectImpl
94: implements org.omg.CORBA.Object,
95: InvokeHandler,
96: CurrentOperations,
97: IorProvider
98: {
99:
105: private Servant servant;
106:
107:
110: public final byte[] Id;
111:
112:
115: public final gnuPOA poa;
116:
117:
120: public final POAManager manager;
121:
122:
125: public final ORB_1_4 orb;
126:
127:
131: public final String[] repository_ids;
132:
133:
141: public gnuServantObject(String[] a_repository_ids, byte[] an_id,
142: gnuPOA a_poa, ORB_1_4 an_orb
143: )
144: {
145: repository_ids = a_repository_ids;
146: Id = an_id;
147: manager = a_poa.the_POAManager();
148: poa = a_poa;
149: orb = an_orb;
150: }
151:
152:
155: public IOR getIor()
156: {
157: return orb.getLocalIor(this);
158: }
159:
160:
168: public gnuServantObject(Servant a_servant, byte[] an_id, ORB_1_4 an_orb,
169: gnuPOA a_poa
170: )
171: {
172: Id = an_id;
173: setServant(a_servant);
174: poa = a_poa;
175: if (poa != null)
176: {
177: manager = poa.the_POAManager();
178: }
179: else
180: {
181: manager = null;
182: }
183: repository_ids = null;
184: orb = an_orb;
185: }
186:
187:
196: public void setServant(Servant a_servant)
197: {
198: if (a_servant != null &&
199: !(a_servant instanceof InvokeHandler) &&
200: !(a_servant instanceof DynamicImplementation)
201: )
202: {
203: throw new BAD_PARAM("Must be either InvokeHandler or " +
204: "DynamicImplementation, but is " + a_servant
205: );
206: }
207: servant = a_servant;
208: }
209:
210:
213: public Servant getServant()
214: {
215: return servant;
216: }
217:
218:
221: public InvokeHandler getHandler(String operation, CookieHolder cookie,
222: boolean forwarding_allowed
223: ) throws gnuForwardRequest
224: {
225: if (servant != null)
226: {
227: return servantToHandler(servant);
228: }
229: else
230: {
231:
232: if (poa.servant_locator != null)
233: {
234: try
235: {
236: servant =
237: poa.servant_locator.preinvoke(Id, poa, operation, cookie);
238: return servantToHandler(servant);
239: }
240: catch (org.omg.PortableServer.ForwardRequest forw_ex)
241: {
242: if (forwarding_allowed)
243: {
244: throw new gnuForwardRequest(forw_ex.forward_reference);
245: }
246: else
247: {
248: servant =
249: ForwardedServant.create(forw_ex.forward_reference);
250: return servantToHandler(servant);
251: }
252: }
253: }
254: else
255:
256: if (poa.applies(ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION) &&
257: poa.applies(ServantRetentionPolicyValue.RETAIN)
258: )
259: {
260: try
261: {
262: poa.activate_object_with_id(Id, servant, forwarding_allowed);
263: servant = poa.id_to_servant(Id);
264: return servantToHandler(servant);
265: }
266: catch (gnuForwardRequest forwarded)
267: {
268: throw forwarded;
269: }
270: catch (Exception ex)
271: {
272: BAD_OPERATION bad =
273: new BAD_OPERATION("Unable to activate", Minor.Activation,
274: CompletionStatus.COMPLETED_NO
275: );
276: bad.initCause(ex);
277: throw bad;
278: }
279: }
280: else if (poa.default_servant != null)
281: {
282: servant = poa.default_servant;
283: return servantToHandler(servant);
284: }
285:
286:
287: else
288: {
289: throw new BAD_OPERATION("Unable to activate", Minor.Activation,
290: CompletionStatus.COMPLETED_NO
291: );
292: }
293: }
294: }
295:
296:
299: public InvokeHandler servantToHandler(Servant a_servant)
300: {
301: if (a_servant instanceof InvokeHandler)
302: {
303: return (InvokeHandler) a_servant;
304: }
305: else if (a_servant instanceof DynamicImplementation)
306: {
307: return new DynamicImpHandler((DynamicImplementation) a_servant);
308: }
309: else
310: {
311: throw new BAD_OPERATION(a_servant +
312: " must be either InvokeHandler or " + "POA DynamicImplementation"
313: );
314: }
315: }
316:
317:
325: public gnuServantObject(Servant a_servant, gnuPOA a_poa)
326: {
327: this(a_servant, a_servant._object_id(), (ORB_1_4) a_servant._orb(), a_poa);
328: }
329:
330:
333: public String[] _ids()
334: {
335: if (repository_ids == null)
336: {
337: return getServant()._all_interfaces(poa, Id);
338: }
339: else
340: {
341: return repository_ids;
342: }
343: }
344:
345:
348: public String toString()
349: {
350: StringBuffer b = new StringBuffer("Servant object (");
351: for (int i = 0; i < Id.length; i++)
352: {
353: b.append(Integer.toHexString(Id [ i ] & 0xFF));
354: b.append(' ');
355: }
356: b.append(')');
357: return b.toString();
358: }
359:
360:
363: public boolean _is_local()
364: {
365: return true;
366: }
367:
368:
375: public boolean _is_a(String idl_id)
376: {
377: String[] maybe = _ids();
378: for (int i = 0; i < maybe.length; i++)
379: {
380: if (maybe [ i ].equals(idl_id))
381: {
382: return true;
383: }
384: }
385: return false;
386: }
387:
388:
393: public ORB _orb()
394: {
395: return getServant()._orb();
396: }
397:
398:
408: public OutputStream _invoke(String method, InputStream input,
409: ResponseHandler r_handler
410: ) throws SystemException
411: {
412: boolean intercept = false;
413: ServerRequestInterceptorOperations interceptor = null;
414: gnuServerRequestInfo info = null;
415: ResponseHandlerImpl i_handler = null;
416:
417: try
418: {
419: if (orb.iServer != null &&
420: r_handler instanceof ResponseHandlerImpl
421: )
422: {
423: interceptor = orb.iServer;
424:
425: i_handler = (ResponseHandlerImpl) r_handler;
426:
427: info =
428: new gnuServerRequestInfo(this, i_handler.request_header,
429: i_handler.reply_header
430: );
431: intercept = true;
432:
433: interceptor.receive_request_service_contexts(info);
434: }
435:
436: try
437: {
438: CookieHolder cookie = null;
439: AOM.Obj self = poa.aom.get(Id);
440:
441: if (poa.servant_locator != null)
442: {
443:
444:
445: self.servant = servant = null;
446: cookie = new CookieHolder();
447: }
448: else if (self != null && self.isDeactiveted())
449: {
450: if (poa.applies(
451: ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION
452: ) &&
453: poa.servant_activator != null
454: )
455: {
456:
457: servant = null;
458: }
459: else
460: {
461: throw new OBJECT_NOT_EXIST("Object deactivated",
462: 0x535503ec, CompletionStatus.COMPLETED_NO
463: );
464: }
465: }
466:
467: InvokeHandler handler = getHandler(method, cookie, true);
468:
469: Delegate d = null;
470:
471: try
472: {
473: d = servant._get_delegate();
474: orb.currents.put(Thread.currentThread(), this);
475: }
476: catch (Exception ex)
477: {
478:
479: }
480: if (d instanceof ServantDelegateImpl)
481: {
482:
483:
484: if (((ServantDelegateImpl) d).object != this)
485: {
486: servant._set_delegate(new ServantDelegateImpl(servant, poa, Id));
487: }
488: }
489: else
490: {
491: servant._set_delegate(new ServantDelegateImpl(servant, poa, Id));
492: }
493:
494: try
495: {
496: switch (manager.get_state().value())
497: {
498: case State._ACTIVE :
499:
500: OutputStream rt;
501: try
502: {
503: if (intercept)
504: {
505: interceptor.receive_request(info);
506: }
507:
508: rt = handler._invoke(method, input, r_handler);
509:
510: if (intercept)
511: {
512:
513: if (i_handler.isExceptionReply())
514: {
515: info.m_reply_header.reply_status =
516: ReplyHeader.USER_EXCEPTION;
517:
518:
519: Any a = orb.create_any();
520: OutputStream buf = i_handler.getBuffer();
521: InputStream in = buf.create_input_stream();
522: String uex_idl = "unknown";
523: try
524: {
525: in.mark(Integer.MAX_VALUE);
526: uex_idl = in.read_string();
527: in.reset();
528: }
529: catch (IOException e)
530: {
531: throw new Unexpected(e);
532: }
533:
534: try
535: {
536: UserException exception =
537: ObjectCreator.readUserException(uex_idl,
538: in
539: );
540:
541: ObjectCreator.insertWithHelper(a,
542: exception
543: );
544: }
545: catch (Exception e)
546: {
547:
548:
549: a.insert_Streamable(new StreamHolder(
550: buf.create_input_stream()
551: )
552: );
553:
554: RecordTypeCode r =
555: new RecordTypeCode(TCKind.tk_except);
556: r.setId(uex_idl);
557: r.setName(ObjectCreator.getDefaultName(
558: uex_idl
559: )
560: );
561: }
562:
563: info.m_usr_exception = a;
564: interceptor.send_exception(info);
565: }
566: else
567: {
568: info.m_reply_header.reply_status =
569: ReplyHeader.NO_EXCEPTION;
570: interceptor.send_reply(info);
571: }
572: }
573: }
574: catch (SystemException sys_ex)
575: {
576: if (intercept)
577: {
578: info.m_reply_header.reply_status =
579: ReplyHeader.SYSTEM_EXCEPTION;
580: info.m_sys_exception = sys_ex;
581: interceptor.send_exception(info);
582: }
583: throw sys_ex;
584: }
585:
586: return rt;
587:
588: case State._HOLDING :
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600: int sleep = 5;
601: int max = 500;
602:
603:
604:
605: while (manager.get_state().value() == State._HOLDING)
606: {
607: try
608: {
609: Thread.sleep(sleep);
610: if (sleep < max)
611: {
612: sleep = max;
613: }
614: }
615: catch (InterruptedException ex)
616: {
617: }
618: }
619:
620:
621: return _invoke(method, input, r_handler);
622:
623: case State._DISCARDING :
624: throw new TRANSIENT("Discarding mode", 0x535503e9,
625: CompletionStatus.COMPLETED_NO
626: );
627:
628: case State._INACTIVE :
629: throw new OBJ_ADAPTER("POA deactivated", 0x535503ea,
630: CompletionStatus.COMPLETED_NO
631: );
632:
633: default :
634: throw new InternalError();
635: }
636: }
637: finally
638: {
639: if (poa.servant_locator != null)
640: {
641: poa.servant_locator.postinvoke(Id, poa, method,
642: cookie.value, servant
643: );
644: servant = null;
645: }
646: }
647: }
648: finally
649: {
650: orb.currents.remove(Thread.currentThread());
651: }
652: }
653: catch (ForwardRequest fex)
654: {
655:
656: if (intercept)
657: {
658: Forwarding:
659: while (true)
660: {
661: info.m_reply_header.reply_status =
662: ReplyHeader.LOCATION_FORWARD;
663: info.m_forward_reference = fex.forward;
664: try
665: {
666: interceptor.send_other(info);
667: break Forwarding;
668: }
669: catch (ForwardRequest fex2)
670: {
671: info.m_forward_reference = fex2.forward;
672: fex.forward = info.m_forward_reference;
673: }
674: }
675: }
676: throw new gnuForwardRequest(fex.forward);
677: }
678: catch (gnuForwardRequest fex)
679: {
680:
681: if (intercept)
682: {
683: Forwarding:
684: while (true)
685: {
686: info.m_reply_header.reply_status =
687: ReplyHeader.LOCATION_FORWARD;
688: info.m_forward_reference = fex.forward_reference;
689: try
690: {
691: interceptor.send_other(info);
692: break Forwarding;
693: }
694: catch (ForwardRequest fex2)
695: {
696: info.m_forward_reference = fex2.forward;
697: fex.forward_reference = (ObjectImpl) fex2.forward;
698: }
699: }
700: }
701: throw fex;
702: }
703: }
704:
705:
708: public boolean equals(java.lang.Object other)
709: {
710: if (other instanceof gnuServantObject)
711: {
712: gnuServantObject o = (gnuServantObject) other;
713:
714: return Arrays.equals(o.Id, Id);
715: }
716: else
717: {
718: return false;
719: }
720: }
721:
722:
725: public int hashCode()
726: {
727: long s = 0;
728: int v = 1;
729: for (int i = 0; i < Id.length; i++)
730: {
731: s += Id [ i ] * v;
732: if (s > Integer.MAX_VALUE)
733: {
734: s = s % Integer.MAX_VALUE;
735: v = 1;
736: }
737: v = v * 8;
738: }
739: return (int) (s % Integer.MAX_VALUE);
740: }
741:
742:
745: public byte[] get_object_id()
746: {
747: return Id;
748: }
749:
750:
753: public POA get_POA()
754: {
755: return poa;
756: }
757:
758:
761: public void _release()
762: {
763: }
764:
765:
768: public void _releaseReply(InputStream stream)
769: {
770: }
771:
772:
780: public boolean _is_equivalent(org.omg.CORBA.Object other)
781: {
782: if (other instanceof gnuServantObject)
783: {
784: gnuServantObject g = (gnuServantObject) other;
785: return orb == g.orb && poa == g.poa && Arrays.equals(Id, g.Id);
786: }
787: else if (other instanceof IorObject)
788: {
789: IorObject ir = ((IorObject) other);
790: try
791: {
792: IorDelegate ird = (IorDelegate) ir._get_delegate();
793: byte[] ior_id = poa.idFormIor(ird.getIor().key);
794: if (ior_id != null && Arrays.equals(ior_id, Id))
795: {
796: return true;
797: }
798: else
799: {
800: return false;
801: }
802: }
803: catch (Exception ex)
804: {
805:
806:
807: return super._is_equivalent(other);
808: }
809: }
810: return super._is_equivalent(other);
811: }