package org.jboss.invocation.http.server;
import java.util.ArrayList;
import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.MBeanInfo;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanParameterInfo;
import org.jboss.ha.framework.interfaces.HARMIResponse;
import org.jboss.ha.framework.interfaces.GenericClusteringException;
import org.jboss.ha.framework.server.HATarget;
import org.jboss.invocation.Invocation;
import org.jboss.logging.Logger;
import org.jboss.mx.util.JMXExceptionDecoder;
import org.jboss.mx.util.DynamicMBeanSupport;
public class HAInvokerWrapper extends DynamicMBeanSupport
{
private static Logger log = Logger.getLogger(HAInvokerWrapper.class);
private MBeanServer mbeanServer;
private MBeanInfo info;
private ObjectName targetName;
private HATarget target;
public HAInvokerWrapper(MBeanServer mbeanServer, ObjectName targetName, HATarget target)
{
this.mbeanServer = mbeanServer;
this.targetName = targetName;
this.target = target;
MBeanAttributeInfo[] attrInfo = null;
MBeanConstructorInfo[] ctorInfo = null;
MBeanParameterInfo[] sig = {
new MBeanParameterInfo("invocation", Invocation.class.getName(),
"The invocation content information")
};
MBeanOperationInfo[] opInfo = {
new MBeanOperationInfo("invoke", "The detached invoker entry point",
sig, "java.lang.Object", MBeanOperationInfo.ACTION)
};
MBeanNotificationInfo[] eventInfo = null;
this.info = new MBeanInfo(getClass().getName(),
"A wrapper inovker that delegates to the target invoker",
attrInfo,
ctorInfo,
opInfo,
eventInfo);
}
public Object invoke(String actionName, Object[] params, String[] signature)
throws MBeanException, ReflectionException
{
if( params == null || params.length != 1 ||
(params[0] instanceof Invocation) == false )
{
NoSuchMethodException e = new NoSuchMethodException(actionName);
throw new ReflectionException(e, actionName);
}
Invocation invocation = (Invocation) params[0];
try
{
Object value = invoke(invocation);
return value;
}
catch(Exception e)
{
throw new ReflectionException(e, "Invoke failure");
}
}
public Object invoke(Invocation invocation)
throws Exception
{
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try
{
Object[] args = {invocation};
String[] sig = {"org.jboss.invocation.Invocation"};
Object rtn = mbeanServer.invoke(targetName, "invoke", args, sig);
Long clientViewId = (Long) invocation.getValue("CLUSTER_VIEW_ID");
HARMIResponse rsp = new HARMIResponse();
if (clientViewId.longValue() != target.getCurrentViewId())
{
rsp.newReplicants = new ArrayList(target.getReplicants());
rsp.currentViewId = target.getCurrentViewId();
}
rsp.response = rtn;
return rsp;
}
catch (Exception e)
{
e = (Exception) JMXExceptionDecoder.decode(e);
if( e instanceof JMException )
e = new GenericClusteringException (GenericClusteringException.COMPLETED_NO, e.getMessage());
if( log.isTraceEnabled() )
log.trace("operation failed", e);
throw e;
}
finally
{
Thread.currentThread().setContextClassLoader(oldCl);
}
}
public MBeanInfo getMBeanInfo()
{
return info;
}
}