最近コメント無視されてるみたいなのでコピペしておきます。
デバッグ情報を標準出力に出力するメソッドを使用する代わりに、そのデバッグ情報を返します。
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 クライアントに出力するために、そのインタープリターをオブジェクトに公開する必要があります。最も簡単な方法は、server() スクリプト コマンドを使用する代わりに、デフォルトの telnet サーバーの一部を変更し、変更したものを使用してサーバーを起動することです (これは 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); }
}
}