package org.jboss.invocation.http.server;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.naming.InitialContext;
import org.jboss.invocation.Invoker;
import org.jboss.invocation.InvokerInterceptor;
import org.jboss.invocation.http.interfaces.ClientMethodInterceptorHA;
import org.jboss.invocation.http.interfaces.HttpInvokerProxyHA;
import org.jboss.system.server.ServerConfigUtil;
import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.ha.framework.server.HATarget;
public class HttpProxyFactoryHA extends HttpProxyFactory
implements HttpProxyFactoryHAMBean
{
private ObjectName realJmxInvokerName;
private ObjectName wrappedJmxInvokerName;
private String partitionName = ServerConfigUtil.getDefaultPartitionName();
private Class policyClass;
private HAInvokerWrapper invokerWrapper;
private HATarget invokerTarget;
public Class getLoadBalancePolicy()
{
return this.policyClass;
}
public void setLoadBalancePolicy(Class policyClass)
{
this.policyClass = policyClass;
}
public String getPartitionName()
{
return this.partitionName;
}
public void setPartitionName(String name)
{
this.partitionName = name;
}
public void setInvokerName(ObjectName jmxInvokerName)
{
realJmxInvokerName = jmxInvokerName;
ObjectName factoryName = getServiceName();
Hashtable props = factoryName.getKeyPropertyList();
props.put("wrapperType", "httpHA");
try
{
wrappedJmxInvokerName = new ObjectName(factoryName.getDomain(), props);
super.setInvokerName(wrappedJmxInvokerName);
}
catch(MalformedObjectNameException e)
{
throw new IllegalStateException("Was not able to create wrapped ObjectName");
}
}
public ObjectName getRealJmxInvokerName()
{
return realJmxInvokerName;
}
protected ArrayList defineInterceptors()
{
ArrayList interceptorClasses = new ArrayList();
interceptorClasses.add(ClientMethodInterceptorHA.class);
interceptorClasses.add(InvokerInterceptor.class);
return interceptorClasses;
}
protected Invoker createInvoker() throws Exception
{
InitialContext iniCtx = new InitialContext();
HAPartition partition = (HAPartition) iniCtx.lookup("/HAPartition/" + partitionName);
checkInvokerURL();
Serializable invokerStub = super.getInvokerURL();
invokerTarget = new HATarget(partition, wrappedJmxInvokerName.toString(),
invokerStub, HATarget.MAKE_INVOCATIONS_WAIT);
log.debug("Created invoker: "+invokerTarget);
MBeanServer mbeanServer = super.getServer();
invokerWrapper = new HAInvokerWrapper(mbeanServer, realJmxInvokerName, invokerTarget);
mbeanServer.registerMBean(invokerWrapper, wrappedJmxInvokerName);
LoadBalancePolicy policy = (LoadBalancePolicy) policyClass.newInstance();
String clusterFamilyName = partitionName + "/" + wrappedJmxInvokerName.toString();
Invoker delegateInvoker = new HttpInvokerProxyHA(invokerTarget.getReplicants(), invokerTarget.getCurrentViewId (),
policy, clusterFamilyName);
return delegateInvoker;
}
protected void stopService() throws Exception
{
try
{
MBeanServer mbeanServer = super.getServer();
mbeanServer.unregisterMBean(wrappedJmxInvokerName);
}
catch(Exception e)
{
log.debug("Failed to unregister HAInvokerWrapper: "+wrappedJmxInvokerName, e);
}
super.stopService();
}
public void destroy()
{
super.destroy();
try
{
invokerTarget.destroy();
}
catch(Exception ignore)
{
}
}
}