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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/transport/socket/SocketClientInvoker.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.socket;
 
24
 
 
25
import org.jboss.logging.Logger;
 
26
import org.jboss.remoting.CannotConnectException;
 
27
import org.jboss.remoting.InvocationFailureException;
 
28
import org.jboss.remoting.InvokerLocator;
 
29
import org.jboss.remoting.serialization.ClassLoaderUtility;
 
30
import org.jboss.remoting.util.SecurityUtility;
 
31
 
 
32
import javax.net.SocketFactory;
 
33
import java.io.IOException;
 
34
import java.lang.reflect.Constructor;
 
35
import java.net.InetAddress;
 
36
import java.net.Socket;
 
37
import java.net.SocketTimeoutException;
 
38
import java.net.InetSocketAddress;
 
39
import java.security.AccessController;
 
40
import java.security.PrivilegedActionException;
 
41
import java.security.PrivilegedExceptionAction;
 
42
import java.util.Map;
 
43
 
 
44
/**
 
45
 * SocketClientInvoker uses Sockets to remotely connect to the a remote ServerInvoker, which
 
46
 * must be a SocketServerInvoker.
 
47
 *
 
48
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 
49
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
50
 * @version $Revision: 5739 $
 
51
 */
 
52
public class SocketClientInvoker extends MicroSocketClientInvoker
 
53
{
 
54
   private static final Logger log = Logger.getLogger(SocketClientInvoker.class);
 
55
   private static final boolean isTraceEnabled = log.isTraceEnabled();
 
56
 
 
57
   public static final String SO_TIMEOUT_FLAG = "timeout";
 
58
 
 
59
   /**
 
60
    * Default value for socket timeout is 30 minutes.
 
61
    */
 
62
   public static final int SO_TIMEOUT_DEFAULT = 1800000;
 
63
 
 
64
   protected int timeout = SO_TIMEOUT_DEFAULT;
 
65
 
 
66
   private Constructor clientSocketConstructor = null;
 
67
 
 
68
   /**
 
69
    * Set number of retries in getSocket method
 
70
    */
 
71
   public SocketClientInvoker(InvokerLocator locator)
 
72
   {
 
73
      this(locator, null);
 
74
   }
 
75
 
 
76
   public SocketClientInvoker(InvokerLocator locator, Map configuration)
 
77
   {
 
78
      super(locator, configuration);
 
79
      configureParameters();
 
80
   }
 
81
 
 
82
   protected ServerAddress createServerAddress(InetAddress addr, int port)
 
83
   {
 
84
      return new ServerAddress(addr.getHostAddress(), port, enableTcpNoDelay, timeout, maxPoolSize);
 
85
   }
 
86
 
 
87
 
 
88
   protected void configureParameters()
 
89
   {
 
90
      super.configureParameters();
 
91
      
 
92
      // For JBREM-1188
 
93
      timeout = SO_TIMEOUT_DEFAULT;
 
94
      
 
95
      Map params = configuration;
 
96
      if (params != null)
 
97
      {
 
98
         // look for socketTimeout param
 
99
         Object val = params.get(SO_TIMEOUT_FLAG);
 
100
         if (val != null)
 
101
         {
 
102
            try
 
103
            {
 
104
               timeout = Integer.valueOf((String) val).intValue();;
 
105
               log.debug(this + " setting timeout to " + timeout);
 
106
            }
 
107
            catch (Exception e)
 
108
            {
 
109
               log.warn(this + " could not convert " + SO_TIMEOUT_FLAG + " value of " +
 
110
                        val + " to a int value.");
 
111
            }
 
112
         }
 
113
      }
 
114
   }
 
115
 
 
116
   protected Object handleException(Exception ex, SocketWrapper socketWrapper)
 
117
         throws ClassNotFoundException, InvocationFailureException, CannotConnectException
 
118
   {
 
119
      if (ex instanceof ClassNotFoundException)
 
120
      {
 
121
         //TODO: -TME Add better exception handling for class not found exception
 
122
         log.debug("Error loading classes from remote call result.", ex);
 
123
         throw (ClassNotFoundException) ex;
 
124
      }
 
125
      
 
126
      if (ex instanceof CannotConnectException)
 
127
      {
 
128
         log.debug(this, ex);
 
129
         throw (CannotConnectException) ex;
 
130
      }
 
131
      
 
132
      if (ex instanceof SocketTimeoutException)
 
133
      {
 
134
         log.debug("Got SocketTimeoutException, exiting", ex);
 
135
         String message = "Socket timed out.  Waited " + socketWrapper.getTimeout() +
 
136
                          " milliseconds for response while calling on " + getLocator();
 
137
         throw new InvocationFailureException(message, ex);
 
138
      }
 
139
 
 
140
      if (ex instanceof InterruptedException)
 
141
      {
 
142
         log.debug(this, ex);
 
143
         throw new RuntimeException(ex);
 
144
      }
 
145
      
 
146
      throw new InvocationFailureException("Unable to perform invocation", ex);
 
147
   }
 
148
 
 
149
   /**
 
150
    * used for debugging (tracing) connections leaks
 
151
    */
 
152
   protected SocketWrapper createClientSocket(Socket socket, int timeout, Map metadata) throws Exception
 
153
   {
 
154
      if (clientSocketConstructor == null)
 
155
      {
 
156
         if(clientSocketClass == null)
 
157
         {
 
158
            clientSocketClass = ClassLoaderUtility.loadClass(getClass(), clientSocketClassName);
 
159
         }
 
160
 
 
161
         try
 
162
         {
 
163
            clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class, Map.class, Integer.class});
 
164
         }
 
165
         catch (NoSuchMethodException e)
 
166
         {
 
167
            clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class});
 
168
         }
 
169
 
 
170
      }
 
171
 
 
172
      SocketWrapper clientSocketWrapper = null;
 
173
      if (clientSocketConstructor.getParameterTypes().length == 3)
 
174
      {
 
175
         clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket, metadata, new Integer(timeout)});
 
176
      }
 
177
      else
 
178
      {
 
179
         clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket});
 
180
         clientSocketWrapper.setTimeout(timeout);
 
181
      }
 
182
 
 
183
      return clientSocketWrapper;
 
184
   }
 
185
 
 
186
 
 
187
   protected Socket createSocket(String address, int port, int timeout) throws IOException
 
188
   {
 
189
      Socket s = null;
 
190
      SocketFactory socketFactory = getSocketFactory();
 
191
      if (socketFactory != null)
 
192
      {
 
193
         s = socketFactory.createSocket();
 
194
      }
 
195
      else
 
196
      {
 
197
          s = new Socket();
 
198
      }
 
199
 
 
200
      configureSocket(s);
 
201
      InetSocketAddress inetAddr = new InetSocketAddress(address, port);
 
202
      
 
203
      if (timeout < 0)
 
204
      {
 
205
         timeout = getTimeout();
 
206
         if (timeout < 0)
 
207
            timeout = 0;
 
208
      }
 
209
 
 
210
      connect(s, inetAddr, timeout);
 
211
      return s;
 
212
   }
 
213
 
 
214
   protected SocketWrapper getPooledConnection()
 
215
   {
 
216
      SocketWrapper socketWrapper = null;
 
217
      while (pool.size() > 0)
 
218
      {
 
219
         socketWrapper = (SocketWrapper) pool.removeFirst();
 
220
         try
 
221
         {
 
222
            if (socketWrapper != null)
 
223
            {
 
224
               if (socketWrapper instanceof OpenConnectionChecker)
 
225
               {
 
226
                  ((OpenConnectionChecker) socketWrapper).checkOpenConnection();
 
227
               }
 
228
               if (shouldCheckConnection)
 
229
               {
 
230
                  socketWrapper.checkConnection();
 
231
                  return socketWrapper;
 
232
               }
 
233
               else
 
234
               {
 
235
                  if (socketWrapper.getSocket().isConnected())
 
236
                  {
 
237
                     return socketWrapper;
 
238
                  }
 
239
                  else
 
240
                  {
 
241
                     try
 
242
                     {
 
243
                        socketWrapper.close();
 
244
                     }
 
245
                     catch (IOException e)
 
246
                     {
 
247
                     }
 
248
                     return null;
 
249
                  }
 
250
               }
 
251
            }
 
252
         }
 
253
         catch (Exception ex)
 
254
         {
 
255
            if (isTraceEnabled)
 
256
            {
 
257
               log.trace("Couldn't reuse connection from pool", ex);
 
258
            }
 
259
            try
 
260
            {
 
261
               socketWrapper.close();
 
262
            }
 
263
            catch (Exception ignored)
 
264
            {
 
265
            }
 
266
         }
 
267
      }
 
268
      return null;
 
269
   }
 
270
 
 
271
 
 
272
   /**
 
273
    * Getter for property timeout
 
274
    *
 
275
    * @return Value of property timeout
 
276
    */
 
277
   public int getTimeout()
 
278
   {
 
279
      return timeout;
 
280
   }
 
281
 
 
282
   public String toString()
 
283
   {
 
284
      return "SocketClientInvoker[" + Integer.toHexString(System.identityHashCode(this)) + ", " +
 
285
         locator.getProtocol() + "://" + locator.getHost() + ":" + locator.getPort() + "]";
 
286
   }
 
287
   
 
288
   static private void connect(final Socket socket, final InetSocketAddress address, final int timeout)
 
289
   throws IOException
 
290
   {
 
291
      if (SecurityUtility.skipAccessControl())
 
292
      {
 
293
         socket.connect(address, timeout);
 
294
         return;
 
295
      }
 
296
      
 
297
      try
 
298
      {
 
299
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
300
         {
 
301
            public Object run() throws Exception
 
302
            {
 
303
               socket.connect(address, timeout);
 
304
               return null;
 
305
            }
 
306
         });
 
307
      }
 
308
      catch (PrivilegedActionException e)
 
309
      {
 
310
         throw (IOException) e.getCause();
 
311
      }   
 
312
   }
 
313
}