< prev index next >

## src/com/sun/javatest/agent/Agent.java

Print this page

     186         return retryDelay;
     187     }
     188
     189     /**
     190      * Set the translation map to be used to localize incoming requests.
     191      * If an incoming request permits it, occurrences of certain substrings
     192      * will be replaced with corresponding local variants. This is typically
     193      * used to remap file systems which might have different mount points
     194      * on different systems, but has been superceded by the environment's
     195      * map substitution facility.
     196      * @param map       The translation map to be used.
     197      */
     198     public synchronized void setMap(Map map) {
     199         this.map = map;
     200         if (tracing) {
     201             if (map == null)
     202                 traceOut.println("set map null");
     203             else {
     204                 traceOut.println("set map:");
     205                 map.setTracing(tracing, traceOut);
     206                 for (Enumeration e = map.enumerate(); e.hasMoreElements(); ) {
     207                     String[] entry = (String[])(e.nextElement());
     208                     traceOut.println("map-from: " + entry[0]);
     209                     traceOut.println("map-to:   " + entry[1]);
     210                 }
     211                 traceOut.println("end of map");
     212             }
     213         }
     214     }
     215
     216
     217     /**
     218      * Enable or disable tracing for agent activities.
     219      * It is best to call this as early as possible - objects created by
     220      * this class will inherit the setting as they are created/set.
     221      * @param state True if tracing output should be provided.
     222      */
     223     public void setTracing(boolean state) {
     224         tracing = state;
     225     }
     226
     227     //--------------------------------------------------------------------------
     318     /**
     319      * Interrupt this agent. The thread running the {@link #run run} method
     320      * will be interrupted.
     321      */
     322     public synchronized void interrupt() {
     323         if (mainThread != null)
     324             mainThread.interrupt();
     325     }
     326
     327     /**
     328      * Close this agent. Any requests in progress will be allowed to complete,
     329      * and no new requests will be accepted.
     330      * @throws InterruptedException if this thread is interrupted while waiting
     331      * for outstanding requests to complete.
     332      */
     333     public synchronized void close() throws InterruptedException {
     334         closing = true;  // will prevent new tasks from being created
     335
     336         // interrupt any threads that are running
     337         for (int i = 0; i < threads.size(); i++) {
     338             Thread t = (Thread)(threads.elementAt(i));
     339             if (tracing)
     340                 traceOut.println("INTERRUPTING THREAD " + t.getName());
     341             t.interrupt();
     342         }
     343
     344         // wait 3s for shutdown
     345         traceOut.println("WAITING 3s FOR THREADS TO CLEANUP");
     346         Thread.currentThread().sleep(3000);
     347
     348         // close any tasks that are running
     349         for (int i = 0; i < tasks.size(); i++) {
     350             Task t = (Task)(tasks.elementAt(i));
     351             if (tracing) {
     352                 Connection c = t.connection; // maybe null; if it is, task is already closing
     353                 traceOut.println("CLOSING TASK " + (c == null ? "[unknown]" : c.getName()));
     354             }
     355             t.close();
     356         }
     357
     358         try {
     359             if (tracing)
     360                 traceOut.println("CLOSING CONNECTION FACTORY");
     361
     362             connectionFactory.close();
     363         } catch (ConnectionFactory.Fault ignore) {
     364         }
     365
     366         // allow main loop to exit
     367         notifyAll();
     368
     369         if (tracing)
     370             traceOut.println("WAITING FOR TASKS TO EXIT");
     525         System.setOut(out);
     526         System.setErr(err);
     527     }
     528
     529     private synchronized void resetSystemStreams(Object owner) throws SecurityException {
     530         if (owner == null)
     531             throw new NullPointerException();
     532
     533         if (owner != currSystemStreamOwner)
     534             throw new IllegalStateException("expected: " + owner + " found: " + currSystemStreamOwner);
     535
     536         currSystemStreamOwner = null;
     537         System.setOut(saveOut);
     538         System.setErr(saveErr);
     539         notifyAll();
     540     }
     541
     542     private boolean closing;
     543     private Thread mainThread;
     544     private int maxThreads;
     545     private Vector threads = new Vector();
     546     private Vector tasks = new Vector();
     547     private Notifier notifier = new Notifier();
     548     private Object currSystemStreamOwner = null;
     549     private PrintStream saveOut;
     550     private PrintStream saveErr;
     551
     552     /**
     553      * A flag to enable debug tracing of the operation of the agent.
     554      */
     555     protected boolean tracing = false;
     556     protected PrintStream traceOut = System.out;
     557
     558     /**
     559      * The default time to wait after a failed attempt to open a connection,
     560      * and before trying again.
     561      * @see #setRetryDelay
     562      */
     563     public static final int DEFAULT_RETRY_DELAY = 5;
     564     private int retryDelay = DEFAULT_RETRY_DELAY;
     565     private ConnectionFactory connectionFactory;
     566
     919                             LOCK.wait();
     920                         }
     921                         catch (InterruptedException e) {
     922                             // treat interrupt as exit request
     923                             break;
     924                         }
     925                     }
     926                 }
     927
     928                 executeThread.setPriority(Thread.MIN_PRIORITY);
     929                 executeThread.interrupt();
     930                 if (alarmTimer != null){
     931                     alarmTimer.finished();
     932                 }
     933
     934                 return result;
     935             }
     936
     937         }
     938
     939         private Status executeMain(Class c, String[] args,
     940                 PrintWriter testLog, PrintWriter testRef)
     941                 throws IOException, ClassNotFoundException, IllegalAccessException {
     942             notifier.execMain(connection, tag, c.getName(), args);
     943
     944             PrintStream out = Deprecated.createPrintStream(new WriterStream(testRef));
     945             PrintStream err = Deprecated.createPrintStream(new WriterStream(testLog));
     946             try {
     947                 setSystemStreams(this, out, err);
     948                 Method main = c.getDeclaredMethod("main", new Class[] {String[].class});
     949                 main.invoke(null, new Object[] {args});
     950                 return Status.passed("OK");
     951             } catch (NoSuchMethodException e) {
     952                 return Status.error("Can't find `public static void main(String[] args)' for `" + c.getName() + "'");
     953             } catch (InvocationTargetException e) {
     954                 Throwable t = e.getTargetException();
     955                 t.printStackTrace(err);
     956                 return Status.failed(t.toString());
     957             } catch (InterruptedException e) {
     958                 return Status.failed("interrupted while waiting for access to system streams");
     959             } finally {
    1090                     throw new IOException(resourceName + ": EOF while reading resource data");
    1091                 else
    1092                     offset += n;
    1093             }
    1094
    1095             //System.err.println(data.length);
    1096             //for (int i = 0; i < min(10, data.length); i++) {
    1097             //    System.err.print(data[i] + " ");
    1098             //}
    1099             //System.err.print(" ... ");
    1100             //for (int i = max(0, data.length - 10); i < data.length; i++) {
    1101             //    System.err.print(data[i] + " ");
    1102             //}
    1103             //System.err.println();
    1104
    1105             return data;
    1106         }
    1107
    1108         private ClassLoader getAgentClassLoader(boolean useSharedClassLoader)
    1109                 throws InstantiationException, IllegalAccessException {
    1110             Class classLoaderClass;
    1111             try {
    1112                 String s = getClass().getName();
    1113                 String pkg = s.substring(0, s.lastIndexOf('.'));
    1114                 classLoaderClass = Class.forName(pkg + ".AgentClassLoader2");
    1115             } catch (Throwable t) {
    1116                 classLoaderClass = AgentClassLoader.class;
    1117             }
    1118
    1119             Class[] argTypes = {Task.class};
    1120             if (useSharedClassLoader && factoryMethod == null) {
    1121                 try {
    1122                     factoryMethod = classLoaderClass.getDeclaredMethod("getInstance", argTypes);
    1123                 } catch (NoSuchMethodException e) {
    1124                     e.printStackTrace();
    1125                 }
    1126             }
    1127
    1128             if (classLoaderConstructor == null) {
    1129                 try {
    1130                     classLoaderConstructor = classLoaderClass.getDeclaredConstructor(argTypes);
     186         return retryDelay;
     187     }
     188
     189     /**
     190      * Set the translation map to be used to localize incoming requests.
     191      * If an incoming request permits it, occurrences of certain substrings
     192      * will be replaced with corresponding local variants. This is typically
     193      * used to remap file systems which might have different mount points
     194      * on different systems, but has been superceded by the environment's
     195      * map substitution facility.
     196      * @param map       The translation map to be used.
     197      */
     198     public synchronized void setMap(Map map) {
     199         this.map = map;
     200         if (tracing) {
     201             if (map == null)
     202                 traceOut.println("set map null");
     203             else {
     204                 traceOut.println("set map:");
     205                 map.setTracing(tracing, traceOut);
     206                 for (Enumeration<String[]> e = map.enumerate(); e.hasMoreElements(); ) {
     207                     String[] entry = e.nextElement();
     208                     traceOut.println("map-from: " + entry[0]);
     209                     traceOut.println("map-to:   " + entry[1]);
     210                 }
     211                 traceOut.println("end of map");
     212             }
     213         }
     214     }
     215
     216
     217     /**
     218      * Enable or disable tracing for agent activities.
     219      * It is best to call this as early as possible - objects created by
     220      * this class will inherit the setting as they are created/set.
     221      * @param state True if tracing output should be provided.
     222      */
     223     public void setTracing(boolean state) {
     224         tracing = state;
     225     }
     226
     227     //--------------------------------------------------------------------------
     318     /**
     319      * Interrupt this agent. The thread running the {@link #run run} method
     320      * will be interrupted.
     321      */
     322     public synchronized void interrupt() {
     323         if (mainThread != null)
     324             mainThread.interrupt();
     325     }
     326
     327     /**
     328      * Close this agent. Any requests in progress will be allowed to complete,
     329      * and no new requests will be accepted.
     330      * @throws InterruptedException if this thread is interrupted while waiting
     331      * for outstanding requests to complete.
     332      */
     333     public synchronized void close() throws InterruptedException {
     334         closing = true;  // will prevent new tasks from being created
     335
     336         // interrupt any threads that are running
     337         for (int i = 0; i < threads.size(); i++) {
     338             Thread t = threads.elementAt(i);
     339             if (tracing)
     340                 traceOut.println("INTERRUPTING THREAD " + t.getName());
     341             t.interrupt();
     342         }
     343
     344         // wait 3s for shutdown
     345         traceOut.println("WAITING 3s FOR THREADS TO CLEANUP");
     346         Thread.currentThread().sleep(3000);
     347
     348         // close any tasks that are running
     349         for (int i = 0; i < tasks.size(); i++) {
     350             Task t = tasks.elementAt(i);
     351             if (tracing) {
     352                 Connection c = t.connection; // maybe null; if it is, task is already closing
     353                 traceOut.println("CLOSING TASK " + (c == null ? "[unknown]" : c.getName()));
     354             }
     355             t.close();
     356         }
     357
     358         try {
     359             if (tracing)
     360                 traceOut.println("CLOSING CONNECTION FACTORY");
     361
     362             connectionFactory.close();
     363         } catch (ConnectionFactory.Fault ignore) {
     364         }
     365
     366         // allow main loop to exit
     367         notifyAll();
     368
     369         if (tracing)
     370             traceOut.println("WAITING FOR TASKS TO EXIT");
     525         System.setOut(out);
     526         System.setErr(err);
     527     }
     528
     529     private synchronized void resetSystemStreams(Object owner) throws SecurityException {
     530         if (owner == null)
     531             throw new NullPointerException();
     532
     533         if (owner != currSystemStreamOwner)
     534             throw new IllegalStateException("expected: " + owner + " found: " + currSystemStreamOwner);
     535
     536         currSystemStreamOwner = null;
     537         System.setOut(saveOut);
     538         System.setErr(saveErr);
     539         notifyAll();
     540     }
     541
     542     private boolean closing;
     543     private Thread mainThread;
     544     private int maxThreads;
     545     private Vector<Thread> threads = new Vector<>();
     546     private Vector<Task> tasks = new Vector<>();
     547     private Notifier notifier = new Notifier();
     548     private Object currSystemStreamOwner = null;
     549     private PrintStream saveOut;
     550     private PrintStream saveErr;
     551
     552     /**
     553      * A flag to enable debug tracing of the operation of the agent.
     554      */
     555     protected boolean tracing = false;
     556     protected PrintStream traceOut = System.out;
     557
     558     /**
     559      * The default time to wait after a failed attempt to open a connection,
     560      * and before trying again.
     561      * @see #setRetryDelay
     562      */
     563     public static final int DEFAULT_RETRY_DELAY = 5;
     564     private int retryDelay = DEFAULT_RETRY_DELAY;
     565     private ConnectionFactory connectionFactory;
     566
     919                             LOCK.wait();
     920                         }
     921                         catch (InterruptedException e) {
     922                             // treat interrupt as exit request
     923                             break;
     924                         }
     925                     }
     926                 }
     927
     928                 executeThread.setPriority(Thread.MIN_PRIORITY);
     929                 executeThread.interrupt();
     930                 if (alarmTimer != null){
     931                     alarmTimer.finished();
     932                 }
     933
     934                 return result;
     935             }
     936
     937         }
     938
     939         private Status executeMain(Class<?> c, String[] args,
     940                 PrintWriter testLog, PrintWriter testRef)
     941                 throws IOException, ClassNotFoundException, IllegalAccessException {
     942             notifier.execMain(connection, tag, c.getName(), args);
     943
     944             PrintStream out = Deprecated.createPrintStream(new WriterStream(testRef));
     945             PrintStream err = Deprecated.createPrintStream(new WriterStream(testLog));
     946             try {
     947                 setSystemStreams(this, out, err);
     948                 Method main = c.getDeclaredMethod("main", new Class[] {String[].class});
     949                 main.invoke(null, new Object[] {args});
     950                 return Status.passed("OK");
     951             } catch (NoSuchMethodException e) {
     952                 return Status.error("Can't find `public static void main(String[] args)' for `" + c.getName() + "'");
     953             } catch (InvocationTargetException e) {
     954                 Throwable t = e.getTargetException();
     955                 t.printStackTrace(err);
     956                 return Status.failed(t.toString());
     957             } catch (InterruptedException e) {
     958                 return Status.failed("interrupted while waiting for access to system streams");
     959             } finally {
    1090                     throw new IOException(resourceName + ": EOF while reading resource data");
    1091                 else
    1092                     offset += n;
    1093             }
    1094
    1095             //System.err.println(data.length);
    1096             //for (int i = 0; i < min(10, data.length); i++) {
    1097             //    System.err.print(data[i] + " ");
    1098             //}
    1099             //System.err.print(" ... ");
    1100             //for (int i = max(0, data.length - 10); i < data.length; i++) {
    1101             //    System.err.print(data[i] + " ");
    1102             //}
    1103             //System.err.println();
    1104
    1105             return data;
    1106         }
    1107
    1108         private ClassLoader getAgentClassLoader(boolean useSharedClassLoader)
    1109                 throws InstantiationException, IllegalAccessException {
    1110             Class<?> classLoaderClass;
    1111             try {
    1112                 String s = getClass().getName();
    1113                 String pkg = s.substring(0, s.lastIndexOf('.'));
    1114                 classLoaderClass = Class.forName(pkg + ".AgentClassLoader2");
    1115             } catch (Throwable t) {
    1116                 classLoaderClass = AgentClassLoader.class;
    1117             }
    1118
    1119             Class[] argTypes = {Task.class};
    1120             if (useSharedClassLoader && factoryMethod == null) {
    1121                 try {
    1122                     factoryMethod = classLoaderClass.getDeclaredMethod("getInstance", argTypes);
    1123                 } catch (NoSuchMethodException e) {
    1124                     e.printStackTrace();
    1125                 }
    1126             }
    1127
    1128             if (classLoaderConstructor == null) {
    1129                 try {
    1130                     classLoaderConstructor = classLoaderClass.getDeclaredConstructor(argTypes);

< prev index next >