/* (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();
}
}