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: 5020 $
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();
91
Map params = configuration;
94
// look for socketTimeout param
95
Object val = params.get(SO_TIMEOUT_FLAG);
100
timeout = Integer.valueOf((String) val).intValue();;
101
log.debug(this + " setting timeout to " + timeout);
105
log.warn(this + " could not convert " + SO_TIMEOUT_FLAG + " value of " +
106
val + " to a int value.");
112
protected Object handleException(Exception ex, SocketWrapper socketWrapper)
113
throws ClassNotFoundException, InvocationFailureException, CannotConnectException
115
if (ex instanceof ClassNotFoundException)
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;
122
if (ex instanceof CannotConnectException)
125
throw (CannotConnectException) ex;
128
if (ex instanceof SocketTimeoutException)
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);
136
if (ex instanceof InterruptedException)
139
throw new RuntimeException(ex);
142
throw new InvocationFailureException("Unable to perform invocation", ex);
146
* used for debugging (tracing) connections leaks
148
protected SocketWrapper createClientSocket(Socket socket, int timeout, Map metadata) throws Exception
150
if (clientSocketConstructor == null)
152
if(clientSocketClass == null)
154
clientSocketClass = ClassLoaderUtility.loadClass(getClass(), clientSocketClassName);
159
clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class, Map.class, Integer.class});
161
catch (NoSuchMethodException e)
163
clientSocketConstructor = clientSocketClass.getConstructor(new Class[]{Socket.class});
168
SocketWrapper clientSocketWrapper = null;
169
if (clientSocketConstructor.getParameterTypes().length == 3)
171
clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket, metadata, new Integer(timeout)});
175
clientSocketWrapper = (SocketWrapper) clientSocketConstructor.newInstance(new Object[]{socket});
176
clientSocketWrapper.setTimeout(timeout);
179
return clientSocketWrapper;
183
protected Socket createSocket(String address, int port, int timeout) throws IOException
186
SocketFactory socketFactory = getSocketFactory();
187
if (socketFactory != null)
189
s = socketFactory.createSocket();
197
InetSocketAddress inetAddr = new InetSocketAddress(address, port);
201
timeout = getTimeout();
206
connect(s, inetAddr, timeout);
210
protected SocketWrapper getPooledConnection()
212
SocketWrapper socketWrapper = null;
213
while (pool.size() > 0)
215
socketWrapper = (SocketWrapper) pool.removeFirst();
218
if (socketWrapper != null)
220
if (socketWrapper instanceof OpenConnectionChecker)
222
((OpenConnectionChecker) socketWrapper).checkOpenConnection();
224
if (shouldCheckConnection)
226
socketWrapper.checkConnection();
227
return socketWrapper;
231
if (socketWrapper.getSocket().isConnected())
233
return socketWrapper;
239
socketWrapper.close();
241
catch (IOException e)
253
log.trace("Couldn't reuse connection from pool", ex);
257
socketWrapper.close();
259
catch (Exception ignored)
269
* Getter for property timeout
271
* @return Value of property timeout
273
public int getTimeout()
278
public String toString()
280
return "SocketClientInvoker[" + Integer.toHexString(System.identityHashCode(this)) + ", " +
281
locator.getProtocol() + "://" + locator.getHost() + ":" + locator.getPort() + "]";
284
static private void connect(final Socket socket, final InetSocketAddress address, final int timeout)
287
if (SecurityUtility.skipAccessControl())
289
socket.connect(address, timeout);
295
AccessController.doPrivileged( new PrivilegedExceptionAction()
297
public Object run() throws Exception
299
socket.connect(address, timeout);
304
catch (PrivilegedActionException e)
306
throw (IOException) e.getCause();