1. /*
  2. * @(#)RMISocketFactory.java 1.16 02/05/29
  3. *
  4. * Copyright 2002 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.16, 05/29/02
  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 java.lang.SecurityManager#checkSetFactory()
  79. * @since JDK1.1
  80. */
  81. public synchronized static void setSocketFactory(RMISocketFactory fac)
  82. throws IOException
  83. {
  84. if (factory != null) {
  85. throw new SocketException("factory already defined");
  86. }
  87. SecurityManager security = System.getSecurityManager();
  88. if (security != null) {
  89. security.checkSetFactory();
  90. }
  91. factory = fac;
  92. }
  93. /**
  94. * Returns the socket factory set by the <code>setSocketFactory</code>
  95. * method. Returns <code>null</code> if no socket factory has been
  96. * set.
  97. * @return the socket factory
  98. * @see #setSocketFactory(RMISocketFactory)
  99. * @since JDK1.1
  100. */
  101. public synchronized static RMISocketFactory getSocketFactory()
  102. {
  103. return factory;
  104. }
  105. /**
  106. * Returns a reference to the default socket factory used
  107. * by this RMI implementation. This will be the factory used
  108. * by the RMI runtime when <code>getSocketFactory</code>
  109. * returns <code>null</code>.
  110. * @return the default RMI socket factory
  111. * @since JDK1.1
  112. */
  113. public synchronized static RMISocketFactory getDefaultSocketFactory() {
  114. if (defaultSocketFactory == null) {
  115. defaultSocketFactory =
  116. new sun.rmi.transport.proxy.RMIMasterSocketFactory();
  117. }
  118. return defaultSocketFactory;
  119. }
  120. /**
  121. * Sets the failure handler to be called by the RMI runtime if server
  122. * socket creation fails. By default, if no failure handler is installed
  123. * and server socket creation fails, the RMI runtime does attempt to
  124. * recreate the server socket.
  125. * @param fh the failure handler
  126. * @see java.rmi.server.RMIFailureHandler#failure(Exception)
  127. * @since JDK1.1
  128. */
  129. public synchronized static void setFailureHandler(RMIFailureHandler fh)
  130. {
  131. SecurityManager security = System.getSecurityManager();
  132. if (security != null) {
  133. security.checkSetFactory();
  134. }
  135. handler = fh;
  136. }
  137. /**
  138. * Returns the handler for socket creation failure set by the
  139. * <code>setFailureHandler</code> method.
  140. * @return the failure handler
  141. * @see #setFailureHandler(RMIFailureHandler)
  142. * @since JDK1.1
  143. */
  144. public synchronized static RMIFailureHandler getFailureHandler()
  145. {
  146. return handler;
  147. }
  148. }