package org.jboss.naming;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.Hashtable;
import java.util.Properties;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;
import javax.naming.ldap.Control;
import javax.naming.spi.ObjectFactory;
import org.jboss.system.ServiceMBeanSupport;
public class ExternalContext
extends ServiceMBeanSupport
implements ExternalContextMBean
{
private boolean remoteAccess;
private SerializableInitialContext contextInfo = new SerializableInitialContext();
public ExternalContext()
{
super();
}
public ExternalContext(String jndiName, String contextPropsURL)
throws IOException, NamingException
{
setJndiName(jndiName);
setPropertiesURL(contextPropsURL);
}
public String getJndiName()
{
return contextInfo.getJndiName();
}
public void setJndiName(String jndiName) throws NamingException
{
contextInfo.setJndiName(jndiName);
if( super.getState() == STARTED )
{
unbind(jndiName);
try
{
rebind();
}
catch(Exception e)
{
NamingException ne = new NamingException("Failed to update jndiName");
ne.setRootCause(e);
throw ne;
}
}
}
public boolean getRemoteAccess()
{
return remoteAccess;
}
public void setRemoteAccess(final boolean remoteAccess)
{
this.remoteAccess = remoteAccess;
}
public boolean getCacheContext()
{
return contextInfo.getCacheContext();
}
public void setCacheContext(boolean cacheContext)
{
contextInfo.setCacheContext(cacheContext);
}
public String getInitialContext()
{
return contextInfo.getInitialContext();
}
public void setInitialContext(String className) throws ClassNotFoundException
{
contextInfo.loadClass(className);
}
public void setPropertiesURL(String contextPropsURL) throws IOException
{
contextInfo.loadProperties(contextPropsURL);
}
public void setProperties(final Properties props) throws IOException
{
contextInfo.setProperties(props);
}
public Properties getProperties() throws IOException
{
return contextInfo.getProperties();
}
protected void startService() throws Exception
{
rebind();
}
protected void stopService() throws Exception
{
if( contextInfo.getCacheContext() )
unbind(contextInfo.getJndiName());
}
private static Context createContext(Context rootContext, Name name) throws NamingException
{
Context subctx = rootContext;
for(int n = 0; n < name.size(); n ++)
{
String atom = name.get(n);
try
{
Object obj = subctx.lookup(atom);
subctx = (Context) obj;
}
catch(NamingException e)
{
subctx = subctx.createSubcontext(atom);
}
}
return subctx;
}
private void rebind() throws Exception
{
boolean debug = log.isDebugEnabled();
Context ctx = contextInfo.newContext();
Context rootCtx = (Context) new InitialContext();
if (debug)
log.debug("ctx="+ctx+", env="+ctx.getEnvironment());
String jndiName = contextInfo.getJndiName();
Name fullName = rootCtx.getNameParser("").parse(jndiName);
if (debug)
log.debug("fullName="+fullName);
Name parentName = fullName;
if( fullName.size() > 1 )
parentName = fullName.getPrefix(fullName.size()-1);
else
parentName = new CompositeName();
if (debug)
log.debug("parentName="+parentName);
Context parentCtx = createContext(rootCtx, parentName);
if (debug)
log.debug("parentCtx="+parentCtx);
Name atomName = fullName.getSuffix(fullName.size()-1);
String atom = atomName.get(0);
boolean cacheContext = contextInfo.getCacheContext();
if( remoteAccess == true )
{
parentCtx.rebind(atom, contextInfo);
if( cacheContext == true )
{
ctx = CachedContext.createProxyContext(ctx);
NonSerializableFactory.rebind(jndiName, ctx);
}
}
else if( cacheContext == true )
{
Context proxyCtx = CachedContext.createProxyContext(ctx);
NonSerializableFactory.rebind(rootCtx, jndiName, proxyCtx);
}
else
{
parentCtx.rebind(atom, contextInfo);
}
}
private void unbind(String jndiName)
{
try
{
Context rootCtx = new InitialContext();
Context ctx = (Context) rootCtx.lookup(jndiName);
if( ctx != null )
ctx.close();
rootCtx.unbind(jndiName);
NonSerializableFactory.unbind(jndiName);
}
catch(NamingException e)
{
log.error("unbind failed", e);
}
}
public static class SerializableInitialContext
extends RefAddr
implements Referenceable, Serializable, ObjectFactory
{
private static final long serialVersionUID = -6512260531255770463L;
private String jndiName;
private Class contextClass = javax.naming.InitialContext.class;
private Properties contextProps;
private boolean cacheContext = true;
private transient Context initialContext;
public SerializableInitialContext()
{
this("SerializableInitialContext");
}
public SerializableInitialContext(String addrType)
{
super(addrType);
}
public String getJndiName()
{
return jndiName;
}
public void setJndiName(final String jndiName)
{
this.jndiName = jndiName;
}
public boolean getCacheContext()
{
return cacheContext;
}
public void setCacheContext(final boolean cacheContext)
{
this.cacheContext = cacheContext;
}
public String getInitialContext()
{
return contextClass.getName();
}
public void loadClass(String className) throws ClassNotFoundException
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
contextClass = loader.loadClass(className);
}
public void setProperties(final Properties props)
{
contextProps = props;
}
public Properties getProperties()
{
return contextProps;
}
public void loadProperties(String contextPropsURL) throws IOException
{
InputStream is = null;
contextProps = new Properties();
try
{
URL url = new URL(contextPropsURL);
is = url.openStream();
contextProps.load(is);
return;
}
catch (IOException e)
{ is = null;
}
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(contextPropsURL);
if( is == null )
{
throw new IOException("Failed to locate context props as URL or resource:"+contextPropsURL);
}
contextProps.load(is);
}
Context newContext() throws Exception
{
initialContext = (Context) NonSerializableFactory.lookup(jndiName);
if( initialContext == null )
initialContext = newContext(contextClass, contextProps);
return initialContext;
}
static Context newContext(Class contextClass, Properties contextProps)
throws Exception
{
Context ctx = null;
try
{
ctx = newDefaultContext(contextClass, contextProps);
}
catch(NoSuchMethodException e)
{
ctx = newLdapContext(contextClass, contextProps);
}
return ctx;
}
private static Context newDefaultContext(Class contextClass, Properties contextProps)
throws Exception
{
Context ctx = null;
Class[] types = {Hashtable.class};
Constructor ctor = contextClass.getConstructor(types);
Object[] args = {contextProps};
ctx = (Context) ctor.newInstance(args);
return ctx;
}
private static Context newLdapContext(Class contextClass, Properties contextProps)
throws Exception
{
Context ctx = null;
Class[] types = {Hashtable.class, Control[].class};
Constructor ctor = contextClass.getConstructor(types);
Object[] args = {contextProps, null};
ctx = (Context) ctor.newInstance(args);
return ctx;
}
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment)
throws Exception
{
Reference ref = (Reference) obj;
SerializableInitialContext sic = (SerializableInitialContext) ref.get(0);
return sic.newContext();
}
public Reference getReference() throws NamingException
{
Reference ref = new Reference(Context.class.getName(), this, this.getClass().getName(), null);
return ref;
}
public Object getContent()
{
return null;
}
}
static class CachedContext implements InvocationHandler
{
Context externalCtx;
CachedContext(Context externalCtx)
{
this.externalCtx = externalCtx;
}
static Context createProxyContext(Context ctx)
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class[] interfaces = ctx.getClass().getInterfaces();
InvocationHandler handler = new CachedContext(ctx);
Context proxyCtx = (Context) Proxy.newProxyInstance(loader, interfaces, handler);
return proxyCtx;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Object value = null;
if( method.getName().equals("close") )
{
}
else
{
try
{
value = method.invoke(externalCtx, args);
}
catch(InvocationTargetException e)
{
throw e.getTargetException();
}
}
return value;
}
}
}