User: slaboure Date: 01/11/26 06:20:01 Modified: src/main/org/jboss/ha/framework/server HAPartitionImpl.java Log: Added javadoc comments. Reorganized code to JBoss coding conventions. Revision Changes Path 1.10 +256 -239 jbossmx/src/main/org/jboss/ha/framework/server/HAPartitionImpl.java Index: HAPartitionImpl.java =================================================================== RCS file: /cvsroot/jboss/jbossmx/src/main/org/jboss/ha/framework/server/HAPartitionImpl.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- HAPartitionImpl.java 2001/11/26 03:24:51 1.9 +++ HAPartitionImpl.java 2001/11/26 14:20:01 1.10 @@ -7,61 +7,46 @@ package org.jboss.ha.framework.server; - -import JavaGroups.MethodLookup; -import JavaGroups.MethodLookupClos; -import JavaGroups.MethodLookupJava; -import JavaGroups.MembershipListener; -import JavaGroups.MessageListener; -import JavaGroups.JChannel; -import JavaGroups.Channel; -import JavaGroups.Message; -import JavaGroups.MethodCall; -import JavaGroups.RpcDispatcher; -import JavaGroups.RspList; -import JavaGroups.Rsp; -import JavaGroups.GroupRequest; -import JavaGroups.View; -import JavaGroups.Address; -import JavaGroups.Util; - import java.util.Vector; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; -import java.io.Serializable; - -import javax.naming.InitialContext; -import javax.naming.Reference; -import javax.naming.StringRefAddr; -import javax.naming.Name; import javax.naming.Context; -import javax.naming.NamingException; -import javax.naming.NameNotFoundException; - -import org.jboss.naming.NonSerializableFactory; - -import java.lang.reflect.Method; +import javax.naming.StringRefAddr; +import javax.naming.InitialContext; -import org.jboss.logging.Logger; +import JavaGroups.MethodCall; -import org.jboss.ha.framework.interfaces.HAPartition; import org.jboss.ha.framework.interfaces.DistributedReplicantManager; import org.jboss.ha.framework.interfaces.DistributedState; +import org.jboss.ha.framework.interfaces.HAPartition; +import org.jboss.ha.framework.interfaces.HAPartition.HAPartitionStateTransfer; +import org.jboss.ha.framework.interfaces.HAPartition.HAMembershipListener; +import org.jboss.naming.NonSerializableFactory; +import org.jboss.logging.Logger; -/** - * This class is an abstraction class for a JavaGroups RPCDispatch and JChannel. +/** + * This class is an abstraction class for a JavaGroups RPCDispatch and JChannel. + * It is a default implementation of HAPartition for the <a href="http://www.javagroups.com/">JavaGroups</A> framework * - * @author [EMAIL PROTECTED] - * @author [EMAIL PROTECTED] - * @version $Revision: 1.9 $ + * @author <a href="mailto:[EMAIL PROTECTED]">Sacha Labourey</a>. + * @author <a href="mailto:[EMAIL PROTECTED]">Bill Burke</a>. + * @version $Revision: 1.10 $ * * <p><b>Revisions:</b><br> -*/ -public class HAPartitionImpl extends RpcDispatcher implements HAPartition, MembershipListener, MessageListener + */ + +public class HAPartitionImpl + extends JavaGroups.RpcDispatcher + implements JavaGroups.MessageListener, JavaGroups.MembershipListener, HAPartition { + // Constants ----------------------------------------------------- + + final JavaGroups.MethodLookup method_lookup_clos=new JavaGroups.MethodLookupClos (); + + // Attributes ---------------------------------------------------- + protected HashMap rpcHandlers = new HashMap (); protected HashMap stateHandlers = new HashMap (); protected ArrayList listeners = new ArrayList (); @@ -71,7 +56,7 @@ protected String nodeName; protected int timeout = 500; - protected JChannel channel = null; + protected JavaGroups.JChannel channel = null; protected DistributedReplicantManagerImpl replicantManager = null; protected DistributedStateImpl dsManager = null; @@ -80,9 +65,11 @@ protected long currentViewId = -1; - final MethodLookup method_lookup_clos=new MethodLookupClos (); + // Static -------------------------------------------------------- - public HAPartitionImpl (String partitionName, JChannel channel, boolean deadlock_detection) throws Exception + // Constructors -------------------------------------------------- + + public HAPartitionImpl (String partitionName, JavaGroups.JChannel channel, boolean deadlock_detection) throws Exception { super(channel, null, null, new Object (), false); // init RpcDispatcher with a fake target object this.log = Logger.getLogger("HAPartition:" + partitionName); @@ -90,6 +77,8 @@ this.partitionName = partitionName; } + // Public -------------------------------------------------------- + public void init () throws Exception { // Subscribe to dHA events comming generated by the JavaGroups protocol stack @@ -124,35 +113,6 @@ log.info ("done initing.."); } - protected void bind (String jndiName, Object who, Class classType, Context ctx) throws Exception - { - // Ah ! This service isn't serializable, so we use a helper class - // - NonSerializableFactory.bind (jndiName, who); - Name n = ctx.getNameParser ("").parse (jndiName); - while (n.size () > 1) - { - String ctxName = n.get (0); - try - { - ctx = (Context)ctx.lookup (ctxName); - } - catch (NameNotFoundException e) - { - log.info ("creating Subcontext" + ctxName); - ctx = ctx.createSubcontext (ctxName); - } - n = n.getSuffix (1); - } - - // The helper class NonSerializableFactory uses address type nns, we go on to - // use the helper class to bind the service object in JNDI - // - StringRefAddr addr = new StringRefAddr ("nns", jndiName); - Reference ref = new Reference ( classType.getName (), addr, NonSerializableFactory.class.getName (), null); - ctx.rebind (n.get (0), ref); - } - public void start () throws Exception { // get current JG group properties @@ -160,7 +120,7 @@ log.debug ("get nodeName"); this.nodeName = channel.GetLocalAddress ().toString (); log.debug ("Get current members"); - View view = channel.GetView (); + JavaGroups.View view = channel.GetView (); this.members = (Vector)view.GetMembers().clone(); log.info ("Num cluster members: " + members.size ()); @@ -195,119 +155,9 @@ new InitialContext ().unbind ("/HAPartition/" + partitionName); log.info ("Partition " + partitionName + " closed."); } - - public String getNodeName () - { - return nodeName; - } - - public String getPartitionName () - { - return partitionName; - } - - public DistributedReplicantManager getDistributedReplicantManager () - { - return replicantManager; - } - - public DistributedState getDistributedStateService () - { - return this.dsManager; - } - - public long getCurrentViewId() - { - return this.currentViewId; - } - - public Vector getCurrentView () - { - return this.members; - } - - // *************************** - // *************************** - // RPC multicast communication - // *************************** - // *************************** - // - public void registerRPCHandler (String objName, Object subscriber) - { - rpcHandlers.put (objName, subscriber); - } - - public void unregisterRPCHandler (String objName, Object subscriber) - { - rpcHandlers.remove (objName); - } - - /** - * This function is an abstraction of RpcDispatcher. - */ - public ArrayList callMethodOnCluster (String objName, String methodName, Object[] args, boolean excludeSelf) throws Exception - { - ArrayList rtn = new ArrayList (); - MethodCall m = new MethodCall (objName + "." + methodName, args); - RspList rsp = this.CallRemoteMethods (null, m, GroupRequest.GET_ALL, timeout); - if (rsp != null) - { - for (int i = 0; i < rsp.size (); i++) - { - Object item = rsp.elementAt (i); - if (item instanceof Rsp) - { - item = ((Rsp)item).GetValue (); - } - rtn.add (item); - } - } - - if (!excludeSelf) - { - m.SetName (methodName); - Object handler = rpcHandlers.get (objName); - Object retval=m.Invoke (handler, method_lookup_clos); - if (retval != null) rtn.add (retval); - } - return rtn; - } - - /** - * This function is an abstraction of RpcDispatcher for asynchronous messages - */ - public void callAsynchMethodOnCluster (String objName, String methodName, Object[] args, boolean excludeSelf) throws Exception - { - MethodCall m = new MethodCall (objName + "." + methodName, args); - this.CallRemoteMethods (null, m, GroupRequest.GET_NONE, timeout); + // JavaGroups.MessageListener implementation ---------------------------------------------- - if (!excludeSelf) - { - m.SetName (methodName); - Object handler = rpcHandlers.get (objName); - m.Invoke (handler, method_lookup_clos); - } - } - // ************************* - // ************************* - // State transfer management - // ************************* - // ************************* - // - - - public void subscribeToStateTransferEvents (String objectName, HAPartition.HAPartitionStateTransfer subscriber) - { - stateHandlers.put (objectName, subscriber); - } - - public void unsubscribeFromStateTransferEvents (String objectName, HAPartition.HAPartitionStateTransfer subscriber) - { - stateHandlers.remove (objectName); - } - - // MessageListener methods // public Object GetState () @@ -319,7 +169,7 @@ // build a "macro" state // HashMap state = new HashMap (); - Iterator keys = stateHandlers.keySet ().iterator (); + java.util.Iterator keys = stateHandlers.keySet ().iterator (); while (keys.hasNext ()) { String key = (String)keys.next (); @@ -348,7 +198,7 @@ } HashMap state = (HashMap)obj; - Iterator keys = state.keySet ().iterator (); + java.util.Iterator keys = state.keySet ().iterator (); while (keys.hasNext ()) { String key = (String)keys.next (); @@ -357,7 +207,7 @@ HAPartition.HAPartitionStateTransfer subscriber = (HAPartition.HAPartitionStateTransfer)stateHandlers.get (key); if (subscriber != null) { - subscriber.setCurrentState ((Serializable)someState); + subscriber.setCurrentState ((java.io.Serializable)someState); } else { @@ -371,39 +221,16 @@ } } - public void Receive (Message msg) + public void Receive (JavaGroups.Message msg) { /* complete */} - - // ************************* - // ************************* - // Group Membership listeners - // ************************* - // ************************* - // - - public void registerMembershipListener (HAPartition.HAMembershipListener listener) - { - synchronized(this.listeners) - { - this.listeners.add (listener); - } - } - - public void unregisterMembershipListener (HAPartition.HAMembershipListener listener) - { - synchronized(this.listeners) - { - this.listeners.remove (listener); - } - } - // MembershipListener required methods + // JavaGroups.MembershipListener implementation ---------------------------------------------- - public void Suspect (Address suspected_mbr) { log.info ("Suspected member: " + suspected_mbr); /* complete */ } + public void Suspect (JavaGroups.Address suspected_mbr) { log.info ("Suspected member: " + suspected_mbr); /* complete */ } public void Block () {} - public void ViewAccepted (View newView) + public void ViewAccepted (JavaGroups.View newView) { try { @@ -453,47 +280,150 @@ } } - protected Vector getDeadMembers (Vector oldMembers, Vector newMembers) + // HAPartition implementation ---------------------------------------------- + + public String getNodeName () { - Vector dead = new Vector (); - for (int i=0; i<oldMembers.size ();i++) + return nodeName; + } + + public String getPartitionName () + { + return partitionName; + } + + public DistributedReplicantManager getDistributedReplicantManager () + { + return replicantManager; + } + + public DistributedState getDistributedStateService () + { + return this.dsManager; + } + + public long getCurrentViewId() + { + return this.currentViewId; + } + + public Vector getCurrentView () + { + return this.members; + } + + // *************************** + // *************************** + // RPC multicast communication + // *************************** + // *************************** + // + public void registerRPCHandler (String objName, Object subscriber) + { + rpcHandlers.put (objName, subscriber); + } + + public void unregisterRPCHandler (String objName, Object subscriber) + { + rpcHandlers.remove (objName); + } + + /** + * This function is an abstraction of RpcDispatcher. + */ + public ArrayList callMethodOnCluster (String objName, String methodName, Object[] args, boolean excludeSelf) throws Exception + { + ArrayList rtn = new ArrayList (); + MethodCall m = new MethodCall (objName + "." + methodName, args); + JavaGroups.RspList rsp = this.CallRemoteMethods (null, m, JavaGroups.GroupRequest.GET_ALL, timeout); + if (rsp != null) { - log.info("is node " + oldMembers.elementAt(i).toString() + "dead?"); - if (!newMembers.contains (oldMembers.elementAt (i))) - { - log.info("node " + oldMembers.elementAt(i).toString() + "is dead"); - dead.add (oldMembers.elementAt (i)); - } - else + for (int i = 0; i < rsp.size (); i++) { - log.info("node " + oldMembers.elementAt(i).toString() + "is NOT dead"); + Object item = rsp.elementAt (i); + if (item instanceof JavaGroups.Rsp) + { + item = ((JavaGroups.Rsp)item).GetValue (); + } + rtn.add (item); } } - return dead; + if (!excludeSelf) + { + m.SetName (methodName); + Object handler = rpcHandlers.get (objName); + Object retval=m.Invoke (handler, method_lookup_clos); + if (retval != null) rtn.add (retval); + } + return rtn; } - protected Vector getNewMembers (Vector oldMembers, Vector allMembers) + /** + * This function is an abstraction of RpcDispatcher for asynchronous messages + */ + public void callAsynchMethodOnCluster (String objName, String methodName, Object[] args, boolean excludeSelf) throws Exception { - Vector newMembers = new Vector (); - for (int i=0; i<allMembers.size ();i++) - if (!oldMembers.contains (allMembers.elementAt (i))) - newMembers.add (allMembers.elementAt (i)); - return newMembers; - } - - - + MethodCall m = new MethodCall (objName + "." + methodName, args); + this.CallRemoteMethods (null, m, JavaGroups.GroupRequest.GET_NONE, timeout); - /////////////////////////////////////////////////////////////// + if (!excludeSelf) + { + m.SetName (methodName); + Object handler = rpcHandlers.get (objName); + m.Invoke (handler, method_lookup_clos); + } + } + // ************************* + // ************************* + // State transfer management + // ************************* + // ************************* + // + public void subscribeToStateTransferEvents (String objectName, HAPartitionStateTransfer subscriber) + { + stateHandlers.put (objectName, subscriber); + } + + public void unsubscribeFromStateTransferEvents (String objectName, HAPartitionStateTransfer subscriber) + { + stateHandlers.remove (objectName); + } + + // ************************* + // ************************* + // Group Membership listeners + // ************************* + // ************************* + // + public void registerMembershipListener (HAMembershipListener listener) + { + synchronized(this.listeners) + { + this.listeners.add (listener); + } + } + + public void unregisterMembershipListener (HAMembershipListener listener) + { + synchronized(this.listeners) + { + this.listeners.remove (listener); + } + } + + // JavaGroups.RpcDispatcher overrides --------------------------------------------------- + /** * Message contains MethodCall. Execute it against *this* object and return result. * Use MethodCall.Invoke() to do this. Return result. * * This overrides RpcDispatcher.Handle so that we can dispatch to many different objects. + * @param req The JavaGroups representation of the method invocation + * @return The serializable return value from the invocation */ - public Object Handle (Message req) + public Object Handle (JavaGroups.Message req) { Object body = null; Object retval = null; @@ -507,7 +437,7 @@ try { - body=Util.ObjectFromByteBuffer (req.GetBuffer ()); + body=JavaGroups.Util.ObjectFromByteBuffer (req.GetBuffer ()); } catch(Exception e) { @@ -541,4 +471,91 @@ return retval; } + + // Package protected --------------------------------------------- + + // Protected ----------------------------------------------------- + + /** + * Helper method that binds the partition in the JNDI tree. + * @param jndiName Name under which the object must be bound + * @param who Object to bind in JNDI + * @param classType Class type under which should appear the bound object + * @param ctx Naming context under which we bind the object + * @throws Exception Thrown if a naming exception occurs during binding + */ + protected void bind (String jndiName, Object who, Class classType, Context ctx) throws Exception + { + // Ah ! This service isn't serializable, so we use a helper class + // + NonSerializableFactory.bind (jndiName, who); + javax.naming.Name n = ctx.getNameParser ("").parse (jndiName); + while (n.size () > 1) + { + String ctxName = n.get (0); + try + { + ctx = (Context)ctx.lookup (ctxName); + } + catch (javax.naming.NameNotFoundException e) + { + log.info ("creating Subcontext" + ctxName); + ctx = ctx.createSubcontext (ctxName); + } + n = n.getSuffix (1); + } + + // The helper class NonSerializableFactory uses address type nns, we go on to + // use the helper class to bind the service object in JNDI + // + StringRefAddr addr = new StringRefAddr ("nns", jndiName); + javax.naming.Reference ref = new javax.naming.Reference ( classType.getName (), addr, NonSerializableFactory.class.getName (), null); + ctx.rebind (n.get (0), ref); + } + + /** + * Helper method that returns a vector of dead members from two input vectors: new and old vectors of two views. + * @param oldMembers Vector of old members + * @param newMembers Vector of new members + * @return Vector of members that have died between the two views + */ + protected Vector getDeadMembers (Vector oldMembers, Vector newMembers) + { + Vector dead = new Vector (); + for (int i=0; i<oldMembers.size ();i++) + { + log.info("is node " + oldMembers.elementAt(i).toString() + "dead?"); + if (!newMembers.contains (oldMembers.elementAt (i))) + { + log.info("node " + oldMembers.elementAt(i).toString() + "is dead"); + dead.add (oldMembers.elementAt (i)); + } + else + { + log.info("node " + oldMembers.elementAt(i).toString() + "is NOT dead"); + } + } + + return dead; + } + + /** + * Helper method that returns a vector of new members from two input vectors: new and old vectors of two views. + * @param oldMembers Vector of old members + * @param allMembers Vector of new members + * @return Vector of members that have joined the partition between the two views + */ + protected Vector getNewMembers (Vector oldMembers, Vector allMembers) + { + Vector newMembers = new Vector (); + for (int i=0; i<allMembers.size ();i++) + if (!oldMembers.contains (allMembers.elementAt (i))) + newMembers.add (allMembers.elementAt (i)); + return newMembers; + } + + // Private ------------------------------------------------------- + + // Inner classes ------------------------------------------------- + }
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development
