我会在那里复制它,因为这些天似乎忽略了评论。
您可以:而不是将调试信息打印到标准输出的方法返回该调试信息:
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); }
}
}