📄 staxblockaction.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2002 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/package com.ibm.staf.service.stax;import com.ibm.staf.*;import java.util.TreeMap;import java.util.LinkedList;import java.util.Iterator;import java.util.HashMap;import org.python.core.Py;public class STAXBlockAction implements STAXAction{ static final int INIT = 0; static final int ACTION_CALLED = 1; static final int COMPLETE = 2; static final String INIT_STRING = "INIT"; static final String ACTION_CALLED_STRING = "ACTION_CALLED"; static final String COMPLETE_STRING = "COMPLETE"; static final String STATE_UNKNOWN_STRING = "UNKNOWN"; static final int BLOCK_HELD = 0; static final int BLOCK_RUNNING = 1; static final String BLOCK_HELD_STRING = new String("Held"); static final String BLOCK_RUNNING_STRING = new String("Running"); static final String BLOCK_UNKNOWN_STRING = new String("Unknown"); private STAXBlockAction() { /* Do Nothing */ } public STAXBlockAction(String name, STAXAction action) { fName = name; fUnevalName = name; fAction = action; } public String getName() { return fName; } public STAXThread getOwningThread() { return fOwningThread; } public STAXTimestamp getStartTimestamp() { return fStartTimestamp; } public int getBlockState() { return fBlockState; } public String getBlockStateAsString() { switch (fBlockState) { case BLOCK_HELD: return BLOCK_HELD_STRING; case BLOCK_RUNNING: return BLOCK_RUNNING_STRING; default: return BLOCK_UNKNOWN_STRING; } } public void holdBlock() { fBlockState = BLOCK_HELD; String msg = "Holding block: " + fName; fOwningThread.getJob().log(STAXJob.JOB_LOG, "info", msg); HashMap blockHoldMap = new HashMap(); blockHoldMap.put("type", "block"); blockHoldMap.put("block", fName); blockHoldMap.put("status", "hold"); blockHoldMap.put("name", fName); fOwningThread.getJob().generateEvent( STAXBlockActionFactory.STAX_BLOCK_EVENT, blockHoldMap); fOwningThread.visitChildren(new STAXVisitorHelper(fHoldCondition) { public void visit(Object o, Iterator iter) { STAXThread childThread = (STAXThread)o; childThread.visitChildren(this); childThread.addCondition((STAXCondition)fData); } }); fOwningThread.addCondition(fHoldCondition); } public void releaseBlock() { fBlockState = BLOCK_RUNNING; String msg = "Releasing block: " + fName; fOwningThread.getJob().log(STAXJob.JOB_LOG, "info", msg); HashMap blockReleaseMap = new HashMap(); blockReleaseMap.put("type", "block"); blockReleaseMap.put("block", fName); blockReleaseMap.put("status", "release"); blockReleaseMap.put("name", fName); fOwningThread.getJob().generateEvent( STAXBlockActionFactory.STAX_BLOCK_EVENT, blockReleaseMap); fOwningThread.visitChildren(new STAXVisitorHelper(fHoldCondition) { public void visit(Object o, Iterator iter) { STAXThread childThread = (STAXThread)o; childThread.visitChildren(this); childThread.removeCondition((STAXCondition)fData); childThread.schedule(); } }); fOwningThread.removeCondition(fHoldCondition); fOwningThread.schedule(); } public void terminateBlock() { String msg = "Terminating block: " + fName; fOwningThread.getJob().log(STAXJob.JOB_LOG, "info", msg); HashMap blockTermMap = new HashMap(); blockTermMap.put("type", "block"); blockTermMap.put("block", fName); blockTermMap.put("status", "terminate"); blockTermMap.put("name", fName); fOwningThread.getJob().generateEvent( STAXBlockActionFactory.STAX_BLOCK_EVENT, blockTermMap); fOwningThread.visitChildren(new STAXVisitorHelper(fTermCondition) { public void visit(Object o, Iterator iter) { STAXThread childThread = (STAXThread)o; childThread.visitChildren(this); childThread.addCondition((STAXCondition)fData); childThread.schedule(); } }); fOwningThread.addCondition(fTermCondition); fOwningThread.schedule(); } public String getStateAsString() { switch (fState) { case INIT: return INIT_STRING; case ACTION_CALLED: return ACTION_CALLED_STRING; case COMPLETE: return COMPLETE_STRING; default: return STATE_UNKNOWN_STRING; } } public String getXMLInfo() { return "<block name=\"" + fUnevalName + "\">"; } public String getInfo() { return fName; } public String getDetails() { return "Name:" + fName + ";Action:" + fAction + ";State:" + getStateAsString() + ";BlockState:" + getBlockStateAsString() + ";StartTimestamp:" + fStartTimestamp + ";OwningThread:" + fOwningThread + ";HoldThreadCondition:" + fHoldCondition + ";TerminateBlockCondition:" + fTermCondition; } public void execute(STAXThread thread) { if (fState == INIT) { // Get the current date and time and set as the starting date/time fStartTimestamp = new STAXTimestamp(); // Get the Current Block Name (aka Parent Block Name) String parentBlockName; try { // STAXCurrentBlock = "None" if no current block parentBlockName = thread.pyStringEval("STAXCurrentBlock"); } catch (STAXPythonEvaluationException e) { parentBlockName = "None"; } if (!parentBlockName.equals("None")) { try { fName = parentBlockName + "." + thread.pyStringEval(fUnevalName); } catch (STAXPythonEvaluationException e) { fState = COMPLETE; thread.popAction(); thread.setSignalMsgVar("STAXPythonEvalMsg", getXMLInfo(), e); thread.raiseSignal("STAXPythonEvaluationError"); return; } } // Enter Block TreeMap blockMap = (TreeMap)thread.getJob().getData("blockMap"); boolean valid_blockName; synchronized (blockMap) { if (blockMap.containsKey(fName)) valid_blockName = false; else { blockMap.put(fName, this); valid_blockName = true; } } if (!valid_blockName) { fState = COMPLETE; thread.popAction(); String msg = getXMLInfo() + "\n\n Full Block Name: " + fName + "\n\n A block with this name already exists. " + "You cannot have more than one block with the same name." + "\n Note that this situation can easily occur if you " + "specify a <block> element using a literal block name " + "within a <paralleliterate> element.\n Instead, use a " + "variable in your block name to uniquely identify it. " + " For example: <block name=\"'BlockA_%s' % machName\">"; thread.setSignalMsgVar("STAXInvalidBlockNameMsg", msg); thread.raiseSignal("STAXInvalidBlockName"); return; } try { thread.pySetVar("STAXCurrentBlock", fName); thread.pyExec("STAXBlockStack.append(STAXCurrentBlock)"); } catch (STAXPythonEvaluationException e) { fState = COMPLETE; thread.popAction(); thread.getJob().log(STAXJob.JOB_LOG, "error", "STAXBlockAction: Enter block " + fName + " failed with " + e.toString()); return; } HashMap blockBeginMap = new HashMap(); blockBeginMap.put("type", "block"); blockBeginMap.put("block", fName); blockBeginMap.put("status", "begin"); blockBeginMap.put("name", fName); thread.getJob().generateEvent( STAXBlockActionFactory.STAX_BLOCK_EVENT, blockBeginMap); thread.pushAction(fAction.cloneAction()); fOwningThread = thread; fState = ACTION_CALLED; } else if (fState == ACTION_CALLED) { HashMap blockEndMap = new HashMap(); blockEndMap.put("type", "block"); blockEndMap.put("block", fName); blockEndMap.put("status", "end"); blockEndMap.put("name", fName); thread.getJob().generateEvent( STAXBlockActionFactory.STAX_BLOCK_EVENT, blockEndMap); fState = COMPLETE; // Exit Block exitBlock(fName, thread); thread.popAction(); } } public void exitBlock(String name, STAXThread thread) { try { if (thread.pyBoolEval( "STAXBlockStack[len(STAXBlockStack)-1] != " + "STAXCurrentBlock")) { thread.getJob().log(STAXJob.JOB_LOG, "error", "STAXBlockAction: Exit Block " + name + " failed. This block is not on the STAXBlockStack"); } else { thread.pyExec("STAXBlockStack.pop()"); if (thread.pyBoolEval("len(STAXBlockStack) > 0")) thread.pyExec("STAXCurrentBlock = " + "STAXBlockStack[len(STAXBlockStack)-1]"); else thread.pySetVar("STAXCurrentBlock", Py.None); } } catch (STAXPythonEvaluationException e) { thread.getJob().log(STAXJob.JOB_LOG, "error", "STAXBlockAction: Exit Block(" + name + " failed with " + e.toString()); } TreeMap blockMap = (TreeMap)thread.getJob().getData("blockMap"); synchronized (blockMap) { blockMap.remove(name); } } public void handleCondition(STAXThread thread, STAXCondition cond) { if (fState == ACTION_CALLED) { HashMap blockEndMap = new HashMap(); blockEndMap.put("type", "block"); blockEndMap.put("block", fName); blockEndMap.put("status", "end"); blockEndMap.put("name", fName); thread.getJob().generateEvent( STAXBlockActionFactory.STAX_BLOCK_EVENT, blockEndMap); // Exit Block exitBlock(fName, thread); } fState = COMPLETE; thread.removeCondition(fHoldCondition); thread.removeCondition(fTermCondition); thread.popAction(); } public STAXAction cloneAction() { STAXBlockAction clone = new STAXBlockAction(); clone.fUnevalName = fUnevalName; clone.fName = fName; clone.fAction = fAction; return clone; } private int fState = INIT; private int fBlockState = BLOCK_RUNNING; private String fName; private String fUnevalName; private STAXAction fAction = null; private STAXThread fOwningThread = null; private STAXHoldThreadCondition fHoldCondition = new STAXHoldThreadCondition("Block"); private STAXTerminateBlockCondition fTermCondition = new STAXTerminateBlockCondition("Block"); private STAXTimestamp fStartTimestamp;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -