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.util;
020:
021: import java.lang.reflect.InvocationTargetException;
022: import java.lang.reflect.Method;
023:
024: import org.apache.jorphan.logging.LoggingManager;
025: import org.apache.log.Logger;
026:
027: /**
028: * Implements a BeanShell server to allow access to JMeter variables and
029: * methods.
030: *
031: * To enable, define the JMeter property: beanshell.server.port (see
032: * JMeter.java) beanshell.server.file (optional, startup file)
033: *
034: * @version $Revision: 501270 $ Last updated: $Date: 2007-01-30 01:16:29 +0000 (Tue, 30 Jan 2007) $
035: */
036: public class BeanShellServer implements Runnable {
037:
038: private static final Logger log = LoggingManager
039: .getLoggerForClass();
040:
041: private final int serverport;
042:
043: private final String serverfile;
044:
045: /**
046: *
047: */
048: public BeanShellServer(int port, String file) {
049: super ();
050: serverfile = file;// can be the empty string
051: serverport = port;
052: }
053:
054: private BeanShellServer() {// do not use!
055: super ();
056: serverport = 0;
057: serverfile = "";
058: }
059:
060: // For use by the server script
061: private static String getprop(String s) {
062: return JMeterUtils.getPropDefault(s, s);
063: }
064:
065: // For use by the server script
066: private static void setprop(String s, String v) {
067: JMeterUtils.getJMeterProperties().setProperty(s, v);
068: }
069:
070: public void run() {
071:
072: ClassLoader loader = Thread.currentThread()
073: .getContextClassLoader();
074:
075: try {
076: Class Interpreter = loader.loadClass("bsh.Interpreter");//$NON-NLS-1$
077: Object instance = Interpreter.newInstance();
078: Class string = String.class;
079: Class object = Object.class;
080:
081: Method eval = Interpreter.getMethod(
082: "eval", new Class[] { string });//$NON-NLS-1$
083: Method setObj = Interpreter.getMethod(
084: "set", new Class[] { string, object });//$NON-NLS-1$
085: Method setInt = Interpreter.getMethod(
086: "set", new Class[] { string, int.class });//$NON-NLS-1$
087: Method source = Interpreter.getMethod(
088: "source", new Class[] { string });//$NON-NLS-1$
089:
090: setObj.invoke(instance, new Object[] { "t", this });//$NON-NLS-1$
091: setInt.invoke(instance, new Object[] {
092: "portnum", new Integer(serverport) });//$NON-NLS-1$
093:
094: if (serverfile.length() > 0) {
095: try {
096: source
097: .invoke(instance,
098: new Object[] { serverfile });
099: } catch (InvocationTargetException e1) {
100: log.warn("Could not source " + serverfile);
101: Throwable t = e1.getCause();
102: if (t != null)
103: log.warn(t.toString());
104: }
105: }
106: eval.invoke(instance,
107: new Object[] { "setAccessibility(true);" });//$NON-NLS-1$
108: eval.invoke(instance, new Object[] { "server(portnum);" });//$NON-NLS-1$
109:
110: } catch (ClassNotFoundException e) {
111: log.error("Beanshell Interpreter not found");
112: } catch (Exception e) {
113: log.error("Problem starting BeanShell server ", e);
114: }
115: }
116: }
|