001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.action;
020:
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.jmeter.JMeter;
029: import org.apache.jmeter.engine.ClientJMeterEngine;
030: import org.apache.jmeter.engine.JMeterEngine;
031: import org.apache.jmeter.engine.JMeterEngineException;
032: import org.apache.jmeter.gui.GuiPackage;
033: import org.apache.jmeter.util.JMeterUtils;
034: import org.apache.jorphan.collections.HashTree;
035: import org.apache.jorphan.logging.LoggingManager;
036: import org.apache.log.Logger;
037:
038: public class RemoteStart extends AbstractAction {
039:
040: private static final Logger log = LoggingManager
041: .getLoggerForClass();
042:
043: private static final String LOCAL_HOST = "127.0.0.1"; // $NON-NLS-1$
044:
045: private static final String REMOTE_HOSTS = "remote_hosts"; // $NON-NLS-1$ jmeter.properties
046:
047: private static final String REMOTE_HOSTS_SEPARATOR = ","; // $NON-NLS-1$
048:
049: private static Set commands = new HashSet();
050: static {
051: commands.add(ActionNames.REMOTE_START);
052: commands.add(ActionNames.REMOTE_STOP);
053: commands.add(ActionNames.REMOTE_START_ALL);
054: commands.add(ActionNames.REMOTE_STOP_ALL);
055: commands.add(ActionNames.REMOTE_EXIT);
056: commands.add(ActionNames.REMOTE_EXIT_ALL);
057: }
058:
059: private Map remoteEngines = new HashMap();
060:
061: public RemoteStart() {
062: }
063:
064: public void doAction(ActionEvent e) {
065: String name = ((Component) e.getSource()).getName();
066: if (name != null) {
067: name = name.trim();
068: }
069: String action = e.getActionCommand();
070: if (action.equals(ActionNames.REMOTE_STOP)) {
071: doRemoteStop(name);
072: } else if (action.equals(ActionNames.REMOTE_START)) {
073: popupShouldSave(e);
074: doRemoteInit(name);
075: doRemoteStart(name);
076: } else if (action.equals(ActionNames.REMOTE_START_ALL)) {
077: popupShouldSave(e);
078: String remote_hosts_string = JMeterUtils.getPropDefault(
079: REMOTE_HOSTS, LOCAL_HOST);
080: java.util.StringTokenizer st = new java.util.StringTokenizer(
081: remote_hosts_string, REMOTE_HOSTS_SEPARATOR);
082: while (st.hasMoreElements()) {
083: String el = (String) st.nextElement();
084: doRemoteInit(el.trim());
085: }
086: st = new java.util.StringTokenizer(remote_hosts_string,
087: REMOTE_HOSTS_SEPARATOR);
088: while (st.hasMoreElements()) {
089: String el = (String) st.nextElement();
090: doRemoteStart(el.trim());
091: }
092: } else if (action.equals(ActionNames.REMOTE_STOP_ALL)) {
093: String remote_hosts_string = JMeterUtils.getPropDefault(
094: REMOTE_HOSTS, LOCAL_HOST);
095: java.util.StringTokenizer st = new java.util.StringTokenizer(
096: remote_hosts_string, REMOTE_HOSTS_SEPARATOR);
097: while (st.hasMoreElements()) {
098: String el = (String) st.nextElement();
099: doRemoteStop(el.trim());
100: }
101: } else if (action.equals(ActionNames.REMOTE_EXIT)) {
102: doRemoteExit(name);
103: } else if (action.equals(ActionNames.REMOTE_EXIT_ALL)) {
104: String remote_hosts_string = JMeterUtils.getPropDefault(
105: REMOTE_HOSTS, LOCAL_HOST);
106: java.util.StringTokenizer st = new java.util.StringTokenizer(
107: remote_hosts_string, REMOTE_HOSTS_SEPARATOR);
108: while (st.hasMoreElements()) {
109: String el = (String) st.nextElement();
110: doRemoteExit(el.trim());
111: }
112: }
113: }
114:
115: /**
116: * Stops a remote testing engine
117: *
118: * @param name
119: * the DNS name or IP address of the remote testing engine
120: *
121: */
122: private void doRemoteStop(String name) {
123: GuiPackage.getInstance().getMainFrame().showStoppingMessage(
124: name);
125: JMeterEngine engine = (JMeterEngine) remoteEngines.get(name);
126: engine.stopTest();
127: }
128:
129: /**
130: * Exits a remote testing engine
131: *
132: * @param name
133: * the DNS name or IP address of the remote testing engine
134: *
135: */
136: private void doRemoteExit(String name) {
137: JMeterEngine engine = (JMeterEngine) remoteEngines.get(name);
138: if (engine == null)
139: return;
140: // GuiPackage.getInstance().getMainFrame().showStoppingMessage(name);
141: engine.exit();
142: }
143:
144: /**
145: * Starts a remote testing engine
146: *
147: * @param name
148: * the DNS name or IP address of the remote testing engine
149: *
150: */
151: private void doRemoteStart(String name) {
152: JMeterEngine engine = (JMeterEngine) remoteEngines.get(name);
153: if (engine != null) {
154: try {
155: engine.runTest();
156: } catch (JMeterEngineException e) {
157: JMeterUtils.reportErrorToUser(e.getMessage(),
158: JMeterUtils
159: .getResString("remote_error_starting")); // $NON-NLS-1$
160: }
161: }
162: }
163:
164: /**
165: * Initializes remote engines
166: */
167: private void doRemoteInit(String name) {
168: JMeterEngine engine = (JMeterEngine) remoteEngines.get(name);
169: if (engine == null) {
170: try {
171: engine = new ClientJMeterEngine(name);
172: remoteEngines.put(name, engine);
173: } catch (Exception ex) {
174: log.error("Failed to initialise remote engine", ex);
175: JMeterUtils.reportErrorToUser(ex.getMessage(),
176: JMeterUtils.getResString("remote_error_init")); // $NON-NLS-1$
177: return;
178: }
179: } else {
180: engine.reset();
181: }
182: initEngine(engine, name);
183: }
184:
185: public Set getActionNames() {
186: return commands;
187: }
188:
189: /**
190: * Initializes test on engine.
191: *
192: * @param engine
193: * remote engine object
194: * @param host
195: * host the engine will run on
196: */
197: private void initEngine(JMeterEngine engine, String host) {
198: GuiPackage gui = GuiPackage.getInstance();
199: HashTree testTree = gui.getTreeModel().getTestPlan();
200: JMeter.convertSubTree(testTree);
201: testTree.add(testTree.getArray()[0], gui.getMainFrame());
202: engine.configure(testTree);
203: }
204: }
|