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