1. /*
  2. * @(#)RMISocketFactory.java 1.21 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.rmi.server;
  8. import java.io.*;
  9. import java.net.*;
  10. /**
  11. * An <code>RMISocketFactory</code> instance is used by the RMI runtime
  12. * in order to obtain client and server sockets for RMI calls. An
  13. * application may use the <code>setSocketFactory</code> method to
  14. * request that the RMI runtime use its socket factory instance
  15. * instead of the default implementation.<p>
  16. *
  17. * The default socket factory implementation used goes through a
  18. * three-tiered approach to creating client sockets. First, a direct
  19. * socket connection to the remote VM is attempted. If that fails
  20. * (due to a firewall), the runtime uses HTTP with the explicit port
  21. * number of the server. If the firewall does not allow this type of
  22. * communication, then HTTP to a cgi-bin script on the server is used
  23. * to POST the RMI call.<p>
  24. *
  25. * @version 1.21, 12/19/03
  26. * @author Ann Wollrath
  27. * @author Peter Jones
  28. * @since JDK1.1
  29. */
  30. public abstract class RMISocketFactory
  31. implements RMIClientSocketFactory, RMIServerSocketFactory
  32. {
  33. /** Client/server socket factory to be used by RMI runtime */
  34. private static RMISocketFactory factory = null;
  35. /** default socket factory used by this RMI implementation */
  36. private static RMISocketFactory defaultSocketFactory;
  37. /** Handler for socket creation failure */
  38. private static RMIFailureHandler handler = null;
  39. /**
  40. * Constructs an <code>RMISocketFactory</code>.
  41. * @since JDK1.1
  42. */
  43. public RMISocketFactory() {
  44. super();
  45. }
  46. /**
  47. * Creates a client socket connected to the specified host and port.
  48. * @param host the host name
  49. * @param port the port number
  50. * @return a socket connected to the specified host and port.
  51. * @exception IOException if an I/O error occurs during socket creation
  52. * @since JDK1.1
  53. */
  54. public abstract Socket createSocket(String host, int port)
  55. throws IOException;
  56. /**
  57. * Create a server socket on the specified port (port 0 indicates
  58. * an anonymous port).
  59. * @param port the port number
  60. * @return the server socket on the specified port
  61. * @exception IOException if an I/O error occurs during server socket
  62. * creation
  63. * @since JDK1.1
  64. */
  65. public abstract ServerSocket createServerSocket(int port)
  66. throws IOException;
  67. /**
  68. * Set the global socket factory from which RMI gets sockets (if the
  69. * remote object is not associated with a specific client and/or server
  70. * socket factory). The RMI socket factory can only be set once. Note: The
  71. * RMISocketFactory may only be set if the current security manager allows
  72. * setting a socket factory; if disallowed, a SecurityException will be
  73. * thrown.
  74. * @param fac the socket factory
  75. * @exception IOException if the RMI socket factory is already set
  76. * @exception SecurityException if a security manager exists and its
  77. * <code>checkSetFactory</code> method doesn't allow the operation.
  78. * @see #getSocketFactory
  79. * @see java.lang.SecurityManager#checkSetFactory()
  80. * @since JDK1.1
  81. */
  82. public synchronized static void setSocketFactory(RMISocketFactory fac)
  83. throws IOException
  84. {
  85. if (factory != null) {
  86. throw new SocketException("factory already defined");
  87. }
  88. SecurityManager security = System.getSecurityManager();
  89. if (security != null) {
  90. security.checkSetFactory();
  91. }
  92. factory = fac;
  93. }
  94. /**
  95. * Returns the socket factory set by the <code>setSocketFactory</code>
  96. * method. Returns <code>null</code> if no socket factory has been
  97. * set.
  98. * @return the socket factory
  99. * @see #setSocketFactory(RMISocketFactory)
  100. * @since JDK1.1
  101. */
  102. public synchronized static RMISocketFactory getSocketFactory()
  103. {
  104. return factory;
  105. }
  106. /**
  107. * Returns a reference to the default socket factory used
  108. * by this RMI implementation. This will be the factory used
  109. * by the RMI runtime when <code>getSocketFactory</code>
  110. * returns <code>null</code>.
  111. * @return the default RMI socket factory
  112. * @since JDK1.1
  113. */
  114. public synchronized static RMISocketFactory getDefaultSocketFactory() {
  115. if (defaultSocketFactory == null) {
  116. defaultSocketFactory =
  117. new sun.rmi.transport.proxy.RMIMasterSocketFactory();
  118. }
  119. return defaultSocketFactory;
  120. }
  121. /**
  122. * Sets the failure handler to be called by the RMI runtime if server
  123. * socket creation fails. By default, if no failure handler is installed
  124. * and server socket creation fails, the RMI runtime does attempt to
  125. * recreate the server socket.
  126. *
  127. * <p>If there is a security manager, this method first calls
  128. * the security manager's <code>checkSetFactory</code> method
  129. * to ensure the operation is allowed.
  130. * This could result in a <code>SecurityException</code>.
  131. *
  132. * @param fh the failure handler
  133. * @throws SecurityException if a security manager exists and its
  134. * <code>checkSetFactory</code> method doesn't allow the
  135. * operation.
  136. * @see #getFailureHandler
  137. * @see java.rmi.server.RMIFailureHandler#failure(Exception)
  138. * @since JDK1.1
  139. */
  140. public synchronized static void setFailureHandler(RMIFailureHandler fh)
  141. {
  142. SecurityManager security = System.getSecurityManager();
  143. if (security != null) {
  144. security.checkSetFactory();
  145. }
  146. handler = fh;
  147. }
  148. /**
  149. * Returns the handler for socket creation failure set by the
  150. * <code>setFailureHandler</code> method.
  151. * @return the failure handler
  152. * @see #setFailureHandler(RMIFailureHandler)
  153. * @since JDK1.1
  154. */
  155. public synchronized static RMIFailureHandler getFailureHandler()
  156. {
  157. return handler;
  158. }
  159. }