KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > rmi > server > RMISocketFactory


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
8 package java.rmi.server;
9
10 import java.io.*;
11 import java.net.*;
12
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.21, 12/19/03
29  * @author Ann Wollrath
30  * @author Peter Jones
31  * @since JDK1.1
32  */

33 public abstract class RMISocketFactory
34     implements RMIClientSocketFactory JavaDoc, RMIServerSocketFactory JavaDoc
35 {
36
37     /** Client/server socket factory to be used by RMI runtime */
38     private static RMISocketFactory JavaDoc factory = null;
39     /** default socket factory used by this RMI implementation */
40     private static RMISocketFactory JavaDoc defaultSocketFactory;
41     /** Handler for socket creation failure */
42     private static RMIFailureHandler JavaDoc handler = null;
43
44     /**
45      * Constructs an <code>RMISocketFactory</code>.
46      * @since JDK1.1
47      */

48     public RMISocketFactory() {
49     super();
50     }
51     
52     /**
53      * Creates a client socket connected to the specified host and port.
54      * @param host the host name
55      * @param port the port number
56      * @return a socket connected to the specified host and port.
57      * @exception IOException if an I/O error occurs during socket creation
58      * @since JDK1.1
59      */

60     public abstract Socket createSocket(String JavaDoc host, int port)
61     throws IOException;
62
63     /**
64      * Create a server socket on the specified port (port 0 indicates
65      * an anonymous port).
66      * @param port the port number
67      * @return the server socket on the specified port
68      * @exception IOException if an I/O error occurs during server socket
69      * creation
70      * @since JDK1.1
71      */

72     public abstract ServerSocket createServerSocket(int port)
73     throws IOException;
74
75     /**
76      * Set the global socket factory from which RMI gets sockets (if the
77      * remote object is not associated with a specific client and/or server
78      * socket factory). The RMI socket factory can only be set once. Note: The
79      * RMISocketFactory may only be set if the current security manager allows
80      * setting a socket factory; if disallowed, a SecurityException will be
81      * thrown.
82      * @param fac the socket factory
83      * @exception IOException if the RMI socket factory is already set
84      * @exception SecurityException if a security manager exists and its
85      * <code>checkSetFactory</code> method doesn't allow the operation.
86      * @see #getSocketFactory
87      * @see java.lang.SecurityManager#checkSetFactory()
88      * @since JDK1.1
89      */

90     public synchronized static void setSocketFactory(RMISocketFactory JavaDoc fac)
91     throws IOException
92     {
93         if (factory != null) {
94         throw new SocketException("factory already defined");
95     }
96     SecurityManager JavaDoc security = System.getSecurityManager();
97     if (security != null) {
98         security.checkSetFactory();
99     }
100     factory = fac;
101     }
102
103     /**
104      * Returns the socket factory set by the <code>setSocketFactory</code>
105      * method. Returns <code>null</code> if no socket factory has been
106      * set.
107      * @return the socket factory
108      * @see #setSocketFactory(RMISocketFactory)
109      * @since JDK1.1
110      */

111     public synchronized static RMISocketFactory JavaDoc getSocketFactory()
112     {
113     return factory;
114     }
115
116     /**
117      * Returns a reference to the default socket factory used
118      * by this RMI implementation. This will be the factory used
119      * by the RMI runtime when <code>getSocketFactory</code>
120      * returns <code>null</code>.
121      * @return the default RMI socket factory
122      * @since JDK1.1
123      */

124     public synchronized static RMISocketFactory JavaDoc getDefaultSocketFactory() {
125     if (defaultSocketFactory == null) {
126         defaultSocketFactory =
127         new sun.rmi.transport.proxy.RMIMasterSocketFactory();
128     }
129     return defaultSocketFactory;
130     }
131
132     /**
133      * Sets the failure handler to be called by the RMI runtime if server
134      * socket creation fails. By default, if no failure handler is installed
135      * and server socket creation fails, the RMI runtime does attempt to
136      * recreate the server socket.
137      *
138      * <p>If there is a security manager, this method first calls
139      * the security manager's <code>checkSetFactory</code> method
140      * to ensure the operation is allowed.
141      * This could result in a <code>SecurityException</code>.
142      *
143      * @param fh the failure handler
144      * @throws SecurityException if a security manager exists and its
145      * <code>checkSetFactory</code> method doesn't allow the
146      * operation.
147      * @see #getFailureHandler
148      * @see java.rmi.server.RMIFailureHandler#failure(Exception)
149      * @since JDK1.1
150      */

151     public synchronized static void setFailureHandler(RMIFailureHandler JavaDoc fh)
152     {
153     SecurityManager JavaDoc security = System.getSecurityManager();
154     if (security != null) {
155         security.checkSetFactory();
156     }
157     handler = fh;
158     }
159
160     /**
161      * Returns the handler for socket creation failure set by the
162      * <code>setFailureHandler</code> method.
163      * @return the failure handler
164      * @see #setFailureHandler(RMIFailureHandler)
165      * @since JDK1.1
166      */

167     public synchronized static RMIFailureHandler JavaDoc getFailureHandler()
168     {
169     return handler;
170     }
171 }
172
Popular Tags