Can I write a Beanshell console with Java?

I use Beanshell as a built-in debugging tool in my application. This means that I can connect to my application and connect to it with my internal components during its launch (usually I end a telnet session with rlwrap).

The problem is that the only way I found to print in the Beanshell console, and not for the application itself, is the print () method in Beanshell.

But I would like to write Java code that I can call from Beanshell, which will be output to the Beanshell console - i.e. it will be shown in my telnet session, and not sent to the stdout of the application, as it happens if you try to use System.out or System.err.

Is it possible?


edit: To clarify, I am setting up a Beanshell server as follows:

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);
    }
}

How can I change this so that I can write a Java function that is output to a telnet session (and not to my application's System.out).

+3
source share
3 answers

I will copy it there, as it seems that comments are not currently taken into account.

you can: instead of having a method that prints debugging information to standard output, returns this debugging information:

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

and then calling it from beanshell

print(clients.debugCientList());

will give you an output on your telnet

or if you need more login, you need to interact directly with the Interpreter object.

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); }
    }
}
+2

, - ..., , BSH telnet-.

, , bsh.util.Sessiond. telnet-. , , bsh.Interpreter ( ) .

, , telnet, System.out System.err .

, : System.out System.err , reset .

, bsh.util.Sessiond - mybsh.util.DebuggerSessiond, SessiondConnection bsh/commands/server.bsh, "" telnet- ( ). ( , script ...)

: beanshell

0

- , appender/handler, , , beanshell ? beanshell.

( beanshell, !)

-1

Source: https://habr.com/ru/post/1723306/


All Articles