1. /*
  2. * @(#)RMIJRMPServerImpl.java 1.27 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management.remote.rmi;
  8. import java.io.IOException;
  9. import java.rmi.NoSuchObjectException;
  10. import java.rmi.Remote;
  11. import java.rmi.RemoteException;
  12. import java.rmi.server.RMIClientSocketFactory;
  13. import java.rmi.server.RMIServerSocketFactory;
  14. import java.rmi.server.UnicastRemoteObject;
  15. import java.rmi.server.RemoteObject;
  16. import java.util.Map;
  17. import java.util.Collections;
  18. import javax.security.auth.Subject;
  19. import com.sun.jmx.remote.internal.RMIExporter;
  20. /**
  21. * <p>An {@link RMIServer} object that is exported through JRMP and that
  22. * creates client connections as RMI objects exported through JRMP.
  23. * User code does not usually reference this class directly.</p>
  24. *
  25. * @see RMIServerImpl
  26. *
  27. * @since 1.5
  28. * @since.unbundled 1.0
  29. */
  30. public class RMIJRMPServerImpl extends RMIServerImpl {
  31. /**
  32. * <p>Creates a new {@link RMIServer} object that will be exported
  33. * on the given port using the given socket factories.</p>
  34. *
  35. * @param port the port on which this object and the {@link
  36. * RMIConnectionImpl} objects it creates will be exported. Can be
  37. * zero, to indicate any available port.
  38. *
  39. * @param csf the client socket factory for the created RMI
  40. * objects. Can be null.
  41. *
  42. * @param ssf the server socket factory for the created RMI
  43. * objects. Can be null.
  44. *
  45. * @param env the environment map. Can be null.
  46. *
  47. * @exception IOException if the {@link RMIServer} object
  48. * cannot be created.
  49. *
  50. * @exception IllegalArgumentException if <code>port</code> is
  51. * negative.
  52. */
  53. public RMIJRMPServerImpl(int port,
  54. RMIClientSocketFactory csf,
  55. RMIServerSocketFactory ssf,
  56. Map<String,?> env)
  57. throws IOException {
  58. super(env);
  59. if (port < 0)
  60. throw new IllegalArgumentException("Negative port: " + port);
  61. this.port = port;
  62. this.csf = csf;
  63. this.ssf = ssf;
  64. this.env = (env == null) ? Collections.EMPTY_MAP : env;
  65. }
  66. protected void export() throws IOException {
  67. export(this);
  68. }
  69. private void export(Remote obj) throws RemoteException {
  70. RMIExporter exporter =
  71. (RMIExporter) env.get(RMIExporter.EXPORTER_ATTRIBUTE);
  72. if (exporter == null)
  73. UnicastRemoteObject.exportObject(obj, port, csf, ssf);
  74. else
  75. exporter.exportObject(obj, port, csf, ssf);
  76. }
  77. private void unexport(Remote obj, boolean force)
  78. throws NoSuchObjectException {
  79. RMIExporter exporter =
  80. (RMIExporter) env.get(RMIExporter.EXPORTER_ATTRIBUTE);
  81. if (exporter == null)
  82. UnicastRemoteObject.unexportObject(obj, force);
  83. else
  84. exporter.unexportObject(obj, force);
  85. }
  86. protected String getProtocol() {
  87. return "rmi";
  88. }
  89. /**
  90. * <p>Returns a serializable stub for this {@link RMIServer} object.</p>
  91. *
  92. * @return a serializable stub.
  93. *
  94. * @exception IOException if the stub cannot be obtained - e.g the
  95. * RMIJRMPServerImpl has not been exported yet.
  96. */
  97. public Remote toStub() throws IOException {
  98. return RemoteObject.toStub(this);
  99. }
  100. /**
  101. * <p>Creates a new client connection as an RMI object exported
  102. * through JRMP. The port and socket factories for the new
  103. * {@link RMIConnection} object are the ones supplied
  104. * to the <code>RMIJRMPServerImpl</code> constructor.</p>
  105. *
  106. * @param connectionId the ID of the new connection. Every
  107. * connection opened by this connector server will have a
  108. * different id. The behavior is unspecified if this parameter is
  109. * null.
  110. *
  111. * @param subject the authenticated subject. Can be null.
  112. *
  113. * @return the newly-created <code>RMIConnection</code>.
  114. *
  115. * @exception IOException if the new {@link RMIConnection}
  116. * object cannot be created or exported.
  117. */
  118. protected RMIConnection makeClient(String connectionId, Subject subject)
  119. throws IOException {
  120. if (connectionId == null)
  121. throw new NullPointerException("Null connectionId");
  122. RMIConnection client =
  123. new RMIConnectionImpl(this, connectionId, getDefaultClassLoader(),
  124. subject, env);
  125. export(client);
  126. return client;
  127. }
  128. protected void closeClient(RMIConnection client) throws IOException {
  129. unexport(client, true);
  130. }
  131. /**
  132. * <p>Called by {@link #close()} to close the connector server by
  133. * unexporting this object. After returning from this method, the
  134. * connector server must not accept any new connections.</p>
  135. *
  136. * @exception IOException if the attempt to close the connector
  137. * server failed.
  138. */
  139. protected void closeServer() throws IOException {
  140. unexport(this, true);
  141. }
  142. private final int port;
  143. private final RMIClientSocketFactory csf;
  144. private final RMIServerSocketFactory ssf;
  145. private final Map env;
  146. }