package org.jboss.proxy.ejb;
import java.util.List;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import org.jboss.system.Registry;
import org.jboss.logging.Logger;
import org.jboss.ha.framework.interfaces.LoadBalancePolicy;
import org.jboss.ha.framework.interfaces.DistributedReplicantManager;
import org.jboss.ha.framework.interfaces.HAPartition;
import org.jboss.ha.framework.server.HATarget;
import javax.management.AttributeChangeNotificationFilter;
import javax.management.NotificationListener;
import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import org.jboss.invocation.InvokerProxyHA;
import org.jboss.invocation.InvokerHA;
import org.jboss.system.ServiceMBean;
import org.jboss.naming.Util;
public class ProxyFactoryHA
extends ProxyFactory
implements DistributedReplicantManager.ReplicantListener, ClusterProxyFactory
{
protected static Logger log = Logger.getLogger(ProxyFactory.class);
protected String replicantName = null;
protected InvokerHA jrmp;
protected HATarget target;
protected DistributedReplicantManager drm = null;
public void create () throws Exception
{
super.create ();
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter ();
filter.enableAttribute ("State");
this.container.getServer ().
addNotificationListener (this.container.getEjbModule ().getServiceName (),
new ProxyFactoryHA.StateChangeListener (),
filter,
null);
}
public void start () throws Exception
{
super.start ();
}
protected void setupInvokers() throws Exception
{
String partitionName = container.getBeanMetaData().getClusterConfigMetaData().getPartitionName();
InitialContext ctx = new InitialContext();
HAPartition partition = (HAPartition) ctx.lookup("/HAPartition/" + partitionName);
this.drm = partition.getDistributedReplicantManager ();
replicantName = jmxName.toString ();
ObjectName oname = new ObjectName(invokerMetaData.getInvokerMBean());
jrmp = (InvokerHA)Registry.lookup(oname);
if (jrmp == null)
throw new RuntimeException("home JRMPInvokerHA is null: " + oname);
target = new HATarget(partition, replicantName, jrmp.getStub (), HATarget.MAKE_INVOCATIONS_WAIT);
jrmp.registerBean(jmxName, target);
String clusterFamilyName = partitionName + "/" + jmxName + "/";
drm.registerListener (replicantName, this);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class clazz;
LoadBalancePolicy policy;
clazz = cl.loadClass(container.getBeanMetaData().getClusterConfigMetaData().getHomeLoadBalancePolicy());
policy = (LoadBalancePolicy)clazz.newInstance();
homeInvoker = jrmp.createProxy(jmxName, policy, clusterFamilyName + "H");
String homeName = jndiBinding + "-HomeInvoker";
log.debug("(re-)Binding Home invoker under: " + homeName);
Util.rebind(ctx,
homeName,
homeInvoker
);
clazz = cl.loadClass(container.getBeanMetaData().getClusterConfigMetaData().getBeanLoadBalancePolicy());
policy = (LoadBalancePolicy)clazz.newInstance();
beanInvoker = jrmp.createProxy(jmxName, policy, clusterFamilyName + "R");
String beanName = jndiBinding + "-RemoteInvoker";
log.debug("(re-)Binding Remote invoker under: " + beanName);
Util.rebind(ctx,
beanName,
beanInvoker
);
}
public void destroy ()
{
super.destroy ();
try
{
jrmp.unregisterBean(jmxName);
target.destroy();
}
catch (Exception ignored)
{
}
try
{
InitialContext ctx = new InitialContext();
String homeInvokerName = jndiBinding + "-HomeInvoker";
ctx.unbind(homeInvokerName);
}
catch(Exception ignored)
{
}
try
{
InitialContext ctx = new InitialContext();
String beanInvokerName = jndiBinding + "-RemoteInvoker";
ctx.unbind(beanInvokerName);
}
catch(Exception ignored)
{
}
if( drm != null )
drm.unregisterListener (replicantName, this);
}
protected void containerIsFullyStarted ()
{
if( target != null )
target.setInvocationsAuthorization (HATarget.ENABLE_INVOCATIONS);
}
protected void containerIsAboutToStop ()
{
if( target != null )
{
target.setInvocationsAuthorization (HATarget.DISABLE_INVOCATIONS);
target.disable ();
}
}
public void replicantsChanged (String key, List newReplicants, int newReplicantsViewId)
{
try
{
if (homeInvoker instanceof InvokerProxyHA)
{
((InvokerProxyHA)homeInvoker).updateClusterInfo (target.getReplicants(), target.getCurrentViewId ());
}
if (beanInvoker instanceof InvokerProxyHA)
{
((InvokerProxyHA)beanInvoker).updateClusterInfo (target.getReplicants(), target.getCurrentViewId ());
}
log.debug ("Rebinding in JNDI... " + key);
rebindHomeProxy ();
}
catch (Exception none)
{
log.debug (none);
}
}
class StateChangeListener implements NotificationListener
{
public void handleNotification (Notification notification, java.lang.Object handback)
{
if (notification instanceof AttributeChangeNotification)
{
AttributeChangeNotification notif = (AttributeChangeNotification) notification;
int value = ((Integer)notif.getNewValue()).intValue ();
if (value == ServiceMBean.STARTED)
{
log.debug ("Container fully started: enabling HA-RMI access to bean");
containerIsFullyStarted ();
}
else if (value == ServiceMBean.STOPPING)
{
log.debug ("Container about to stop: disabling HA-RMI access to bean");
containerIsAboutToStop ();
}
}
}
}
}