KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > remote > rmi > RMIJRMPServerImpl


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
8 package javax.management.remote.rmi;
9
10 import java.io.IOException JavaDoc;
11 import java.rmi.NoSuchObjectException JavaDoc;
12 import java.rmi.Remote JavaDoc;
13 import java.rmi.RemoteException JavaDoc;
14 import java.rmi.server.RMIClientSocketFactory JavaDoc;
15 import java.rmi.server.RMIServerSocketFactory JavaDoc;
16 import java.rmi.server.UnicastRemoteObject JavaDoc;
17 import java.rmi.server.RemoteObject JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Collections JavaDoc;
20 import javax.security.auth.Subject JavaDoc;
21
22 import com.sun.jmx.remote.internal.RMIExporter;
23
24 /**
25  * <p>An {@link RMIServer} object that is exported through JRMP and that
26  * creates client connections as RMI objects exported through JRMP.
27  * User code does not usually reference this class directly.</p>
28  *
29  * @see RMIServerImpl
30  *
31  * @since 1.5
32  * @since.unbundled 1.0
33  */

34 public class RMIJRMPServerImpl extends RMIServerImpl JavaDoc {
35     /**
36      * <p>Creates a new {@link RMIServer} object that will be exported
37      * on the given port using the given socket factories.</p>
38      *
39      * @param port the port on which this object and the {@link
40      * RMIConnectionImpl} objects it creates will be exported. Can be
41      * zero, to indicate any available port.
42      *
43      * @param csf the client socket factory for the created RMI
44      * objects. Can be null.
45      *
46      * @param ssf the server socket factory for the created RMI
47      * objects. Can be null.
48      *
49      * @param env the environment map. Can be null.
50      *
51      * @exception IOException if the {@link RMIServer} object
52      * cannot be created.
53      *
54      * @exception IllegalArgumentException if <code>port</code> is
55      * negative.
56      */

57     public RMIJRMPServerImpl(int port,
58                              RMIClientSocketFactory JavaDoc csf,
59                              RMIServerSocketFactory JavaDoc ssf,
60                              Map JavaDoc<String JavaDoc,?> env)
61         throws IOException JavaDoc {
62
63         super(env);
64
65     if (port < 0)
66         throw new IllegalArgumentException JavaDoc("Negative port: " + port);
67
68         this.port = port;
69         this.csf = csf;
70         this.ssf = ssf;
71     this.env = (env == null) ? Collections.EMPTY_MAP : env;
72     }
73
74     protected void export() throws IOException JavaDoc {
75     export(this);
76     }
77
78     private void export(Remote JavaDoc obj) throws RemoteException JavaDoc {
79     RMIExporter exporter =
80         (RMIExporter) env.get(RMIExporter.EXPORTER_ATTRIBUTE);
81     if (exporter == null)
82         UnicastRemoteObject.exportObject(obj, port, csf, ssf);
83     else
84         exporter.exportObject(obj, port, csf, ssf);
85     }
86
87     private void unexport(Remote JavaDoc obj, boolean force)
88         throws NoSuchObjectException JavaDoc {
89     RMIExporter exporter =
90         (RMIExporter) env.get(RMIExporter.EXPORTER_ATTRIBUTE);
91     if (exporter == null)
92         UnicastRemoteObject.unexportObject(obj, force);
93     else
94         exporter.unexportObject(obj, force);
95     }
96
97     protected String JavaDoc getProtocol() {
98     return "rmi";
99     }
100
101     /**
102      * <p>Returns a serializable stub for this {@link RMIServer} object.</p>
103      *
104      * @return a serializable stub.
105      *
106      * @exception IOException if the stub cannot be obtained - e.g the
107      * RMIJRMPServerImpl has not been exported yet.
108      */

109     public Remote JavaDoc toStub() throws IOException JavaDoc {
110         return RemoteObject.toStub(this);
111     }
112
113     /**
114      * <p>Creates a new client connection as an RMI object exported
115      * through JRMP. The port and socket factories for the new
116      * {@link RMIConnection} object are the ones supplied
117      * to the <code>RMIJRMPServerImpl</code> constructor.</p>
118      *
119      * @param connectionId the ID of the new connection. Every
120      * connection opened by this connector server will have a
121      * different id. The behavior is unspecified if this parameter is
122      * null.
123      *
124      * @param subject the authenticated subject. Can be null.
125      *
126      * @return the newly-created <code>RMIConnection</code>.
127      *
128      * @exception IOException if the new {@link RMIConnection}
129      * object cannot be created or exported.
130      */

131     protected RMIConnection JavaDoc makeClient(String JavaDoc connectionId, Subject JavaDoc subject)
132         throws IOException JavaDoc {
133
134     if (connectionId == null)
135         throw new NullPointerException JavaDoc("Null connectionId");
136
137         RMIConnection JavaDoc client =
138             new RMIConnectionImpl JavaDoc(this, connectionId, getDefaultClassLoader(),
139                   subject, env);
140     export(client);
141         return client;
142     }
143
144     protected void closeClient(RMIConnection JavaDoc client) throws IOException JavaDoc {
145         unexport(client, true);
146     }
147
148     /**
149      * <p>Called by {@link #close()} to close the connector server by
150      * unexporting this object. After returning from this method, the
151      * connector server must not accept any new connections.</p>
152      *
153      * @exception IOException if the attempt to close the connector
154      * server failed.
155      */

156     protected void closeServer() throws IOException JavaDoc {
157         unexport(this, true);
158     }
159
160     private final int port;
161     private final RMIClientSocketFactory JavaDoc csf;
162     private final RMIServerSocketFactory JavaDoc ssf;
163     private final Map JavaDoc env;
164 }
165
Popular Tags