Package org.jboss.fresh.shell.client

Source Code of org.jboss.fresh.shell.client.ClientShell$AppCallbackHandler

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 org.apache.log4j.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

//import org.apache.log4j.Logger;


public class ClientShell {
  private static final Logger log = Logger.getLogger(ClientShell.class);
  InitialContext ctx;
  RemoteShellHome home;
  RemoteShell shell, shell2;
  static boolean authenticate = true;
  static boolean verbose = true;

  int bsizei = 1// �e damo to na ve�, imamo problem, ker ni nobenega avtomatskega timerbased flushanja, ampak �aka, da napolni. Ampak ne ve kdaj se na serverju kon�a polnenje, ker se ne zapre output stream o�itno, zato kar �aka ...
  // lahko bi kve�jemu vedno znova ob koncu zaprli output stream in bi s tem forcali, da pride buffer na clienta.

  InputStream in;

  public void init() throws Exception {

    Properties p = System.getProperties();

    if (verbose == true) {
      log.debug("");
      log.debug(Context.INITIAL_CONTEXT_FACTORY + " :: " + p.get(Context.INITIAL_CONTEXT_FACTORY));
      log.debug(Context.PROVIDER_URL + " :: " + p.get(Context.PROVIDER_URL));
      log.debug(Context.SECURITY_PRINCIPAL + " :: " + p.get(Context.SECURITY_PRINCIPAL));
      log.debug(Context.SECURITY_CREDENTIALS + " :: " + p.get(Context.SECURITY_CREDENTIALS));
    }

    ctx = new InitialContext(p);

    home = (RemoteShellHome) ctx.lookup("ShellSession");
    shell = home.create(true);
    shell2 = home.create(shell.getSessionID(), true);
    ShellObjectReader sor = new ShellObjectReader(shell2, "0");

    String bsize = System.getProperty("org.jboss.fresh.parsek.cp2.client.bufSize");
    if (bsize != null) {
      try {
        bsizei = Integer.parseInt(bsize);
      } catch (NumberFormatException ex) {
      }
    }

    sor.setBufferSize(bsizei);

    in = new BufferedInputStream(new ShellConsoleInputStream(sor));

    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 {

//          log.debug(">>>> read");
                //System.out.write(buf, 0, rc);
                rc = in.read(buf, 0, buf.length);
//          log.debug("<<<< read");
              } catch (ShellIOException ex) {
                Throwable th = ex;
                log.debug("EXCEPTION: ", th);
/*
          while(th instanceof ShellIOException) {
            th=((ShellIOException)th).getRootThrowable();

            if(th!=null) {
              log.debug("Caused By: ");
              th.printStackTrace();
            }
          }

          while(th instanceof ApplicationIOException) {
            th=((ApplicationIOException)th).getRootThrowable();

            if(th!=null) {
              log.debug("Caused By: ");
              th.printStackTrace();
            }
          }
*/
                break;
//          log.debug("An application exception has occured: ");
//          ex.getRootThrowable().printStackTrace();
//        } catch(EOFException ex) {
//          break;
              } catch (Exception ex) {
                //ex.printStackTrace();
                log.debug("EXCEPTION from shell-stdin: ", ex);
                break;
              }
            }

//        log.debug("Reinitialized input from remote...");
            ShellObjectReader sor = new ShellObjectReader(shell2, "0");
            sor.setBufferSize(bsizei);
            in = new BufferedInputStream(new ShellConsoleInputStream(sor));
          }
        } catch (Throwable ex) {
          //ex.printStackTrace();
          log.debug("EXCEPTION from shell-stdin: ", ex);

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

    t.start();

  }

  public void start() throws Exception {

    String enc = System.getProperty("org.jboss.fresh.parsek.cp2.client.encoding");
    BufferedReader sin = null;
    if (enc != null) {
      sin = new BufferedReader(new InputStreamReader(System.in, enc)); //, "Cp852"
    } else {
      sin = new BufferedReader(new InputStreamReader(System.in)); //, "Cp852"
    }

    if (authenticate) login(sin);

    init();

    while (true) {
      String line = sin.readLine();
      while (line != null) {
        try {
          if (line.equals("exit")) {
            shell2.remove();
            shell.remove();
            System.exit(0);
          }

          shell.execute(line);

        } catch (Exception ex) {
          log.debug("Exception occured while executing command: ");
          Throwable th = ex;
          log.debug("EXCEPTION: ", th);

/*
          while(th instanceof ShellIOException) {
            th=((ShellIOException)th).getRootThrowable();

            if(th!=null) {
              log.debug("EXCEPTION: ");
              th.printStackTrace();
            }
          }

          while(th instanceof ApplicationIOException) {
            th=((ApplicationIOException)th).getRootThrowable();

            if(th!=null) {
              log.debug("EXCEPTION: ");
              th.printStackTrace();
            }
          }
*/
        }
        line = sin.readLine();
      }
    }
  }

  public static void main(String[] args) throws Exception {
    log.debug("CP2 ClientShell bootup sequence. Please wait a few seconds...");
    if (args.length != 0) {
      if (args[0].equals("-noauth")) {
        authenticate = false;
      } else if (args[0].equals("-v")) {
        verbose = true;
      }
    }
    new ClientShell().start();
  }


  private void login(BufferedReader in) throws Exception {

    log.debug("Username: ");
    String user = in.readLine();
    log.debug("Password: ");
    String pass = in.readLine();

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 100; i++) {
      sb.append("\n");
    }

    log.debug(sb.toString());
    //log.debug(" authenticating ...");


    char[] password = pass.toCharArray();
    AppCallbackHandler handler = new AppCallbackHandler(user, password);
    LoginContext lc = new LoginContext("cp2", handler);
    //log.debug("Created LoginContext");
    lc.login();

  }


  static class AppCallbackHandler implements CallbackHandler {
    private String username;
    private char[] password;

    public AppCallbackHandler(String username, char[] password) {
      this.username = username;
      this.password = password;
    }

    public void handle(Callback[] callbacks) throws
        java.io.IOException, UnsupportedCallbackException {
      for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
          NameCallback nc = (NameCallback) callbacks[i];
          nc.setName(username);
        } else if (callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) callbacks[i];
          pc.setPassword(password);
        } else {
          throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
        }
      }
    }
  }

}
TOP

Related Classes of org.jboss.fresh.shell.client.ClientShell$AppCallbackHandler

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.