1 /*****************************************************************************/ 2 /* Software Testing Automation Framework (STAF) */ 3 /* (C) Copyright IBM Corp. 2002 */ 4 /* */ 5 /* This software is licensed under the Eclipse Public License (EPL) V1.0. */ 6 /*****************************************************************************/ 7 8 package com.ibm.staf.service.stax; 9 10 import java.util.Iterator; 11 import java.util.Hashtable; 12 import java.util.Map; 13 import java.util.HashMap; 14 import java.util.List; 15 import org.python.core.Py; 16 import org.python.core.PyObject; 17 import org.python.core.PyList; 18 import org.python.core.PyDictionary; 19 20 public class STAXCatchAction extends STAXActionDefaultImpl 21 { 22 private static final int INIT = 0; 23 private static final int CALLED_ACTION = 1; 24 private static final int COMPLETE = 2; 25 26 private static final String INIT_STRING = "INIT"; 27 private static final String CALLED_ACTION_STRING = "CALLED_ACTION"; 28 private static final String COMPLETE_STRING = "COMPLETE"; 29 private static final String STATE_UNKNOWN_STRING = "UNKNOWN"; 30 STAXCatchAction()31 public STAXCatchAction() 32 { /* Do Nothing */ } 33 STAXCatchAction(String exceptionName, String varName, String typeVarName, String sourceVarName, STAXAction action)34 public STAXCatchAction(String exceptionName, String varName, 35 String typeVarName, String sourceVarName, 36 STAXAction action) 37 { 38 fExceptionName = exceptionName; 39 fVarName = varName; 40 fTypeVarName = typeVarName; 41 fSourceVarName = sourceVarName; 42 fAction = action; 43 } 44 setExceptionName(String exceptionName)45 public void setExceptionName(String exceptionName) 46 { 47 fExceptionName = exceptionName; 48 } 49 setVarName(String varName)50 public void setVarName(String varName) 51 { 52 fVarName = varName; 53 } 54 setTypeVarName(String typeVarName)55 public void setTypeVarName(String typeVarName) 56 { 57 fTypeVarName = typeVarName; 58 } 59 setSourceVarName(String sourceVarName)60 public void setSourceVarName(String sourceVarName) 61 { 62 fSourceVarName = sourceVarName; 63 } 64 setAction(STAXAction action)65 public void setAction(STAXAction action) 66 { 67 fAction = action; 68 } 69 getCatchableExceptionName()70 public String getCatchableExceptionName() { return fExceptionName; } 71 setException(STAXExceptionCondition cond)72 public void setException(STAXExceptionCondition cond) 73 { 74 fThrownException = cond; 75 } 76 getStateAsString()77 public String getStateAsString() 78 { 79 switch (fState) 80 { 81 case INIT: 82 return INIT_STRING; 83 case CALLED_ACTION: 84 return CALLED_ACTION_STRING; 85 case COMPLETE: 86 return COMPLETE_STRING; 87 default: 88 return STATE_UNKNOWN_STRING; 89 } 90 } 91 getInfo()92 public String getInfo() { return fExceptionName; } 93 getDetails()94 public String getDetails() 95 { 96 return "State:" + getStateAsString() + 97 ";ExceptionName:" + fExceptionName + 98 ";VarName:" + fVarName + 99 ";TypeVarName:" + fTypeVarName + 100 ";SourceVarName:" + fSourceVarName + 101 ";Action:" + fAction + 102 ";ExceptionCondition:" + fThrownException; 103 } 104 execute(STAXThread thread)105 public void execute(STAXThread thread) 106 { 107 if (fState == INIT) 108 { 109 if (fVarName != null) 110 thread.pySetVar(fVarName, fThrownException.getData()); 111 112 if (fTypeVarName != null) 113 thread.pySetVar(fTypeVarName, fThrownException.getName()); 114 115 if (fSourceVarName != null) 116 { 117 String SOURCE_VAR_PYCODE = 118 fSourceVarName + " = STAXExceptionSource(" + 119 createSourceMap() + ") \n" + 120 "\n"; 121 122 try 123 { 124 thread.pyExec(SOURCE_VAR_PYCODE); 125 } 126 catch (STAXPythonEvaluationException ex) 127 { 128 STAX.logToJVMLog( 129 "Error", thread, 130 "Exception evaluating SOURCE_VAR_PYCODE=" + 131 SOURCE_VAR_PYCODE); 132 ex.printStackTrace(); 133 } 134 } 135 136 thread.pushAction(fAction.cloneAction()); 137 fState = CALLED_ACTION; 138 } 139 else 140 { 141 fState = COMPLETE; 142 thread.popAction(); 143 } 144 } 145 handleCondition(STAXThread thread, STAXCondition cond)146 public void handleCondition(STAXThread thread, STAXCondition cond) 147 { 148 fState = COMPLETE; 149 thread.popAction(); 150 151 // Remove any rethrow exception conditions from the condition stack 152 // that this catch action handles. This way, there won't be any 153 // "dangling" rethrow exception conditions hanging around if a 154 // terminate block condition is added to the condition stack. 155 // Also, set a flag to indicate if any rethrow exception conditions 156 // were on the condition stack so that we can add an exception 157 // condition to the condition stack. 158 159 fFoundRethrow = false; 160 161 thread.visitConditions(new STAXVisitor() 162 { 163 public void visit(Object o, Iterator iter) 164 { 165 if (o instanceof STAXRethrowExceptionCondition) 166 { 167 iter.remove(); 168 fFoundRethrow = true; 169 } 170 } 171 }); 172 173 if (fFoundRethrow) 174 { 175 thread.addCondition(fThrownException); 176 } 177 } 178 cloneAction()179 public STAXAction cloneAction() 180 { 181 STAXCatchAction clone = new STAXCatchAction(); 182 183 clone.setElement(getElement()); 184 clone.setLineNumberMap(getLineNumberMap()); 185 clone.setXmlFile(getXmlFile()); 186 clone.setXmlMachine(getXmlMachine()); 187 188 clone.fExceptionName = fExceptionName; 189 clone.fVarName = fVarName; 190 clone.fTypeVarName = fTypeVarName; 191 clone.fSourceVarName = fSourceVarName; 192 clone.fAction = fAction; 193 194 return clone; 195 } 196 createSourceMap()197 private PyDictionary createSourceMap() 198 { 199 Map<PyObject, PyObject> m = new HashMap<PyObject, PyObject>(); 200 201 m.put(Py.java2py("source"), 202 Py.java2py(fThrownException.getSource())); 203 204 m.put(Py.java2py("stackTrace"), 205 convertJavaStackTraceToPython(fThrownException.getStackTrace())); 206 207 PyDictionary pyDictionary = new PyDictionary( 208 new Hashtable<PyObject, PyObject>(m)); 209 210 return pyDictionary; 211 } 212 convertJavaStackTraceToPython(List<String> javaList)213 private static PyList convertJavaStackTraceToPython(List<String> javaList) 214 { 215 PyList pyList = new PyList(); 216 217 for (String currentItem : javaList) 218 { 219 pyList.append(Py.java2py(currentItem)); 220 } 221 222 return pyList; 223 } 224 225 private int fState = INIT; 226 private String fExceptionName = null; 227 private String fVarName = null; 228 private String fTypeVarName = null; 229 private String fSourceVarName = null; 230 private STAXAction fAction = null; 231 private STAXExceptionCondition fThrownException; 232 private boolean fFoundRethrow = false; 233 } 234