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 u = false;
boolean help = false;
boolean tex = false;
String spiObjectFactory = null;
ArrayList spiParams = null;
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("-u") || val.equals("--unbind")) {
u = 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) {
error("--factory parameter must be followed by jndi-factory specification");
return;
}
} else if (val.equals("--bind-ref") || val.equals("-r")) {
try {
spiObjectFactory = params[++i];
} catch (Exception ex) {
error("--bind-ref parameter must be followed by spi-object-factory specification");
return;
}
// copy remaining parameters in paramList
if("JNDI2Registry".equals(spiObjectFactory)) {
spiParams = new ArrayList(2);
try {
name = params[++i];
spiParams.add(name);
spiParams.add(params[++i]);
} catch (Exception ex) {
error(spiObjectFactory + " requires 2 parameters: <bind-name> <referenced-name>");
return;
}
}
} else if (val.equals("--host")) {
try {
host = params[++i];
} catch (Exception ex) {
error("--host parameter must be followed by inital-context-url specification");
return;
}
} 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(" -r, --bind-ref <spi-object-factory> <object-factory-specific-params> ... : create and bind a reference");
out.println(" --factory <jndi factory class>: use the specified jndi factory class");
out.println(" --host <jndi host> : connect to the specified host");
out.println(" -h, --help : this help");
out.println();
out.println(" Example:");
out.println(" jndi --bind-ref JNDI2Registry java:/CP2/bof_zrk/Persistence java:/CP2/bof_zrk/Persistence");
out.flush();
log.debug("done");
return;
}
if (factory != null || host != null) {
env = new Hashtable();
if (factory != null) env.put(InitialContext.INITIAL_CONTEXT_FACTORY, factory);
if (host != null) env.put(InitialContext.PROVIDER_URL, host);
}
Context ctx = null;
if (env == null)
ctx = new InitialContext();
else
ctx = new InitialContext(env);
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.flush();
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.flush();
return;
}
if (u) {
ctx.unbind(name);
return;
}
if (b || spiObjectFactory != null) {
Object o = null;
String factoryCName = null;
if(spiObjectFactory == null || "StaticStore".equals(spiObjectFactory)) {
BufferObjectReader oin = new BufferObjectReader(getStdIn());
if(!oin.isFinished()) {
o = oin.readObject();
}
} else if("JNDI2Registry".equals(spiObjectFactory)) {
o = new RegistryContext().lookup((String) spiParams.get(1));
factoryCName = org.jboss.fresh.registry.JNDI2RegistryResolverFactory.class.getName();
} else if("StaticStore".equals(spiObjectFactory)) {
factoryCName = org.jboss.fresh.naming.StaticObjectStore.class.getName();
}
Name n = ctx.getNameParser("").parse(name);
Context ct = ctx;
while (n.size() > 1) {
String ctxName = n.get(0);
log.debug("CtxName: "+ctxName);
try {
ct = (Context) ct.lookup(ctxName);
if (ct==null){//to workaround resin bug!
log.debug("Lookup retured null, will create new subcontext");
ct = ct.createSubcontext(ctxName);
}
} catch (NameNotFoundException e) {
log.warn("Could not lookup for ctx ",e);
ct = ct.createSubcontext(ctxName);
log.debug("ctx after create subcontext: "+ct);
}
n = n.getSuffix(1);
}
if(spiObjectFactory != null) {
StringRefAddr addr = new StringRefAddr("nns", name);
Reference ref = new Reference(o.getClass().getName(), addr, factoryCName, null);
o = ref;
}
ct.bind(n.get(0), o);
out.flush();
return;
}
log.debug("done");