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

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/transport/rmi/RemotingRMIClientSocketFactory.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 java.io.IOException;
26
 
import java.io.Serializable;
27
 
import java.net.InetAddress;
28
 
import java.net.Socket;
29
 
import java.net.UnknownHostException;
30
 
import java.rmi.server.RMIClientSocketFactory;
31
 
import java.security.AccessController;
32
 
import java.security.PrivilegedActionException;
33
 
import java.security.PrivilegedExceptionAction;
34
 
import java.util.Collections;
35
 
import java.util.HashMap;
36
 
import java.util.Map;
37
 
 
38
 
import javax.net.SocketFactory;
39
 
 
40
 
import org.jboss.logging.Logger;
41
 
import org.jboss.remoting.AbstractInvoker;
42
 
import org.jboss.remoting.InvokerLocator;
43
 
import org.jboss.remoting.Remoting;
44
 
import org.jboss.remoting.Version;
45
 
import org.jboss.remoting.util.SecurityUtility;
46
 
 
47
 
 
48
 
/**
49
 
 * <code>RemotingRMIClientSocketFactory</code> provides two services to <code>RMIServerInvoker</code>.
50
 
 * <ol>
51
 
 * <li>It can be parameterized by a host name, allowing <code>RMIServerInvoker</code> to supply RMI
52
 
 * with a factory which creates sockets connected to a specified host name.
53
 
 * <p/>
54
 
 * <li>It can be parameterized by a <code>SocketFactory</code> allowing <code>RMIServerInvoker</code>
55
 
 * to supply RMI with a factory facility which creates specialized sockets.
56
 
 * </ol>
57
 
 * <p/>
58
 
 * if the <code>SocketFactory</code> parameter is specified, then the <code>RemotingRMIClientSocketFactory</code>
59
 
 * should be used with a matching instance of <code>RemotingRMIServerSocketFactory</code> with a compatible
60
 
 * <code>ServerSocketFactory</code>.
61
 
 * <p/>
62
 
 * If the <code>SocketFactory</code> parameter is not specified, an instance of <code>java.net.Socket</code>
63
 
 * will be created by default.
64
 
 * <p/>
65
 
 * Although there is no apparent need for the host name parameter, since the <code>createSocket()</code>
66
 
 * method receives a host name, it seems that for a server object bound to localhost, the RMI runtime will
67
 
 * pass to <code>createSocket()</code> one of the IP addresses for which the host is configured (other than
68
 
 * 127.0.0.1), resulting in a failure to retrieve the object from the Registry.  If a host name is passed to
69
 
 * a <code>RemotingRMIClientFactory</code> constructor, it will override the host name passed to
70
 
 * <code>createSocket()</code>  In particular, parameterizing <code>RemotingRMIClientSocketFactory</code>
71
 
 * with localhost will allow the retrieval of objects bound to localhost.
72
 
 *
73
 
 * @author <a href="mailto:r.sigal@computer.org">Ron Sigal</a>
74
 
 * @version $Revision: 5017 $
75
 
 *          <p/>
76
 
 *          Copyright (c) 2005
77
 
 *          </p>
78
 
 */
79
 
 
80
 
public class RemotingRMIClientSocketFactory implements RMIClientSocketFactory, Serializable
81
 
{
82
 
   static final long serialVersionUID;
83
 
 
84
 
   protected static Logger log = Logger.getLogger(RemotingRMIClientSocketFactory.class);
85
 
   protected static Map configMaps = Collections.synchronizedMap(new HashMap());
86
 
   protected static Map socketFactories = Collections.synchronizedMap(new HashMap());
87
 
 
88
 
   protected Map configuration;
89
 
   protected InvokerLocator invokerLocator;
90
 
   
91
 
   transient protected SocketFactory socketFactory;
92
 
   private int timeout = 60000;
93
 
 
94
 
   // The commented code below is from an attempt to incorporate a <code>java.lang.reflect.Constructor</code>
95
 
   // parameter to provide a very general way to create sockets.  The problem is that
96
 
   // <code>Constructor</code> does not implement <code>Serializable</code>, which is necessary to
97
 
   // allow the <code>RemotingRMIClientSocketFactory</code> to be transmitted to the client.  The
98
 
   // code is left in place because it could be resurrected by passing in a class name and parameter
99
 
   // types to specify a constructor.  Fortunately, <code>java.lang.Class</code> does implement
100
 
   // <code>Serializable</code>.
101
 
 
102
 
//   private Constructor constructor;
103
 
//   private Object[] args;
104
 
//   private int hostPosition;
105
 
//   private int portPosition;
106
 
 
107
 
   protected String hostName;
108
 
 
109
 
   static
110
 
   {
111
 
      if(Version.getDefaultVersion() == Version.VERSION_1)
112
 
      {
113
 
         serialVersionUID = -7491556589517716155L;
114
 
      }
115
 
      else
116
 
      {
117
 
         serialVersionUID = -3039839695840773968L;
118
 
      }
119
 
   }
120
 
 
121
 
   
122
 
   public static void addLocalConfiguration(InvokerLocator invokerLocator, Map localConfig)
123
 
   {
124
 
      log.debug("adding local configuration for: " + invokerLocator);
125
 
      configMaps.put(new ComparableHolder(invokerLocator), localConfig);
126
 
   }
127
 
 
128
 
   
129
 
   public static void removeLocalConfiguration(InvokerLocator invokerLocator)
130
 
   {
131
 
      log.debug("removing local configuration for: " + invokerLocator);
132
 
      Object o = configMaps.remove(new ComparableHolder(invokerLocator));
133
 
      if (o == null) log.warn("trying to delete unknown key: " + invokerLocator);
134
 
      ComparableHolder holder = new ComparableHolder(invokerLocator);
135
 
      socketFactories.remove(holder);
136
 
   }
137
 
   
138
 
 
139
 
   /**
140
 
    * @param locator
141
 
    * @param hostName
142
 
    * @param timeout
143
 
    * @param config
144
 
    */
145
 
   public RemotingRMIClientSocketFactory(InvokerLocator locator,
146
 
                                         String hostName,
147
 
                                         int timeout,
148
 
                                         Map config)
149
 
   {
150
 
      this.invokerLocator = locator;
151
 
      this.hostName = hostName;
152
 
      this.timeout = timeout;
153
 
      this.configuration = new HashMap(config);
154
 
   }
155
 
 
156
 
 
157
 
//   public RemotingRMISocketFactory(Constructor constructor, Object[] args, int hostPosition, int portPosition)
158
 
//   throws ClassNotFoundException, NoSuchMethodException
159
 
//   {
160
 
//      this.constructor = constructor;
161
 
//      this.args = args;
162
 
//      this.portPosition = portPosition;
163
 
//   }
164
 
 
165
 
 
166
 
   /**
167
 
    * Creates a new socket.  If a <code>SocketFactory</code> was passed to the constructor, it will be used.
168
 
    * Otherwise, a <code>java.net.Socket</code> will be created by default.
169
 
    *
170
 
    * @param host host to which socket should be connected
171
 
    * @param port port to which socket should be connected
172
 
    * @return new socket
173
 
    * @throws IOException if there is a problem creating a socket
174
 
    */
175
 
   public Socket createSocket(String host, final int port) throws IOException
176
 
   {
177
 
      // If invokerLocator isn't in configMaps, an RMICLientInvoker has not been created
178
 
      // yet.  This call was probably made by an RMI thread, and is premature.  Best attempt
179
 
      // is to return a vanilla socket.
180
 
 
181
 
      final String effectiveHost = hostName != null ? hostName : host;
182
 
      if (log.isTraceEnabled())
183
 
         log.trace("host: " + host + ", effective host: " + effectiveHost + ", port: " + port);
184
 
      
185
 
      if (invokerLocator != null)
186
 
      {
187
 
         ComparableHolder holder = new ComparableHolder(invokerLocator);
188
 
         if (!configMaps.containsKey(holder))
189
 
         {
190
 
            if (Thread.currentThread().getName().indexOf("RMI") >= 0)
191
 
            {
192
 
               log.debug("unrecognized invoker locator: " + invokerLocator);
193
 
               log.debug("unable to retrieve socket factory: returning plain socket");
194
 
            }
195
 
            else
196
 
            {
197
 
               log.warn(Thread.currentThread().getName() + " unrecognized invoker locator: " + invokerLocator);
198
 
               log.warn("unable to retrieve socket factory: returning plain socket");
199
 
            }
200
 
         
201
 
            return createSocketPrivate(effectiveHost, port);
202
 
         }
203
 
         
204
 
         socketFactory = retrieveSocketFactory(holder);
205
 
      }
206
 
 
207
 
      Socket socket = null;
208
 
      if(socketFactory != null)
209
 
      {
210
 
         socket = createSocketPrivate(socketFactory, effectiveHost, port);
211
 
      }
212
 
      else
213
 
      {
214
 
         socket = createSocketPrivate(effectiveHost, port);
215
 
      }
216
 
 
217
 
      socket.setSoTimeout(timeout);
218
 
      socketFactory = null;
219
 
      return socket;
220
 
   }
221
 
 
222
 
 
223
 
   public SocketFactory retrieveSocketFactory(ComparableHolder holder)
224
 
      throws IOException
225
 
   {
226
 
      SocketFactory sf = (SocketFactory) socketFactories.get(holder);
227
 
      if (sf == null)
228
 
      {
229
 
         // We want to keep the local configuration map, which might contain a
230
 
         // SocketFactory, separate from the configuration map, which is meant
231
 
         // to contain only serializable objects.
232
 
         Map tempConfig = new HashMap(configuration);
233
 
         Map localConfig = (Map) configMaps.get(holder);
234
 
         if (localConfig != null)
235
 
            tempConfig.putAll(localConfig);
236
 
 
237
 
         if (tempConfig.containsKey(Remoting.CUSTOM_SOCKET_FACTORY))
238
 
         {
239
 
            sf = (SocketFactory) tempConfig.get(Remoting.CUSTOM_SOCKET_FACTORY);
240
 
         }
241
 
         
242
 
         if (sf == null)
243
 
         {
244
 
            sf = SocketFactory.getDefault();
245
 
            sf = AbstractInvoker.wrapSocketFactory(sf, tempConfig);
246
 
         }
247
 
         
248
 
         socketFactories.put(holder, sf);
249
 
      }
250
 
 
251
 
      return sf;
252
 
   }
253
 
   
254
 
   
255
 
   public void clear()
256
 
   {
257
 
      configuration = null;
258
 
      invokerLocator = null;
259
 
      socketFactory = null;
260
 
   }
261
 
   
262
 
 
263
 
   protected static class ComparableHolder
264
 
   {
265
 
      private String protocol;
266
 
      private InetAddress host;
267
 
      private int port;
268
 
      private int hashCode;
269
 
 
270
 
      public ComparableHolder(final InvokerLocator invokerLocator)
271
 
      {
272
 
         protocol = invokerLocator.getProtocol().toLowerCase();
273
 
         
274
 
         try
275
 
         { 
276
 
            host = getAddressByName(invokerLocator.getHost());
277
 
         }
278
 
         catch (UnknownHostException e)
279
 
         {
280
 
            log.error("unable to resolve host: " + invokerLocator.getHost(), e);
281
 
         }
282
 
         
283
 
         port = invokerLocator.getPort();
284
 
         hashCode = protocol.hashCode() * host.hashCode() * port;
285
 
      }
286
 
 
287
 
      public boolean equals(Object obj)
288
 
      {
289
 
         if (obj == null || !(obj instanceof ComparableHolder))
290
 
            return false;
291
 
 
292
 
         ComparableHolder holder = (ComparableHolder) obj;
293
 
 
294
 
         return protocol.equals(holder.protocol.toLowerCase())
295
 
                && host.equals(holder.host)
296
 
                && port == holder.port;
297
 
      }
298
 
 
299
 
      public int hashCode()
300
 
      {
301
 
         return hashCode;
302
 
      }
303
 
   }
304
 
 
305
 
   static private Socket createSocketPrivate(final String host, final int port) throws IOException
306
 
   {
307
 
      if (SecurityUtility.skipAccessControl())
308
 
      {
309
 
         return new Socket(host, port);
310
 
      }
311
 
      
312
 
      try
313
 
      {
314
 
          return (Socket)AccessController.doPrivileged( new PrivilegedExceptionAction()
315
 
          {
316
 
             public Object run() throws IOException
317
 
             {
318
 
                return new Socket(host, port);
319
 
             }
320
 
          });
321
 
      }
322
 
      catch (PrivilegedActionException e)
323
 
      {
324
 
         throw (IOException) e.getCause();
325
 
      }
326
 
   }
327
 
 
328
 
   static private Socket createSocketPrivate(final SocketFactory sf, final String host, final int port)
329
 
   throws IOException
330
 
   {
331
 
      if (SecurityUtility.skipAccessControl())
332
 
      {
333
 
         return sf.createSocket(host, port);
334
 
      }
335
 
      
336
 
      try
337
 
      {
338
 
          return (Socket)AccessController.doPrivileged( new PrivilegedExceptionAction()
339
 
          {
340
 
             public Object run() throws IOException
341
 
             {
342
 
                return sf.createSocket(host, port);
343
 
             }
344
 
          });
345
 
      }
346
 
      catch (PrivilegedActionException e)
347
 
      {
348
 
         throw (IOException) e.getCause();
349
 
      }
350
 
   }
351
 
   
352
 
   static private InetAddress getAddressByName(final String host) throws UnknownHostException
353
 
   {
354
 
      if (SecurityUtility.skipAccessControl())
355
 
      {
356
 
         return InetAddress.getByName(host);
357
 
      }
358
 
      
359
 
      try
360
 
      {
361
 
         return (InetAddress)AccessController.doPrivileged( new PrivilegedExceptionAction()
362
 
         {
363
 
            public Object run() throws IOException
364
 
            {
365
 
               return InetAddress.getByName(host);
366
 
            }
367
 
         });
368
 
      }
369
 
      catch (PrivilegedActionException e)
370
 
      {
371
 
         throw (UnknownHostException) e.getCause();
372
 
      }
373
 
   }
374
 
}
 
 
b'\\ No newline at end of file'