Package org.jboss.fresh.shell.client

Source Code of org.jboss.fresh.shell.client.StressClientShell

package org.jboss.fresh.shell.client;

import org.jboss.fresh.shell.ShellConsoleInputStream;
import org.jboss.fresh.shell.ShellIOException;
import org.jboss.fresh.shell.ShellObjectReader;
import org.jboss.fresh.shell.ejb.RemoteShell;
import org.jboss.fresh.shell.ejb.RemoteShellHome;

import javax.naming.InitialContext;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class StressClientShell {
    private static final Logger log = Logger.getLogger(StressClientShell.class);
     
  InitialContext ctx;
  RemoteShellHome home;
  RemoteShell shell, shell2;

  InputStream in;

  public void init() throws Exception {
    ctx = new InitialContext();
    home = (RemoteShellHome) ctx.lookup("ShellSession");
    shell = home.create(true);
    shell2 = home.create(shell.getSessionID(), true);
    in = new BufferedInputStream(new ShellConsoleInputStream(new ShellObjectReader(shell2, "0")));

    Thread t = new Thread() {
      public void run() {
        byte[] buf = new byte[512];
        try {

          while (true) {
            int rc = in.read(buf, 0, buf.length);
            while (rc != -1) {
              try {

//          System.out.println(">>>> read");
                //System.out.write(buf, 0, rc);
                rc = in.read(buf, 0, buf.length);
//          System.out.println("<<<< read");
              } catch (ShellIOException ex) {
                Throwable th = ex;
                log.error("EXCEPTION: ",th);
               
                break;
//          System.out.println("An application exception has occured: ");
//          ex.getRootThrowable().printStackTrace();
//        } catch(EOFException ex) {
//          break;
              } catch (Exception ex) {
                //ex.printStackTrace();
                log.error("An exception has occured while reading from shell-stdin: " ,ex);
                break;
              }
            }

//        System.out.println("Reinitialized input from remote...");
            in = new BufferedInputStream(new ShellConsoleInputStream(new ShellObjectReader(shell2, "0")));
          }
        } catch (Throwable ex) {
          //ex.printStackTrace();
          log.error("An exception has occured while reading from shell-stdin:\n" ,ex);

        }
        log.info("Main loop exited. Stream closed at the server.");
      }
    };

    t.start();

  }

  public void start() throws Exception {
    init();

    BufferedReader sin = new BufferedReader(new InputStreamReader(System.in, "Cp852"));
    while (true) {
      String line = sin.readLine();
      while (line != null) {
        for (int j = 0; j < 10; j++) {
          try {
            shell.execute(line);
//        } catch(ApplicationIOException ex) {
//          System.out.println("An application exception occured while executing command: ");
//          ex.getRootThrowable().printStackTrace();

          } catch (Exception ex) {
            Throwable th = ex;
            log.error("EXCEPTION: ",th);

            log.error("Exception occured while executing command: " , ex);
          }
        }
        line = sin.readLine();

      }
    }
  }

  public static void main(String[] args) throws Exception {
    new StressClientShell().start();
  }
}
TOP

Related Classes of org.jboss.fresh.shell.client.StressClientShell

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.