package org.jboss.fresh.registry;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameNotFoundException;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.util.Hashtable;
/**
* The whole point of this class is to have a usage similar to jndi InitialContext where you just create a new instance
* possibly passing it info on where and how to connect and then use it. So it's pluggable and allows different implementations
* underneath.
*
* At the same time you can go directly using it and bypass InitialContext initialization routine
*/
public class RegistryContext implements Context {
private StaticRegistryContext reg;
public RegistryContext() throws NamingException {
this("<default>");
}
public RegistryContext(String store) throws NamingException {
reg = new StaticRegistryContext(store);
}
// Default implementation for now is to use StaticRegistry
private StaticRegistryContext getImpl() {
return reg;
}
/**
* Adds a new environment property to the environment of this context.
*/
public Object addToEnvironment(String propName, Object propVal) throws NamingException {
Context ctx = getImpl();
return ctx.addToEnvironment(propName, propVal);
}
/**
* Binds a name to an object.
*/
public void bind(Name name, Object obj) throws NamingException {
Context ctx = getImpl();
ctx.bind(name, obj);
}
/**
* Binds a name to an object.
*/
public void bind(String name, Object obj) throws NamingException {
Context ctx = getImpl();
ctx.bind(name, obj);
}
public void bindWithParents(String name, Object obj) throws NamingException {
Context ctx = getImpl();
Name n = ctx.getNameParser("").parse(name);
while (n.size() > 1) {
String ctxName = n.get(0);
try {
ctx = (Context) ctx.lookup(ctxName);
} catch (NameNotFoundException e) {
ctx = ctx.createSubcontext(ctxName);
}
n = n.getSuffix(1);
}
ctx.bind(n.get(0), obj);
}
public void bindWithParents(Name name, Object obj) throws NamingException {
Context ctx = getImpl();
Name n = name;
while (n.size() > 1) {
String ctxName = n.get(0);
try {
ctx = (Context) ctx.lookup(ctxName);
} catch (NameNotFoundException e) {
ctx = ctx.createSubcontext(ctxName);
}
n = n.getSuffix(1);
}
ctx.bind(n.get(0), obj);
}
/**
* Closes this context.
*/
public void close() throws NamingException {
Context ctx = getImpl();
ctx.close();
}
/**
* Composes the name of this context with a name relative to this context.
*/
public Name composeName(Name name, Name prefix) throws NamingException {
Context ctx = getImpl();
return ctx.composeName(name, prefix);
}
/**
* Composes the name of this context with a name relative to this context.
*/
public String composeName(String name, String prefix) throws NamingException {
Context ctx = getImpl();
return ctx.composeName(name, prefix);
}
/**
* Creates and binds a new context.
*/
public Context createSubcontext(Name name) throws NamingException {
return createSubcontext(name, false);
}
/**
* Creates and binds a new context.
*/
public Context createSubcontext(Name name, boolean mkparents) throws NamingException {
StaticRegistryContext ctx = getImpl();
return ctx.createSubcontext(name, mkparents);
}
/**
* Creates and binds a new context.
*/
public Context createSubcontext(String name) throws NamingException {
return createSubcontext(name, false);
}
/**
* Creates and binds a new context.
*/
public Context createSubcontext(String name, boolean mkparents) throws NamingException {
StaticRegistryContext ctx = getImpl();
return ctx.createSubcontext(name, mkparents);
}
/**
* Destroys the named context and removes it from the namespace.
*/
public void destroySubcontext(Name name) throws NamingException {
Context ctx = getImpl();
ctx.destroySubcontext(name);
}
/**
* Destroys the named context and removes it from the namespace.
*/
public void destroySubcontext(String name) throws NamingException {
Context ctx = getImpl();
ctx.destroySubcontext(name);
}
/**
* Retrieves the environment in effect for this context.
*/
public Hashtable getEnvironment() throws NamingException {
Context ctx = getImpl();
return ctx.getEnvironment();
}
/**
* Retrieves the full name of this context within its own namespace.
*/
public String getNameInNamespace() throws NamingException {
Context ctx = getImpl();
return ctx.getNameInNamespace();
}
/**
* Retrieves the parser associated with the named context.
*/
public NameParser getNameParser(Name name) throws NamingException {
Context ctx = getImpl();
return ctx.getNameParser(name);
}
/**
* Retrieves the parser associated with the named context.
*/
public NameParser getNameParser(String name) throws NamingException {
Context ctx = getImpl();
return ctx.getNameParser(name);
}
/**
* Enumerates the names bound in the named context, along with the class names of objects bound to them.
*/
public NamingEnumeration list(Name name) throws NamingException {
Context ctx = getImpl();
return ctx.list(name);
}
/**
* Enumerates the names bound in the named context, along with the class names of objects bound to them.
*/
public NamingEnumeration list(String name) throws NamingException {
Context ctx = getImpl();
return ctx.list(name);
}
/**
* Enumerates the names bound in the named context, along with the objects bound to them.
*/
public NamingEnumeration listBindings(Name name) throws NamingException {
Context ctx = getImpl();
return ctx.listBindings(name);
}
/**
* Enumerates the names bound in the named context, along with the objects bound to them.
*/
public NamingEnumeration listBindings(String name) throws NamingException {
Context ctx = getImpl();
return ctx.listBindings(name);
}
/**
* Retrieves the named object.
*/
public Object lookup(Name name) throws NamingException {
Context ctx = getImpl();
return ctx.lookup(name);
}
/**
* Retrieves the named object.
*/
public Object lookup(String name) throws NamingException {
Context ctx = getImpl();
return ctx.lookup(name);
}
/**
* Retrieves the named object, following links except for the terminal atomic component of the name.
*/
public Object lookupLink(Name name) throws NamingException {
Context ctx = getImpl();
return ctx.lookupLink(name);
}
/**
* Retrieves the named object, following links except for the terminal atomic component of the name.
*/
public Object lookupLink(String name) throws NamingException {
Context ctx = getImpl();
return ctx.lookupLink(name); }
/**
* Binds a name to an object, overwriting any existing binding.
*/
public void rebind(Name name, Object obj) throws NamingException {
Context ctx = getImpl();
ctx.rebind(name, obj);
}
/**
* Binds a name to an object, overwriting any existing binding.
*/
public void rebind(String name, Object obj) throws NamingException {
Context ctx = getImpl();
ctx.rebind(name, obj);
}
/**
* Removes an environment property from the environment of this context.
*/
public Object removeFromEnvironment(String propName) throws NamingException {
Context ctx = getImpl();
return ctx.removeFromEnvironment(propName);
}
/**
* Binds a new name to the object bound to an old name, and unbinds the old name.
*/
public void rename(Name oldName, Name newName) throws NamingException {
Context ctx = getImpl();
ctx.rename(oldName, newName);
}
/**
* Binds a new name to the object bound to an old name, and unbinds the old name.
*/
public void rename(String oldName, String newName) throws NamingException {
Context ctx = getImpl();
ctx.rename(oldName, newName);
}
/**
* Unbinds the named object.
*/
public void unbind(Name name) throws NamingException {
Context ctx = getImpl();
ctx.unbind(name);
}
/**
* Unbinds the named object.
*/
public void unbind(String name) throws NamingException {
Context ctx = getImpl();
ctx.unbind(name);
}
/**
* You can use the returned object to synchronize on the specific context tree
* @return
*/
public Object getMutex() {
return reg.getMutex();
}
}