2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.transport.rmi;
25
import org.jboss.logging.Logger;
26
import org.jboss.remoting.util.SecurityUtility;
28
import javax.net.ServerSocketFactory;
29
import java.io.IOException;
30
import java.io.Serializable;
31
import java.net.InetAddress;
32
import java.net.ServerSocket;
33
import java.net.UnknownHostException;
34
import java.rmi.server.RMIServerSocketFactory;
35
import java.security.AccessController;
36
import java.security.PrivilegedActionException;
37
import java.security.PrivilegedExceptionAction;
41
* <code>RemotingRMIServerSocketFactory</code> provides two services to <code>RMIServerInvoker</code>.
43
* <li>It can be parameterized by a host name, allowing <code>RMIServerInvoker</code> to supply RMI
44
* with a factory which creates server sockets bound to a specified host name as well as port.
46
* <li>It can be parameterized by a <code>ServerSocketFactory</code> allowing <code>RMIServerInvoker</code>
47
* to supply RMI with a factory facility which creates specialized server sockets.
50
* If the <code>ServerSocketFactory</code> parameter is specified, then the <code>RemotingRMIServerSocketFactory</code>
51
* should be used with a matching instance of <code>RemotingRMIClientSocketFactory</code> with a compatible
52
* <code>SocketFactory</code>.
54
* If the <code>ServerSocketFactory</code> parameter is not specified, an instance of <code>java.net.ServerSocket</code>
55
* will be created by default.
58
* @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
59
* @version $Revision: 5017 $
65
public class RemotingRMIServerSocketFactory implements RMIServerSocketFactory, Serializable
67
protected static final Logger log = Logger.getLogger(RemotingRMIServerSocketFactory.class);
69
private ServerSocketFactory serverSocketFactory;
71
private InetAddress bindAddress;
73
private static final long serialVersionUID = -5851426317709480542L;
75
// The commented code below is from an attempt to incorporate a <code>java.lang.reflect.Constructor</code>
76
// parameter to provide a very general way to create server sockets. The problem is that
77
// <code>Constructor</code> does not implement <code>Serializable</code>, which is necessary to
78
// allow the <code>RemotingRMIServerSocketFactory</code> to be transmitted to the RMI Registry. The
79
// code is left in place because it could be resurrected by passing in a class name and parameter
80
// types to specify a constructor. Fortunately, <code>java.lang.Class</code> does implement
81
// <code>Serializable</code>.
83
// private Constructor constructor;
84
// private Object[] args;
85
// private int portPosition;
89
* @param bindHost name of host to which all generated server sockets should be bound
91
public RemotingRMIServerSocketFactory(String bindHost) throws UnknownHostException
93
this(null, -1, bindHost);
98
* @param backlog to be passed to all generated server sockets
99
* @param bindHost name of host to which all generated server sockets should be bound
100
* @throws UnknownHostException if an IP address for <code>bindHost</code> cannot be found
102
public RemotingRMIServerSocketFactory(int backlog, String bindHost) throws UnknownHostException
104
this(null, backlog, bindHost);
109
* @param serverSocketFactory <code>ServerSocketFactory</code> for generating server sockets
110
* @param backlog to be passed to all generated server sockets
111
* @param bindHost name of host to which all generated server sockets should be bound
112
* @throws UnknownHostException if an IP address for <code>bindHost</code> cannot be found
114
public RemotingRMIServerSocketFactory(ServerSocketFactory serverSocketFactory, int backlog, String bindHost)
115
throws UnknownHostException
117
this(serverSocketFactory, backlog, bindHost, 60000); // TODO: -TME This needs to be fixed so only comes from parent class
120
public RemotingRMIServerSocketFactory(ServerSocketFactory serverSocketFactory, int backlog, final String bindHost, int timeout)
121
throws UnknownHostException
123
this.serverSocketFactory = serverSocketFactory;
124
this.backlog = backlog;
125
this.timeout = timeout;
126
this.bindAddress = getAddressByName(bindHost);
129
public RemotingRMIServerSocketFactory(String bindHost, int timeout) throws UnknownHostException
131
this(null, -1, bindHost, timeout);
134
// public RemotingRMIServerSocketFactory(Constructor constructor, Object[] args, int portPosition)
136
// this.constructor = constructor;
138
// this.portPosition = portPosition;
143
* Creates a server socket bound to the host name passed to the constructor. If a
144
* <code>ServerSocketFactory</code> was passed to the constructor, it will be used to create the
145
* server socket. Otherwise, an instance of <code>java.net.ServerSocket</code> will be created.
147
* @param port the port to which the generated server socket should be bound
148
* @return a new <code>ServerSocket</code>
149
* @throws IOException if there is a problem creating a server socket
151
public ServerSocket createServerSocket(final int port) throws IOException
153
ServerSocket svrSocket = null;
155
if(serverSocketFactory != null)
157
svrSocket = createServerSocket(serverSocketFactory, port, backlog, bindAddress);
160
// if (constructor != null)
164
// if (portPosition != -1)
165
// args[portPosition] = new Integer(port);
167
// return (ServerSocket) constructor.newInstance(args);
169
// catch (Exception e)
171
// throw new IOException(e.getMessage());
177
svrSocket = createServerSocket(port, backlog, bindAddress);
180
svrSocket.setSoTimeout(timeout);
186
* Overrides the <code>equals()</code> method provided by the <code>Object</code> class. It looks for
187
* equality of binding host name and server socket factory parameters passed to constructor.
189
* @param o <code>Object</code> to which <code>code</code> is to be compared.
190
* @return true if and only if <code>o</code> equals <code>this</code>
192
public boolean equals(Object o)
194
if(! (o instanceof RemotingRMIServerSocketFactory))
199
RemotingRMIServerSocketFactory ssf = (RemotingRMIServerSocketFactory) o;
201
// This is for the version that uses a ServerSocketFactory
202
if(serverSocketFactory != null)
204
if(ssf.serverSocketFactory == null ||
205
!serverSocketFactory.equals(ssf.serverSocketFactory) ||
206
backlog != ssf.backlog ||
207
!bindAddress.equals(ssf.bindAddress))
215
// // This is for the version that uses a constructor
216
// if (constructor != null)
218
// if (ssf.constructor == null ||
219
// ! constructor.equals(ssf.constructor) ||
220
// portPosition != ssf.portPosition)
223
// for (int i = 0; i < args.length; i++)
225
// if (! args[i].equals(ssf.args[i]))
232
// This is for the plain vanilla version
233
if(ssf.serverSocketFactory != null || backlog != ssf.backlog || !bindAddress.equals(ssf.bindAddress))
243
* Overrides <code>hashCode()</code> method provided by the <code>Object</code> class.
245
* @return a hashcode for <code>this</code>
247
public int hashCode()
249
if(serverSocketFactory != null)
251
return serverSocketFactory.hashCode() * backlog * bindAddress.hashCode();
254
// if (constructor != null)
256
// int hash = portPosition;
258
// for (int i = 0; i < args.length; i++)
259
// hash *= args[i].hashCode();
264
return backlog * bindAddress.hashCode();
267
static private ServerSocket createServerSocket(final ServerSocketFactory ssf,
268
final int port, final int backlog,
269
final InetAddress inetAddress)
272
if (SecurityUtility.skipAccessControl())
274
return ssf.createServerSocket(port, backlog, inetAddress);
279
return (ServerSocket)AccessController.doPrivileged( new PrivilegedExceptionAction()
281
public Object run() throws Exception
283
return ssf.createServerSocket(port, backlog, inetAddress);
287
catch (PrivilegedActionException e)
289
throw (IOException) e.getCause();
293
static private ServerSocket createServerSocket(final int port, final int backlog,
294
final InetAddress inetAddress)
297
if (SecurityUtility.skipAccessControl())
299
return new ServerSocket(port, backlog, inetAddress);
304
return (ServerSocket)AccessController.doPrivileged( new PrivilegedExceptionAction()
306
public Object run() throws IOException
308
return new ServerSocket(port, backlog, inetAddress);
312
catch (PrivilegedActionException e)
314
throw (IOException) e.getCause();
318
static private InetAddress getAddressByName(final String host) throws UnknownHostException
320
if (SecurityUtility.skipAccessControl())
322
return InetAddress.getByName(host);
327
return (InetAddress)AccessController.doPrivileged( new PrivilegedExceptionAction()
329
public Object run() throws IOException
331
return InetAddress.getByName(host);
335
catch (PrivilegedActionException e)
337
throw (UnknownHostException) e.getCause();
b'\\ No newline at end of file'