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.io.FileReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.OutputStream;
026: import java.net.Socket;
027:
028: /**
029: * Implements a client that can talk to the JMeter BeanShell server.
030: */
031: public class BeanShellClient {
032:
033: private static final int MINARGS = 3;
034:
035: public static void main(String[] args) throws Exception {
036: if (args.length < MINARGS) {
037: System.out.println("Please provide " + MINARGS
038: + " or more arguments:");
039: System.out
040: .println("serverhost serverport filename [arg1 arg2 ...]");
041: System.out.println("e.g. ");
042: System.out
043: .println("localhost 9000 extras/remote.bsh apple blake 7");
044: System.exit(1);
045: }
046: String host = args[0];
047: String portString = args[1];
048: String file = args[2];
049:
050: int port = Integer.parseInt(portString) + 1;// convert to telnet port
051:
052: System.out.println("Connecting to BSH server on " + host + ":"
053: + portString);
054:
055: Socket sock = new Socket(host, port);
056: InputStream is = sock.getInputStream();
057:
058: OutputStream os = sock.getOutputStream();
059:
060: InputStreamReader fis = new FileReader(file);
061:
062: new SockRead(is).start();
063:
064: sendLine("bsh.prompt=\"\";", os);// Prompt is unnecessary
065:
066: sendLine("String [] args={", os);
067: for (int i = MINARGS; i < args.length; i++) {
068: sendLine("\"" + args[i] + "\",\n", os);
069: }
070: sendLine("};", os);
071:
072: int b;
073: while ((b = fis.read()) != -1) {
074: os.write(b);
075: }
076: sendLine("bsh.prompt=\"bsh % \";", os);// Reset for other users
077: os.flush();
078: sock.shutdownOutput(); // Tell server that we are done
079: }
080:
081: private static void sendLine(String line, OutputStream outPipe)
082: throws IOException {
083: outPipe.write(line.getBytes());
084: outPipe.flush();
085: }
086:
087: private static class SockRead extends Thread {
088:
089: private final InputStream is;
090:
091: public SockRead(InputStream _is) {
092: this .is = _is;
093: //this.setDaemon(true);
094: }
095:
096: public void run() {
097: System.out.println("Reading responses from server ...");
098: int x = 0;
099: try {
100: while ((x = is.read()) > -1) {
101: char c = (char) x;
102: System.out.print(c);
103: }
104: } catch (IOException e) {
105: } finally {
106: System.out.println("... disconnected from server.");
107: try {
108: is.close();
109: } catch (IOException e) {
110: }
111: }
112:
113: }
114:
115: }
116: }
|