package org.jboss.fresh.shell.commands;
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.registry.RegistryContext;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.util.Hashtable;
public class RegistryExe extends AbstractExecutable {
private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(RegistryExe.class);
/**
We set the current active project projekt.
*/
public void process(String exepath, String[] params) throws Exception {
log.debug("entered");
PrintWriter2 out = new PrintWriter2(new BufferWriter(getStdOut()));
boolean g = false;
boolean l = false;
boolean b = false;
boolean help = false;
boolean tex = false;
String name = null;
String factory = null;
String host = null;
Hashtable env = null;
// see what to do:
for (int i = 0; i < params.length; i++) {
String val = params[i];
if (val.equals("-g") || val.equals("--get")) {
g = true;
} else if (val.equals("-l") || val.equals("--list")) {
l = true;
} else if (val.equals("-b") || val.equals("--bind")) {
b = true;
} else if (val.equals("-h") || val.equals("--help")) {
help = true;
} else if (val.equals("--ex")) {
tex = true;
} else if (val.equals("--factory")) {
try {
factory = params[++i];
} catch (Exception ex) {
}
} else if (val.equals("--host")) {
try {
host = params[++i];
} catch (Exception ex) {
}
} else {
name = val;
}
}
if (params.length == 0 || help) {
out.println("Usage: jndi [-glbh] <lookup name>");
out.println(" lookup name : lookup name i.e. java:UserTransaction");
out.println(" -g, --get : get the object for the specified name");
out.println(" -l, --list : list the contents of the context");
out.println(" -b, --bind : bind an object passed on stdin under the specified name");
out.println(" -h, --help : this help");
out.close();
log.debug("done");
return;
}
RegistryContext ctx = new RegistryContext();
if (g) {
BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
Object o = null;
try {
o = ctx.lookup(name);
} catch (NamingException ex) {
if (tex)
throw new RuntimeException(ex);
else
out.print("Name not bound: " + name);
return;
}
oout.writeObject(o);
oout.close();
return;
}
if (l) {
BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
Object o = ctx.lookup(name);
if (!(o instanceof Context)) {
if (tex)
throw new RuntimeException("Object is not instance of Context. Can't list it. : " + name);
else
out.print("Object is not instance of Context. Can't list it. : " + name);
return;
}
NamingEnumeration it = ctx.listBindings(name);
while (it.hasMore()) {
Binding bing = (Binding) it.next();
out.println(bing.toString());
}
out.close();
return;
}
if (b) {
out.println("Not implemented yet.");
out.close();
return;
}
log.debug("done");
}
}