On Fri, Nov 10, 2006 at 12:49:33PM +0100, Roland Weber wrote:
> Hello Eugene,
> 
> > As far as I can see this exception handler can handle only exceptions 
> derived
> > from IOEXception - but not java.net.SocketException etc?
> SocketException extends IOException.
> What would be etc?

Really, I missed this point, thanks ;)

> > Just to make it clear - should I set ReflectionSocketFactory.java to
> > HttpClient, or I need to write my own implementation?
> 
> You should write your own. ReflectionSocketFactory is a very
> complicated way to call JDK 1.4 specific methods while still
> compiling against a 1.3 API. But have a look at the comments there.

Okay. I wrote this implementation:

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.log4j.Logger;

public class PlainSocketFactory implements ProtocolSocketFactory {

    private static final Logger log = Logger
            .getLogger(PlainSocketFactory.class);

    private int maxTries;

    /**
     * @param maximum number of tries for getting connection before giving up
     */
    public PlainSocketFactory(int maxTries) {
        this.maxTries = maxTries;
    }

    /**
     [EMAIL PROTECTED] 
org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
 int)
     */
    public Socket createSocket(String host, int port) throws IOException,
            UnknownHostException {
        Socket socket = null;
        int localTries = maxTries;
        SocketException lastException = null;
        while (--localTries > 0)
            try {
                socket = new Socket(host, port);
                maxTries = 0;
                lastException = null;
            } catch (SocketException se) {
                lastException = se;
                log.error(se.getMessage(), se);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    log.error(e, e);
                }
            }
        if (lastException != null)
            throw lastException;
        return socket;
    }

    /**
     [EMAIL PROTECTED] 
org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
 int, java.net.InetAddress, int)
     */
    public Socket createSocket(String host, int port, InetAddress localAddress,
            int localPort) throws IOException, UnknownHostException {
        Socket socket = null;
        int localTries = maxTries;
        SocketException lastException = null;
        while (--localTries > 0)
            try {
                socket = new Socket(host, port, localAddress, localPort);
                maxTries = 0;
                lastException = null;
            } catch (SocketException se) {
                lastException = se;
                log.error(se.getMessage(), se);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    log.error(e, e);
                }
            }
        if (lastException != null)
            throw lastException;
        return socket;
    }

    /**
     [EMAIL PROTECTED] 
org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
 int, java.net.InetAddress, int, 
org.apache.commons.httpclient.params.HttpConnectionParams)
     */
    public Socket createSocket(String host, int port, InetAddress localAddress,
            int localPort, HttpConnectionParams params) throws IOException,
            UnknownHostException, ConnectTimeoutException {
        Socket socket = createSocket(host, port, localAddress, localPort);
        if (socket != null) {
            socket.setKeepAlive(false);
            if (params.getReceiveBufferSize() >= 0)
                socket.setReceiveBufferSize(params.getReceiveBufferSize());
            if (params.getSendBufferSize() >= 0)
                socket.setSendBufferSize(params.getSendBufferSize());
            socket.setReuseAddress(true);
            if (params.getSoTimeout() >= 0)
                socket.setSoTimeout(params.getSoTimeout());
        } else
            log.error("Socket is null");
        return socket;
    }
}


It's just test implementation to see what's going on in the ground-floor. And
I found for some reason after several calls to the method createSocket
subsequent calls to this method start return NULL. After looking at the sources 
of DefaultSocketFactory I found it tries reflection helper first and if it 
returns null - it tries ControllerThreadSocketFactory. So that's why I can see 
that connection errors thrown from ControllerThreadSocketFactory - null value 
is returned by reflection helper, and unregarding to JDK version
ControllerThreadSocketFactory is used - but it becomes interesting - why 
"new Socket(...)" returns null? Is it something depending of operation system?

-- 
Eugene N Dzhurinsky

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to