package org.jboss.invocation.http.server;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import org.jboss.invocation.Invoker;
import org.jboss.invocation.InvokerInterceptor;
import org.jboss.invocation.http.interfaces.HttpInvokerProxy;
import org.jboss.invocation.http.interfaces.ClientMethodInterceptor;
import org.jboss.naming.Util;
import org.jboss.proxy.GenericProxyFactory;
import org.jboss.system.Registry;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.system.server.ServerConfigUtil;
import org.jboss.util.StringPropertyReplacer;
import org.jboss.metadata.MetaData;
import org.w3c.dom.Element;
public class HttpProxyFactory extends ServiceMBeanSupport
implements HttpProxyFactoryMBean
{
private ObjectName jmxInvokerName;
private Object theProxy;
private String invokerURL;
private String invokerURLPrefix = "http://";
private String invokerURLSuffix = ":8080/invoker/JMXInvokerServlet";
private boolean useHostName = false;
private String jndiName;
private Class exportedInterface;
private Element interceptorConfig;
private ArrayList interceptorClasses;
public HttpProxyFactory()
{
}
public ObjectName getInvokerName()
{
return jmxInvokerName;
}
public void setInvokerName(ObjectName jmxInvokerName)
{
this.jmxInvokerName = jmxInvokerName;
}
public String getJndiName()
{
return jndiName;
}
public void setJndiName(String jndiName)
{
this.jndiName = jndiName;
}
public String getInvokerURL()
{
return invokerURL;
}
public void setInvokerURL(String invokerURL)
{
String tmp = StringPropertyReplacer.replaceProperties(invokerURL);
this.invokerURL = tmp;
log.debug("Set invokerURL to "+this.invokerURL);
}
public String getInvokerURLPrefix()
{
return invokerURLPrefix;
}
public void setInvokerURLPrefix(String invokerURLPrefix)
{
this.invokerURLPrefix = invokerURLPrefix;
}
public String getInvokerURLSuffix()
{
return invokerURLSuffix;
}
public void setInvokerURLSuffix(String invokerURLSuffix)
{
this.invokerURLSuffix = invokerURLSuffix;
}
public boolean getUseHostName()
{
return useHostName;
}
public void setUseHostName(boolean flag)
{
this.useHostName = flag;
}
public Class getExportedInterface()
{
return exportedInterface;
}
public void setExportedInterface(Class exportedInterface)
{
this.exportedInterface = exportedInterface;
}
public Element getClientInterceptors()
{
return interceptorConfig;
}
public void setClientInterceptors(Element config) throws Exception
{
this.interceptorConfig = config;
Iterator interceptorElements = MetaData.getChildrenByTagName(interceptorConfig, "interceptor");
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if( interceptorClasses != null )
interceptorClasses.clear();
else
interceptorClasses = new ArrayList();
while( interceptorElements != null && interceptorElements.hasNext() )
{
Element ielement = (Element) interceptorElements.next();
String className = null;
className = MetaData.getElementContent(ielement);
Class clazz = loader.loadClass(className);
interceptorClasses.add(clazz);
}
}
public Object getProxy()
{
return theProxy;
}
public Object getProxy(Object id)
{
Class[] ifaces = {exportedInterface};
ArrayList interceptorClasses = null; ClassLoader loader = Thread.currentThread().getContextClassLoader();
GenericProxyFactory proxyFactory = new GenericProxyFactory();
Object newProxy = null;
return newProxy;
}
protected void startService() throws Exception
{
Invoker delegateInvoker = createInvoker();
Integer nameHash = new Integer(jmxInvokerName.hashCode());
log.debug("Bound delegate: "+delegateInvoker
+" for invoker="+jmxInvokerName);
Registry.bind(nameHash, jmxInvokerName);
Object cacheID = null;
String proxyBindingName = null;
Class[] ifaces = {exportedInterface};
if( interceptorClasses == null )
interceptorClasses = defineDefaultInterceptors();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
GenericProxyFactory proxyFactory = new GenericProxyFactory();
theProxy = proxyFactory.createProxy(cacheID, jmxInvokerName,
delegateInvoker, jndiName, proxyBindingName, interceptorClasses,
loader, ifaces);
log.debug("Created HttpInvokerProxy for invoker="+jmxInvokerName
+", nameHash="+nameHash);
if( jndiName != null )
{
InitialContext iniCtx = new InitialContext();
Util.bind(iniCtx, jndiName, theProxy);
log.debug("Bound proxy under jndiName="+jndiName);
}
}
protected void stopService() throws Exception
{
Integer nameHash = new Integer(jmxInvokerName.hashCode());
Registry.unbind(jmxInvokerName);
Registry.unbind(nameHash);
if( jndiName != null )
{
InitialContext iniCtx = new InitialContext();
Util.unbind(iniCtx, jndiName);
}
}
protected ArrayList defineDefaultInterceptors()
{
ArrayList tmp = new ArrayList();
tmp.add(ClientMethodInterceptor.class);
tmp.add(InvokerInterceptor.class);
return tmp;
}
protected Invoker createInvoker() throws Exception
{
checkInvokerURL();
HttpInvokerProxy delegateInvoker = new HttpInvokerProxy(invokerURL);
return delegateInvoker;
}
protected void checkInvokerURL() throws UnknownHostException
{
if( invokerURL == null )
{
String host = ServerConfigUtil.getSpecificBindAddress();
if( host == null )
{
InetAddress addr = InetAddress.getLocalHost();
host = useHostName ? addr.getHostName() : addr.getHostAddress();
}
String url = invokerURLPrefix + host + invokerURLSuffix;
setInvokerURL(url);
}
}
}