3

我在我的应用程序中使用 Beanshell 作为嵌入式调试工具。这意味着我可以远程登录到我的应用程序并在它运行时查看它的内部结构(我通常使用 rlwrap 包装远程登录会话)。

问题是我发现打印到 Beanshell 控制台而不是应用程序本身的标准输出的唯一方法是 Beanshell 中的 print() 方法。

但是我想用 Java 编写可以从 Beanshell 调用的代码,它将输出到 Beanshell 控制台 - 即。它将显示在我的 telnet 会话中,而不是发送到应用程序的标准输出,就像您尝试使用 System.out 或 System.err 时一样。

这可能吗?


编辑:为了进一步澄清,我正在设置一个 Beanshell 服务器,如下所示:

public static void setUpBeanshell() {
    try {
        i.setShowResults(true);
        i.eval(new InputStreamReader(Bsh.class.getResourceAsStream("init.bsh")));
        i.eval("server(" + Main.globalConfig.beanShellPort + ");");
    } catch (final EvalError e) {
        Main.log.error("Error generated while starting BeanShell server", e);
    }
}

我将如何修改它以便我可以编写一个输出到 telnet 会话(而不是我的应用程序的 System.out)的 Java 函数

4

3 回答 3

2

我会在那里复制它,因为这些天似乎忽略了评论。

您可以:而不是将调试信息打印到标准输出的方法返回该调试信息:

class ClientList {
 Integer clients = 0;
 public String debugClientList() {
   return clients.toString();
 }

然后从 beanshell 调用它

print(clients.debugCientList());

会在你的 telnet 上给你一个输出

或者如果您需要更多类似记录器的功能,则需要直接与 Interpreter 对象进行交互

InterpreterSingleton {  
    public static final void Console console = new Interpreter();
}
....

class ClientList {
 Integer clients = 0;
 public void addClient(Client c) {
    ....
    InterpreterSingleton.console.print("Client added, clients now are " + clients);
 }

我正在那里回复评论,因为它需要更多编码;telnet 实现对每个连接使用不同的解释器,因此您必须将该解释器暴露给对象以打印到 telnet 客户端。最快的方法是在默认的 telnet 服务器中进行一些更改并使用修改后的服务器来启动您的服务器,而不是使用 server() 脚本命令(它在 lgpl 或 sun 许可条款下)

请注意,这种方式会为每个连接启动一个解释器;简单快捷的解决方法是维护所有正在运行的解释器的列表,并向每个解释器打印调试信息,因此:

class InterpreterSingletonList {  
    public static final void Set<Interpreter> is = new HashSet();
    void printToAll(String s) {
         for (Interpreter i: is) {
             i.print(s);
         }
    }
}



package bsh.util;

import java.io.*;

import java.net.Socket;
import java.net.ServerSocket;
import bsh.*;

/**
    BeanShell remote session server.
    Starts instances of bsh for client connections.
    Note: the sessiond effectively maps all connections to the same interpreter
    (shared namespace).
*/
public class Sessiond extends Thread
{
    private ServerSocket ss;
    NameSpace globalNameSpace;

    public Sessiond(NameSpace globalNameSpace, int port) throws IOException
    {
        ss = new ServerSocket(port);
        this.globalNameSpace = globalNameSpace;
    }

    public void run()
    {
        try
        {
            while(true)
                new SessiondConnection(globalNameSpace, ss.accept()).start();
        }
        catch(IOException e) { System.out.println(e); }
    }
}

class SessiondConnection extends Thread
{
    NameSpace globalNameSpace;
    Socket client;

    SessiondConnection(NameSpace globalNameSpace, Socket client)
    {
        this.client = client;
        this.globalNameSpace = globalNameSpace;
    }

    public void run()
    {
        try
        {
            InputStream in = client.getInputStream();
            PrintStream out = new PrintStream(client.getOutputStream());
            /* this is the one you're looking for */
                        Interpreter i = new Interpreter( 
                new InputStreamReader(in), out, out, true, globalNameSpace);
            i.setExitOnEOF( false ); // don't exit interp
                    /*store the interpreter on the list*/
                    InterpreterSingletonList.is.add(i);
            i.run();
                    /*remove it (i.run() blocks)*/
                    InterpreterSingletonList.is.remove(i);
        }
        catch(IOException e) { System.out.println(e); }
    }
}
于 2009-11-26T15:55:53.940 回答
0

我认为没有 hack 是不可能的……抱歉,调整 BSH 的 telnet 服务器实现。

我们正在查看的类是bsh.util.Sessiond. 一旦启动,它就会打开并维护一个 telnet 服务器。当它收到一个命令时,它会创建一个新的工作线程,这会创建一个新的 bsh.Interpreter 并使用正确的输入和输出流(从套接字派生)并运行解释器。

因此,只有解释命令的输出才会发送到 telnet 客户端,这是有道理的,因为System.out并且System.err没有被重定向。

但这正是您的情况所要做的:在解释器运行命令之前重定向System.outSystem.err到套接字输出流,并在完成后重置流。

我建议,你将bsh.util.Sessiond类复制到类似的东西mybsh.util.DebuggerSessiond,将重定向代码应用到内部类 SessiondConnection 的 run 方法并修改bsh/commands/server.bsh以另外启动这个“新”telnet 服务器(或代替原来的)。(我猜,这个脚本启动服务器......)

源代码可以在这里找到:beanshell 存储库

于 2009-12-01T13:48:48.673 回答
-1

如果你的应用程序的所有输出都是使用一些日志框架编写的,你可以编写一个自定义的附加程序/处理程序,除了记录说一个文件还会写入 beanshell 控制台吗?可能在执行一些 beanshell 命令后启用和禁用控制台日志记录。

(我以前不知道 beanshell,但它似乎很有用!)

于 2009-11-25T23:10:06.900 回答