1 18 19 package org.apache.jmeter.gui.action; 20 import java.awt.Component ; 21 import java.awt.event.ActionEvent ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.swing.JOptionPane ; 28 29 import org.apache.jmeter.engine.ClientJMeterEngine; 30 import org.apache.jmeter.engine.JMeterEngine; 31 import org.apache.jmeter.engine.JMeterEngineException; 32 import org.apache.jmeter.gui.GuiPackage; 33 import org.apache.jmeter.util.JMeterUtils; 34 import org.apache.jorphan.collections.HashTree; 35 import org.apache.jorphan.logging.LoggingManager; 36 import org.apache.log.Logger; 37 38 43 public class RemoteStart extends AbstractAction 44 { 45 transient private static Logger log = LoggingManager.getLoggerForClass(); 46 47 private static Set commands = new HashSet (); 48 static 49 { 50 commands.add("remote_start"); 51 commands.add("remote_stop"); 52 commands.add("remote_start_all"); 53 commands.add("remote_stop_all"); 54 commands.add("remote_exit"); 55 commands.add("remote_exit_all"); 56 } 57 58 private Map remoteEngines = new HashMap (); 59 60 public RemoteStart() 61 { 62 } 63 64 public void doAction(ActionEvent e) 65 { 66 String name = ((Component ) e.getSource()).getName(); 67 if(name != null) 68 { 69 name = name.trim(); 70 } 71 String action = e.getActionCommand(); 72 if (action.equals("remote_stop")) 73 { 74 doRemoteStop(name); 75 } 76 else if (action.equals("remote_start")) 77 { 78 doRemoteInit(name); 79 doRemoteStart(name); 80 } 81 else if (action.equals("remote_start_all")) 82 { 83 String remote_hosts_string = 84 JMeterUtils.getPropDefault("remote_hosts", "127.0.0.1"); 85 java.util.StringTokenizer st = 86 new java.util.StringTokenizer (remote_hosts_string, ","); 87 while (st.hasMoreElements()) 88 { 89 String el = (String ) st.nextElement(); 90 doRemoteInit(el.trim()); 91 } 92 st = new java.util.StringTokenizer (remote_hosts_string, ","); 93 while (st.hasMoreElements()) 94 { 95 String el = (String ) st.nextElement(); 96 doRemoteStart(el.trim()); 97 } 98 } 99 else if (action.equals("remote_stop_all")) 100 { 101 String remote_hosts_string = 102 JMeterUtils.getPropDefault("remote_hosts", "127.0.0.1"); 103 java.util.StringTokenizer st = 104 new java.util.StringTokenizer (remote_hosts_string, ","); 105 while (st.hasMoreElements()) 106 { 107 String el = (String ) st.nextElement(); 108 doRemoteStop(el.trim()); 109 } 110 } 111 else if (action.equals("remote_exit")) 112 { 113 doRemoteExit(name); 114 } 115 else if (action.equals("remote_exit_all")) 116 { 117 String remote_hosts_string = 118 JMeterUtils.getPropDefault("remote_hosts", "127.0.0.1"); 119 java.util.StringTokenizer st = 120 new java.util.StringTokenizer (remote_hosts_string, ","); 121 while (st.hasMoreElements()) 122 { 123 String el = (String ) st.nextElement(); 124 doRemoteExit(el.trim()); 125 } 126 } 127 } 128 129 135 private void doRemoteStop(String name) 136 { 137 GuiPackage.getInstance().getMainFrame().showStoppingMessage(name); 138 JMeterEngine engine = (JMeterEngine) remoteEngines.get(name); 139 engine.stopTest(); 140 } 141 142 148 private void doRemoteExit(String name) 149 { 150 JMeterEngine engine = (JMeterEngine) remoteEngines.get(name); 151 if (engine == null) return; 152 engine.exit(); 154 } 155 156 162 private void doRemoteStart(String name) 163 { 164 JMeterEngine engine = (JMeterEngine) remoteEngines.get(name); 165 if (engine == null) 166 { 167 try 168 { 169 engine = new ClientJMeterEngine(name); 170 remoteEngines.put(name, engine); 171 } 172 catch (Exception ex) 173 { 174 log.error("", ex); 175 JMeterUtils.reportErrorToUser("Bad call to remote host"); 176 return; 177 } 178 } 179 else 180 { 181 engine.reset(); 182 } 183 startEngine(engine, name); 184 } 185 188 private void doRemoteInit(String name) 189 { 190 JMeterEngine engine = (JMeterEngine) remoteEngines.get(name); 191 if (engine == null) 192 { 193 try 194 { 195 engine = new ClientJMeterEngine(name); 196 remoteEngines.put(name, engine); 197 } 198 catch (Exception ex) 199 { 200 log.error("", ex); 201 JMeterUtils.reportErrorToUser("Bad call to remote host"); 202 return; 203 } 204 } 205 else 206 { 207 engine.reset(); 208 } 209 initEngine(engine, name); 210 } 211 212 public Set getActionNames() 213 { 214 return commands; 215 } 216 217 223 private void initEngine(JMeterEngine engine, String host) 224 { 225 GuiPackage gui = GuiPackage.getInstance(); 226 HashTree testTree = gui.getTreeModel().getTestPlan(); 227 convertSubTree(testTree); 228 testTree.add(testTree.getArray()[0], gui.getMainFrame()); 229 engine.configure(testTree); 230 } 231 232 235 private void startEngine(JMeterEngine engine, String host) 236 { 237 GuiPackage gui = GuiPackage.getInstance(); 238 try 239 { 240 engine.runTest(); 241 } 242 catch (JMeterEngineException e) 243 { 244 JOptionPane.showMessageDialog( 245 gui.getMainFrame(), 246 e.getMessage(), 247 JMeterUtils.getResString("Error Occurred"), 248 JOptionPane.ERROR_MESSAGE); 249 } 250 } 251 } 252 | Popular Tags |