package org.jboss.fresh.shell.commands;
import org.jboss.fresh.ctx.Context;
import org.jboss.fresh.io.BufferObjectReader;
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 java.io.BufferedWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
public class ContextExe extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(ContextExe.class);
static final int BIND = 0;
static final int UNBIND = 1;
static final int GET = 2;
static final int LIST = 3;
// ctx -b bindname1 bindname2 bindname3
// Will read 3 objects from shell-in and bind them under the specified names
public void process(String exename, String[] params) throws Exception {
log.debug("entered");
if (helpRequested()) {
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
out.print("Usage:\n");
out.print(" BIND some objects in the context under their respective names:\n");
out.print(" <OBJ_GEN> | ctx [-ex] -b name1, name2, name3\n\n");
out.print(" UNBIND some objects from the context:\n");
out.print(" ctx [-ex] -u name1, name2, name3\n\n");
out.print(" GET some objects from the context:\n");
out.print(" ctx [-ex] -g name1, name2, name3 | <OBJ_CONSUMER>\n\n");
out.print(" LIST children of an existing object to some other existing object:\n");
out.print(" ctx [-ex] -l\n\n");
out.print(" --help : this help\n");
out.close();
log.debug("done");
return;
}
int action = -1;
PrintWriter2 err = new PrintWriter2(new BufferWriter(getStdOut())), pout = err;
LinkedList names = new LinkedList();
Context ctx = shell.getContext();
if (params.length == 0) {
if (canThrowEx()) {
throw new Exception("Need to specify some parameters.");
} else {
printUsage();
log.debug("done");
return;
}
}
for (int i = 0; i < params.length; i++) {
String tmp = params[i];
if (tmp.equals("-b")) {
action = BIND;
// zdaj bere� dokler je:
for (int j = i + 1; j < params.length; j++, i++) {
names.add(params[j]);
}
if (names.size() == 0) {
if (canThrowEx()) {
throw new Exception("No bind name specified.");
} else {
err.println("No bind name specified.");
printUsage();
log.debug("done");
return;
}
}
} else if (tmp.equals("-u")) {
action = UNBIND;
// zdaj bere� dokler je:
for (int j = i + 1; j < params.length; j++, i++) {
names.add(params[j]);
}
if (names.size() == 0) {
if (canThrowEx()) {
throw new Exception("No unbind name specified.");
} else {
err.println("No unbind name specified.");
printUsage();
log.debug("done");
return;
}
}
} else if (tmp.equals("-g")) {
action = GET;
for (int j = i + 1; j < params.length; j++, i++) {
names.add(params[j]);
}
if (names.size() == 0) {
if (canThrowEx()) {
throw new Exception("No get name specified.");
} else {
err.println("No get name specified.");
printUsage();
log.debug("done");
return;
}
}
} else if (tmp.equals("-l")) {
action = LIST;
} else {
printUsage();
log.debug("done");
return;
}
}
switch (action) {
case BIND:
{
BufferObjectReader objin = new BufferObjectReader(getStdIn());
// read from input names.length() objects
// bind each one as you go
Iterator it = names.iterator();
while (it.hasNext()) {
String name = String.valueOf(it.next());
Object obj = null;
if (!objin.isFinished()) obj = objin.readObject();
if (obj == null) {
if (canThrowEx()) {
throw new Exception("Could not read object from std-in for name: " + name);
} else {
err.println("Could not read object from std-in for name: " + name);
printUsage();
log.debug("done");
return;
}
}
ctx.put(name, obj);
}
}
break;
case UNBIND:
{
// unbind one by one
Iterator it = names.iterator();
while (it.hasNext()) {
String name = String.valueOf(it.next());
ctx.remove(name);
}
}
break;
case GET:
{
// unbind one by one
BufferObjectWriter objout = new BufferObjectWriter(getStdOut());
Iterator it = names.iterator();
while (it.hasNext()) {
String name = String.valueOf(it.next());
Object obj = ctx.get(name);
objout.writeObject(obj);
}
objout.close();
}
break;
case LIST:
{
HashMap map = new HashMap();
ctx.loadMappings(map);
TreeMap sm = new TreeMap(map);
Iterator it = sm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
pout.println(ent.getKey() + "\t=\t" + ent.getValue() + "\t" + ent.getValue().getClass().getName());
}
pout.close();
}
break;
default:
}
}
private void printUsage() {
PrintWriter2 pout = new PrintWriter2(new BufferedWriter(new BufferWriter(getStdOut())));
pout.println("Usage:\n");
pout.println(" BIND some objects in the context under their respective names:");
pout.println(" <OBJ_GEN> | ctx [-ex] -b name1, name2, name3");
pout.println();
pout.println(" UNBIND some objects from the context:");
pout.println(" ctx [-ex] -u name1, name2, name3");
pout.println();
pout.println(" GET some objects from the context:");
pout.println(" ctx [-ex] -g name1, name2, name3 | <OBJ_CONSUMER>");
pout.println();
pout.println(" LIST children of an existing object to some other existing object:");
pout.println(" ctx [-ex] -l");
pout.println();
pout.flush();
}
}