📄 staxthread.java
字号:
""); //Debugging: Add following line after " for STAXkey in ..." //" print '%s, %s' % (STAXkey, type(STAXclone[STAXkey]))\n" + // Get the current date and time and set as the starting date/time fStartTimestamp = new STAXTimestamp(); } // // Thread functions // // This creates a child thread of this thread public STAXThread createChildThread() { STAXThread child = new STAXThread(); child.fJob = fJob; child.fThreadNumber = fJob.getNextThreadNumber(); child.fParent = this; child.fSignalHandlerMap = (HashMap)fSignalHandlerMap.clone(); synchronized (fPythonInterpreter) { child.fPythonInterpreter = fPythonInterpreter.clonePyi(); } child.fPythonInterpreter.set("STAXThreadID", new Integer(child.fThreadNumber)); // Get the current date and time and set as the starting date/time child.fStartTimestamp = new STAXTimestamp(); child.addCompletionNotifiee(this); synchronized (fChildMap) { fChildMap.put(child.getThreadNumberAsInteger(), child); } return child; } // Note: When using the function below, do not try to modify the iterator // passed to you, as it will be null public void visitChildren(STAXVisitor visitor) { synchronized (fChildMap) { Iterator iter = fChildMap.values().iterator(); while (iter.hasNext()) visitor.visit(iter.next(), null); } } public STAXThread getParentThread() { return fParent; } // XXX: Try to remove this function public TreeMap getChildrenThreads() { return fChildMap; } public STAXPythonInterpreter getPythonInterpreter() { synchronized(fPythonInterpreter) { return fPythonInterpreter; } } public void setPythonInterpreter(STAXPythonInterpreter pyi) { synchronized(fPythonInterpreter) { fPythonInterpreter = pyi; } } // // Miscellaneous information functions // public STAXJob getJob() { return fJob; } public int getThreadNumber() { return fThreadNumber; } public Integer getThreadNumberAsInteger() { return new Integer(fThreadNumber); } public STAXTimestamp getStartTimestamp() { return fStartTimestamp; } // // Completion notification function // public void addCompletionNotifiee(STAXThreadCompleteListener listener) { fCompletionNotifiees.addLast(listener); } // // Python Interpreter functions // // Set a Jython variable (e.g. RC, STAFResult) to a specified object. public void pySetVar(String varName, Object value) { synchronized(fPythonInterpreter) { try { fPythonInterpreter.set(varName, value); } catch (PyException e) { System.out.println("PySetVar PyException caught setting var=" + varName + " to value=" + value + e.toString()); } } } // Execute Python code. public void pyExec(String value) throws STAXPythonEvaluationException { synchronized(fPythonInterpreter) { try { if (STAX.CACHE_PYTHON_CODE) { PyCode codeObject = fJob.getCompiledPyCode(value, "exec"); synchronized(fPythonInterpreter) { fPythonInterpreter.exec(codeObject); } } else { fPythonInterpreter.exec(value); } } catch (PyException e) { throw new STAXPythonEvaluationException(e.toString()); } } } // Compile Python code (or get from cache if already compiled). // The combination of __builtin__.compile and the exec version, which // takes a code object, is used so that if Jython code is executed // more than once, the compiled code is cached, eliminating the need // to recompile the string each time it is executed. public PyObject pyCompile(String codeString) { if (STAX.CACHE_PYTHON_CODE) { PyCode codeObject = fJob.getCompiledPyCode(codeString, "eval"); synchronized(fPythonInterpreter) { // XXX: Would prefer to do fPythonInterpreter.eval(code) // instead of the following 2 lines so that could remove // assignment to STAXPyEvalResult in getCompiledPyCode, // but I don't know how to evaluate a PyCode object via // a Python Interpreter as eval only accepts a string, // not a PyCode object. fPythonInterpreter.exec(codeObject); return fPythonInterpreter.get("STAXPyEvalResult"); } } else { synchronized(fPythonInterpreter) { return fPythonInterpreter.eval(codeString); } } } // Evaluate a value using Jython and return a PyObject result. public PyObject pyObjectEval(String value) throws STAXPythonEvaluationException { try { return pyCompile(value); } catch (PyException e) { throw new STAXPythonEvaluationException( "\nPython object evaluation failed for:\n" + value + "\n\n" + e.toString()); } } // Evaluate a value using Jython and return a String result. public String pyStringEval(String value) throws STAXPythonEvaluationException { try { return pyCompile(value).__str__().toString(); } catch (PyException e) { throw new STAXPythonEvaluationException( "\nPython string evaluation failed for:\n" + value + "\n\n" + e.toString()); } } // Evaluate an expression using Jython and return a boolean result. public boolean pyBoolEval(String expression) throws STAXPythonEvaluationException { try { return Py.py2boolean(pyCompile(expression)); } catch (PyException e) { throw new STAXPythonEvaluationException( "\nPython boolean evaluation failed for:\n" + expression + "\n\n" + e.toString()); } } // Evaluate a value using Jython and return an integer result. public int pyIntEval(String value) throws STAXPythonEvaluationException { try { return Py.py2int(pyCompile(value)); } catch (PyException e) { throw new STAXPythonEvaluationException( "\nPython integer evaluation failed for:\n" + value + "\n\n" + e.toString()); } } // Evaluate a value (which evaluates to a list or tuple) using Jython, // extract the Jython list/tuple into a Java List, and return a List. // Examples of possible values passed in: // "machList", ['machA','machB'], ('machA','machB',machC'), // machList[1:], machList[:-1], ['machA','machB'] + ['machC','machD'] public List pyListEval(String value) throws STAXPythonEvaluationException { List jList = new ArrayList(); synchronized(fPythonInterpreter) { PyObject result = null; try { result = pyCompile(value); } catch (PyException e) { throw new STAXPythonEvaluationException( "\nPython list evaluation failed for:\n" + value + "\n" + e.toString()); } // Check if value is a STAXGlobal object try { if (pyBoolEval("isinstance(" + value + ", STAXGlobal)")) { // Use the STAXGlobal class's get() method to retrieve // it's contents result = pyCompile(value + ".get()"); } } catch (PyException e) { // Ignore error and assume not a STAXGlobal object } // Check if already is a Java List object Object javaObj = result.__tojava__(Object.class); if (javaObj instanceof java.util.List) return (List)javaObj; try { // Convert Python object to a Java ArrayList object fPythonInterpreter.set("STAXIterateList", result); if (Py.py2boolean(result)) { jList = new ArrayList(Arrays.asList( (Object[])fPythonInterpreter.get( "STAXIterateList", Object[].class))); } } catch (PyException e) { try { jList = new ArrayList(); jList.add(javaObj); } catch (PyException e2) { throw new STAXPythonEvaluationException( "\nPython list evaluation failed for:\n" + value + "\n" + e.toString() + "\n" + e2.toString()); } } } return jList; } // Evaluate a value (which evaluates to a Python dictionary) using Jython, // extract the contents of the PyDictionary into a Java Map, and return a // Map. // Example of possible value that can be passed in: // "{'server': 'machineA', 'testDir': 'C:/tests'}" public Map pyMapEval(String value) throws STAXPythonEvaluationException { // Extract a Python dictionary into a Java Map // Create an empty HashMap Map jMap = new HashMap(); try { PyObject result = pyCompile(value); // If a null string, an empty string, or "None" is passed in, // return an empty map. if ((result != null) && !(result.toString().equals("None")) && (result.toString() != "")) { if (result instanceof PyDictionary) { PyList pa = ((PyDictionary)result).items(); while (pa.__len__() != 0) { PyTuple po = (PyTuple)pa.pop(); Object first = po.__finditem__(0).__tojava__(Object.class); PyObject second = po.__finditem__(1); jMap.put(first, second); } } else { throw new STAXPythonEvaluationException( "\nThe following string does not evaluate " + "to a PyDictionary:\n" + value); } } } catch (PyException e) { throw new STAXPythonEvaluationException( "\nPython dictionary evaluation failed for:\n" + value + "\n" + e.toString()); } return jMap; } // // Signal functions // public void registerSignalHandler(String name, STAXSignalExecutionAction signalHandler) { fSignalHandlerMap.put(name, signalHandler); } public void raiseSignal(String name) { // Check for this signal already // XXX: Should also check if same signalhandler action if (fSignalStack.contains(name)) { getJob().log(STAXJob.JOB_LOG, "error", "Duplicate signal " + name + ". Terminating job."); terminate(THREAD_END_DUPLICATE_SIGNAL); return; } STAXAction action = (STAXAction)fSignalHandlerMap.get(name); if (action == null) { if (name != "STAXNoSuchSignalHandler") { setSignalMsgVar("STAXNoSuchSignalHandlerMsg", "<raise signal=\"" + name + "\"/>"); raiseSignal("STAXNoSuchSignalHandler"); } else { // Should not happen - needed to break recursion getJob().log(STAXJob.JOB_LOG, "error", "No signal " + "handler exists for signal STAXNoSuchSignalHandler"); } } else { pushAction(action.cloneAction()); fSignalStack.addLast(name); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -