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