Author: chris.laprun(a)jboss.com
Date: 2009-05-06 10:46:25 -0400 (Wed, 06 May 2009)
New Revision: 13310
Modified:
branches/JBoss_Portal_Branch_2_7/jems/src/main/org/jboss/portal/jems/as/JMX.java
Log:
- Improved getMBeanProxy methods so that they return a cast object.
- Added isRegistered method to check if an MBean is known by the server without having to
retrieve a proxy to it.
Modified:
branches/JBoss_Portal_Branch_2_7/jems/src/main/org/jboss/portal/jems/as/JMX.java
===================================================================
---
branches/JBoss_Portal_Branch_2_7/jems/src/main/org/jboss/portal/jems/as/JMX.java 2009-05-06
14:37:18 UTC (rev 13309)
+++
branches/JBoss_Portal_Branch_2_7/jems/src/main/org/jboss/portal/jems/as/JMX.java 2009-05-06
14:46:25 UTC (rev 13310)
@@ -1,6 +1,6 @@
/******************************************************************************
* JBoss, a division of Red Hat *
- * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * Copyright 2009, Red Hat Middleware, LLC, and individual *
* contributors as indicated by the @authors tag. See the *
* copyright.txt in the distribution for a full listing of *
* individual contributors. *
@@ -24,7 +24,6 @@
import org.apache.log4j.Logger;
import org.jboss.mx.util.MBeanProxy;
-import org.jboss.mx.util.MBeanProxyCreationException;
import org.jboss.mx.util.MBeanServerLocator;
import org.jboss.mx.util.ObjectNameConverter;
@@ -108,21 +107,21 @@
/**
* Retrieves the MBeanProxy associated with the given class and name from the
specified MBeanServer.
*
- * @param clazz the interface implemented by the MBean which is to be retrieved
- * @param name the MBean's ObjectName
- * @param server the MBeanServer from which to retrieve the MBeanProxy
+ * @param expectedClass the expected class of the MBean's proxy
+ * @param name the MBean's ObjectName
+ * @param server the MBeanServer from which to retrieve the MBeanProxy
* @return a MBeanProxy for the specified MBean if it exists
* @throws RuntimeException if the MBean couldn't be retrieved
*/
- public static Object getMBeanProxy(Class clazz, ObjectName name, MBeanServer server)
+ public static <T> T getMBeanProxy(Class<T> expectedClass, ObjectName name,
MBeanServer server)
{
try
{
- return MBeanProxy.get(clazz, name, server);
+ return expectedClass.cast(MBeanProxy.get(expectedClass, name, server));
}
- catch (MBeanProxyCreationException e)
+ catch (Exception e)
{
- String message = "Couldn't retrieve '" +
name.getCanonicalName() + "' MBean with class " + clazz.getName();
+ String message = "Couldn't retrieve '" +
name.getCanonicalName() + "' MBean with class " + expectedClass.getName();
log.error(message, e);
throw new RuntimeException(message, e);
}
@@ -132,16 +131,23 @@
* Retrieves the MBeanProxy associated with the given class and name from the JBoss
microkernel as returned by
* <code>MBeanServerLocator.locateJBoss()</code>.
*
- * @param clazz the interface implemented by the MBean which is to be retrieved
- * @param name a String representation of the MBean's ObjectName
+ * @param expectedClass the expected class of the MBean's proxy
+ * @param name a String representation of the MBean's ObjectName
* @return a MBeanProxy for the specified MBean if it exists
* @throws IllegalArgumentException if the given name is not a valid ObjectName
* @throws RuntimeException if the MBean couldn't be retrieved
* @see #getMBeanProxy(Class, javax.management.ObjectName,
javax.management.MBeanServer)
* @since 2.4
*/
- public static Object getMBeanProxy(Class clazz, String name)
+ public static <T> T getMBeanProxy(Class<T> expectedClass, String name)
{
+ ObjectName objecName = createObjectName(name);
+ MBeanServer server = MBeanServerLocator.locateJBoss();
+ return getMBeanProxy(expectedClass, objecName, server);
+ }
+
+ private static ObjectName createObjectName(String name)
+ {
ObjectName objecName;
try
{
@@ -151,8 +157,20 @@
{
throw new IllegalArgumentException("'" + name + "' is not
a valid ObjectName");
}
+ return objecName;
+ }
+
+ /**
+ * Checks whether the JBoss MBeanServer knows about an MBean with the specified name
without having to retrieve a
+ * proxy to it.
+ *
+ * @param name a String representation of the MBean object name which registration
status we're interested in
+ * @return <code>true</code> if the server knows of an MBean with the
specified name, <code>false</code> otherwise.
+ */
+ public static boolean isRegistered(String name)
+ {
MBeanServer server = MBeanServerLocator.locateJBoss();
- return getMBeanProxy(clazz, objecName, server);
+ return server.isRegistered(createObjectName(name));
}
public static boolean addNotificationListener(
Show replies by date