servant._set_delegate(new servantDelegate(servant, poa, Id));
servant._set_delegate(new ServantDelegateImpl(servant, poa, Id));
  public OutputStream _invoke(String method, InputStream input,                              ResponseHandler r_handler  ) throws SystemException  {    boolean intercept = false;    ServerRequestInterceptorOperations interceptor = null;    gnuServerRequestInfo info = null;    bufferedResponseHandler i_handler = null;    try      {        if (orb.iServer != null &&          r_handler instanceof bufferedResponseHandler                             )  {            interceptor = orb.iServer;            i_handler = (bufferedResponseHandler) r_handler;            info =              new gnuServerRequestInfo(this, i_handler.request_header,                i_handler.reply_header              );            intercept = true;            interceptor.receive_request_service_contexts(info);          }    try      {        CookieHolder cookie = null;        activeObjectMap.Obj self = poa.aom.get(Id);        if (poa.servant_locator != null)          {            // If the servant locator is in use, it is always responsible            // for providing the servant.            self.servant = servant = null;            cookie = new CookieHolder();          }        else if (self != null && self.isDeactiveted())          {                if (poa.applies(                    ImplicitActivationPolicyValue.IMPLICIT_ACTIVATION                  ) &&                poa.servant_activator != null               )              {                // Reset the servant, forcing the subsequent activation.                servant = null;              }            else                  {                    throw new OBJECT_NOT_EXIST("Object deactivated",                      0x535503ec, CompletionStatus.COMPLETED_NO                                        );          }              }        InvokeHandler handler = getHandler(method, cookie, true);        Delegate d = null;        try          {            d = servant._get_delegate();            orb.currents.put(Thread.currentThread(), this);          }        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, check maybe we can            // reuse the existing instance.            if (((servantDelegate) d).object != this)                  {              servant._set_delegate(new servantDelegate(servant, poa, Id));          }              }        else          {            servant._set_delegate(new servantDelegate(servant, poa, Id));          }        try          {            switch (manager.get_state().value())              {                case State._ACTIVE :                      OutputStream rt;                      try                        {                          if (intercept)                            {                              interceptor.receive_request(info);                            }                          rt = handler._invoke(method, input, r_handler);                          if (intercept)                            {                              // Handler is casted into i_handler.                              if (i_handler.isExceptionReply())                                {                                  info.m_reply_header.reply_status =                                    ReplyHeader.USER_EXCEPTION;                                  // Make Any, holding the user exception.                                  Any a = orb.create_any();                                  OutputStream buf = i_handler.getBuffer();                                  InputStream in = buf.create_input_stream();                                  String uex_idl = "unknown";                                  try                                    {                                      in.mark(Integer.MAX_VALUE);                                      uex_idl = in.read_string();                                      in.reset();                                    }                                  catch (IOException e)                                    {                                      throw new Unexpected(e);                                    }                                  try                                    {                                      UserException exception =                                        ObjectCreator.readUserException(uex_idl,                                          in                                        );                                      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                                        )                                      );                                    }                                  info.m_usr_exception = a;                                  interceptor.send_exception(info);                                }                              else                                {                                  info.m_reply_header.reply_status =                                    ReplyHeader.NO_EXCEPTION;                                  interceptor.send_reply(info);                                }                            }                        }                      catch (SystemException sys_ex)                        {                          if (intercept)                            {                              info.m_reply_header.reply_status =                                ReplyHeader.SYSTEM_EXCEPTION;                              info.m_sys_exception = sys_ex;                              interceptor.send_exception(info);                            }                          throw sys_ex;                        }                      return rt;                case State._HOLDING :                  // The holding mode is implemented                  // relying on the holding capabilites of the network                  // support (if any).                      // TODO FIXME in more recent CORBA applications, the                      // client                  // ORB can free the connection and wait for a server side                      // notification about the completed request. Implement                      // this                      // as soon as JDK specification would allow bidirectional                      // policy.                  int sleep = 5;                  int max = 500;                  // Wait till the state will be switched into some other                  // mode.                  while (manager.get_state().value() == State._HOLDING)                    {                      try                        {                          Thread.sleep(sleep);                          if (sleep < max)                                {                            sleep = max;                        }                            }                      catch (InterruptedException ex)                        {                        }                    }                  // Handle another mode.                  return _invoke(method, input, r_handler);                case State._DISCARDING :                  throw new TRANSIENT("Discarding mode", 0x535503e9,                                      CompletionStatus.COMPLETED_NO                                     );                case State._INACTIVE :                  throw new OBJ_ADAPTER("POA deactivated", 0x535503ea,                                        CompletionStatus.COMPLETED_NO                                       );                default :                  throw new InternalError(); // No more states.              }          }        finally          {            if (poa.servant_locator != null)              {                    poa.servant_locator.postinvoke(Id, poa, method,                      cookie.value, servant                                              );                servant = null;              }          }      }    finally      {        orb.currents.remove(Thread.currentThread());      }  }    catch (ForwardRequest fex)      {        // May be thrown by interceptor.        if (intercept)          {            Forwarding:            while (true)              {                info.m_reply_header.reply_status =                  ReplyHeader.LOCATION_FORWARD;                info.m_forward_reference = fex.forward;                try                  {                    interceptor.send_other(info);                    break Forwarding;                  }                catch (ForwardRequest fex2)                  {                    info.m_forward_reference = fex2.forward;                    fex.forward = info.m_forward_reference;                  }              }          }        throw new gnuForwardRequest(fex.forward);      }    catch (gnuForwardRequest fex)      {        // May be thrown during activation.        if (intercept)          {            Forwarding:            while (true)              {                info.m_reply_header.reply_status =                  ReplyHeader.LOCATION_FORWARD;                info.m_forward_reference = fex.forward_reference;                try                  {                    interceptor.send_other(info);                    break Forwarding;                  }                catch (ForwardRequest fex2)                  {                    info.m_forward_reference = fex2.forward;                    fex.forward_reference = (ObjectImpl) fex2.forward;                  }              }          }        throw fex;      }  }