Permalink
Browse files

Remove various new RuntimeException() with a JmxException that can

be asked for the reason of the exception to determine action depending
on the cause.
  • Loading branch information...
1 parent d1cb4de commit 8955fa407a7e48c514c4a11944c939accb6715fc Henning Schmiedehausen committed with Jan 29, 2011
View
@@ -6,3 +6,4 @@
.classpath
.project
.settings
+/bin
@@ -15,9 +15,6 @@
*/
package org.weakref.jmx;
-import javax.management.Descriptor;
-import javax.management.DescriptorKey;
-import javax.management.ImmutableDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -27,6 +24,12 @@
import java.util.Set;
import java.util.TreeMap;
+import javax.management.Descriptor;
+import javax.management.DescriptorKey;
+import javax.management.ImmutableDescriptor;
+
+import org.weakref.jmx.JmxException.JmxCause;
+
final class AnnotationUtils
{
private AnnotationUtils()
@@ -62,13 +65,10 @@ public static Descriptor buildDescriptor(Annotation... annotations)
Throwable cause = e;
if (e instanceof InvocationTargetException) {
cause = e.getCause();
-
}
- throw new RuntimeException(String.format(
+ throw new JmxException(JmxCause.CONFIG, cause,
"Unexpected exception getting value from @DescriptorKey field type: annotationClass=%s, field=%s",
- annotation.annotationType().getName(),
- field.getName()),
- cause);
+ annotation.annotationType().getName(), field.getName());
}
// skip null values, since that is the default
@@ -108,10 +108,10 @@ else if (componentType.isEnum()) {
}
}
else if (value instanceof Annotation) {
- throw new IllegalArgumentException(String.format(
+ throw new JmxException(JmxCause.CONFIG,
"@DescriptorKey can not be applied to an annotation field type: annotationClass=%s, field=%s",
annotation.annotationType().getName(),
- field.getName()));
+ field.getName());
}
fields.put(name, value);
@@ -0,0 +1,41 @@
+package org.weakref.jmx;
+
+public class JmxException extends RuntimeException
+{
+ private static final long serialVersionUID = 1L;
+
+ public enum JmxCause
+ {
+ CONFIG,
+ MALFORMED_OBJECT_NAME,
+ INSTANCE_ALREADY_EXISTS,
+ INSTANCE_NOT_FOUND,
+ MBEAN_REGISTRATION,
+ NOT_COMPLIANT_MBEAN;
+ }
+
+ private final JmxCause jmxCause;
+
+ JmxException(final JmxCause jmxCause, final String message, final Object ... args)
+ {
+ super(String.format(message, args));
+ this.jmxCause = jmxCause;
+ }
+
+ JmxException(final JmxCause jmxCause, final Throwable throwable)
+ {
+ super(throwable);
+ this.jmxCause = jmxCause;
+ }
+
+ JmxException(final JmxCause jmxCause, final Throwable cause, final String message, final Object ... args)
+ {
+ super(String.format(message, args), cause);
+ this.jmxCause = jmxCause;
+ }
+
+ public JmxCause getJmxCause()
+ {
+ return jmxCause;
+ }
+}
@@ -15,8 +15,16 @@
*/
package org.weakref.jmx;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
+
+import org.weakref.jmx.JmxException.JmxCause;
+
import java.lang.management.ManagementFactory;
public class MBeanExporter
@@ -30,35 +38,47 @@ public MBeanExporter(MBeanServer server)
public void export(String name, Object object)
{
+ ObjectName objectName;
try {
- ObjectName objectName = new ObjectName(name);
-
+ objectName = new ObjectName(name);
MBeanBuilder builder = new MBeanBuilder(object);
MBean mbean = builder.build();
server.registerMBean(mbean, objectName);
}
- catch (RuntimeException e) {
- throw e;
+ catch (MalformedObjectNameException mone) {
+ throw new JmxException(JmxCause.MALFORMED_OBJECT_NAME, mone.getMessage());
+ }
+ catch (InstanceAlreadyExistsException iae) {
+ throw new JmxException(JmxCause.INSTANCE_ALREADY_EXISTS, iae.getMessage());
}
- catch (Exception e) {
- throw new RuntimeException(e);
+ catch (MBeanRegistrationException mre) {
+ throw new JmxException(JmxCause.MBEAN_REGISTRATION, mre.getMessage(), mre.getCause());
+ }
+ catch (NotCompliantMBeanException ncme) {
+ throw new JmxException(JmxCause.NOT_COMPLIANT_MBEAN, ncme.getMessage());
}
}
public void unexport(String name)
{
- try {
- ObjectName objectName = new ObjectName(name);
-
- server.unregisterMBean(objectName);
- }
- catch (RuntimeException e) {
- throw e;
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
+ ObjectName objectName;
+
+ try {
+ objectName = new ObjectName(name);
+
+ server.unregisterMBean(objectName);
+ }
+ catch (MalformedObjectNameException mone) {
+ throw new JmxException(JmxCause.MALFORMED_OBJECT_NAME, mone.getMessage());
+ }
+ catch (MBeanRegistrationException mre) {
+ throw new JmxException(JmxCause.MBEAN_REGISTRATION, mre.getMessage(), mre.getCause());
+ }
+ catch (InstanceNotFoundException infe) {
+ throw new JmxException(JmxCause.INSTANCE_NOT_FOUND, infe.getMessage());
+ }
+
}
/**
@@ -0,0 +1,87 @@
+package org.weakref.jmx;
+
+import java.lang.management.ManagementFactory;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+import org.weakref.jmx.JmxException.JmxCause;
+
+public class TestExports
+{
+ private MBeanServer server = null;
+ private MBeanExporter exporter = null;
+
+ private ObjectName objectName = null;
+ private String name = null;
+
+ @BeforeMethod
+ public void setUp()
+ {
+ Assert.assertNull(name);
+ objectName = Util.getUniqueObjectName();
+ name = objectName.getCanonicalName();
+ server = ManagementFactory.getPlatformMBeanServer();
+ exporter = new MBeanExporter(server);
+
+ Assert.assertNotNull(server);
+ Assert.assertNotNull(exporter);
+ Assert.assertNotNull(objectName);
+ Assert.assertNotNull(name);
+ }
+
+ @AfterMethod
+ public void tearDown()
+ {
+ Assert.assertNotNull(name);
+ Assert.assertNotNull(server);
+ Assert.assertNotNull(exporter);
+ Assert.assertNotNull(objectName);
+
+ exporter.unexport(name);
+
+ name = null;
+ server = null;
+ exporter = null;
+ objectName = null;
+ }
+
+ @Test
+ public void testExportOk() throws Exception
+ {
+ exporter.export(name, new TestBean());
+
+ Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
+ }
+
+ @Test(expectedExceptions = JmxException.class)
+ public void testExportDouble() throws Throwable
+ {
+ exporter.export(name, new TestBean());
+
+ Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
+
+ try {
+ exporter.export(name, new TestBean());
+ }
+ catch (Throwable t) {
+ Assert.assertTrue(t instanceof JmxException);
+ Assert.assertEquals(((JmxException) t).getJmxCause(), JmxCause.INSTANCE_ALREADY_EXISTS);
+ throw t;
+ }
+ }
+
+ public static class TestBean
+ {
+ @Managed
+ public String getHello()
+ {
+ return "Hello!";
+ }
+ }
+}
@@ -7,44 +7,84 @@
import javax.management.ObjectName;
import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
+import org.weakref.jmx.JmxException.JmxCause;
public class TestUnexporter
{
- @Test
- public void testUnexport()
- throws Exception
+ private MBeanServer server = null;
+ private MBeanExporter exporter = null;
+
+ private ObjectName objectName = null;
+ private String name = null;
+
+ @BeforeMethod
+ public void setUp()
{
- ObjectName objectName = Util.getUniqueObjectName();
- String name = objectName.getCanonicalName();
- MBeanServer server = ManagementFactory.getPlatformMBeanServer();
- MBeanExporter exporter = new MBeanExporter(server);
+ Assert.assertNull(name);
+ objectName = Util.getUniqueObjectName();
+ name = objectName.getCanonicalName();
+ server = ManagementFactory.getPlatformMBeanServer();
+ exporter = new MBeanExporter(server);
+
exporter.export(name, new TestBean());
- try {
- Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
- }
- finally {
- exporter.unexport(name);
- }
- boolean caught = false;
- try {
- server.getAttribute(objectName, "Hello");
- Assert.fail();
- }
- catch (InstanceNotFoundException e) {
- caught = true;
+
+ Assert.assertNotNull(server);
+ Assert.assertNotNull(exporter);
+ Assert.assertNotNull(objectName);
+ Assert.assertNotNull(name);
+ }
+
+ @AfterMethod
+ public void tearDown()
+ {
+ Assert.assertNotNull(server);
+ Assert.assertNotNull(exporter);
+ Assert.assertNotNull(objectName);
+
+ if (name != null) {
+ try {
+ exporter.unexport(name);
+ }
+ catch (JmxException je) {
+ Assert.fail("Could not unexport mbean", je);
+ }
+ name = null;
}
- Assert.assertTrue(caught);
- caught = false;
+
+ server = null;
+ exporter = null;
+ objectName = null;
+ }
+
+ @Test(expectedExceptions = InstanceNotFoundException.class)
+ public void testUnexportOk() throws Exception
+ {
+ Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
+ exporter.unexport(name);
+ name = null;
+ server.getAttribute(objectName, "Hello");
+ }
+
+ @Test(expectedExceptions = JmxException.class)
+ public void testUnexportDouble() throws Throwable
+ {
+ Assert.assertEquals("Hello!", server.getAttribute(objectName, "Hello"));
+ exporter.unexport(name);
+
try {
exporter.unexport(name);
- Assert.fail();
}
- catch (RuntimeException e) {
- Assert.assertTrue(e.getCause() instanceof InstanceNotFoundException);
- caught = true;
+ catch (Throwable t) {
+ Assert.assertTrue(t instanceof JmxException);
+ Assert.assertEquals(((JmxException) t).getJmxCause(), JmxCause.INSTANCE_NOT_FOUND);
+ throw t;
+ }
+ finally {
+ name = null;
}
- Assert.assertTrue(caught);
}
public static class TestBean

0 comments on commit 8955fa4

Please sign in to comment.