1. /*
  2. * @(#)ServerResponseImpl.java 1.41 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.iiop;
  8. import org.omg.CORBA.SystemException;
  9. import com.sun.corba.se.internal.core.ServerResponse;
  10. import com.sun.corba.se.internal.core.ServerRequest;
  11. import com.sun.corba.se.internal.core.IOR;
  12. import com.sun.corba.se.internal.core.ServiceContext;
  13. import com.sun.corba.se.internal.core.ServiceContexts;
  14. import com.sun.corba.se.internal.iiop.messages.MessageBase;
  15. import com.sun.corba.se.internal.iiop.messages.ReplyMessage;
  16. import com.sun.corba.se.internal.POA.POAImpl; // REVISIT - remove later
  17. import com.sun.corba.se.internal.POA.POAORB; // REVISIT - remove later
  18. public class ServerResponseImpl
  19. extends IIOPOutputStream
  20. implements ServerResponse
  21. {
  22. protected ServerResponseImpl(ServerRequestImpl request,
  23. ServiceContexts svc)
  24. {
  25. this(request,
  26. MessageBase.createReply(
  27. (com.sun.corba.se.internal.iiop.ORB) request.orb(),
  28. request.getGIOPVersion(),
  29. request.getRequestId(), ReplyMessage.NO_EXCEPTION,
  30. svc, null),
  31. null);
  32. }
  33. protected ServerResponseImpl(ServerRequestImpl request,
  34. ServiceContexts svc, boolean user)
  35. {
  36. this(request,
  37. MessageBase.createReply(
  38. (com.sun.corba.se.internal.iiop.ORB) request.orb(),
  39. request.getGIOPVersion(), request.getRequestId(),
  40. user ? ReplyMessage.USER_EXCEPTION :
  41. ReplyMessage.SYSTEM_EXCEPTION,
  42. svc, null),
  43. null);
  44. }
  45. protected ServerResponseImpl(ServerRequestImpl request, ReplyMessage reply, IOR ior)
  46. {
  47. super( request.getGIOPVersion(),
  48. request.getConnection().getORB(),
  49. request.getConnection());
  50. setMessage(reply);
  51. ORB orb = (ORB)request.getConnection().getORB();
  52. runServantPostInvoke(orb, request);
  53. if( request.executePIInResponseConstructor() ) {
  54. // Invoke server request ending interception points (send_*):
  55. // Note: this may end up with a SystemException or an internal
  56. // Runtime ForwardRequest
  57. orb.invokeServerPIEndingPoint( reply );
  58. // Note this will be executed even if a ForwardRequest or
  59. // SystemException is thrown by a Portable Interceptors ending
  60. // point since we end up in this constructor again anyway.
  61. orb.cleanupServerPIRequest();
  62. // See (Local)ServerRequestImpl.createSystemExceptionResponse
  63. // for why this is necesary.
  64. request.setExecutePIInResponseConstructor(false);
  65. }
  66. // Once you get here then the final reply is available (i.e.,
  67. // postinvoke and interceptors have completed.
  68. if (request.executeRemoveThreadInfoInResponseConstructor()) {
  69. removeThreadInfo(orb, request);
  70. }
  71. reply.write(this);
  72. if (reply.getIOR() != null) {
  73. reply.getIOR().write(this);
  74. }
  75. this.reply = reply;
  76. this.ior = reply.getIOR();
  77. }
  78. public boolean isSystemException() {
  79. if (reply != null)
  80. return reply.getReplyStatus() == ReplyMessage.SYSTEM_EXCEPTION;
  81. return false;
  82. }
  83. public boolean isUserException() {
  84. if (reply != null)
  85. return reply.getReplyStatus() == ReplyMessage.USER_EXCEPTION;
  86. return false;
  87. }
  88. public boolean isLocationForward() {
  89. if (ior != null)
  90. return true;
  91. return false;
  92. }
  93. public IOR getForwardedIOR() {
  94. return ior;
  95. }
  96. public int getRequestId() {
  97. if (reply != null)
  98. return reply.getRequestId();
  99. return -1;
  100. }
  101. public ServiceContexts getServiceContexts() {
  102. if (reply != null)
  103. return reply.getServiceContexts();
  104. return null;
  105. }
  106. public SystemException getSystemException() {
  107. if (reply != null)
  108. return reply.getSystemException();
  109. return null;
  110. }
  111. /**
  112. * Check to see if response is local.
  113. */
  114. public boolean isLocal(){
  115. return false;
  116. }
  117. static void runServantPostInvoke(ORB orb, ServerRequest request)
  118. {
  119. // Run ServantLocator::postinvoke. This may cause a SystemException
  120. // which will throw out of the constructor and return later
  121. // to construct a reply for that exception. The internal logic
  122. // of returnServant makes sure that postinvoke is only called once.
  123. // REVISIT: instead of instanceof, put method on all orbs.
  124. POAORB poaorb = null;
  125. // This flag is to deal with BootstrapServer use of reply streams,
  126. // with ServerDelegate's use of reply streams when a POA ORB is
  127. // present, etc.
  128. if (request.executeReturnServantInResponseConstructor()) {
  129. // It is possible to get marshaling errors in the skeleton after
  130. // postinvoke has completed. We must set this to false so that
  131. // when the error exception reply is constructed we don't try
  132. // to incorrectly access poa current (which will be the wrong
  133. // one or an empty stack.
  134. request.setExecuteReturnServantInResponseConstructor(false);
  135. request.setExecuteRemoveThreadInfoInResponseConstructor(true);
  136. if (orb instanceof POAORB) {
  137. poaorb = (POAORB) orb;
  138. POAImpl poaimpl = poaorb.getCurrent().getPOA();
  139. // REVISIT: is synchronization necessary still?
  140. synchronized (poaimpl) {
  141. poaimpl.returnServant();
  142. }
  143. }
  144. }
  145. }
  146. static void removeThreadInfo(ORB orb, ServerRequest request)
  147. {
  148. request.setExecuteRemoveThreadInfoInResponseConstructor(false);
  149. POAORB poaorb = (POAORB)orb;
  150. POAImpl poaimpl = poaorb.getCurrent().getPOA();
  151. // REVISIT: is synchronization necessary still?
  152. synchronized (poaimpl) {
  153. poaimpl.removeThreadInfo();
  154. }
  155. }
  156. private ReplyMessage reply;
  157. private IOR ior; // forwarded IOR
  158. }