~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/transport/rmi/RemotingRMIServerSocketFactory.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
 
 
23
package org.jboss.remoting.transport.rmi;
 
24
 
 
25
import org.jboss.logging.Logger;
 
26
import org.jboss.remoting.util.SecurityUtility;
 
27
 
 
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;
 
38
 
 
39
 
 
40
/**
 
41
 * <code>RemotingRMIServerSocketFactory</code> provides two services to <code>RMIServerInvoker</code>.
 
42
 * <ol>
 
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.
 
45
 * <p/>
 
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.
 
48
 * </ol>
 
49
 * <p/>
 
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>.
 
53
 * <p/>
 
54
 * If the <code>ServerSocketFactory</code> parameter is not specified, an instance of <code>java.net.ServerSocket</code>
 
55
 * will be created by default.
 
56
 * <p/>
 
57
 *
 
58
 * @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
 
59
 * @version $Revision: 5017 $
 
60
 *          <p/>
 
61
 *          Copyright (c) 2005
 
62
 *          </p>
 
63
 */
 
64
 
 
65
public class RemotingRMIServerSocketFactory implements RMIServerSocketFactory, Serializable
 
66
{
 
67
   protected static final Logger log = Logger.getLogger(RemotingRMIServerSocketFactory.class);
 
68
 
 
69
   private ServerSocketFactory serverSocketFactory;
 
70
   private int backlog;
 
71
   private InetAddress bindAddress;
 
72
   private int timeout;
 
73
   private static final long serialVersionUID = -5851426317709480542L;
 
74
 
 
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>.
 
82
 
 
83
//   private Constructor constructor;
 
84
//   private Object[] args;
 
85
//   private int portPosition;
 
86
 
 
87
 
 
88
   /**
 
89
    * @param bindHost name of host to which all generated server sockets should be bound
 
90
    */
 
91
   public RemotingRMIServerSocketFactory(String bindHost) throws UnknownHostException
 
92
   {
 
93
      this(null, -1, bindHost);
 
94
   }
 
95
 
 
96
 
 
97
   /**
 
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
 
101
    */
 
102
   public RemotingRMIServerSocketFactory(int backlog, String bindHost) throws UnknownHostException
 
103
   {
 
104
      this(null, backlog, bindHost);
 
105
   }
 
106
 
 
107
 
 
108
   /**
 
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
 
113
    */
 
114
   public RemotingRMIServerSocketFactory(ServerSocketFactory serverSocketFactory, int backlog, String bindHost)
 
115
         throws UnknownHostException
 
116
   {
 
117
      this(serverSocketFactory, backlog, bindHost, 60000); // TODO: -TME This needs to be fixed so only comes from parent class
 
118
   }
 
119
 
 
120
   public RemotingRMIServerSocketFactory(ServerSocketFactory serverSocketFactory, int backlog, final String bindHost, int timeout)
 
121
         throws UnknownHostException
 
122
   {
 
123
      this.serverSocketFactory = serverSocketFactory;
 
124
      this.backlog = backlog;
 
125
      this.timeout = timeout;
 
126
      this.bindAddress = getAddressByName(bindHost);
 
127
   }
 
128
 
 
129
   public RemotingRMIServerSocketFactory(String bindHost, int timeout) throws UnknownHostException
 
130
   {
 
131
      this(null, -1, bindHost, timeout);
 
132
   }
 
133
 
 
134
//   public RemotingRMIServerSocketFactory(Constructor constructor, Object[] args, int portPosition)
 
135
//   {
 
136
//      this.constructor = constructor;
 
137
//      this.args = args;
 
138
//      this.portPosition = portPosition;
 
139
//   }
 
140
 
 
141
 
 
142
   /**
 
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.
 
146
    *
 
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
 
150
    */
 
151
   public ServerSocket createServerSocket(final int port) throws IOException
 
152
   {
 
153
      ServerSocket svrSocket = null;
 
154
 
 
155
      if(serverSocketFactory != null)
 
156
      {
 
157
         svrSocket = createServerSocket(serverSocketFactory, port, backlog, bindAddress);
 
158
      }
 
159
 
 
160
//      if (constructor != null)
 
161
//      {
 
162
//         try
 
163
//         {
 
164
//            if (portPosition != -1)
 
165
//               args[portPosition] = new Integer(port);
 
166
//
 
167
//            return (ServerSocket) constructor.newInstance(args);
 
168
//         }
 
169
//         catch (Exception e)
 
170
//         {
 
171
//            throw new IOException(e.getMessage());
 
172
//         }
 
173
//      }
 
174
 
 
175
      else
 
176
      {
 
177
         svrSocket = createServerSocket(port, backlog, bindAddress);
 
178
      }
 
179
 
 
180
      svrSocket.setSoTimeout(timeout);
 
181
      return svrSocket;
 
182
   }
 
183
 
 
184
 
 
185
   /**
 
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.
 
188
    *
 
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>
 
191
    */
 
192
   public boolean equals(Object o)
 
193
   {
 
194
      if(! (o instanceof RemotingRMIServerSocketFactory))
 
195
      {
 
196
         return false;
 
197
      }
 
198
 
 
199
      RemotingRMIServerSocketFactory ssf = (RemotingRMIServerSocketFactory) o;
 
200
 
 
201
      // This is for the version that uses a ServerSocketFactory
 
202
      if(serverSocketFactory != null)
 
203
      {
 
204
         if(ssf.serverSocketFactory == null ||
 
205
            !serverSocketFactory.equals(ssf.serverSocketFactory) ||
 
206
            backlog != ssf.backlog ||
 
207
            !bindAddress.equals(ssf.bindAddress))
 
208
         {
 
209
            return false;
 
210
         }
 
211
 
 
212
         return true;
 
213
      }
 
214
 
 
215
//      // This is for the version that uses a constructor
 
216
//      if (constructor != null)
 
217
//      {
 
218
//         if (ssf.constructor == null ||
 
219
//               ! constructor.equals(ssf.constructor) ||
 
220
//               portPosition != ssf.portPosition)
 
221
//            return false;
 
222
//
 
223
//         for (int i = 0; i < args.length; i++)
 
224
//         {
 
225
//            if (! args[i].equals(ssf.args[i]))
 
226
//               return false;
 
227
//         }
 
228
//
 
229
//         return true;
 
230
//      }
 
231
 
 
232
      // This is for the plain vanilla version
 
233
      if(ssf.serverSocketFactory != null || backlog != ssf.backlog || !bindAddress.equals(ssf.bindAddress))
 
234
      {
 
235
         return false;
 
236
      }
 
237
 
 
238
      return true;
 
239
   }
 
240
 
 
241
 
 
242
   /**
 
243
    * Overrides <code>hashCode()</code> method provided by the <code>Object</code> class.
 
244
    *
 
245
    * @return a hashcode for <code>this</code>
 
246
    */
 
247
   public int hashCode()
 
248
   {
 
249
      if(serverSocketFactory != null)
 
250
      {
 
251
         return serverSocketFactory.hashCode() * backlog * bindAddress.hashCode();
 
252
      }
 
253
 
 
254
//      if (constructor != null)
 
255
//      {
 
256
//         int hash = portPosition;
 
257
//         
 
258
//         for (int i = 0; i < args.length; i++)
 
259
//            hash *= args[i].hashCode();
 
260
//         
 
261
//         return hash;
 
262
//      }
 
263
 
 
264
      return backlog * bindAddress.hashCode();
 
265
   }
 
266
 
 
267
   static private ServerSocket createServerSocket(final ServerSocketFactory ssf,
 
268
                                                 final int port, final int backlog,
 
269
                                                 final InetAddress inetAddress)
 
270
   throws IOException
 
271
   {
 
272
      if (SecurityUtility.skipAccessControl())
 
273
      {
 
274
         return ssf.createServerSocket(port, backlog, inetAddress);
 
275
      }
 
276
 
 
277
      try
 
278
      {
 
279
         return (ServerSocket)AccessController.doPrivileged( new PrivilegedExceptionAction()
 
280
         {
 
281
            public Object run() throws Exception
 
282
            {
 
283
               return ssf.createServerSocket(port, backlog, inetAddress);
 
284
            }
 
285
         });
 
286
      }
 
287
      catch (PrivilegedActionException e)
 
288
      {
 
289
         throw (IOException) e.getCause();
 
290
      }
 
291
   }
 
292
 
 
293
   static private ServerSocket createServerSocket(final int port, final int backlog,
 
294
                                                 final InetAddress inetAddress)
 
295
   throws IOException
 
296
   {
 
297
      if (SecurityUtility.skipAccessControl())
 
298
      {
 
299
         return new ServerSocket(port, backlog, inetAddress);
 
300
      }
 
301
 
 
302
      try
 
303
      {
 
304
         return (ServerSocket)AccessController.doPrivileged( new PrivilegedExceptionAction()
 
305
         {
 
306
            public Object run() throws IOException
 
307
            {
 
308
               return new ServerSocket(port, backlog, inetAddress);
 
309
            }
 
310
         });
 
311
      }
 
312
      catch (PrivilegedActionException e)
 
313
      {
 
314
         throw (IOException) e.getCause();
 
315
      }
 
316
   }
 
317
   
 
318
   static private InetAddress getAddressByName(final String host) throws UnknownHostException
 
319
   {
 
320
      if (SecurityUtility.skipAccessControl())
 
321
      {
 
322
         return InetAddress.getByName(host);
 
323
      }
 
324
      
 
325
      try
 
326
      {
 
327
         return (InetAddress)AccessController.doPrivileged( new PrivilegedExceptionAction()
 
328
         {
 
329
            public Object run() throws IOException
 
330
            {
 
331
               return InetAddress.getByName(host);
 
332
            }
 
333
         });
 
334
      }
 
335
      catch (PrivilegedActionException e)
 
336
      {
 
337
         throw (UnknownHostException) e.getCause();
 
338
      }
 
339
   }
 
340
}
 
 
b'\\ No newline at end of file'