package org.jboss.invocation.jrmp.server;
import java.rmi.MarshalledObject;
import javax.management.ObjectName;
import javax.management.InstanceNotFoundException;
import javax.management.ReflectionException;
import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.Invoker;
import org.jboss.invocation.InvokerHA;
import org.jboss.invocation.MarshalledInvocation;
import org.jboss.system.Registry;
import org.jboss.ha.framework.interfaces.HARMIResponse;
import org.jboss.ha.framework.server.HATarget;
import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
import org.jboss.ha.framework.interfaces.GenericClusteringException;
import java.util.ArrayList;
import java.util.HashMap;
public class JRMPInvokerHA
extends JRMPInvoker
implements InvokerHA
{
protected HashMap beanMap = new HashMap();
public JRMPInvokerHA()
{
super();
}
protected void startService() throws Exception
{
loadCustomSocketFactories();
if (log.isDebugEnabled())
{
log.debug("RMI Port='" + (rmiPort == ANONYMOUS_PORT ?
"Anonymous" : Integer.toString(rmiPort)+"'"));
log.debug("Client SocketFactory='" + (clientSocketFactory == null ?
"Default" : clientSocketFactory.toString()+"'"));
log.debug("Server SocketFactory='" + (serverSocketFactory == null ?
"Default" : serverSocketFactory.toString()+"'"));
log.debug("Server SocketAddr='" + (serverAddress == null ?
"Default" : serverAddress+"'"));
log.debug("SecurityDomain='" + (sslDomain == null ?
"None" : sslDomain+"'"));
}
exportCI();
Registry.bind(support.getServiceName(), this);
}
protected void stopService() throws Exception
{
unexportCI();
}
public void registerBean(ObjectName beanName, HATarget target) throws Exception
{
Integer hash = new Integer(beanName.hashCode());
log.debug("registerBean: "+beanName);
if (beanMap.containsKey(hash))
{
throw new IllegalStateException("Trying to register bean with the existing hashCode");
}
beanMap.put(hash, target);
}
public Invoker createProxy(ObjectName beanName, LoadBalancePolicy policy,
String proxyFamilyName) throws Exception
{
Integer hash = new Integer(beanName.hashCode());
HATarget target = (HATarget) beanMap.get(hash);
if (target == null)
{
throw new IllegalStateException("The bean hashCode not found");
}
String familyName = proxyFamilyName;
if (familyName == null)
familyName= target.getAssociatedPartition().getPartitionName() + "/" + beanName;
JRMPInvokerProxyHA proxy = new JRMPInvokerProxyHA(target.getReplicants(),
policy,
familyName,
target.getCurrentViewId ());
return proxy;
}
public void unregisterBean(ObjectName beanName) throws Exception
{
Integer hash = new Integer(beanName.hashCode());
beanMap.remove(hash);
}
public Object invoke(Invocation invocation)
throws Exception
{
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try
{
invocation.setTransaction(importTPC(((MarshalledInvocation) invocation).getTransactionPropagationContext()));
ObjectName mbean = (ObjectName) Registry.lookup(invocation.getObjectName());
long clientViewId = ((Long)invocation.getValue("CLUSTER_VIEW_ID")).longValue();
HATarget target = (HATarget)beanMap.get(invocation.getObjectName());
if (target == null)
{
throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO,
"target is not/no more registered on this node");
}
if (!target.invocationsAllowed ())
throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO,
"invocations are currently not allowed on this target");
Object rtn = support.getServer().invoke(mbean,
"invoke",
new Object[] { invocation },
Invocation.INVOKE_SIGNATURE);
HARMIResponse rsp = new HARMIResponse();
if (clientViewId != target.getCurrentViewId())
{
rsp.newReplicants = new ArrayList(target.getReplicants());
rsp.currentViewId = target.getCurrentViewId();
}
rsp.response = rtn;
return new MarshalledObject(rsp);
}
catch (InstanceNotFoundException e)
{
throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, e);
}
catch (ReflectionException e)
{
throw new GenericClusteringException(GenericClusteringException.COMPLETED_NO, e);
}
catch (Exception e)
{
org.jboss.mx.util.JMXExceptionDecoder.rethrow(e);
throw new org.jboss.util.UnreachableStatementException();
}
finally
{
Thread.currentThread().setContextClassLoader(oldCl);
}
}
}