1. /*
  2. * @(#)Connection.java 1.85 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. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.internal.iiop;
  16. import java.io.IOException;
  17. import java.net.Socket;
  18. import java.net.UnknownHostException;
  19. import java.net.InetAddress;
  20. import org.omg.CORBA.SystemException;
  21. import org.omg.CORBA.CompletionStatus;
  22. import org.omg.CORBA.OBJECT_NOT_EXIST;
  23. import org.omg.CORBA.MARSHAL;
  24. import org.omg.CORBA.INTERNAL;
  25. import org.omg.CORBA.DATA_CONVERSION;
  26. import org.omg.CORBA.portable.*;
  27. import com.sun.org.omg.SendingContext.CodeBase;
  28. import com.sun.corba.se.internal.core.ServerGIOP;
  29. import com.sun.corba.se.internal.core.EndPoint;
  30. import com.sun.corba.se.internal.core.IOR;
  31. import com.sun.corba.se.internal.core.MarshalOutputStream;
  32. import com.sun.corba.se.internal.orbutil.MinorCodes; //d11638
  33. import com.sun.corba.se.internal.orbutil.ORBUtility; //d11638
  34. import com.sun.corba.se.internal.core.GIOPVersion;
  35. import com.sun.corba.se.internal.iiop.messages.MessageBase;
  36. import com.sun.corba.se.internal.iiop.messages.LocateRequestMessage;
  37. import com.sun.corba.se.internal.iiop.messages.LocateReplyMessage;
  38. import com.sun.corba.se.internal.core.CodeSetComponentInfo;
  39. import com.sun.corba.se.internal.core.OSFCodeSetRegistry;
  40. /**
  41. * Common connection base class.
  42. */
  43. abstract public class Connection
  44. implements
  45. com.sun.corba.se.connection.Connection
  46. {
  47. // Connection close states
  48. // REVISIT - rather than define these "intermediate" constants,
  49. // just use the one from MinorCodes directly where these are
  50. // referenced.
  51. public static final int CONN_ABORT = MinorCodes.CONN_ABORT;
  52. public static final int CONN_REBIND = MinorCodes.CONN_REBIND;
  53. protected ORB orb;
  54. protected Socket socket; // The socket used for this connection.
  55. protected long timeStamp = 0;
  56. protected boolean isServer = false;
  57. protected ConnectionTable connectionTable = null;
  58. // Negotiated code sets for char and wchar data
  59. protected CodeSetComponentInfo.CodeSetContext codeSetContext = null;
  60. void dprint(String msg) {
  61. ORBUtility.dprint(this, msg);
  62. }
  63. public ORB getORB() {
  64. return orb;
  65. }
  66. public Socket getSocket() {
  67. return socket;
  68. }
  69. abstract public IIOPInputStream invoke(IIOPOutputStream s)
  70. throws SystemException;
  71. abstract public void delete();
  72. abstract public java.io.InputStream getInputStream();
  73. abstract public ServerGIOP getServerGIOP();
  74. abstract public IIOPInputStream send(IIOPOutputStream s, boolean oneWay);
  75. abstract public void sendReply(IIOPOutputStream s) throws Exception;
  76. // All of Following abstract required for Connection cleanup
  77. abstract public void cleanUp() throws java.lang.Exception;
  78. abstract public boolean isBusy();
  79. abstract public void requestBegins();
  80. abstract public void requestEnds(IIOPInputStream request);
  81. abstract public void print();
  82. abstract public void setConnection(Socket _socket, ConnectionTable ctab)
  83. throws java.lang.Exception;
  84. abstract public void abortConnection();
  85. // Indicates whether or not ServiceContexts have even been exchange yet
  86. abstract public boolean isPostInitialContexts();
  87. // Sets to true the state that ServiceContexts have indeed been exchanged
  88. // once already
  89. abstract public void setPostInitialContexts();
  90. public IOR locate(int id, byte [] key, IOR ior)
  91. {
  92. LocateRequestMessage msg;
  93. IIOPOutputStream os;
  94. IIOPInputStream is;
  95. GIOPVersion requestVersion =
  96. GIOPVersion.chooseRequestVersion(orb, ior);
  97. msg = MessageBase.createLocateRequest(orb, requestVersion, id, key);
  98. // This chooses the right buffering strategy for the locate msg.
  99. // locate msgs 1.0 & 1.1 :=> grow, 1.2 :=> stream
  100. //os = orb.newOutputStream(this);
  101. os = com.sun.corba.se.internal.iiop.IIOPOutputStream.
  102. createIIOPOutputStreamForLocateMsg(
  103. requestVersion, orb, this);
  104. os.setMessage(msg);
  105. msg.write(os);
  106. is = send(os, false);
  107. LocateReplyMessage reply;
  108. reply = (LocateReplyMessage) is.getMessage();
  109. switch (reply.getReplyStatus()) {
  110. case LocateReplyMessage.UNKNOWN_OBJECT:
  111. throw new OBJECT_NOT_EXIST( MinorCodes.LOCATE_UNKNOWN_OBJECT,
  112. CompletionStatus.COMPLETED_NO );
  113. case LocateReplyMessage.OBJECT_HERE:
  114. return null;
  115. case LocateReplyMessage.OBJECT_FORWARD:
  116. case LocateReplyMessage.OBJECT_FORWARD_PERM: {
  117. /*
  118. IOR ior;
  119. ior = new IOR(orb);
  120. ior.read(is);
  121. return ior;
  122. */
  123. return reply.getIOR();
  124. }
  125. }
  126. throw new INTERNAL( MinorCodes.BAD_LOCATE_REQUEST_STATUS,
  127. CompletionStatus.COMPLETED_NO );
  128. }
  129. void shutdown() {
  130. try {
  131. socket.close();
  132. } catch (IOException ioex) {}
  133. }
  134. public void stampTime() {
  135. connectionTable.stampTime(this);
  136. }
  137. // Sets this connection's code base IOR. This is done after
  138. // getting the IOR out of the SendingContext service context.
  139. // Our ORBs always send this, but it's optional in CORBA.
  140. public abstract void setCodeBaseIOR(IOR codeBase);
  141. abstract IOR getCodeBaseIOR();
  142. abstract CodeBase getCodeBase();
  143. public synchronized CodeSetComponentInfo.CodeSetContext getCodeSetContext() {
  144. // Needs to be synchronized for the following case when the client
  145. // doesn't send the code set context twice, and we have two threads
  146. // in ServerDelegate processCodeSetContext.
  147. //
  148. // Thread A checks to see if there is a context, there is none, so
  149. // it calls setCodeSetContext, getting the synch lock.
  150. // Thread B checks to see if there is a context. If we didn't synch,
  151. // it might decide to outlaw wchar/wstring.
  152. return codeSetContext;
  153. }
  154. public synchronized void setCodeSetContext(CodeSetComponentInfo.CodeSetContext csc) {
  155. // Check whether or not we should set this. Technically,
  156. // someone should only send a duplicate code set service context,
  157. // but this makes sure we always use the first one we got.
  158. if (codeSetContext == null) {
  159. if (OSFCodeSetRegistry.lookupEntry(csc.getCharCodeSet()) == null ||
  160. OSFCodeSetRegistry.lookupEntry(csc.getWCharCodeSet()) == null) {
  161. // If the client says it's negotiated a code set that
  162. // isn't a fallback and we never said we support, then
  163. // it has a bug.
  164. throw new DATA_CONVERSION(MinorCodes.BAD_CODESETS_FROM_CLIENT,
  165. CompletionStatus.COMPLETED_NO);
  166. }
  167. codeSetContext = csc;
  168. }
  169. }
  170. final boolean isServer()
  171. {
  172. return isServer;
  173. }
  174. }