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.socket;
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;
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;
45
* SocketClientInvoker uses Sockets to remotely connect to the a remote ServerInvoker, which
46
* must be a SocketServerInvoker.
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 $
52
public class SocketClientInvoker extends MicroSocketClientInvoker
54
private static final Logger log = Logger.getLogger(SocketClientInvoker.class);
55
private static final boolean isTraceEnabled = log.isTraceEnabled();
57
public static final String SO_TIMEOUT_FLAG = "timeout";
60
* Default value for socket timeout is 30 minutes.
62
public static final int SO_TIMEOUT_DEFAULT = 1800000;
64
protected int timeout = SO_TIMEOUT_DEFAULT;
66
private Constructor clientSocketConstructor = null;
69
* Set number of retries in getSocket method
71
public SocketClientInvoker(InvokerLocator locator)
76
public SocketClientInvoker(InvokerLocator locator, Map configuration)
78
super(locator, configuration);
79
configureParameters();
82
protected ServerAddress createServerAddress(InetAddress addr, int port)
84
return new ServerAddress(addr.getHostAddress(), port, enableTcpNoDelay, timeout, maxPoolSize);
88
protected void configureParameters()
90
super.configureParameters();
93
timeout = SO_TIMEOUT_DEFAULT;
95
Map params = configuration;
98
// look for socketTimeout param
99
Object val = params.get(SO_TIMEOUT_FLAG);
104
timeout = Integer.valueOf((String) val).intValue();;
105
log.debug(this + " setting timeout to " + timeout);
109
log.warn(this + " could not convert " + SO_TIMEOUT_FLAG + " value of " +
110
val + " to a int value.");
116
protected Object handleException(Exception ex, SocketWrapper socketWrapper)
117
throws ClassNotFoundException, InvocationFailureException, CannotConnectException
119
if (ex instanceof ClassNotFoundException)
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;
126
if (ex instanceof CannotConnectException)
129
throw (CannotConnectException) ex;
132
if (ex instanceof SocketTimeoutException)
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);
140
if (ex instanceof InterruptedException)
143
throw new RuntimeException(ex);
146
throw new InvocationFailureException("Unable to perform invocation", ex);
150
* used for debugging (tracing) connections leaks
152
protected SocketWrapper createClientSocket(Socket socket, int timeout, Map metadata) throws Exception
154
if (clientSocketConstructor == null)
156
if(clientSocketClass == null)
158
clientSocketClass = ClassLoaderUtility.loadClass(getClass(), clientSocketClassName);
163
clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class, Map.class, Integer.class});
165
catch (NoSuchMethodException e)
167
clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class});
172
SocketWrapper clientSocketWrapper = null;
173
if (clientSocketConstructor.getParameterTypes().length == 3)
175
clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket, metadata, new Integer(timeout)});
179
clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket});
180
clientSocketWrapper.setTimeout(timeout);
183
return clientSocketWrapper;
187
protected Socket createSocket(String address, int port, int timeout) throws IOException
190
SocketFactory socketFactory = getSocketFactory();
191
if (socketFactory != null)
193
s = socketFactory.createSocket();
201
InetSocketAddress inetAddr = new InetSocketAddress(address, port);
205
timeout = getTimeout();
210
connect(s, inetAddr, timeout);
214
protected SocketWrapper getPooledConnection()
216
SocketWrapper socketWrapper = null;
217
while (pool.size() > 0)
219
socketWrapper = (SocketWrapper) pool.removeFirst();
222
if (socketWrapper != null)
224
if (socketWrapper instanceof OpenConnectionChecker)
226
((OpenConnectionChecker) socketWrapper).checkOpenConnection();
228
if (shouldCheckConnection)
230
socketWrapper.checkConnection();
231
return socketWrapper;
235
if (socketWrapper.getSocket().isConnected())
237
return socketWrapper;
243
socketWrapper.close();
245
catch (IOException e)
257
log.trace("Couldn't reuse connection from pool", ex);
261
socketWrapper.close();
263
catch (Exception ignored)
273
* Getter for property timeout
275
* @return Value of property timeout
277
public int getTimeout()
282
public String toString()
284
return "SocketClientInvoker[" + Integer.toHexString(System.identityHashCode(this)) + ", " +
285
locator.getProtocol() + "://" + locator.getHost() + ":" + locator.getPort() + "]";
288
static private void connect(final Socket socket, final InetSocketAddress address, final int timeout)
291
if (SecurityUtility.skipAccessControl())
293
socket.connect(address, timeout);
299
AccessController.doPrivileged( new PrivilegedExceptionAction()
301
public Object run() throws Exception
303
socket.connect(address, timeout);
308
catch (PrivilegedActionException e)
310
throw (IOException) e.getCause();