sd = (servantDelegate) d;
sd = (ServantDelegateImpl) d;
  public org.omg.CORBA.portable.InputStream v_invoke(InvokeHandler handler)  {    // Local request must be intercepted both by server and request    // interceptors.    boolean s_intercept = false;    ServerRequestInterceptorOperations s_interceptor = null;    gnuServerRequestInfo s_info = null;    boolean c_intercept = false;    ClientRequestInterceptorOperations c_interceptor = null;    gnuClientRequestInfo c_info = null;    try      {        if (poa.m_orb.iServer != null || poa.m_orb.iClient != null)          {            setORB(poa.m_orb);            // These two are only needed with interceptors.            m_rqh = new RequestHeader();            m_rqh.operation = m_operation;            m_rph = new ReplyHeader();            m_rqh.object_key = object.Id;            m_rph.request_id = m_rqh.request_id;          }        if (poa.m_orb.iClient != null)          {            c_interceptor = poa.m_orb.iClient;            c_info = new gnuClientRequestInfo(this);            c_intercept = true;            c_interceptor.send_request(c_info);            m_target = object;          }        if (poa.m_orb.iServer != null)          {            s_interceptor = poa.m_orb.iServer;            s_info = new gnuServerRequestInfo(object, m_rqh, m_rph);            s_info.m_request = this;            s_intercept = true;            s_interceptor.receive_request_service_contexts(s_info);          }    if (handler == null)          {      handler = object.getHandler(operation(), cookie, false);          }    cdrBufOutput request_part = new cdrBufOutput();    request_part.setOrb(orb());    if (m_args != null && m_args.count() > 0)      {        write_parameters(header, request_part);        if (m_parameter_buffer != null)              {          throw new BAD_INV_ORDER("Please either add parameters or " +                  "write them into stream, but not both " + "at once."                                 );      }          }    if (m_parameter_buffer != null)      {        write_parameter_buffer(header, request_part);      }    Servant servant;    if (handler instanceof Servant)          {      servant = (Servant) handler;          }    else          {      throw new BAD_OPERATION("Unexpected handler type " + handler);          }    org.omg.CORBA.portable.InputStream input =      request_part.create_input_stream();    // Ensure the servant (handler) has a delegate set.    servantDelegate sd = null;    Delegate d = null;    try      {        d = servant._get_delegate();      }    catch (Exception ex)      {        // In some cases exception is thrown if the delegate is not set.      }    if (d instanceof servantDelegate)      {        // If the delegate is already set, try to reuse the existing        // instance.        sd = (servantDelegate) d;        if (sd.object != object)              {          sd = new servantDelegate(servant, poa, Id);      }          }    else          {      sd = new servantDelegate(servant, poa, Id);          }    servant._set_delegate(sd);    try      {        ORB o = orb();        if (o instanceof ORB_1_4)          {            ((ORB_1_4) o).currents.put(Thread.currentThread(), this);          }            try              {                if (s_intercept)                  {                    s_interceptor.receive_request(s_info);                  }        handler._invoke(m_operation, input, this);                // Handler is casted into i_handler.                if ((s_intercept || c_intercept) && isExceptionReply())                  {                    s_info.m_reply_header.reply_status =                      ReplyHeader.USER_EXCEPTION;                    m_rph.reply_status = ReplyHeader.USER_EXCEPTION;                    // Make Any, holding the user exception.                    Any a = new gnuAny();                    OutputStream buf = getBuffer();                    InputStream in = buf.create_input_stream();                    String uex_idl = "unknown";                    try                      {                        in.mark(Integer.MAX_VALUE);                        uex_idl = in.read_string();                        m_exception_id = uex_idl;                        in.reset();                      }                    catch (IOException e)                      {                        throw new Unexpected(e);                      }                    try                      {                        UserException exception =                          ObjectCreator.readUserException(uex_idl, in);                        m_environment.exception(exception);                        ObjectCreator.insertWithHelper(a, exception);                      }                    catch (Exception e)                      {                        // Failed due any reason, insert without                        // helper.                        a.insert_Streamable(new streamReadyHolder(                            buf.create_input_stream()                          )                        );                        recordTypeCode r =                          new recordTypeCode(TCKind.tk_except);                        r.setId(uex_idl);                        r.setName(ObjectCreator.getDefaultName(uex_idl));                      }                    s_info.m_usr_exception = a;                    c_info.m_wrapped_exception = a;                    s_interceptor.send_exception(s_info);                    c_interceptor.receive_exception(c_info);                  }                else                  {                    if (s_intercept)                      {                        s_info.m_reply_header.reply_status =                          ReplyHeader.NO_EXCEPTION;                        s_interceptor.send_reply(s_info);                      }                    if (c_intercept)                      {                        m_rph.reply_status = ReplyHeader.NO_EXCEPTION;                        c_interceptor.receive_reply(c_info);                      }                  }              }            catch (SystemException sys_ex)              {                if (s_intercept)                  {                    s_info.m_reply_header.reply_status =                      ReplyHeader.SYSTEM_EXCEPTION;                    s_info.m_sys_exception = sys_ex;                    s_interceptor.send_exception(s_info);                  }                if (c_intercept)                  {                    m_rph.reply_status = ReplyHeader.SYSTEM_EXCEPTION;                    Any a = new gnuAny();                    if (ObjectCreator.insertSysException(a, sys_ex))                      {                        c_info.m_wrapped_exception = a;                      }                    c_interceptor.receive_exception(c_info);                  }                throw sys_ex;              }      }    finally      {        ORB o = orb();        if (o instanceof ORB_1_4)              {          ((ORB_1_4) o).currents.remove(Thread.currentThread());      }          }    if (poa.servant_locator != null)          {            poa.servant_locator.postinvoke(object.Id, poa, operation(),              cookie.value, object.getServant()                                    );          }    return buffer.create_input_stream();  }    catch (ForwardRequest fex)      {        // May be thrown by interceptor.        if (s_intercept)          {            Forwarding:            while (true)              {                s_info.m_reply_header.reply_status =                  ReplyHeader.LOCATION_FORWARD;                s_info.m_forward_reference = fex.forward;                try                  {                    s_interceptor.send_other(s_info);                    break Forwarding;                  }                catch (ForwardRequest fex2)                  {                    s_info.m_forward_reference = fex2.forward;                    fex.forward = s_info.m_forward_reference;                  }              }          }        if (c_intercept)          {            this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;            this.m_forwarding_target = fex.forward;            try              {                c_interceptor.receive_other(c_info);              }            catch (ForwardRequest fex2)              {                fex.forward = fex2.forward;              }          }        throw new gnuForwardRequest(fex.forward);      }    catch (gnuForwardRequest fex)      {        // May be thrown during activation.        // May be thrown during activation.        if (s_intercept)          {            Forwarding:            while (true)              {                s_info.m_reply_header.reply_status =                  ReplyHeader.LOCATION_FORWARD;                s_info.m_forward_reference = fex.forward_reference;                try                  {                    s_interceptor.send_other(s_info);                    break Forwarding;                  }                catch (ForwardRequest fex2)                  {                    s_info.m_forward_reference = fex2.forward;                    fex.forward_reference = (ObjectImpl) fex2.forward;                  }              }          }        if (c_intercept)          {            this.m_rph.reply_status = ReplyHeader.LOCATION_FORWARD;            this.m_forwarding_target = fex.forward_reference;            try              {                c_interceptor.receive_other(c_info);              }            catch (ForwardRequest fex2)              {                fex.forward_reference = (ObjectImpl) fex2.forward;              }          }        throw fex;      }  }