📄 stafdemocontroller.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001, 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/import java.io.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import com.ibm.staf.*;public class STAFDemoController extends JFrame{ public static void main(String []argv) { STAFDemoController app = new STAFDemoController(); // Dimension dim = app.getPreferredSize(); // app.setSize(dim.width, dim.height); app.setSize(300, 200); app.show(); } static STAFHandle handle; static String stafMachineName; static String stafMachineNickname; static int numResources = 2; // This version of STAFDemo can only communicate with machines // running STAF V3.x. static int mainVersion = 3; JPanel stafStartPanel; String targetJavacmd; String targetClasspath; public STAFDemoController() { super("STAF Demo Controller"); try { handle = new STAFHandle("STAFDemo_Controller"); } catch(STAFException e) { System.out.println("Error registering with STAF, RC: " + e.rc); System.exit(e.rc); } STAFResult stafResult = handle.submit2( "local", "VAR", "RESOLVE STRING {STAF/Config/Machine}"); if (stafResult.rc != 0) System.out.println("Error getting local machine name RC: "+ stafResult.rc + " Result: " + stafResult.result); stafMachineName = stafResult.result; stafResult = handle.submit2( "local", "VAR", "RESOLVE STRING {STAF/Config/MachineNickname}"); if (stafResult.rc != 0) System.out.println("Error getting local machine nickname RC:" + " " + stafResult.rc + " Result: " + stafResult.result); stafMachineNickname = stafResult.result; stafResult = handle.submit2( "local", "RESPOOL", "DELETE POOL STAFDemo CONFIRM FORCE"); if ((stafResult.rc != 0) && (stafResult.rc != STAFResult.DoesNotExist)) System.out.println("Error deleting STAFDemo resource pool RC: "+ stafResult.rc + " Result: " + stafResult.result); stafResult = handle.submit2( "local", "RESPOOL", "CREATE POOL STAFDemo DESCRIPTION \"" + "STAF Demo Resource Pool\""); if (stafResult.rc != 0) System.out.println("Error creating STAFDemo resource pool RC: " + stafResult.rc + " Result: " + stafResult.result); for (int i = 1; i <= numResources; ++i) { stafResult = handle.submit2( "local", "RESPOOL", "ADD POOL " + "STAFDemo ENTRY Resource" + String.valueOf(i)); if (stafResult.rc != 0) System.out.println("Error adding resource to STAFDemo RC: "+ stafResult.rc + " Result: " + stafResult.result); } ProcessEndThread procThread = new ProcessEndThread(); procThread.start(); stafStartPanel = new STAFStartPanel(); getContentPane().add(stafStartPanel); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { try { handle.submit2( "local", "QUEUE", "QUEUE HANDLE " + handle.getHandle() + " TYPE STAF/STAFDemo/Stop MESSAGE " + STAFUtil.wrapData("")); handle.unRegister(); } catch(STAFException e) { System.out.println("Error unregistering with STAF, RC: " + e.rc); } dispose(); System.exit(0); } }); } class ProcessEndThread extends Thread { public void run() { for(;;) { STAFResult stafResult = STAFDemoController.handle.submit2( "local", "QUEUE", "GET WAIT"); int index = 0; // Unmarshall the result from a QUEUE GET request STAFMarshallingContext outputContext = STAFMarshallingContext.unmarshall(stafResult.result); Map queueMap = (Map)outputContext.getRootObject(); String type = (String)queueMap.get("type"); if (type != null) { if (type.equalsIgnoreCase("STAF/Process/End")) { String theHandle = (String)queueMap.get("handle"); String theMachine = (String)queueMap.get("machine"); STAFDemoController.handle.submit2( theMachine, "PROCESS", "FREE HANDLE " + theHandle); } else if (type.equalsIgnoreCase("STAF/STAFDemo/Stop")) { return; } } } } } public class STAFStartPanel extends JPanel { JTextField fMachine = new JTextField(25); JTextField fHandle = new JTextField(25); JButton fStart = new JButton("Start"); JButton fConnect = new JButton("Connect"); JLabel fNumResourcesLabel = new JLabel(); JButton fAddResource = new JButton("+"); JButton fDeleteResource = new JButton("-"); String fMachineNickname; public STAFStartPanel() { // Setup display GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(gbl); gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; // Setup Process portion of panel JPanel processPanel = new JPanel(); processPanel.setBorder(new TitledBorder("Process")); processPanel.setLayout(new GridBagLayout()); GridBagConstraints procGBC = new GridBagConstraints(); procGBC.anchor = GridBagConstraints.WEST; procGBC.fill = GridBagConstraints.HORIZONTAL; procGBC.gridwidth = 1; processPanel.add(new JLabel("Machine:"), procGBC); processPanel.add(Box.createHorizontalStrut(5)); procGBC.gridwidth = GridBagConstraints.REMAINDER; fMachine.setText(stafMachineName); processPanel.add(fMachine, procGBC); procGBC.gridwidth = 1; processPanel.add(fStart, procGBC); processPanel.add(Box.createHorizontalStrut(5)); procGBC.gridwidth = GridBagConstraints.REMAINDER; processPanel.add(new JLabel("a new process"), procGBC); procGBC.gridwidth = 1; processPanel.add(fConnect, procGBC); processPanel.add(Box.createHorizontalStrut(5)); processPanel.add(new JLabel("to handle:")); processPanel.add(Box.createHorizontalStrut(5)); procGBC.gridwidth = GridBagConstraints.REMAINDER; processPanel.add(fHandle, procGBC); add(processPanel, gbc); // Setup ResPool panel JPanel respoolPanel = new JPanel(); respoolPanel.setBorder(new TitledBorder("Resource Pool")); respoolPanel.add(new JLabel("Number of resource:")); respoolPanel.add(Box.createHorizontalStrut(5)); fNumResourcesLabel.setText( String.valueOf(STAFDemoController.numResources)); respoolPanel.add(fNumResourcesLabel); respoolPanel.add(Box.createHorizontalStrut(10)); respoolPanel.add(fAddResource); respoolPanel.add(fDeleteResource); add(respoolPanel, gbc); // Setup button actions fStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String machineName = fMachine.getText(); STAFResult stafResult = handle.submit2( machineName, "MISC", "VERSION"); if (stafResult.rc == 0) { String versionString = stafResult.result; int version = 0; try { version = Integer.parseInt( versionString.substring(0, 1)); } catch(NumberFormatException e) { /* Do Nothing */ } if (version < mainVersion) { String errMsg = "\nMachine " + machineName + " is running STAF Version " + versionString + ".\nThis version of STAFDemo requires " + "STAF Version " + mainVersion + ".0.0 or later"; JOptionPane.showMessageDialog(stafStartPanel, errMsg, "Error starting process", JOptionPane.ERROR_MESSAGE); return; } } else { String errMsg = "\nThis machine could not communicate with " + "machine " + machineName + "\nRC: " + stafResult.rc + " Result: " + stafResult.result; JOptionPane.showMessageDialog(stafStartPanel, errMsg, "Error starting process", JOptionPane.ERROR_MESSAGE); return; } String sepPath = ";"; String sepFile = "/"; String jstafDir = ""; stafResult = handle.submit2( machineName, "VAR", "RESOLVE STRING {STAF/Config/MachineNickname}" + " STRING {STAF/Config/Sep/Path}" + " STRING {STAF/Config/Sep/File}" + " STRING {STAF/Config/OS/Name}"); if (stafResult.rc != 0) { String errMsg = "\nThis machine could not resolve STAF variables on " + "machine " + machineName + "\nRC: " + stafResult.rc + " Result: " + stafResult.result; JOptionPane.showMessageDialog(stafStartPanel, errMsg, "Error starting process", JOptionPane.ERROR_MESSAGE); return; } // Unmarshall the marshalled list of var resolve results STAFMarshallingContext mc = STAFMarshallingContext.unmarshall(stafResult.result); java.util.List varList = (java.util.List)mc.getRootObject(); for (int i = 0; i < varList.size(); i++) { Map varInfo = (Map)varList.get(i); String varRC = (String)varInfo.get("rc"); if (!varRC.equals("0")) { String errMsg = "\nThis machine could not resolve a STAF " + "variable on machine " + machineName + "\nRC: " + varInfo.get("rc") + " Result: " + varInfo.get("result"); JOptionPane.showMessageDialog(stafStartPanel, errMsg, "Error starting process", JOptionPane.ERROR_MESSAGE); return; } switch (i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -