package org.jboss.fresh.registry;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.CompositeName;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.LinkedList;
public class StaticRegistryContext implements Context {
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(StaticRegistryContext.class);
private StaticRegistryStore regStore;
private Hashtable env = new Hashtable();
private CompositeName name;
private String root;
public StaticRegistryContext(String root) throws NamingException {
this.root = root;
regStore = StaticRegistryStore.getStore(root);
name = new CompositeName("");
}
public StaticRegistryContext(String root, CompositeName nm) {
this.root = root;
regStore = StaticRegistryStore.getStore(root);
name = nm;
}
public StaticRegistryContext(String root, Name nm) throws NamingException {
this.root = root;
regStore = StaticRegistryStore.getStore(root);
name = new CompositeName();
Enumeration enom = nm.getAll();
while(enom.hasMoreElements()) {
name.add((String)enom.nextElement());
}
}
private void getRegistryStore() {
if(regStore!=null) return;
regStore = StaticRegistryStore.getStore(root);
}
private synchronized void ensureStore() {
if(regStore!=null) return;
regStore = StaticRegistryStore.createStore(root);
}
/**
* Adds a new environment property to the environment of this context.
*/
public Object addToEnvironment(String propName, Object propVal) throws NamingException {
return env.put(propName, propVal);
}
/**
* Binds a name to an object.
*/
public void bind(Name name, Object obj) throws NamingException {
final String oldName = name==null ? "<null>" : name.toString();
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
log.debug("bind(): Name revamped: " + oldName + " -> " + name);
ensureStore();
regStore.bind(name, obj);
}
/**
* Binds a name to an object.
*/
public void bind(String name, Object obj) throws NamingException {
bind(new CompositeName(name), obj);
}
/**
* Closes this context.
*/
public void close() throws NamingException {
}
private Name normalize(Name name) {
if(name.size() > 0) {
Name nm = name.getPrefix(1);
if(nm.size() == 0)
name = name.getSuffix(1);
}
return name;
}
/**
* Composes the name of this context with a name relative to this context.
*/
public Name composeName(Name name, Name prefix) throws NamingException {
CompositeName retName = new CompositeName();
retName.addAll(prefix);
retName.addAll(name);
return retName;
}
/**
* Composes the name of this context with a name relative to this context.
*/
public String composeName(String name, String prefix) throws NamingException {
if(name.startsWith("/")) name = name.substring(1);
if(prefix.endsWith("/")) prefix = prefix.substring(0, prefix.length()-1);
CompositeName retName = new CompositeName(prefix + "/" + name);
return retName.toString();
}
/**
* 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 {
final String oldName = name == null ? "<null>" : name.toString();
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
log.debug("createSubcontext(): Name recomposed: " + oldName + " -> " + name);
ensureStore();
return regStore.createSubcontext(name, mkparents);
}
/**
* Creates and binds a new context.
*/
public Context createSubcontext(String name) throws NamingException {
return createSubcontext(new CompositeName(name), false);
}
/**
* Creates and binds a new context.
*/
public Context createSubcontext(String name, boolean mkparents) throws NamingException {
return createSubcontext(new CompositeName(name), mkparents);
}
/**
* Destroys the named context and removes it from the namespace.
*/
public void destroySubcontext(Name name) throws NamingException {
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
getRegistryStore();
if(regStore!=null) regStore.destroySubcontext(name);
}
/**
* Destroys the named context and removes it from the namespace.
*/
public void destroySubcontext(String name) throws NamingException {
destroySubcontext(new CompositeName(name));
}
/**
* Retrieves the environment in effect for this context.
*/
public Hashtable getEnvironment() throws NamingException {
return env;
}
/**
* Retrieves the full name of this context within its own namespace.
*/
public String getNameInNamespace() throws NamingException {
return String.valueOf(name);
}
/**
* Retrieves the parser associated with the named context.
*/
public NameParser getNameParser(Name name) throws NamingException {
return StaticRegistryStore.getNameParser();
}
/**
* Retrieves the parser associated with the named context.
*/
public NameParser getNameParser(String name) throws NamingException {
return StaticRegistryStore.getNameParser();
}
/**
* 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 {
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
getRegistryStore();
if(regStore!=null)
return regStore.list(name);
else
return new StaticRegistryStore.ListNameEnum(new LinkedList());
}
/**
* 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 {
return list(new CompositeName(name));
}
/**
* Enumerates the names bound in the named context, along with the objects bound to them.
*/
public NamingEnumeration listBindings(Name name) throws NamingException {
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
getRegistryStore();
if(regStore!=null)
return regStore.listBindings(name);
else
return new StaticRegistryStore.ListNameEnum(new LinkedList());
}
/**
* Enumerates the names bound in the named context, along with the objects bound to them.
*/
public NamingEnumeration listBindings(String name) throws NamingException {
return listBindings(new CompositeName(name));
}
/**
* Retrieves the named object.
*/
public Object lookup(Name name) throws NamingException {
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
getRegistryStore();
if(regStore == null) {
String strn = String.valueOf(name);
if("".equals(strn) || "/".equals(strn)) {
return this;
}
throw new NameNotFoundException("Name not found: " + name + " (Store does not exist: " + root + ")");
}
return regStore.lookup(name);
}
/**
* Retrieves the named object.
*/
public Object lookup(String name) throws NamingException {
return lookup(new CompositeName(name));
}
/**
* Retrieves the named object, following links except for the terminal atomic component of the name.
*/
public Object lookupLink(Name name) throws NamingException {
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
getRegistryStore();
if(regStore == null) throw new NameNotFoundException("Name not found: " + name + " (Store does not exist: " + root + ")");
return regStore.lookupLink(name);
}
/**
* Retrieves the named object, following links except for the terminal atomic component of the name.
*/
public Object lookupLink(String name) throws NamingException {
return lookupLink(new CompositeName(name));
}
/**
* Binds a name to an object, overwriting any existing binding.
*/
public void rebind(Name name, Object obj) throws NamingException {
// Always prefix it - you don't want a user to escape the cage
name = composeName(name, this.name);
ensureStore();
regStore.rebind(name, obj);
}
/**
* Binds a name to an object, overwriting any existing binding.
*/
public void rebind(String name, Object obj) throws NamingException {
rebind(new CompositeName(name), obj);
}
/**
* Removes an environment property from the environment of this context.
*/
public Object removeFromEnvironment(String propName) throws NamingException {
return env.remove(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 {
// Always prefix it - you don't want a user to escape the cage
oldName = composeName(oldName, this.name);
newName = composeName(newName, this.name);
ensureStore();
regStore.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 {
rename(new CompositeName(oldName), new CompositeName(newName));
}
/**
* Unbinds the named object.
*/
public void unbind(Name name) throws NamingException {
// Always prefix it - you don't want a user to escape the cage
getRegistryStore();
if(regStore == null) return;
name = composeName(name, this.name);
regStore.unbind(name);
}
/**
* Unbinds the named object.
*/
public void unbind(String name) throws NamingException {
unbind(new CompositeName(name));
}
public String toString() {
return super.toString() + ": " + String.valueOf(name);
}
public Object getMutex() {
ensureStore();
return regStore.getMutex();
}
}