You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

6.3. Programmatically connect to the console

A connection to the kernel can also be done programmatically.
The following code is a simplified version of the code from the client library.

import org.apache.geronimo.gshell.remote.crypto.CryptoContext;
import org.apache.geronimo.gshell.remote.client.RshClient;
import org.apache.geronimo.gshell.remote.client.RemoteExecuteException;
import org.apache.geronimo.gshell.remote.client.handler.EchoHandler;
import org.apache.geronimo.gshell.remote.client.handler.ClientMessageHandler;
import org.apache.geronimo.gshell.whisper.transport.TransportException;
import org.apache.geronimo.gshell.whisper.transport.TransportFactory;
import org.apache.geronimo.gshell.whisper.transport.TransportFactoryLocator;
import org.apache.geronimo.gshell.whisper.transport.tcp.SpringTcpTransportFactory;
import org.apache.geronimo.gshell.whisper.stream.StreamFeeder;
import org.apache.geronimo.gshell.layout.NotFoundException;
import org.apache.geronimo.gshell.ExitNotification;

public class Main {

    public static void main(String[] args) throws Exception {
        RshClient client = null;
        try {
            CryptoContext context = new CryptoContext("RSA", null);
            List<ClientMessageHandler> handlers = new LinkedList<ClientMessageHandler>();
            handlers.add(new EchoHandler());
            client = new RshClient(context, new Locator(), handlers);

            client.initialize();
            client.connect(address, new URI("tcp://0.0.0.0:0"));
            client.login(user, password);
            StreamFeeder outputFeeder = new StreamFeeder(client.getInputStream(), System.out);
            outputFeeder.createThread().start();
            client.openShell();
            System.out.println("Connected");

            client.execute(args[0]);
        } catch (ExitNotification e) {
            System.exit(0);
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        } finally {
            try {
                client.closeShell();
                client.close();
            } catch (Throwable t) { }
        }
        System.exit(0);
    }

    private static class Locator implements TransportFactoryLocator {
        SpringTcpTransportFactory factory = new SpringTcpTransportFactory();

        public TransportFactory locate(URI arg0) throws TransportException {
            return factory;
        }

    }
}

You can find a more complete example at the following location.

6.3. Programmatically connect to the console

  • No labels