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.engine;
020:
021: import java.net.MalformedURLException;
022: import java.rmi.Naming;
023: import java.rmi.NotBoundException;
024: import java.rmi.RemoteException;
025: import java.util.Properties;
026:
027: import org.apache.jmeter.testelement.TestListener;
028: import org.apache.jmeter.threads.JMeterContextService;
029: import org.apache.jorphan.collections.HashTree;
030: import org.apache.jorphan.collections.SearchByClass;
031: import org.apache.jorphan.logging.LoggingManager;
032: import org.apache.log.Logger;
033:
034: /**
035: * Class to run remote tests from the client JMeter and collect remote samples
036: */
037: public class ClientJMeterEngine implements JMeterEngine, Runnable {
038: private static final Logger log = LoggingManager
039: .getLoggerForClass();
040:
041: RemoteJMeterEngine remote;
042:
043: HashTree test;
044:
045: SearchByClass testListeners;
046:
047: ConvertListeners sampleListeners;
048:
049: private String host;
050:
051: private static RemoteJMeterEngine getEngine(String h)
052: throws MalformedURLException, RemoteException,
053: NotBoundException {
054: return (RemoteJMeterEngine) Naming.lookup("//" + h
055: + "/JMeterEngine"); // $NON-NLS-1$ $NON-NLS-2$
056: }
057:
058: public ClientJMeterEngine(String host)
059: throws MalformedURLException, NotBoundException,
060: RemoteException {
061: this (getEngine(host));
062: this .host = host;
063: }
064:
065: public ClientJMeterEngine(RemoteJMeterEngine remote) {
066: this .remote = remote;
067: }
068:
069: protected HashTree getTestTree() {
070: return test;
071: }
072:
073: public void configure(HashTree testTree) {
074: test = testTree;
075: }
076:
077: public void setHost(String host) {
078: this .host = host;
079: }
080:
081: public void runTest() {
082: log.info("about to run remote test");
083: new Thread(this ).start();
084: log.info("done initiating run command");
085: }
086:
087: public void stopTest() {
088: log.info("about to stop remote test on " + host);
089: try {
090: remote.stopTest();
091: } catch (Exception ex) {
092: log.error("", ex); // $NON-NLS-1$
093: }
094: }
095:
096: public void reset() {
097: try {
098: try {
099: remote.reset();
100: } catch (java.rmi.ConnectException e) {
101: remote = getEngine(host);
102: remote.reset();
103: }
104: } catch (Exception ex) {
105: log.error("", ex); // $NON-NLS-1$
106: }
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see java.lang.Runnable#run()
113: */
114: public void run() {
115: log.info("running clientengine run method");
116: testListeners = new SearchByClass(TestListener.class);
117: sampleListeners = new ConvertListeners();
118: HashTree testTree = getTestTree();
119: PreCompiler compiler = new PreCompiler(true); // limit the changes to client only test elements
120: synchronized (testTree) {
121: testTree.traverse(compiler);
122: testTree.traverse(new TurnElementsOn());
123: testTree.traverse(testListeners);
124: testTree.traverse(sampleListeners);
125: }
126:
127: try {
128: JMeterContextService.startTest();
129: remote.setHost(host);
130: log.info("sent host =" + host);
131: remote.configure(test);
132: log.info("sent test");
133: remote.runTest();
134: log.info("sent run command");
135: } catch (Exception ex) {
136: log.error("", ex); // $NON-NLS-1$
137: }
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see org.apache.jmeter.engine.JMeterEngine#exit()
144: */
145: public void exit() {
146: log.info("about to exit remote server on " + host);
147: try {
148: remote.exit();
149: } catch (RemoteException e) {
150: log.warn("Could not perform remote exit: " + e.toString());
151: }
152: }
153:
154: public void setProperties(Properties p) {
155: log.info("Sending properties " + p);
156: try {
157: remote.setProperties(p);
158: } catch (RemoteException e) {
159: log.warn("Could not set properties: " + e.toString());
160: }
161: }
162: }
|