atom feed1 message in net.java.dev.jtharness.commitssvn commit: r824 - trunk/code/src/com...
FromSent OnAttachments
bkur...@dev.java.netMar 24, 2008 2:37 am 
Subject:svn commit: r824 - trunk/code/src/com/sun/javatest/agent
From:bkur...@dev.java.net (bkur@dev.java.net)
Date:Mar 24, 2008 2:37:57 am
List:net.java.dev.jtharness.commits

Author: bkurotsu Date: 2008-03-24 09:37:55+0000 New Revision: 824

Modified: trunk/code/src/com/sun/javatest/agent/Agent.java trunk/code/src/com/sun/javatest/agent/SocketConnection.java

Log: Integrate Issue 35. (large diff due to removal of tabs)

Modified: trunk/code/src/com/sun/javatest/agent/Agent.java Url:
https://jtharness.dev.java.net/source/browse/jtharness/trunk/code/src/com/sun/javatest/agent/Agent.java?view=diff&rev=824&p1=trunk/code/src/com/sun/javatest/agent/Agent.java&p2=trunk/code/src/com/sun/javatest/agent/Agent.java&r1=823&r2=824 ============================================================================== --- trunk/code/src/com/sun/javatest/agent/Agent.java (original) +++ trunk/code/src/com/sun/javatest/agent/Agent.java 2008-03-24 09:37:55+0000 @@ -57,136 +57,134 @@ * @see AgentManager */

-public class Agent implements Runnable -{ +public class Agent implements Runnable { /** * An interface for observing activity on an agent. */ - public interface Observer - { - /** - * Called when an agent's run method has been entered. - * @param agent The agent being started. - * @see Agent#run - */ - void started(Agent agent); - - /** - * Called if an agent's run method has trouble accepting a connection. - * @param agent The agent trying to open a connection - * @param e The exception that occurred - * @see Agent#run - */ - void errorOpeningConnection(Agent agent, Exception e); - - /** - * Called when an agent's run method completed. Normally, the method will - * run until an error occurs, or until the thread is interrupted or stopped. - * @param agent The agent which has completed the work. - * @see Agent#run - */ - void finished(Agent agent); - - - /** - * Called when an agent has successfully opened a connection to service - * a request. - * @param agent The agent which opened the connection. - * @param c The connection which was opened. - */ - void openedConnection(Agent agent, Connection c); - - /** - * Called when an agent is about to execute a request to execute a Test
object. - * @param agent The agent about to do the work. - * @param c The connection to the client requesting the work. - * @param tag A tag identifying the work. - * @param className The name of the class to be run - * @param args Arguments for the class to be run. - */ - void execTest(Agent agent, Connection c, String tag, String className,
String[] args); - - /** - * Called when am agent is about to execute a request to execute a Command
object. - * @param agent The agent about to do the work. - * @param c The connection to the client requesting the work. - * @param tag A tag identifying the work. - * @param className The name of the class to be run - * @param args Arguments for the class to be run. - */ - void execCommand(Agent agent, Connection c, String tag, String className,
String[] args); - - /** - * Called when the agent is about to execute a request to execute a main
program. - * @param agent The agent about to do the work. - * @param c The connection to the client requesting the work. - * @param tag A tag identifying the work. - * @param className The name of the class to be run - * @param args Arguments for the class to be run. - */ - void execMain(Agent agent, Connection c, String tag, String className,
String[] args); - - /** - * Called when the agent has successfully completed a request to execute a
class. - * @param agent The agent that performed the work. - * @param c The connection to the client requesting the work. - * @param result The result status of the work - */ - void result(Agent agent, Connection c, Status result); - - /** - * Called when the agent has failed to execute a class, - * or has failed to report the results back to the agent requesting the
action, - * because an exception occurred. - * @param agent The agent that performed the work. - * @param c The connection to the client requesting the work. - * @param e The exception that occurred. - */ - void exception(Agent agent, Connection c, Throwable e); - - /** - * Called when the agent has completed all processing of the request - * that arrived on a particular connection. - * @param agent The agent that performed the work. - * @param c The connection to the client requesting the work. - */ - void completed(Agent agent, Connection c); + public interface Observer { + /** + * Called when an agent's run method has been entered. + * @param agent The agent being started. + * @see Agent#run + */ + void started(Agent agent); + + /** + * Called if an agent's run method has trouble accepting a connection. + * @param agent The agent trying to open a connection + * @param e The exception that occurred + * @see Agent#run + */ + void errorOpeningConnection(Agent agent, Exception e); + + /** + * Called when an agent's run method completed. Normally, the method
will + * run until an error occurs, or until the thread is interrupted or
stopped. + * @param agent The agent which has completed the work. + * @see Agent#run + */ + void finished(Agent agent); + + + /** + * Called when an agent has successfully opened a connection to service + * a request. + * @param agent The agent which opened the connection. + * @param c The connection which was opened. + */ + void openedConnection(Agent agent, Connection c); + + /** + * Called when an agent is about to execute a request to execute a Test
object. + * @param agent The agent about to do the work. + * @param c The connection to the client requesting the work. + * @param tag A tag identifying the work. + * @param className The name of the class to be run + * @param args Arguments for the class to be run. + */ + void execTest(Agent agent, Connection c, String tag, String className,
String[] args); + + /** + * Called when am agent is about to execute a request to execute a
Command object. + * @param agent The agent about to do the work. + * @param c The connection to the client requesting the work. + * @param tag A tag identifying the work. + * @param className The name of the class to be run + * @param args Arguments for the class to be run. + */ + void execCommand(Agent agent, Connection c, String tag, String
className, String[] args); + + /** + * Called when the agent is about to execute a request to execute a
main program. + * @param agent The agent about to do the work. + * @param c The connection to the client requesting the work. + * @param tag A tag identifying the work. + * @param className The name of the class to be run + * @param args Arguments for the class to be run. + */ + void execMain(Agent agent, Connection c, String tag, String className,
String[] args); + + /** + * Called when the agent has successfully completed a request to
execute a class. + * @param agent The agent that performed the work. + * @param c The connection to the client requesting the work. + * @param result The result status of the work + */ + void result(Agent agent, Connection c, Status result); + + /** + * Called when the agent has failed to execute a class, + * or has failed to report the results back to the agent requesting the
action, + * because an exception occurred. + * @param agent The agent that performed the work. + * @param c The connection to the client requesting the work. + * @param e The exception that occurred. + */ + void exception(Agent agent, Connection c, Throwable e); + + /** + * Called when the agent has completed all processing of the request + * that arrived on a particular connection. + * @param agent The agent that performed the work. + * @param c The connection to the client requesting the work. + */ + void completed(Agent agent, Connection c); } - + /** * Create an agent that connects to clients using a specified connection
factory. * @param connectionFactory The factory from which to get connections to
clients. * @param concurrency The number of simultaneous requests to be accepted. */ public Agent(ConnectionFactory connectionFactory, int concurrency) { - if (concurrency <= 0) - throw new IllegalArgumentException("bad concurrency: " + concurrency); - - this.connectionFactory = connectionFactory; - maxThreads = concurrency; + if (concurrency <= 0) + throw new IllegalArgumentException("bad concurrency: " +
concurrency); + + this.connectionFactory = connectionFactory; + maxThreads = concurrency; } - + /** * Set the delay to wait after failing to open a connection amd before
trying again. * @param delay The number of seconds to wait before attempting to open a
new connection. * @see #getRetryDelay */ public void setRetryDelay(int delay) { - if (delay <= 0) - throw new IllegalArgumentException("invalid delay"); - - retryDelay = delay; + if (delay <= 0) + throw new IllegalArgumentException("invalid delay"); + + retryDelay = delay; } - + /** * Get the delay to wait after failing to open a connection and before
trying again. * @return the number of seconds to wait before attempting to open a new
connection. * @see #setRetryDelay */ public int getRetryDelay() { - return retryDelay; + return retryDelay; } - + /** * Set the translation map to be used to localize incoming requests. * If an incoming request permits it, occurrences of certain substrings @@ -197,127 +195,122 @@ * @param map The translation map to be used. */ public synchronized void setMap(Map map) { - this.map = map; - if (tracing) { - if (map == null) - traceOut.println("set map null"); - else { - traceOut.println("set map:"); - for (Enumeration e = map.enumerate(); e.hasMoreElements(); ) { - String[] entry = (String[])(e.nextElement()); - traceOut.println("map-from: " + entry[0]); - traceOut.println("map-to: " + entry[1]); - } - traceOut.println("end of map"); - } - } + this.map = map; + if (tracing) { + if (map == null) + traceOut.println("set map null"); + else { + traceOut.println("set map:"); + for (Enumeration e = map.enumerate(); e.hasMoreElements(); ) { + String[] entry = (String[])(e.nextElement()); + traceOut.println("map-from: " + entry[0]); + traceOut.println("map-to: " + entry[1]); + } + traceOut.println("end of map"); + } + } } - + //-------------------------------------------------------------------------- - + /** * Add an observer to monitor the progress of the TestFinder. * @param o the observer */ public void addObserver(Observer o) { - notifier.addObserver(o); + notifier.addObserver(o); } - + /** * Remove an observer form the set currently monitoring the progress * of the TestFinder. * @param o the observer */ public void removeObserver(Observer o) { - notifier.removeObserver(o); + notifier.removeObserver(o); } - + //-------------------------------------------------------------------------- - + /** * Run the agent. Since an Agent is {@link Runnable runnable}, this method * will typically be called on a separate thread. */ public synchronized void run() { - if (mainThread != null) - throw new IllegalStateException("Agent already running"); - - mainThread = Thread.currentThread(); - - timer = new Timer(); - closing = false; - - try { - if (tracing) - traceOut.println("AGENT STARTED, maxThreads=" + maxThreads); - - notifier.started(); - - if (maxThreads <= 0) - // self defense: stops infinite wait, but the test should - // have already been done in the constructor and an argument - // thrown. - return; - - - while (!closing) { - while (threads.size() < maxThreads && !closing) { - Thread t = new Thread(new Runnable() { - public void run() { - Thread curr = Thread.currentThread(); - if (tracing) - traceOut.println("THREAD " + curr.getName() + " STARTED " +
getClass().getName()); - - try { - handleRequestsUntilClosed(); - } - catch (InterruptedException e) { - } - finally { - synchronized (Agent.this) { - threads.removeElement(curr); - Agent.this.notifyAll(); - } - if (tracing) - traceOut.println("THREAD " + curr.getName() + " EXITING"); - } - } - }); - t.setName("Agent" + nextThreadNum()); - int currPrio = Thread.currentThread().getPriority(); - int slvPrio = ((currPrio + Thread.MIN_PRIORITY) / 2); - t.setPriority(slvPrio); - t.start(); - threads.addElement(t); - } - wait(); - } - } - catch (InterruptedException e) { - try { - close(); - } - catch (InterruptedException ignore) { - } - } - finally { - timer.finished(); - notifier.finished(); - if (tracing) - traceOut.println("AGENT EXITING"); - mainThread = null; - } + if (mainThread != null) + throw new IllegalStateException("Agent already running"); + + mainThread = Thread.currentThread(); + + timer = new Timer(); + closing = false; + + try { + if (tracing) + traceOut.println("AGENT STARTED, maxThreads=" + maxThreads); + + notifier.started(); + + if (maxThreads <= 0) + // self defense: stops infinite wait, but the test should + // have already been done in the constructor and an argument + // thrown. + return; + + + while (!closing) { + while (threads.size() < maxThreads && !closing) { + Thread t = new Thread(new Runnable() { + public void run() { + Thread curr = Thread.currentThread(); + if (tracing) + traceOut.println("THREAD " + curr.getName() + "
STARTED " + getClass().getName()); + + try { + handleRequestsUntilClosed(); + } catch (InterruptedException e) { + } finally { + synchronized (Agent.this) { + threads.removeElement(curr); + Agent.this.notifyAll(); + } + if (tracing) + traceOut.println("THREAD " + curr.getName()
+ " EXITING"); + } + } + }); + t.setName("Agent" + nextThreadNum()); + int currPrio = Thread.currentThread().getPriority(); + int slvPrio = ((currPrio + Thread.MIN_PRIORITY) / 2); + t.setPriority(slvPrio); + t.start(); + threads.addElement(t); + } + wait(); + } + } catch (InterruptedException e) { + try { + close(); + } catch (InterruptedException ignore) { + } + } finally { + timer.finished(); + notifier.finished(); + if (tracing) + traceOut.println("AGENT EXITING"); + mainThread = null; + } } - + /** * Interrupt this agent. The thread running the {@link #run run} method * will be interrupted. */ public synchronized void interrupt() { - if (mainThread != null) - mainThread.interrupt(); + if (mainThread != null) + mainThread.interrupt(); } - + /** * Close this agent. Any requests in progress will be allowed to complete, * and no new requests will be accepted. @@ -325,207 +318,202 @@ * for outstanding requests to complete. */ public synchronized void close() throws InterruptedException { - closing = true; // will prevent new tasks from being created - - // interrupt any threads that are running - for (int i = 0; i < threads.size(); i++) { - Thread t = (Thread)(threads.elementAt(i)); - if (tracing) - traceOut.println("INTERRUPTING THREAD " + t.getName()); - t.interrupt(); - } - + closing = true; // will prevent new tasks from being created + + // interrupt any threads that are running + for (int i = 0; i < threads.size(); i++) { + Thread t = (Thread)(threads.elementAt(i)); + if (tracing) + traceOut.println("INTERRUPTING THREAD " + t.getName()); + t.interrupt(); + } + // wait 3s for shutdown traceOut.println("WAITING 3s FOR THREADS TO CLEANUP"); Thread.currentThread().sleep(3000); - - // close any tasks that are running - for (int i = 0; i < tasks.size(); i++) { - Task t = (Task)(tasks.elementAt(i)); - if (tracing) { - Connection c = t.connection; // maybe null; if it is, task is already closing - traceOut.println("CLOSING TASK " + (c == null ? "[unknown]" : c.getName())); - } - t.close(); - } - - try { - if (tracing) - traceOut.println("CLOSING CONNECTION FACTORY"); - - connectionFactory.close(); - } - catch (ConnectionFactory.Fault ignore) { - } - - // allow main loop to exit - notifyAll(); - - if (tracing) - traceOut.println("WAITING FOR TASKS TO EXIT"); - - // wait for tasks to go away - while (tasks.size() > 0) { - wait(); - } - - if (tracing) - traceOut.println("CLOSED"); + + // close any tasks that are running + for (int i = 0; i < tasks.size(); i++) { + Task t = (Task)(tasks.elementAt(i)); + if (tracing) { + Connection c = t.connection; // maybe null; if it is, task is
already closing + traceOut.println("CLOSING TASK " + (c == null ? "[unknown]" :
c.getName())); + } + t.close(); + } + + try { + if (tracing) + traceOut.println("CLOSING CONNECTION FACTORY"); + + connectionFactory.close(); + } catch (ConnectionFactory.Fault ignore) { + } + + // allow main loop to exit + notifyAll(); + + if (tracing) + traceOut.println("WAITING FOR TASKS TO EXIT"); + + // wait for tasks to go away + while (tasks.size() > 0) { + wait(); + } + + if (tracing) + traceOut.println("CLOSED"); } - + //---------------------------------------------------------------------------- - + private void handleRequestsUntilClosed() throws InterruptedException { - while (!closing) { - try { - // The call of nextConnection() will block until a connection is - // open; this can be for an indefinite amount of time. - // Therefore we must not hold the object lock while calling this routine. - Connection connection = connectionFactory.nextConnection(); - - Task t; - synchronized (this) { - // Having opened a connection, we check that the agent has not been - // marked for shutdown before updating connection. - if (closing) { - closeIgnoreExceptions(connection); - return; - } - - t = new Task(connection); - tasks.addElement(t); - } - - try { - t.handleRequest(); - } - finally { - synchronized (this) { - tasks.removeElement(t); - } - } - } - catch (ConnectionFactory.Fault e) { - notifier.errorOpeningConnection(e.getException()); - if (tracing) - traceOut.println("THREAD " + Thread.currentThread().getName() + " " + e); - - if (e.isFatal()) { - close(); - return; - } - else { - int millis = 1000*min(5, getRetryDelay()); - Thread.currentThread().sleep(millis); - continue; - } - } - - } + while (!closing) { + try { + // The call of nextConnection() will block until a connection
is + // open; this can be for an indefinite amount of time. + // Therefore we must not hold the object lock while calling
this routine. + Connection connection = connectionFactory.nextConnection(); + + Task t; + synchronized (this) { + // Having opened a connection, we check that the agent has
not been + // marked for shutdown before updating connection. + if (closing) { + closeIgnoreExceptions(connection); + return; + } + + t = new Task(connection); + tasks.addElement(t); + } + + try { + t.handleRequest(); + } finally { + synchronized (this) { + tasks.removeElement(t); + } + } + } catch (ConnectionFactory.Fault e) { + notifier.errorOpeningConnection(e.getException()); + if (tracing) + traceOut.println("THREAD " +
Thread.currentThread().getName() + " " + e); + + if (e.isFatal()) { + close(); + return; + } else { + int millis = 1000*min(5, getRetryDelay()); + Thread.currentThread().sleep(millis); + continue; + } + } + + } }

private static void closeIgnoreExceptions(Connection c) { - try { - c.close(); - } - catch (IOException e) { - } + try { + c.close(); + } catch (IOException e) { + } } - + private static final int min(int a, int b) { - return (a < b ? a : b); + return (a < b ? a : b); } - + private class Notifier { - public synchronized void addObserver(Observer o) { - observers = (Observer[])(DynamicArray.append(observers, o)); - } - - public synchronized void removeObserver(Agent.Observer o) { - observers = (Observer[])(DynamicArray.remove(observers, o)); - } - - public synchronized void started() { - for (int i = 0; i < observers.length; i++) - observers[i].started(Agent.this); - } - - public synchronized void finished() { - for (int i = 0; i < observers.length; i++) - observers[i].finished(Agent.this); - } - - public synchronized void openedConnection(Connection connection) { - for (int i = 0; i < observers.length; i++) - observers[i].openedConnection(Agent.this, connection); - } - - public synchronized void errorOpeningConnection(Exception e) { - for (int i = 0; i < observers.length; i++) - observers[i].errorOpeningConnection(Agent.this, e); - } - - public synchronized void execTest(Connection cconnection, String tag, String
className, String[] args) { - for (int i = 0; i < observers.length; i++) - observers[i].execTest(Agent.this, cconnection, tag, className, args); - } - - public synchronized void execCommand(Connection cconnection, String tag,
String className, String[] args) { - for (int i = 0; i < observers.length; i++) - observers[i].execCommand(Agent.this, cconnection, tag, className, args); - } - - public synchronized void execMain(Connection connection, String tag, String
className, String[] args) { - for (int i = 0; i < observers.length; i++) - observers[i].execMain(Agent.this, connection, tag, className, args); - } - - public synchronized void result(Connection connection, Status status) { - for (int i = 0; i < observers.length; i++) - observers[i].result(Agent.this, connection, status); - } - - public synchronized void exception(Connection connection, Exception e) { - for (int i = 0; i < observers.length; i++) - observers[i].exception(Agent.this, connection, e); - } - - public synchronized void completed(Connection connection) { - for (int i = 0; i < observers.length; i++) - observers[i].completed(Agent.this, connection); - } - - private Observer[] observers = new Observer[0]; + public synchronized void addObserver(Observer o) { + observers = (Observer[])(DynamicArray.append(observers, o)); + } + + public synchronized void removeObserver(Agent.Observer o) { + observers = (Observer[])(DynamicArray.remove(observers, o)); + } + + public synchronized void started() { + for (int i = 0; i < observers.length; i++) + observers[i].started(Agent.this); + } + + public synchronized void finished() { + for (int i = 0; i < observers.length; i++) + observers[i].finished(Agent.this); + } + + public synchronized void openedConnection(Connection connection) { + for (int i = 0; i < observers.length; i++) + observers[i].openedConnection(Agent.this, connection); + } + + public synchronized void errorOpeningConnection(Exception e) { + for (int i = 0; i < observers.length; i++) + observers[i].errorOpeningConnection(Agent.this, e); + } + + public synchronized void execTest(Connection cconnection, String tag,
String className, String[] args) { + for (int i = 0; i < observers.length; i++) + observers[i].execTest(Agent.this, cconnection, tag, className,
args); + } + + public synchronized void execCommand(Connection cconnection, String
tag, String className, String[] args) { + for (int i = 0; i < observers.length; i++) + observers[i].execCommand(Agent.this, cconnection, tag,
className, args); + } + + public synchronized void execMain(Connection connection, String tag,
String className, String[] args) { + for (int i = 0; i < observers.length; i++) + observers[i].execMain(Agent.this, connection, tag, className,
args); + } + + public synchronized void result(Connection connection, Status status) { + for (int i = 0; i < observers.length; i++) + observers[i].result(Agent.this, connection, status); + } + + public synchronized void exception(Connection connection, Exception e)
{ + for (int i = 0; i < observers.length; i++) + observers[i].exception(Agent.this, connection, e); + } + + public synchronized void completed(Connection connection) { + for (int i = 0; i < observers.length; i++) + observers[i].completed(Agent.this, connection); + } + + private Observer[] observers = new Observer[0]; }; - - private synchronized void setSystemStreams(Object owner, PrintStream out,
PrintStream err) - throws InterruptedException, SecurityException { - if (owner == null) - throw new NullPointerException(); - - while (currSystemStreamOwner != null) - wait(); - - currSystemStreamOwner = owner; - saveOut = System.out; - saveErr = System.err; - System.setOut(out); - System.setErr(err); + + private synchronized void setSystemStreams(Object owner, PrintStream out,
PrintStream err) + throws InterruptedException, SecurityException { + if (owner == null) + throw new NullPointerException(); + + while (currSystemStreamOwner != null) + wait(); + + currSystemStreamOwner = owner; + saveOut = System.out; + saveErr = System.err; + System.setOut(out); + System.setErr(err); } - + private synchronized void resetSystemStreams(Object owner) throws
SecurityException { - if (owner == null) - throw new NullPointerException(); - - if (owner != currSystemStreamOwner) - throw new IllegalStateException("expected: " + owner + " found: " +
currSystemStreamOwner); - - currSystemStreamOwner = null; - System.setOut(saveOut); - System.setErr(saveErr); - notifyAll(); + if (owner == null) + throw new NullPointerException(); + + if (owner != currSystemStreamOwner) + throw new IllegalStateException("expected: " + owner + " found: " +
currSystemStreamOwner); + + currSystemStreamOwner = null; + System.setOut(saveOut); + System.setErr(saveErr); + notifyAll(); } - + private boolean closing; private Thread mainThread; private int maxThreads; @@ -535,45 +523,45 @@ private Object currSystemStreamOwner = null; private PrintStream saveOut; private PrintStream saveErr; - + /** * A flag to enable debug tracing of the operation of the agent. */ static public boolean tracing = false; // hmm, public?? static PrintStream traceOut = System.out; - + /** * The default time to wait after a failed attempt to open a connection, - * and before trying again. + * and before trying again. * @see #setRetryDelay */ public static final int DEFAULT_RETRY_DELAY = 5; private int retryDelay = DEFAULT_RETRY_DELAY; private ConnectionFactory connectionFactory; - + private Map map; private Timer timer; - + /* For autonumbering agent tasks. */ private static int threadInitNumber; - + private static synchronized int nextThreadNum() { - return threadInitNumber++; + return threadInitNumber++; }

// The following is used to ensure consistency between Agent and
AgentManager static final short protocolVersion = 103; - + /** * The default port to which active agents will try and connect on a
nominated host. */ public static final int defaultActivePort = 1907; - + /** * The default port on which passive ports will listen for incoming
connections. */ public static final int defaultPassivePort = 1908; - + static final byte CLASS = (byte)'C'; static final byte DATA = (byte)'D'; static final byte LOG = (byte)'L'; @@ -581,481 +569,455 @@ static final byte REF = (byte)'R'; static final byte REF_FLUSH = (byte)'r'; static final byte STATUS = (byte)'S'; - + static final String productName = "JT Harness Agent"; static final String productVersion = "JTA_3.1.4"; static final String productCopyright = "Copyright © 1996-2008 Sun
Microsystems, Inc"; - - + + /** - * Tasks handle the individual requests received by Agent. + * Tasks handle the individual requests received by Agent. * They read the request from the connection, execute the request, which
means - * running the test class on behalf of the client, and any output from the + * running the test class on behalf of the client, and any output from the * test class is written back to the client via the connection. */ class Task { - Task(Connection c) { - if (c == null) - throw new NullPointerException(); - connection = c; - } - - public void handleRequest() throws ConnectionFactory.Fault { - - try { - notifier.openedConnection(connection); - - if (tracing) - traceOut.println("REQUEST FROM " + connection.getName()); - - in = new DataInputStream(connection.getInputStream()); - short pVer = in.readShort(); - if (pVer != protocolVersion) - throw new IOException("protocol mismatch;" + - " expected " + protocolVersion + - " received " + pVer); - - tag = in.readUTF(); - - if (tracing) - traceOut.println("TAG IS `" + tag + "'"); - - request = in.readUTF(); - - if (tracing) - traceOut.println("REQUEST IS `" + request + "'"); - - out = new DataOutputStream(new
BufferedOutputStream(connection.getOutputStream())); - - Status status; - - if (request.equals("executeTest") || request.equals("executeCommand") ||
request.equals("executeMain") ) - status = execute(); - else { - if (tracing) - traceOut.println("Unrecognized request for agent: `" + request + "'"); - status = Status.error("Unrecognized request for agent: `" + request +
"'"); - } - - if (tracing) - traceOut.println("RETURN " + status); - - notifier.result(connection, status); - - if (tracing) - traceOut.println("SEND STATUS"); - - sendStatus(status); - - if (tracing) - traceOut.println("FLUSH"); - - out.flush(); - - if (tracing) - traceOut.println("AWAIT CLOSE"); - - /* - final Thread taskThread = Thread.currentThread(); - - Timer.Timeable timeOutHandler = new Timer.Timeable() { - public void timeout() { - if (tracing) - traceOut.println("EOF TIMEOUT"); - IOException e = new IOException("timeout communicating with AgentManager"); - synchronized (Agent.this) { - for (int i = 0; i < observers.length; i++) - observers[i].exception(Agent.this, connection, e); - } - close(); // give up - taskThread.interrupt(); - traceOut.println("EOF TIMEOUT CLOSED"); - } - }; - - Timer.Entry te = timer.requestDelayedCallback(timeOutHandler, 5000); - while (in.read() != -1) ; - - if (tracing) - traceOut.println("RECEIVED EOF"); - - timer.cancel(te); - - notifier.completed(connection); - */ - - connection.waitUntilClosed(5000); - if (connection.isClosed()) - notifier.completed(connection); - else - notifier.exception(connection, new IOException("timeout awaiting close
from AgentManager")); - } - catch (InterruptedException e) { - if (tracing) { - traceOut.println("Interrupted"); - } - - notifier.exception(connection, e); - } - catch (InterruptedIOException e) { - if (tracing) { - traceOut.println("Interrupted (IO)"); - } - - notifier.exception(connection, e); - } - catch (IOException e) { - if (tracing) { - traceOut.println("EXCEPTION IS `" + e + "'"); - e.printStackTrace(traceOut); - } - - notifier.exception(connection, e); - } - finally { - close(); - } - } - - private Status execute() throws IOException { - String className = in.readUTF(); - - if (tracing) - traceOut.println("CLASSNAME: " + className); - - int n = in.readShort(); - - if (tracing) - traceOut.println("nArgs: " + n); - - String[] args = new String[n]; - for (int i = 0; i < args.length; i++) { - args[i] = in.readUTF(); - if (tracing) - traceOut.println("arg[" + i + "]: " + args[i]); - } - - boolean mapArgs = in.readBoolean(); - - if (tracing) - traceOut.println("mapArgs: " + mapArgs); - - boolean remoteClasses = in.readBoolean(); - - if (tracing) - traceOut.println("remoteClasses: " + remoteClasses); - - byte guard = in.readByte(); - if (guard != 0) - throw new IOException("data format error"); - - if (map != null && mapArgs) - map.map(args); - - PrintWriter testLog = new PrintWriter(new AgentWriter(LOG, this)); - PrintWriter testRef = new PrintWriter(new AgentWriter(REF, this)); - - try { - Class c; - ClassLoader cl = null; - if (remoteClasses) { - cl = getAgentClassLoader(); - c = cl.loadClass(className); - } - else - c = Class.forName(className); - - if (request.equals("executeTest")) { - return executeTest(c, args, testLog, testRef); - } - else if (request.equals("executeCommand")) { - return executeCommand(c, args, testLog, testRef, cl); - } - else if (request.equals("executeMain")) { - return executeMain(c, args, testLog, testRef); - } - else - return Status.error("Unrecognized request for agent: `" + request + "'"); - } - catch (ClassCastException e) { - if (tracing) - e.printStackTrace(traceOut); - return Status.error("Can't execute class `" + className + "': required
interface not found"); - } - catch (ClassNotFoundException ex) { - return Status.error("Can't find class `" + className + "'"); - } - catch (IllegalAccessException ex) { - return Status.error("Illegal access to class `" + className + "'"); - } - catch (InstantiationException ex) { - return Status.error("Can't instantiate class`" + className + "'"); - } - catch (ThreadDeath e) { - throw e; - } - catch (Exception e) { - e.printStackTrace(testLog); - return Status.error("Unexpected exception: " + e); - } - catch (Error e) { - e.printStackTrace(testLog); - return Status.error("Unexpected error: " + e); - } - catch (Throwable e) { - e.printStackTrace(testLog); - return Status.error("Unexpected throwable: " + e); - } - finally { - // close the streams used by the test and write the test status back - if (tracing) - traceOut.println("CLOSE TESTREF"); - - testRef.close(); - - if (tracing) - traceOut.println("CLOSE TESTLOG"); - - testLog.close(); - } - } - - private Status executeTest(Class c, String[] args, - PrintWriter testLog, PrintWriter testRef) - throws IOException, ClassNotFoundException, IllegalAccessException,
InstantiationException - { - notifier.execTest(connection, tag, c.getName(), args); - Test t = (Test)(c.newInstance()); - return t.run(args, testLog, testRef); - } - - private Status executeCommand(Class c, String[] args, - PrintWriter testLog, PrintWriter testRef, - ClassLoader cl) - throws IOException, ClassNotFoundException, IllegalAccessException,
InstantiationException - { - notifier.execCommand(connection, tag, c.getName(), args); - - Command tc = (Command)(c.newInstance()); - tc.setClassLoader(cl); - return tc.run(args, testLog, testRef); - } - - private Status executeMain(Class c, String[] args, - PrintWriter testLog, PrintWriter testRef) - throws IOException, ClassNotFoundException, IllegalAccessException - { - notifier.execMain(connection, tag, c.getName(), args); - - PrintStream out = Deprecated.createPrintStream(new WriterStream(testRef)); - PrintStream err = Deprecated.createPrintStream(new WriterStream(testLog)); - try { - setSystemStreams(this, out, err); - Method main = c.getDeclaredMethod("main", new Class[] {String[].class}); - main.invoke(null, new Object[] {args}); - return Status.passed("OK"); - } - catch (NoSuchMethodException e) { - return Status.error("Can't find `public static void main(String[] args)' for
`" + c.getName() + "'"); - } - catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - t.printStackTrace(err); - return Status.failed(t.toString()); - } - catch (InterruptedException e) { - return Status.failed("interrupted while waiting for access to system
streams"); - } - finally { - resetSystemStreams(this); - out.flush(); - err.flush(); - } - } - - /** - * Close the task, abandoning any request in progress. - */ - synchronized void close() { - if (!connection.isClosed()) { - closeIgnoreExceptions(connection); - // don't nullify connections because handleRequest might still be using it - } - - if (in != null) { - try { - //System.err.println("closing in"); - in.close(); - in = null; - } - catch (IOException ignore) { - } - } - - if (out != null) { - try { - //System.err.println("closing out"); - out.close(); - out = null; - } - catch (IOException ignore) { - } - } - } - - /** - * Send wrapped data back to the client. - */ - synchronized void sendChars(byte type, char b[], int off, int len) throws
IOException { - out.write(type); - out.writeUTF(new String(b, off, len)); - switch (type) { - case LOG_FLUSH: - case REF_FLUSH: - out.flush(); - } - } - - /** - * Send the final status back to the client. - */ - private synchronized void sendStatus(Status s) throws IOException { - out.write(STATUS); - out.write((byte)s.getType()); - out.writeUTF(s.getReason()); - } - - /** - * Get the bytecodes for a class - */ - synchronized byte[] getClassData(String className) throws
ClassNotFoundException { - if (tracing) - traceOut.println("REMOTE LOAD " + className); - - try { - out.write(CLASS); - out.writeUTF(className); - out.flush(); - - int size = in.readInt(); - if (size == 0) - throw new ClassNotFoundException(className); - - byte[] data = new byte[size]; - int offset = 0; - while (offset < data.length) { - int n = in.read(data, offset, data.length - offset); - if (n == -1) - throw new ClassNotFoundException(className + ": EOF while reading class
data"); - else - offset += n; - } - - //System.err.println(data.length); - //for (int i = 0; i < min(10, data.length); i++) { - // System.err.print(data[i] + " "); - //} - //System.err.print(" ... "); - //for (int i = max(0, data.length - 10); i < data.length; i++) { - // System.err.print(data[i] + " "); - //} - //System.err.println(); - - return data; - } - catch (IOException e) { - throw new ClassNotFoundException(className + ": " + e); - } - } - - /** - * Get a resource - */ - synchronized byte[] getResourceData(String resourceName) throws
MissingResourceException, IOException { - if (tracing) - traceOut.println("REMOTE LOAD " + resourceName); - - out.write(DATA); - out.writeUTF(resourceName); - out.flush(); - - int size = in.readInt(); - if (size == 0) - throw new MissingResourceException(resourceName, null, resourceName); - - byte[] data = new byte[size]; - int offset = 0; - while (offset < data.length) { - int n = in.read(data, offset, data.length - offset); - if (n == -1) - throw new IOException(resourceName + ": EOF while reading resource
data"); - else - offset += n; - } - - //System.err.println(data.length); - //for (int i = 0; i < min(10, data.length); i++) { - // System.err.print(data[i] + " "); - //} - //System.err.print(" ... "); - //for (int i = max(0, data.length - 10); i < data.length; i++) { - // System.err.print(data[i] + " "); - //} - //System.err.println(); - - return data; - } - - private ClassLoader getAgentClassLoader() - throws InstantiationException, IllegalAccessException - { - if (agentClassLoader == null) { - if (classLoaderConstructor == null) { - Class classLoaderClass; - try { - String s = getClass().getName(); - String pkg = s.substring(0, s.lastIndexOf('.')); - classLoaderClass = Class.forName(pkg + ".AgentClassLoader2"); - } - catch (Throwable t) { - classLoaderClass = AgentClassLoader.class; - } - - try { - Class[] constrArgTypes = { Task.class }; - classLoaderConstructor =
classLoaderClass.getDeclaredConstructor(constrArgTypes); - } - catch (NoSuchMethodException e) { - e.printStackTrace(); - } - } - - try { - Object[] args = { this }; - return (ClassLoader) (classLoaderConstructor.newInstance(args)); - } - catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - if (t instanceof RuntimeException) - throw (RuntimeException) t; - else if (t instanceof Error) - throw (Error) t; - else - throw new Error(e.toString()); - } - } - - return agentClassLoader; - } - - private Connection connection; - private DataInputStream in; - private DataOutputStream out; - private String tag; - private String request; - private ClassLoader agentClassLoader; + Task(Connection c) { + if (c == null) + throw new NullPointerException(); + connection = c; + } + + public void handleRequest() throws ConnectionFactory.Fault { + + try { + notifier.openedConnection(connection); + + if (tracing) + traceOut.println("REQUEST FROM " + connection.getName()); + + in = new DataInputStream(connection.getInputStream()); + short pVer = in.readShort(); + if (pVer != protocolVersion) + throw new IOException("protocol mismatch;" + + " expected " + protocolVersion + + " received " + pVer); + + tag = in.readUTF(); + + if (tracing) + traceOut.println("TAG IS `" + tag + "'"); + + request = in.readUTF(); + + if (tracing) + traceOut.println("REQUEST IS `" + request + "'"); + + out = new DataOutputStream(new
BufferedOutputStream(connection.getOutputStream())); + + Status status; + + if (request.equals("executeTest") ||
request.equals("executeCommand") || request.equals("executeMain") ) + status = execute(); + else { + if (tracing) + traceOut.println("Unrecognized request for agent: `" +
request + "'"); + status = Status.error("Unrecognized request for agent: `" +
request + "'"); + } + + if (tracing) + traceOut.println("RETURN " + status); + + notifier.result(connection, status); + + if (tracing) + traceOut.println("SEND STATUS"); + + sendStatus(status); + + if (tracing) + traceOut.println("FLUSH"); + + out.flush(); + + if (tracing) + traceOut.println("AWAIT CLOSE"); + + /* + final Thread taskThread = Thread.currentThread(); + + Timer.Timeable timeOutHandler = new Timer.Timeable() { + public void timeout() { + if (tracing) + traceOut.println("EOF TIMEOUT"); + IOException e = new IOException("timeout communicating
with AgentManager"); + synchronized (Agent.this) { + for (int i = 0; i < observers.length; i++) + observers[i].exception(Agent.this, connection,
e); + } + close(); // give up + taskThread.interrupt(); + traceOut.println("EOF TIMEOUT CLOSED"); + } + }; + + Timer.Entry te = timer.requestDelayedCallback(timeOutHandler,
5000); + while (in.read() != -1) ; + + if (tracing) + traceOut.println("RECEIVED EOF"); + + timer.cancel(te); + + notifier.completed(connection); + */ + + connection.waitUntilClosed(5000); + + if (Thread.interrupted() && tracing) { + traceOut.println("Thread was interrupted - clearing
interrupted status!"); + } + + if (connection.isClosed()) + notifier.completed(connection); + else + notifier.exception(connection, new IOException("timeout
awaiting close from AgentManager")); + } catch (InterruptedException e) { + if (tracing) { + traceOut.println("Interrupted"); + } + + notifier.exception(connection, e); + } catch (InterruptedIOException e) { + if (tracing) { + traceOut.println("Interrupted (IO)"); + } + + notifier.exception(connection, e); + } catch (IOException e) { + if (tracing) { + traceOut.println("EXCEPTION IS `" + e + "'"); + e.printStackTrace(traceOut); + } + + notifier.exception(connection, e); + } finally { + close(); + } + } + + private Status execute() throws IOException { + String className = in.readUTF(); + + if (tracing) + traceOut.println("CLASSNAME: " + className); + + int n = in.readShort(); + + if (tracing) + traceOut.println("nArgs: " + n); + + String[] args = new String[n]; + for (int i = 0; i < args.length; i++) { + args[i] = in.readUTF(); + if (tracing) + traceOut.println("arg[" + i + "]: " + args[i]); + } + + boolean mapArgs = in.readBoolean(); + + if (tracing) + traceOut.println("mapArgs: " + mapArgs); + + boolean remoteClasses = in.readBoolean(); + + if (tracing) + traceOut.println("remoteClasses: " + remoteClasses); + + byte guard = in.readByte(); + if (guard != 0) + throw new IOException("data format error"); + + if (map != null && mapArgs) + map.map(args); + + PrintWriter testLog = new PrintWriter(new AgentWriter(LOG, this)); + PrintWriter testRef = new PrintWriter(new AgentWriter(REF, this)); + + try { + Class c; + ClassLoader cl = null; + if (remoteClasses) { + cl = getAgentClassLoader(); + c = cl.loadClass(className); + } else + c = Class.forName(className); + + if (request.equals("executeTest")) { + return executeTest(c, args, testLog, testRef); + } else if (request.equals("executeCommand")) { + return executeCommand(c, args, testLog, testRef, cl); + } else if (request.equals("executeMain")) { + return executeMain(c, args, testLog, testRef); + } else + return Status.error("Unrecognized request for agent: `" +
request + "'"); + } catch (ClassCastException e) { + if (tracing) + e.printStackTrace(traceOut); + return Status.error("Can't execute class `" + className + "':
required interface not found"); + } catch (ClassNotFoundException ex) { + return Status.error("Can't find class `" + className + "'"); + } catch (IllegalAccessException ex) { + return Status.error("Illegal access to class `" + className +
"'"); + } catch (InstantiationException ex) { + return Status.error("Can't instantiate class`" + className +
"'"); + } catch (ThreadDeath e) { + throw e; + } catch (Exception e) { + e.printStackTrace(testLog); + return Status.error("Unexpected exception: " + e); + } catch (Error e) { + e.printStackTrace(testLog); + return Status.error("Unexpected error: " + e); + } catch (Throwable e) { + e.printStackTrace(testLog); + return Status.error("Unexpected throwable: " + e); + } finally { + // close the streams used by the test and write the test status
back + if (tracing) + traceOut.println("CLOSE TESTREF"); + + testRef.close(); + + if (tracing) + traceOut.println("CLOSE TESTLOG"); + + testLog.close(); + } + } + + private Status executeTest(Class c, String[] args, + PrintWriter testLog, PrintWriter testRef) + throws IOException, ClassNotFoundException,
IllegalAccessException, InstantiationException { + notifier.execTest(connection, tag, c.getName(), args); + Test t = (Test)(c.newInstance()); + return t.run(args, testLog, testRef); + } + + private Status executeCommand(Class c, String[] args, + PrintWriter testLog, PrintWriter testRef, + ClassLoader cl) + throws IOException, ClassNotFoundException,
IllegalAccessException, InstantiationException { + notifier.execCommand(connection, tag, c.getName(), args); + + Command tc = (Command)(c.newInstance()); + tc.setClassLoader(cl); + return tc.run(args, testLog, testRef); + } + + private Status executeMain(Class c, String[] args, + PrintWriter testLog, PrintWriter testRef) + throws IOException, ClassNotFoundException,
IllegalAccessException { + notifier.execMain(connection, tag, c.getName(), args); + + PrintStream out = Deprecated.createPrintStream(new
WriterStream(testRef)); + PrintStream err = Deprecated.createPrintStream(new
WriterStream(testLog)); + try { + setSystemStreams(this, out, err); + Method main = c.getDeclaredMethod("main", new Class[]
{String[].class}); + main.invoke(null, new Object[] {args}); + return Status.passed("OK"); + } catch (NoSuchMethodException e) { + return Status.error("Can't find `public static void
main(String[] args)' for `" + c.getName() + "'"); + } catch (InvocationTargetException e) { + Throwable t = e.getTargetException(); + t.printStackTrace(err); + return Status.failed(t.toString()); + } catch (InterruptedException e) { + return Status.failed("interrupted while waiting for access to
system streams"); + } finally { + resetSystemStreams(this); + out.flush(); + err.flush(); + } + } + + /** + * Close the task, abandoning any request in progress. + */ + synchronized void close() { + if (!connection.isClosed()) { + closeIgnoreExceptions(connection); + // don't nullify connections because handleRequest might still
be using it + } + + if (in != null) { + try { + //System.err.println("closing in"); + in.close(); + in = null; + } catch (IOException ignore) { + } + } + + if (out != null) { + try { + //System.err.println("closing out"); + out.close(); + out = null; + } catch (IOException ignore) { + } + } + } + + /** + * Send wrapped data back to the client. + */ + synchronized void sendChars(byte type, char b[], int off, int len)
throws IOException { + out.write(type); + out.writeUTF(new String(b, off, len)); + switch (type) { + case LOG_FLUSH: + case REF_FLUSH: + out.flush(); + } + } + + /** + * Send the final status back to the client. + */ + private synchronized void sendStatus(Status s) throws IOException { + out.write(STATUS); + out.write((byte)s.getType()); + out.writeUTF(s.getReason()); + } + + /** + * Get the bytecodes for a class + */ + synchronized byte[] getClassData(String className) throws
ClassNotFoundException { + if (tracing) + traceOut.println("REMOTE LOAD " + className); + + try { + out.write(CLASS); + out.writeUTF(className); + out.flush(); + + int size = in.readInt(); + if (size == 0) + throw new ClassNotFoundException(className); + + byte[] data = new byte[size]; + int offset = 0; + while (offset < data.length) { + int n = in.read(data, offset, data.length - offset); + if (n == -1) + throw new ClassNotFoundException(className + ": EOF
while reading class data"); + else + offset += n; + } + + //System.err.println(data.length); + //for (int i = 0; i < min(10, data.length); i++) { + // System.err.print(data[i] + " "); + //} + //System.err.print(" ... "); + //for (int i = max(0, data.length - 10); i < data.length; i++)
{ + // System.err.print(data[i] + " "); + //} + //System.err.println(); + + return data; + } catch (IOException e) { + throw new ClassNotFoundException(className + ": " + e); + } + } + + /** + * Get a resource + */ + synchronized byte[] getResourceData(String resourceName) throws
MissingResourceException, IOException { + if (tracing) + traceOut.println("REMOTE LOAD " + resourceName); + + out.write(DATA); + out.writeUTF(resourceName); + out.flush(); + + int size = in.readInt(); + if (size == 0) + throw new MissingResourceException(resourceName, null,
resourceName); + + byte[] data = new byte[size]; + int offset = 0; + while (offset < data.length) { + int n = in.read(data, offset, data.length - offset); + if (n == -1) + throw new IOException(resourceName + ": EOF while reading
resource data"); + else + offset += n; + } + + //System.err.println(data.length); + //for (int i = 0; i < min(10, data.length); i++) { + // System.err.print(data[i] + " "); + //} + //System.err.print(" ... "); + //for (int i = max(0, data.length - 10); i < data.length; i++) { + // System.err.print(data[i] + " "); + //} + //System.err.println(); + + return data; + } + + private ClassLoader getAgentClassLoader() + throws InstantiationException, IllegalAccessException { + if (agentClassLoader == null) { + if (classLoaderConstructor == null) { + Class classLoaderClass; + try { + String s = getClass().getName(); + String pkg = s.substring(0, s.lastIndexOf('.')); + classLoaderClass = Class.forName(pkg +
".AgentClassLoader2"); + } catch (Throwable t) { + classLoaderClass = AgentClassLoader.class; + } + + try { + Class[] constrArgTypes = { Task.class }; + classLoaderConstructor =
classLoaderClass.getDeclaredConstructor(constrArgTypes); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + + try { + Object[] args = { this }; + return (ClassLoader)
(classLoaderConstructor.newInstance(args)); + } catch (InvocationTargetException e) { + Throwable t = e.getTargetException(); + if (t instanceof RuntimeException) + throw (RuntimeException) t; + else if (t instanceof Error) + throw (Error) t; + else + throw new Error(e.toString()); + } + } + + return agentClassLoader; + } + + private Connection connection; + private DataInputStream in; + private DataOutputStream out; + private String tag; + private String request; + private ClassLoader agentClassLoader; } - + private static Constructor classLoaderConstructor; }

@@ -1075,10 +1037,10 @@ * @arg parent The parent object to which to pass the data written to the
stream. */ AgentWriter(byte type, Agent.Task parent) { - this.type = type; - this.parent = parent; + this.type = type; + this.parent = parent; } - + /** * Writes a character. This method will block until the character * is actually written. @@ -1086,65 +1048,65 @@ * @exception IOException If an I/O error has occurred. */ public synchronized void write(int ch) throws IOException { - buf[count++] = (char)ch; - if (count == buf.length) { - parent.sendChars(type, buf, 0, count); - count = 0; - } + buf[count++] = (char)ch; + if (count == buf.length) { + parent.sendChars(type, buf, 0, count); + count = 0; + } } - + /** - * Writes an array of characters. This method will block until the + * Writes an array of characters. This method will block until the * characters are actually written. * @param c the data to be written * @exception IOException If an I/O error has occurred. */ public void write(char c[]) throws IOException { - write(c, 0, c.length); + write(c, 0, c.length); } - + /** - * Writes a sub array of characters. + * Writes a sub array of characters. * @param c the data to be written * @param off the start offset in the data * @param len the number of bytes that are written * @exception IOException If an I/O error has occurred. */ public synchronized void write(char c[], int off, int len) throws
IOException { - if (len < buf.length - count) { - // there is room for the bytes in the current buffer - System.arraycopy(c, off, buf, count, len); - count += len; - } else { - // not room in the current buffer, so flush it - flush(); - if (len < buf.length) { - // there is _now_ enough room in the current buffer, so use it - System.arraycopy(c, off, buf, count, len); - count += len; - } else { - // current buffer not big enough; send data directly - parent.sendChars(type, c, off, len); - } - } + if (len < buf.length - count) { + // there is room for the bytes in the current buffer + System.arraycopy(c, off, buf, count, len); + count += len; + } else { + // not room in the current buffer, so flush it + flush(); + if (len < buf.length) { + // there is _now_ enough room in the current buffer, so use it + System.arraycopy(c, off, buf, count, len); + count += len; + } else { + // current buffer not big enough; send data directly + parent.sendChars(type, c, off, len); + } + } } - + /** * Flushes the stream. This will write any buffered * output bytes. * @exception IOException If an I/O error has occurred. */ public synchronized void flush() throws IOException { - if (count > 0) { - switch (type) { - case Agent.LOG: type = Agent.LOG_FLUSH; break; - case Agent.REF: type = Agent.REF_FLUSH; break; - } - parent.sendChars(type, buf, 0, count); - count = 0; - } + if (count > 0) { + switch (type) { + case Agent.LOG: type = Agent.LOG_FLUSH; break; + case Agent.REF: type = Agent.REF_FLUSH; break; + } + parent.sendChars(type, buf, 0, count); + count = 0; + } } - + /** * Closes the stream. This method must be called * to release any resources associated with the @@ -1152,9 +1114,9 @@ * @exception IOException If an I/O error has occurred. */ public void close() throws IOException { - flush(); + flush(); } - + private byte type; private Agent.Task parent; private char[] buf = new char[1024];

Modified: trunk/code/src/com/sun/javatest/agent/SocketConnection.java Url:
https://jtharness.dev.java.net/source/browse/jtharness/trunk/code/src/com/sun/javatest/agent/SocketConnection.java?view=diff&rev=824&p1=trunk/code/src/com/sun/javatest/agent/SocketConnection.java&p2=trunk/code/src/com/sun/javatest/agent/SocketConnection.java&r1=823&r2=824 ============================================================================== --- trunk/code/src/com/sun/javatest/agent/SocketConnection.java (original) +++ trunk/code/src/com/sun/javatest/agent/SocketConnection.java 2008-03-24
09:37:55+0000 @@ -38,8 +38,7 @@ /** * A connection via a TCP/IP socket. */ -public class SocketConnection implements Connection -{ +public class SocketConnection implements Connection { /** * Create a connection via a TCP/IP socket. * @param socket The socket to use for the connection. @@ -47,13 +46,13 @@ * @throws NullPointerException if socket is null */ public SocketConnection(Socket socket) throws IOException { - if (socket == null) - throw new NullPointerException(); - this.socket = socket; - socketInput = socket.getInputStream(); - socketOutput = socket.getOutputStream(); + if (socket == null) + throw new NullPointerException(); + this.socket = socket; + socketInput = socket.getInputStream(); + socketOutput = socket.getOutputStream(); } - + /** * Create a connection via a TCP/IP socket. * @param host The host to which to try to connect to try and get a socket. @@ -61,103 +60,103 @@ * @throws IOException if an error occurs opening the socket. */ public SocketConnection(String host, int port) throws IOException { - if (host == null) - throw new NullPointerException(); + if (host == null) + throw new NullPointerException(); socket = new Socket(host, port); - socketInput = socket.getInputStream(); - socketOutput = socket.getOutputStream(); + socketInput = socket.getInputStream(); + socketOutput = socket.getOutputStream(); }

public String getName() { - if (name == null) { - StringBuffer sb = new StringBuffer(32); - sb.append(getHostName(socket.getInetAddress())); - sb.append(",port="); - sb.append(socket.getPort()); - sb.append(",localport="); - sb.append(socket.getLocalPort()); - name = sb.toString(); - } - return name; + if (name == null) { + StringBuffer sb = new StringBuffer(32); + sb.append(getHostName(socket.getInetAddress())); + sb.append(",port="); + sb.append(socket.getPort()); + sb.append(",localport="); + sb.append(socket.getLocalPort()); + name = sb.toString(); + } + return name; } - + public InputStream getInputStream() { - return socketInput; + return socketInput; } - + public OutputStream getOutputStream() { - return socketOutput; + return socketOutput; }

public synchronized void close() throws IOException { - socket.close(); - socketInput.close(); - socketOutput.close(); - closed = true; - - if (waitThread != null) - waitThread.interrupt(); + socket.close(); + socketInput.close(); + socketOutput.close(); + closed = true; + + if (waitThread != null) + waitThread.interrupt(); } - + public synchronized boolean isClosed() { - return closed; + return closed; } - + public void waitUntilClosed(int timeout) throws InterruptedException { - synchronized (this) { - waitThread = Thread.currentThread(); - } - - Timer.Timeable cb = new Timer.Timeable() { - public void timeout() { - synchronized (SocketConnection.this) { - if (waitThread != null) - waitThread.interrupt(); - try { - socketInput.close(); - } - catch (IOException ignore) { - } - try { - socketOutput.close(); - } - catch (IOException ignore) { - } - } - } - }; - - Timer.Entry e = timer.requestDelayedCallback(cb, timeout); - try { - while (true) { - try { - int i = socketInput.read(); - if (i == -1) - break; - } - catch (IOException ignore) { - break; - } - } - } - finally { - timer.cancel(e); - - synchronized (this) { - waitThread = null; - } - } + synchronized (this) { + waitThread = Thread.currentThread(); + } + + Timer.Timeable cb = new Timer.Timeable() { + public void timeout() { + synchronized (SocketConnection.this) { + if (waitThread != null) + waitThread.interrupt(); + try { + socketInput.close(); + } catch (IOException ignore) { + } + try { + socketOutput.close(); + } catch (IOException ignore) { + } + } + } + }; + + Timer.Entry e = timer.requestDelayedCallback(cb, timeout); + try { + while (true) { + try { + int i = socketInput.read(); + if (i == -1) + break; + } catch (IOException ignore) { + break; + } + } + } finally { + timer.cancel(e); + + if (Thread.interrupted()) { + throw new InterruptedException(); + } + + synchronized (this) { + waitThread = null; + } + } } - + private static String getHostName(InetAddress addr) { - String s = (String)(addressCache.get(addr)); - if (s == null) { - s = addr.getHostName(); - addressCache.put(addr, s); - } - return s; + String s = (String)(addressCache.get(addr)); + if (s == null) { + s = addr.getHostName(); + addressCache.put(addr, s); + } + return s; } - + private final Socket socket; protected final InputStream socketInput; private final OutputStream socketOutput;