< prev index next >

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

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 764             if (tracing) {
 765                 traceOut.println("sharedClassLoader: " + sharedClassLoader);
 766             }
 767 
 768             timeoutValue = in.readInt();
 769 
 770             if (tracing)
 771                 traceOut.println("COMMAND TIMEOUT(seconds) IS `" + timeoutValue + "'");
 772 
 773             byte guard = in.readByte();
 774             if (guard != 0)
 775                 throw new IOException("data format error");
 776 
 777             if (map != null && mapArgs)
 778                 map.map(args);
 779 
 780             PrintWriter testLog = new PrintWriter(new AgentWriter(LOG, this));
 781             PrintWriter testRef = new PrintWriter(new AgentWriter(REF, this));
 782 
 783             try {
 784                 Class c;
 785                 ClassLoader cl = null;
 786                 if (remoteClasses) {
 787                     cl = getAgentClassLoader(sharedClassLoader);
 788                     c = cl.loadClass(className);
 789                 } else {
 790                     c = Class.forName(className);
 791                 }
 792 
 793                 if (request.equals("executeTest")) {
 794                     return executeTest(c, args, testLog, testRef);
 795                 } else if (request.equals("executeCommand")) {
 796                     return executeCommand(c, args, testLog, testRef, cl);
 797                 } else if (request.equals("executeMain")) {
 798                     return executeMain(c, args, testLog, testRef);
 799                 } else {
 800                     return Status.error("Unrecognized request for agent: `" + request + "'");
 801                 }
 802             } catch (ClassCastException e) {
 803                 if (tracing)
 804                     e.printStackTrace(traceOut);


 817             } catch (Error e) {
 818                 e.printStackTrace(testLog);
 819                 return Status.error("Unexpected error: " + e);
 820             } catch (Throwable e) {
 821                 e.printStackTrace(testLog);
 822                 return Status.error("Unexpected throwable: " + e);
 823             } finally {
 824                 // close the streams used by the test and write the test status back
 825                 if (tracing)
 826                     traceOut.println("CLOSE TESTREF");
 827 
 828                 testRef.close();
 829 
 830                 if (tracing)
 831                     traceOut.println("CLOSE TESTLOG");
 832 
 833                 testLog.close();
 834             }
 835         }
 836 
 837         private Status executeTest(Class c, String[] args,
 838                 PrintWriter testLog, PrintWriter testRef)
 839                 throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
 840             notifier.execTest(connection, tag, c.getName(), args);
 841             Test t = (Test)(c.newInstance());
 842             return t.run(args, testLog, testRef);
 843         }
 844 
 845         private Status executeCommand(Class c, String[] args,
 846                 PrintWriter testLog, PrintWriter testRef,
 847                 ClassLoader cl)
 848                 throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
 849             notifier.execCommand(connection, tag, c.getName(), args);
 850 
 851             Command tc = (Command)(c.newInstance());
 852             tc.setClassLoader(cl);
 853             return new CommandExecutor(tc, args, testLog, testRef, timeoutValue).execute();
 854         }
 855 
 856         private class CommandExecutor {
 857 
 858             private String[] args;
 859             private PrintWriter testLog;
 860             private PrintWriter testRef;
 861             private Command tc;
 862             private Status result;
 863 
 864             private final Object LOCK = new Object();
 865             private boolean executed = false;


 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 {
 960                 resetSystemStreams(this);
 961                 out.flush();
 962                 err.flush();
 963             }
 964         }
 965 
 966         /**
 967          * Close the task, abandoning any request in progress.
 968          */


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);
1131                     classLoaderConstructor.setAccessible(true);
1132                 } catch (NoSuchMethodException e) {
1133                     e.printStackTrace();
1134                 }
1135             }
1136 
1137             try {
1138                 Object[] args = {this};
1139                 if (useSharedClassLoader && factoryMethod != null) {
1140                     return (ClassLoader) factoryMethod.invoke(null, args);
1141                 } else {
1142                     return (ClassLoader) (classLoaderConstructor.newInstance(args));
1143                 }
1144             } catch (InvocationTargetException e) {
1145                 Throwable t = e.getTargetException();
1146                 if (t instanceof RuntimeException) {
1147                     throw (RuntimeException) t;
1148                 } else if (t instanceof Error) {
1149                     throw (Error) t;
1150                 } else {
1151                     throw new Error(e.toString());
1152                 }
1153             }
1154 
1155         }
1156 
1157         private Connection connection;
1158         private DataInputStream in;
1159         private DataOutputStream out;
1160         private String tag;
1161         private String request;
1162         private Integer timeoutValue;
1163     }
1164 
1165     private static Constructor classLoaderConstructor;
1166     private static Method factoryMethod = null;
1167 }
1168 
1169 
1170 
1171 
1172 
1173 /**
1174  * Stream passed to the class that is executed on behalf of the client.
1175  * Data written to the stream is buffered and eventually written back to th
1176  * client via the Task's sendChars method.
1177  */
1178 class AgentWriter extends Writer {
1179     /**
1180      * Create a stream that sends its data back to the parent Task.
1181      * @arg type        A tag to pass back to parent.sendChars().
1182      * @arg parent      The parent object to which to pass the data written to the stream.
1183      */
1184     AgentWriter(byte type, Agent.Task parent) {
1185         this.type = type;




 764             if (tracing) {
 765                 traceOut.println("sharedClassLoader: " + sharedClassLoader);
 766             }
 767 
 768             timeoutValue = in.readInt();
 769 
 770             if (tracing)
 771                 traceOut.println("COMMAND TIMEOUT(seconds) IS `" + timeoutValue + "'");
 772 
 773             byte guard = in.readByte();
 774             if (guard != 0)
 775                 throw new IOException("data format error");
 776 
 777             if (map != null && mapArgs)
 778                 map.map(args);
 779 
 780             PrintWriter testLog = new PrintWriter(new AgentWriter(LOG, this));
 781             PrintWriter testRef = new PrintWriter(new AgentWriter(REF, this));
 782 
 783             try {
 784                 Class<?> c;
 785                 ClassLoader cl = null;
 786                 if (remoteClasses) {
 787                     cl = getAgentClassLoader(sharedClassLoader);
 788                     c = cl.loadClass(className);
 789                 } else {
 790                     c = Class.forName(className);
 791                 }
 792 
 793                 if (request.equals("executeTest")) {
 794                     return executeTest(c, args, testLog, testRef);
 795                 } else if (request.equals("executeCommand")) {
 796                     return executeCommand(c, args, testLog, testRef, cl);
 797                 } else if (request.equals("executeMain")) {
 798                     return executeMain(c, args, testLog, testRef);
 799                 } else {
 800                     return Status.error("Unrecognized request for agent: `" + request + "'");
 801                 }
 802             } catch (ClassCastException e) {
 803                 if (tracing)
 804                     e.printStackTrace(traceOut);


 817             } catch (Error e) {
 818                 e.printStackTrace(testLog);
 819                 return Status.error("Unexpected error: " + e);
 820             } catch (Throwable e) {
 821                 e.printStackTrace(testLog);
 822                 return Status.error("Unexpected throwable: " + e);
 823             } finally {
 824                 // close the streams used by the test and write the test status back
 825                 if (tracing)
 826                     traceOut.println("CLOSE TESTREF");
 827 
 828                 testRef.close();
 829 
 830                 if (tracing)
 831                     traceOut.println("CLOSE TESTLOG");
 832 
 833                 testLog.close();
 834             }
 835         }
 836 
 837         private Status executeTest(Class<?> c, String[] args,
 838                 PrintWriter testLog, PrintWriter testRef)
 839                 throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
 840             notifier.execTest(connection, tag, c.getName(), args);
 841             Test t = (Test)(c.newInstance());
 842             return t.run(args, testLog, testRef);
 843         }
 844 
 845         private Status executeCommand(Class<?> c, String[] args,
 846                 PrintWriter testLog, PrintWriter testRef,
 847                 ClassLoader cl)
 848                 throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
 849             notifier.execCommand(connection, tag, c.getName(), args);
 850 
 851             Command tc = (Command)(c.newInstance());
 852             tc.setClassLoader(cl);
 853             return new CommandExecutor(tc, args, testLog, testRef, timeoutValue).execute();
 854         }
 855 
 856         private class CommandExecutor {
 857 
 858             private String[] args;
 859             private PrintWriter testLog;
 860             private PrintWriter testRef;
 861             private Command tc;
 862             private Status result;
 863 
 864             private final Object LOCK = new Object();
 865             private boolean executed = false;


 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 {
 960                 resetSystemStreams(this);
 961                 out.flush();
 962                 err.flush();
 963             }
 964         }
 965 
 966         /**
 967          * Close the task, abandoning any request in progress.
 968          */


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<? extends ClassLoader> classLoaderClass;
1111             try {
1112                 String s = getClass().getName();
1113                 String pkg = s.substring(0, s.lastIndexOf('.'));
1114                 classLoaderClass = (Class<? extends ClassLoader>) 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);
1131                     classLoaderConstructor.setAccessible(true);
1132                 } catch (NoSuchMethodException e) {
1133                     e.printStackTrace();
1134                 }
1135             }
1136 
1137             try {
1138                 Object[] args = {this};
1139                 if (useSharedClassLoader && factoryMethod != null) {
1140                     return (ClassLoader) factoryMethod.invoke(null, args);
1141                 } else {
1142                     return classLoaderConstructor.newInstance(args);
1143                 }
1144             } catch (InvocationTargetException e) {
1145                 Throwable t = e.getTargetException();
1146                 if (t instanceof RuntimeException) {
1147                     throw (RuntimeException) t;
1148                 } else if (t instanceof Error) {
1149                     throw (Error) t;
1150                 } else {
1151                     throw new Error(e.toString());
1152                 }
1153             }
1154 
1155         }
1156 
1157         private Connection connection;
1158         private DataInputStream in;
1159         private DataOutputStream out;
1160         private String tag;
1161         private String request;
1162         private Integer timeoutValue;
1163     }
1164 
1165     private static Constructor<? extends ClassLoader> classLoaderConstructor;
1166     private static Method factoryMethod = null;
1167 }
1168 
1169 
1170 
1171 
1172 
1173 /**
1174  * Stream passed to the class that is executed on behalf of the client.
1175  * Data written to the stream is buffered and eventually written back to th
1176  * client via the Task's sendChars method.
1177  */
1178 class AgentWriter extends Writer {
1179     /**
1180      * Create a stream that sends its data back to the parent Task.
1181      * @arg type        A tag to pass back to parent.sendChars().
1182      * @arg parent      The parent object to which to pass the data written to the stream.
1183      */
1184     AgentWriter(byte type, Agent.Task parent) {
1185         this.type = type;


< prev index next >