package com.googlecode.jmxtrans.util;

import java.net.InetSocketAddress;
import java.net.Socket;

import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Allows us to pool socket connections.
 */
public class SocketFactory extends BaseKeyedPoolableObjectFactory {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SocketFactory.class);

	/** constructor */
	public SocketFactory() {
	}

	/**
	 * Creates the socket and the writer to go with it.
	 */
	@Override
	public Object makeObject(Object key) throws Exception {
		InetSocketAddress details = (InetSocketAddress) key;
		Socket socket = new Socket(details.getHostName(), details.getPort());
		socket.setKeepAlive(true);
		return socket;
	}

	/**
	 * Closes the socket.
	 */
	@Override
	public void destroyObject(Object key, Object obj) throws Exception {
		Socket socket = (Socket) obj;
		socket.close();
	}

	/**
	 * Validates that the socket is good.
	 */
	@Override
	public boolean validateObject(Object key, Object obj) {
		Socket socket = (Socket) obj;
		return socket.isBound() && !socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && !socket.isOutputShutdown();
	}
}

@@ -1117,16 +1117,218 @@
 t) obj;%0A
+%09%09try %7B%0A%09%09%09socket.setSoTimeout(100);%0A%09%09%09if (socket.getInputStream().read() == -1) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D catch (java.net.SocketTimeoutException e) %7B%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09return false;%0A%09%09%7D%0A
 %09%09return

make socket dead detection work