Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.HistoryExe

/*
* Created on 2004.10.25
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.jboss.fresh.shell.commands;

import java.io.BufferedWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.jboss.fresh.ctx.Context;
import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.io.PrintWriter2;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.shell.impl.History;
import org.jboss.fresh.shell.impl.HistoryItem;

/**
* @author strukelj
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class HistoryExe extends AbstractExecutable {

  /* (non-Javadoc)
   * @see org.jboss.fresh.shell.AbstractExecutable#process(java.lang.String, java.lang.String[])
   */
  protected void process(String exepath, String[] args) throws Exception {

    PrintWriter2 pout = new PrintWriter2(new BufferedWriter(new BufferWriter(getStdOut())));

    String[] invalids = getInvalidSwitches(args, "cpxuot", new String[]{"--ex", "--help"});

    boolean clientInfo = isSwitchActive(args, "c", null);
    boolean project = isSwitchActive(args, "p", null);
    boolean global = isSwitchActive(args, "x", null);
    boolean user = isSwitchActive(args, "u", null);
    boolean objs = isSwitchActive(args, "o", null);
    boolean time = isSwitchActive(args, "t", null);
   
    if ((invalids.length > 0)) {

      StringBuffer sb = new StringBuffer("Unknown or invalid switch(es):");
      for (int i = 0; i < invalids.length; i++) {
        sb.append(" " + invalids[i]);
      }

      error(sb.toString());
      return;
    }

    if (helpRequested()) {

      StringBuffer sb = new StringBuffer();
      sb.append("Usage: history [-cpxuo] [--help]\r\n");
      sb.append("    -p : display history for current project\r\n");
      sb.append("    -x : display global history (all projects)\r\n");
      sb.append("    -c : display all client info\r\n");
      sb.append("    -u : display client user\r\n");
      sb.append("    -o : output HistoryItem objects\r\n");
      sb.append("    -t : output timestamps\r\n");
      sb.append("    --help : this help\r\n");
      error(sb.toString());
      return;
    }

    History h = null;
    Context context = getShell().getContext();
    if(project) {
      context = (Context) context.get("AppContext");
    } else if(global) {
      context = (Context) context.get("GlobalContext");
    }

    if(context == null) {
      if(project)
        error("AppContext not available");
      else if(global)
        error("GlobalContext not available");
      else
        error("Context not available");
      return;
    }

    h = (History) context.get("History");

    if(h == null) return;

    BufferObjectWriter oout = null;
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

    String val = null;

    try {
      Iterator it = h.list().iterator();
      while(it.hasNext()) {
        HistoryItem hi = (HistoryItem) it.next();

        if(objs) {
          if(oout == null)
            oout = new BufferObjectWriter(getStdOut());
          oout.writeObject(pout);
        } else {
          StringBuffer sb = new StringBuffer();
          if(time) {
            sb.append(sdf.format(new Date(hi.getTime())));
            sb.append(" ");
          }

          if(global) {
            val = hi.getProject();
            if(val == null)
              val = "<undefined>";
            sb.append(val);
            sb.append(" ");
          }

          if(user) {
            val = hi.getUser();
            if(val == null)
              val = "<undefined>";
            sb.append(val);
            sb.append(" ");         
          }

          sb.append(hi.getCmd());
          sb.append(" ");

          if(clientInfo) {
            String vuser = hi.getUser();
            String vhost = hi.getHost();
            String vapp = hi.getApp();
            if(vuser != null || vhost != null || vapp == null) {

              if(vuser == null) vuser = "<unknown>";
              if(vhost == null) vhost = "<unknown>";
              if(vapp == null) vapp = "<unknown>";

              sb.append("     (").append(vuser).append("@")
                .append(vhost).append(" [")
                .append(vapp).append("])");
            }
          }

          pout.println(sb);
        }
      }
    } finally {
      pout.close();
    }
  }
}
TOP

Related Classes of org.jboss.fresh.shell.commands.HistoryExe

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.