FileDocCategorySizeDatePackage
ClientCommunicatorAdmin.javaAPI DocJava SE 5 API5226Fri Aug 26 14:55:00 BST 2005com.sun.jmx.remote.internal

ClientCommunicatorAdmin

public abstract class ClientCommunicatorAdmin extends Object

Fields Summary
private final Checker
checker
private long
period
private static final int
CONNECTED
private static final int
RE_CONNECTING
private static final int
FAILED
private static final int
TERMINATED
private int
state
private final int[]
lock
private static final ClassLogger
logger
Constructors Summary
public ClientCommunicatorAdmin(long period)

	this.period = period;

	if (period > 0) {
	    checker = new Checker();

	    Thread t = new Thread(checker);
	    t.setDaemon(true);
	    t.start();
	} else
	    checker = null;
    
Methods Summary
protected abstract voidcheckConnection()
Called by this class to check a client connection.

protected abstract voiddoStart()
Tells a client to re-start again.

protected abstract voiddoStop()
Tells a client to stop becaue failing to call checkConnection.

public voidgotIOException(java.io.IOException ioe)
Called by a client to inform of getting an IOException.

	restart(ioe);
    
private voidrestart(java.io.IOException ioe)

	// check state
	synchronized(lock) {
	    if (state == TERMINATED) {
		throw new IOException("The client has been closed.");
	    } else if (state == FAILED) { // already failed to re-start by another thread
		throw ioe;
	    } else if (state == RE_CONNECTING) {
		// restart process has been called by another thread
		// we need to wait
		while(state == RE_CONNECTING) {
		    try {
			lock.wait();
		    } catch (InterruptedException ire) {
			// be asked to give up
			InterruptedIOException iioe = new InterruptedIOException(ire.toString());
			EnvHelp.initCause(iioe, ire);

			throw iioe;
		    }
		}

		if (state == TERMINATED) {
		    throw new IOException("The client has been closed.");
		} else if (state != CONNECTED) {
		    // restarted is failed by another thread
		    throw ioe;
		}
	    } else {
		state = RE_CONNECTING;
		lock.notifyAll();
	    }
	}

	// re-starting
	try {
	    doStart();
	    synchronized(lock) {
		if (state == TERMINATED) {
		    throw new IOException("The client has been closed.");
		}

		state = CONNECTED;

		lock.notifyAll();
	    }

	    return;
	} catch (Exception e) {
	    logger.warning("restart", "Failed to restart: " + e);
	    logger.debug("restart",e);

	    synchronized(lock) {
		if (state == TERMINATED) {
		    throw new IOException("The client has been closed.");
		}

		state = FAILED;

		lock.notifyAll();
	    }

	    try {
		doStop();
	    } catch (Exception eee) {
		// OK.
		// We know there is a problem.
	    }

	    terminate();

	    throw ioe;
	}
    
public voidterminate()
Terminates this object.

	synchronized(lock) {
	    if (state == TERMINATED) {
		return;
	    }

	    state = TERMINATED;

	    lock.notifyAll();

	    if (checker != null)
		checker.stop();
	}