[jboss-cvs] JBossAS SVN: r58563 - trunk/cluster/src/main/org/jboss/ha/singleton

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Nov 18 06:24:52 EST 2006


Author: bstansberry at jboss.com
Date: 2006-11-18 06:24:51 -0500 (Sat, 18 Nov 2006)
New Revision: 58563

Modified:
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonController.java
   trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonControllerMBean.java
Log:
For MC, allow injection of the target itself, not just it's ObjectName

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonController.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonController.java	2006-11-18 11:24:15 UTC (rev 58562)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonController.java	2006-11-18 11:24:51 UTC (rev 58563)
@@ -21,10 +21,11 @@
  */
 package org.jboss.ha.singleton;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.security.InvalidParameterException;
 
 import javax.management.InstanceNotFoundException;
-import javax.management.JMException;
 import javax.management.MBeanException;
 import javax.management.ObjectName;
 import javax.management.ReflectionException;
@@ -45,6 +46,8 @@
  * @author <a href="mailto:scott.stark at jboss.org">Scott Stark</a>
  * @author <a href="mailto:mr at gedoplan.de">Marcus Redeker</a>
  * @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
+ * @author Brian Stansberry
+ * 
  * @version $Revision$
  */
 public class HASingletonController extends HASingletonSupport
@@ -53,13 +56,15 @@
    // Private Data --------------------------------------------------
    
    private ObjectName mSingletonMBean;
-   private String mSingletonMBeanStartMethod = "startSingleton";
-   private String mSingletonMBeanStopMethod  = "stopSingleton";
-   private String mSingletonMBeanStartMethodArgument;
-   private String mSingletonMBeanStopMethodArgument;
+   private Object mSingleton;
+   private String mSingletonStartMethod = "startSingleton";
+   private String mSingletonStopMethod  = "stopSingleton";
+   private String mSingletonStartMethodArgument;
+   private String mSingletonStopMethodArgument;
 
    private static final Object[] NO_ARGS = new Object[0];
-   private static final String[] NO_TYPES = new String[0];
+   private static final String[] NO_TYPE_NAMES = new String[0];
+   private static final Class[] NO_TYPES = new Class[0];
 
    // Constructors --------------------------------------------------
    
@@ -73,6 +78,16 @@
    
    // Attributes ----------------------------------------------------
    
+   public Object getTarget()
+   {
+      return mSingleton;
+   }
+   
+   public void setTarget(Object target)
+   {
+      this.mSingleton = target;
+   }
+   
    public ObjectName getTargetName()
    {
       return mSingletonMBean;
@@ -85,47 +100,47 @@
 
    public String getTargetStartMethod()
    {
-      return mSingletonMBeanStartMethod;
+      return mSingletonStartMethod;
    }
 
    public void setTargetStartMethod(String targetStartMethod)
       throws InvalidParameterException
    {
       if (targetStartMethod != null)
-         mSingletonMBeanStartMethod = targetStartMethod;
+         mSingletonStartMethod = targetStartMethod;
    }
 
 
    public String getTargetStopMethod()
    {
-      return mSingletonMBeanStopMethod;
+      return mSingletonStopMethod;
    }
 
    public void setTargetStopMethod(String targetStopMethod)
       throws InvalidParameterException
    {
       if (targetStopMethod != null)
-         mSingletonMBeanStopMethod = targetStopMethod;
+         mSingletonStopMethod = targetStopMethod;
    }
 
    public String getTargetStartMethodArgument()
    {
-      return mSingletonMBeanStartMethodArgument ;
+      return mSingletonStartMethodArgument ;
    }
 
    public void setTargetStartMethodArgument(String targetStartMethodArgument)
    {
-      mSingletonMBeanStartMethodArgument = targetStartMethodArgument;
+      mSingletonStartMethodArgument = targetStartMethodArgument;
    }
 
    public String getTargetStopMethodArgument()
    {
-      return mSingletonMBeanStopMethodArgument ;
+      return mSingletonStopMethodArgument ;
    }
 
    public void setTargetStopMethodArgument(String targetStopMethodArgument)
    {
-      mSingletonMBeanStopMethodArgument =  targetStopMethodArgument;
+      mSingletonStopMethodArgument =  targetStopMethodArgument;
    }
   
    // HASingleton implementation ------------------------------------
@@ -141,15 +156,30 @@
 
       try
       {
-         invokeSingletonMBeanMethod(
-            mSingletonMBean,
-            mSingletonMBeanStartMethod,
-            mSingletonMBeanStartMethodArgument 
-            );
+         if (mSingleton != null)
+         {
+            invokeSingletonMethod(
+                  mSingleton,
+                  mSingletonStartMethod,
+                  mSingletonStartMethodArgument 
+                  );
+         }
+         else if (mSingletonMBean != null)
+         {
+            invokeSingletonMBeanMethod(
+               mSingletonMBean,
+               mSingletonStartMethod,
+               mSingletonStartMethodArgument 
+               );
+         }
+         else
+         {
+            log.warn("No singleton configured; cannot start");
+         }
       }
-      catch (JMException jme)
+      catch (Exception e)
       {
-         log.error("Controlled Singleton MBean failed to become master", jme);
+         log.error("Controlled Singleton failed to become master", e);
       }
    }
 
@@ -164,20 +194,73 @@
 
       try
       {
-         invokeSingletonMBeanMethod(
-            mSingletonMBean,
-            mSingletonMBeanStopMethod,
-            mSingletonMBeanStopMethodArgument
-            );
+         if (mSingleton != null)
+         {
+            invokeSingletonMethod(
+                  mSingleton,
+                  mSingletonStopMethod,
+                  mSingletonStopMethodArgument 
+                  );
+         }
+         else if (mSingletonMBean != null)
+         {            
+            invokeSingletonMBeanMethod(
+               mSingletonMBean,
+               mSingletonStopMethod,
+               mSingletonStopMethodArgument
+               );
+         }
+         else
+         {
+            log.warn("No singleton configured; cannot start");
+         }
       }
-      catch (JMException jme)
+      catch (Exception e)
       {
-         log.error("Controlled Singleton MBean failed to resign from master position", jme);
+         log.error("Controlled Singleton failed to resign from master position", e);
       }
    }
 
    // Protected -----------------------------------------------------
    
+   protected Object invokeSingletonMethod(Object target,
+      String operationName, Object param)
+      throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
+   {
+      if (target != null && operationName != null)
+      {
+         Object[] params;
+         Class[]  types;
+         
+         if (param != null) 
+         {
+            params = new Object[] { param };
+            types = new Class[] { param.getClass() };
+            
+            log.debug("Calling operation: " + operationName +
+                  "(" + param + "), on target: '" + target + "'");            
+         }
+         else
+         {
+            params = NO_ARGS;
+            types = NO_TYPES;
+            
+            log.debug("Calling operation: " + operationName + 
+                  "(), on target: '" + target + "'");               
+         }
+         
+         Method method = getTargetMethod(target, operationName, types);
+         
+         return method.invoke(target, params);
+      }
+      else
+      {
+         log.debug("No configured target mbean or operation to call");
+         
+         return null;
+      }
+   }
+   
    protected Object invokeSingletonMBeanMethod(ObjectName target,
       String operationName, Object param)
       throws InstanceNotFoundException, MBeanException, ReflectionException
@@ -198,7 +281,7 @@
          else
          {
             params = NO_ARGS;
-            signature = NO_TYPES;
+            signature = NO_TYPE_NAMES;
             
             log.debug("Calling operation: " + operationName + 
                   "(), on target: '" + target + "'");               
@@ -213,4 +296,30 @@
          return null;
       }
    }
+   
+   public static Method getTargetMethod(Object target, String methodName, Class[] types)
+         throws NoSuchMethodException
+   {
+      Class clazz = target.getClass();
+      NoSuchMethodException nsme = null;
+      while (clazz != null)
+      {
+         try
+         {
+            Method method = clazz.getDeclaredMethod(methodName, types);
+            return method;
+         }
+         catch (NoSuchMethodException e)
+         {
+            // Cache the one from the top level class
+            if (nsme == null)
+            {
+               nsme = e;
+            }
+           // Keep searching
+           clazz = clazz.getSuperclass();
+         }
+      }
+      throw nsme;
+   }
 }

Modified: trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonControllerMBean.java
===================================================================
--- trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonControllerMBean.java	2006-11-18 11:24:15 UTC (rev 58562)
+++ trunk/cluster/src/main/org/jboss/ha/singleton/HASingletonControllerMBean.java	2006-11-18 11:24:51 UTC (rev 58563)
@@ -36,8 +36,36 @@
  */
 public interface HASingletonControllerMBean  extends HASingletonSupportMBean
 {
-   /** The controlled target Singleton MBean */
+   /** 
+    * Sets the controlled target singleton
+    */
+   void setTarget(Object target);
+   
+   /** 
+    * Gets controlled target singleton
+    * 
+    * @return the singleton, or <code>null</code> if this object is
+    *           configured to use a 
+    *           {@link #setTargetName(ObjectName) JMX object name}. 
+    */
+   Object getTarget();
+   
+   /** 
+    * Gets the ObjectName of the controlled target Singleton MBean
+    * 
+    * @return the target object name, or <code>null</code> if this object is
+    *           configured via {@link #setTarget(Object)}.
+    *           
+    * @deprecated use {@link #getTarget()}
+    */
    ObjectName getTargetName();
+   /** 
+    * Sets the ObjectName of the controlled target Singleton MBean
+    * 
+    * @param targetObjectName target of the MBean singleton
+    * 
+    * @deprecated use {@link #setTarget(Object)}
+    */
    void setTargetName(ObjectName targetObjectName);   
    
    /** The target method to call when the Singleton is started */




More information about the jboss-cvs-commits mailing list