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

« back to all changes in this revision

Viewing changes to src/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: 5020 $
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
 
      Map params = configuration;
92
 
      if (params != null)
93
 
      {
94
 
         // look for socketTimeout param
95
 
         Object val = params.get(SO_TIMEOUT_FLAG);
96
 
         if (val != null)
97
 
         {
98
 
            try
99
 
            {
100
 
               timeout = Integer.valueOf((String) val).intValue();;
101
 
               log.debug(this + " setting timeout to " + timeout);
102
 
            }
103
 
            catch (Exception e)
104
 
            {
105
 
               log.warn(this + " could not convert " + SO_TIMEOUT_FLAG + " value of " +
106
 
                        val + " to a int value.");
107
 
            }
108
 
         }
109
 
      }
110
 
   }
111
 
 
112
 
   protected Object handleException(Exception ex, SocketWrapper socketWrapper)
113
 
         throws ClassNotFoundException, InvocationFailureException, CannotConnectException
114
 
   {
115
 
      if (ex instanceof ClassNotFoundException)
116
 
      {
117
 
         //TODO: -TME Add better exception handling for class not found exception
118
 
         log.debug("Error loading classes from remote call result.", ex);
119
 
         throw (ClassNotFoundException) ex;
120
 
      }
121
 
      
122
 
      if (ex instanceof CannotConnectException)
123
 
      {
124
 
         log.debug(this, ex);
125
 
         throw (CannotConnectException) ex;
126
 
      }
127
 
      
128
 
      if (ex instanceof SocketTimeoutException)
129
 
      {
130
 
         log.debug("Got SocketTimeoutException, exiting", ex);
131
 
         String message = "Socket timed out.  Waited " + socketWrapper.getTimeout() +
132
 
                          " milliseconds for response while calling on " + getLocator();
133
 
         throw new InvocationFailureException(message, ex);
134
 
      }
135
 
 
136
 
      if (ex instanceof InterruptedException)
137
 
      {
138
 
         log.debug(this, ex);
139
 
         throw new RuntimeException(ex);
140
 
      }
141
 
      
142
 
      throw new InvocationFailureException("Unable to perform invocation", ex);
143
 
   }
144
 
 
145
 
   /**
146
 
    * used for debugging (tracing) connections leaks
147
 
    */
148
 
   protected SocketWrapper createClientSocket(Socket socket, int timeout, Map metadata) throws Exception
149
 
   {
150
 
      if (clientSocketConstructor == null)
151
 
      {
152
 
         if(clientSocketClass == null)
153
 
         {
154
 
            clientSocketClass = ClassLoaderUtility.loadClass(getClass(), clientSocketClassName);
155
 
         }
156
 
 
157
 
         try
158
 
         {
159
 
            clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class, Map.class, Integer.class});
160
 
         }
161
 
         catch (NoSuchMethodException e)
162
 
         {
163
 
            clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class});
164
 
         }
165
 
 
166
 
      }
167
 
 
168
 
      SocketWrapper clientSocketWrapper = null;
169
 
      if (clientSocketConstructor.getParameterTypes().length == 3)
170
 
      {
171
 
         clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket, metadata, new Integer(timeout)});
172
 
      }
173
 
      else
174
 
      {
175
 
         clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket});
176
 
         clientSocketWrapper.setTimeout(timeout);
177
 
      }
178
 
 
179
 
      return clientSocketWrapper;
180
 
   }
181
 
 
182
 
 
183
 
   protected Socket createSocket(String address, int port, int timeout) throws IOException
184
 
   {
185
 
      Socket s = null;
186
 
      SocketFactory socketFactory = getSocketFactory();
187
 
      if (socketFactory != null)
188
 
      {
189
 
         s = socketFactory.createSocket();
190
 
      }
191
 
      else
192
 
      {
193
 
          s = new Socket();
194
 
      }
195
 
 
196
 
      configureSocket(s);
197
 
      InetSocketAddress inetAddr = new InetSocketAddress(address, port);
198
 
      
199
 
      if (timeout < 0)
200
 
      {
201
 
         timeout = getTimeout();
202
 
         if (timeout < 0)
203
 
            timeout = 0;
204
 
      }
205
 
 
206
 
      connect(s, inetAddr, timeout);
207
 
      return s;
208
 
   }
209
 
 
210
 
   protected SocketWrapper getPooledConnection()
211
 
   {
212
 
      SocketWrapper socketWrapper = null;
213
 
      while (pool.size() > 0)
214
 
      {
215
 
         socketWrapper = (SocketWrapper) pool.removeFirst();
216
 
         try
217
 
         {
218
 
            if (socketWrapper != null)
219
 
            {
220
 
               if (socketWrapper instanceof OpenConnectionChecker)
221
 
               {
222
 
                  ((OpenConnectionChecker) socketWrapper).checkOpenConnection();
223
 
               }
224
 
               if (shouldCheckConnection)
225
 
               {
226
 
                  socketWrapper.checkConnection();
227
 
                  return socketWrapper;
228
 
               }
229
 
               else
230
 
               {
231
 
                  if (socketWrapper.getSocket().isConnected())
232
 
                  {
233
 
                     return socketWrapper;
234
 
                  }
235
 
                  else
236
 
                  {
237
 
                     try
238
 
                     {
239
 
                        socketWrapper.close();
240
 
                     }
241
 
                     catch (IOException e)
242
 
                     {
243
 
                     }
244
 
                     return null;
245
 
                  }
246
 
               }
247
 
            }
248
 
         }
249
 
         catch (Exception ex)
250
 
         {
251
 
            if (isTraceEnabled)
252
 
            {
253
 
               log.trace("Couldn't reuse connection from pool", ex);
254
 
            }
255
 
            try
256
 
            {
257
 
               socketWrapper.close();
258
 
            }
259
 
            catch (Exception ignored)
260
 
            {
261
 
            }
262
 
         }
263
 
      }
264
 
      return null;
265
 
   }
266
 
 
267
 
 
268
 
   /**
269
 
    * Getter for property timeout
270
 
    *
271
 
    * @return Value of property timeout
272
 
    */
273
 
   public int getTimeout()
274
 
   {
275
 
      return timeout;
276
 
   }
277
 
 
278
 
   public String toString()
279
 
   {
280
 
      return "SocketClientInvoker[" + Integer.toHexString(System.identityHashCode(this)) + ", " +
281
 
         locator.getProtocol() + "://" + locator.getHost() + ":" + locator.getPort() + "]";
282
 
   }
283
 
   
284
 
   static private void connect(final Socket socket, final InetSocketAddress address, final int timeout)
285
 
   throws IOException
286
 
   {
287
 
      if (SecurityUtility.skipAccessControl())
288
 
      {
289
 
         socket.connect(address, timeout);
290
 
         return;
291
 
      }
292
 
      
293
 
      try
294
 
      {
295
 
         AccessController.doPrivileged( new PrivilegedExceptionAction()
296
 
         {
297
 
            public Object run() throws Exception
298
 
            {
299
 
               socket.connect(address, timeout);
300
 
               return null;
301
 
            }
302
 
         });
303
 
      }
304
 
      catch (PrivilegedActionException e)
305
 
      {
306
 
         throw (IOException) e.getCause();
307
 
      }   
308
 
   }
309
 
}