1*cdf0e10cSrcweir package complexlib; 2*cdf0e10cSrcweir 3*cdf0e10cSrcweir import java.io.PrintWriter; 4*cdf0e10cSrcweir import java.lang.reflect.Method; 5*cdf0e10cSrcweir 6*cdf0e10cSrcweir /** 7*cdf0e10cSrcweir * Invoke a method of a class in an own thread. Provide a method to end 8*cdf0e10cSrcweir * the thread. 9*cdf0e10cSrcweir */ 10*cdf0e10cSrcweir public class MethodThread extends Thread 11*cdf0e10cSrcweir { 12*cdf0e10cSrcweir 13*cdf0e10cSrcweir /** The method that should be executed **/ 14*cdf0e10cSrcweir private Method mTestMethod = null; 15*cdf0e10cSrcweir /** The object that implements the method **/ 16*cdf0e10cSrcweir private Object mInvokeClass = null; 17*cdf0e10cSrcweir /** A PrintWriter for debug Output **/ 18*cdf0e10cSrcweir private PrintWriter mLog = null; 19*cdf0e10cSrcweir /** An Error String **/ 20*cdf0e10cSrcweir private String mErrMessage = null; 21*cdf0e10cSrcweir /** Did an Exception happen? **/ 22*cdf0e10cSrcweir private boolean mExceptionHappened = false; 23*cdf0e10cSrcweir private Object[] mParameter = null; 24*cdf0e10cSrcweir 25*cdf0e10cSrcweir /** 26*cdf0e10cSrcweir * Constructor. 27*cdf0e10cSrcweir * @param testMethod The method that will be invoked. 28*cdf0e10cSrcweir * @param invokeClass The class where the method is invoked. 29*cdf0e10cSrcweir * @param log The logging mechanism. 30*cdf0e10cSrcweir */ 31*cdf0e10cSrcweir public MethodThread(Method testMethod, Object invokeClass, PrintWriter log) 32*cdf0e10cSrcweir { 33*cdf0e10cSrcweir mTestMethod = testMethod; 34*cdf0e10cSrcweir mInvokeClass = invokeClass; 35*cdf0e10cSrcweir mLog = log; 36*cdf0e10cSrcweir } 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir public MethodThread(Method testMethod, Object invokeClass, Object[] parameter, PrintWriter log) 39*cdf0e10cSrcweir { 40*cdf0e10cSrcweir mTestMethod = testMethod; 41*cdf0e10cSrcweir mInvokeClass = invokeClass; 42*cdf0e10cSrcweir mParameter = parameter; 43*cdf0e10cSrcweir mLog = log; 44*cdf0e10cSrcweir } 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir /** 47*cdf0e10cSrcweir * Invoke the method. 48*cdf0e10cSrcweir */ 49*cdf0e10cSrcweir public void run() 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir try 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir mTestMethod.invoke(mInvokeClass, mParameter); 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir catch (IllegalAccessException e) 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir e.printStackTrace(mLog); 58*cdf0e10cSrcweir mErrMessage = e.getMessage(); 59*cdf0e10cSrcweir mExceptionHappened = true; 60*cdf0e10cSrcweir } 61*cdf0e10cSrcweir catch (java.lang.reflect.InvocationTargetException e) 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir Throwable t = e.getTargetException(); 64*cdf0e10cSrcweir if (!(t instanceof ComplexTestCase.AssureException)) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir t.printStackTrace(mLog); 67*cdf0e10cSrcweir mErrMessage = t.getMessage(); 68*cdf0e10cSrcweir if (mErrMessage == null) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir mErrMessage = t.toString(); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir mExceptionHappened = true; 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /** 79*cdf0e10cSrcweir * Get the error message 80*cdf0e10cSrcweir * @return The error message. 81*cdf0e10cSrcweir */ 82*cdf0e10cSrcweir public String getErrorMessage() 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir return mErrMessage; 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir /** 88*cdf0e10cSrcweir * Is there an error message? 89*cdf0e10cSrcweir * @return True, if an error did happen. 90*cdf0e10cSrcweir */ 91*cdf0e10cSrcweir public boolean hasErrorMessage() 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir return mExceptionHappened; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir /** 97*cdf0e10cSrcweir * Stop the running method. 98*cdf0e10cSrcweir */ 99*cdf0e10cSrcweir public void destroy() 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir try 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir interrupt(); 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir catch (SecurityException e) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir e.printStackTrace(mLog); 108*cdf0e10cSrcweir mErrMessage = e.getMessage(); 109*cdf0e10cSrcweir mExceptionHappened = true; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir } 113