KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > action > RemoteStart


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/action/RemoteStart.java,v 1.14 2004/02/12 23:59:02 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.action;
20 import java.awt.Component JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.swing.JOptionPane JavaDoc;
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 /**
39  * @author Michael Stover
40  * @author Drew Gulino
41  * @version $Revision: 1.14 $ Last updated $Date: 2004/02/12 23:59:02 $
42  */

43 public class RemoteStart extends AbstractAction
44 {
45     transient private static Logger log = LoggingManager.getLoggerForClass();
46
47     private static Set JavaDoc commands = new HashSet JavaDoc();
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 JavaDoc remoteEngines = new HashMap JavaDoc();
59
60     public RemoteStart()
61     {
62     }
63
64     public void doAction(ActionEvent JavaDoc e)
65     {
66         String JavaDoc name = ((Component JavaDoc) e.getSource()).getName();
67         if(name != null)
68         {
69             name = name.trim();
70         }
71         String JavaDoc 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 JavaDoc remote_hosts_string =
84                 JMeterUtils.getPropDefault("remote_hosts", "127.0.0.1");
85             java.util.StringTokenizer JavaDoc st =
86                 new java.util.StringTokenizer JavaDoc(remote_hosts_string, ",");
87             while (st.hasMoreElements())
88             {
89                 String JavaDoc el = (String JavaDoc) st.nextElement();
90                 doRemoteInit(el.trim());
91             }
92             st = new java.util.StringTokenizer JavaDoc(remote_hosts_string, ",");
93             while (st.hasMoreElements())
94             {
95                 String JavaDoc el = (String JavaDoc) st.nextElement();
96                 doRemoteStart(el.trim());
97             }
98         }
99         else if (action.equals("remote_stop_all"))
100         {
101             String JavaDoc remote_hosts_string =
102                 JMeterUtils.getPropDefault("remote_hosts", "127.0.0.1");
103             java.util.StringTokenizer JavaDoc st =
104                 new java.util.StringTokenizer JavaDoc(remote_hosts_string, ",");
105             while (st.hasMoreElements())
106             {
107                 String JavaDoc el = (String JavaDoc) 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 JavaDoc remote_hosts_string =
118                 JMeterUtils.getPropDefault("remote_hosts", "127.0.0.1");
119             java.util.StringTokenizer JavaDoc st =
120                 new java.util.StringTokenizer JavaDoc(remote_hosts_string, ",");
121             while (st.hasMoreElements())
122             {
123                 String JavaDoc el = (String JavaDoc) st.nextElement();
124                 doRemoteExit(el.trim());
125             }
126         }
127     }
128
129     /**
130      * Stops a remote testing engine
131      *
132      * @param name the DNS name or IP address of the remote testing engine
133      *
134      */

135     private void doRemoteStop(String JavaDoc name)
136     {
137         GuiPackage.getInstance().getMainFrame().showStoppingMessage(name);
138         JMeterEngine engine = (JMeterEngine) remoteEngines.get(name);
139         engine.stopTest();
140     }
141
142     /**
143      * Exits a remote testing engine
144      *
145      * @param name the DNS name or IP address of the remote testing engine
146      *
147      */

148     private void doRemoteExit(String JavaDoc name)
149     {
150         JMeterEngine engine = (JMeterEngine) remoteEngines.get(name);
151         if (engine == null) return;
152         //GuiPackage.getInstance().getMainFrame().showStoppingMessage(name);
153
engine.exit();
154     }
155
156     /**
157      * Starts a remote testing engine
158      *
159      * @param name the DNS name or IP address of the remote testing engine
160      *
161      */

162     private void doRemoteStart(String JavaDoc 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 JavaDoc 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     /**
186      * Initializes remote engines
187      */

188     private void doRemoteInit(String JavaDoc 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 JavaDoc 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 JavaDoc getActionNames()
213     {
214         return commands;
215     }
216
217     /**
218      * Initializes test on engine.
219      *
220      * @param engine remote engine object
221      * @param host host the engine will run on
222      */

223     private void initEngine(JMeterEngine engine, String JavaDoc 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     /**
233      * Starts the test on the remote engine.
234      */

235     private void startEngine(JMeterEngine engine, String JavaDoc 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