use of javax.management.RuntimeMBeanException in project hbase by apache.
the class JSONBean method write.
/**
* @param mBeanServer
* @param qry
* @param attribute
* @param description
* @return Return non-zero if failed to find bean. 0
* @throws IOException
*/
private static int write(final JsonGenerator jg, final MBeanServer mBeanServer, ObjectName qry, String attribute, final boolean description) throws IOException {
LOG.trace("Listing beans for " + qry);
Set<ObjectName> names = null;
names = mBeanServer.queryNames(qry, null);
jg.writeArrayFieldStart("beans");
Iterator<ObjectName> it = names.iterator();
while (it.hasNext()) {
ObjectName oname = it.next();
MBeanInfo minfo;
String code = "";
String descriptionStr = null;
Object attributeinfo = null;
try {
minfo = mBeanServer.getMBeanInfo(oname);
code = minfo.getClassName();
if (description)
descriptionStr = minfo.getDescription();
String prs = "";
try {
if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
prs = "modelerType";
code = (String) mBeanServer.getAttribute(oname, prs);
}
if (attribute != null) {
prs = attribute;
attributeinfo = mBeanServer.getAttribute(oname, prs);
}
} catch (RuntimeMBeanException e) {
// so no need to log them as errors all the time.
if (e.getCause() instanceof UnsupportedOperationException) {
if (LOG.isTraceEnabled()) {
LOG.trace("Getting attribute " + prs + " of " + oname + " threw " + e);
}
} else {
LOG.error("Getting attribute " + prs + " of " + oname + " threw an exception", e);
}
return 0;
} catch (AttributeNotFoundException e) {
// If the modelerType attribute was not found, the class name is used
// instead.
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
} catch (MBeanException e) {
// The code inside the attribute getter threw an exception so log it,
// and fall back on the class name
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
} catch (RuntimeException e) {
// For some reason even with an MBeanException available to them
// Runtime exceptionscan still find their way through, so treat them
// the same as MBeanException
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
} catch (ReflectionException e) {
// This happens when the code inside the JMX bean (setter?? from the
// java docs) threw an exception, so log it and fall back on the
// class name
LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
}
} catch (InstanceNotFoundException e) {
//Ignored for some reason the bean was not found so don't output it
continue;
} catch (IntrospectionException e) {
// This is an internal error, something odd happened with reflection so
// log it and don't output the bean.
LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
continue;
} catch (ReflectionException e) {
// This happens when the code inside the JMX bean threw an exception, so
// log it and don't output the bean.
LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
continue;
}
jg.writeStartObject();
jg.writeStringField("name", oname.toString());
if (description && descriptionStr != null && descriptionStr.length() > 0) {
jg.writeStringField("description", descriptionStr);
}
jg.writeStringField("modelerType", code);
if (attribute != null && attributeinfo == null) {
jg.writeStringField("result", "ERROR");
jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
jg.writeEndObject();
jg.writeEndArray();
jg.close();
return -1;
}
if (attribute != null) {
writeAttribute(jg, attribute, descriptionStr, attributeinfo);
} else {
MBeanAttributeInfo[] attrs = minfo.getAttributes();
for (int i = 0; i < attrs.length; i++) {
writeAttribute(jg, mBeanServer, oname, description, attrs[i]);
}
}
jg.writeEndObject();
}
jg.writeEndArray();
return 0;
}
use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.
the class MBeanExceptionTest method main.
public static void main(String[] args) throws Exception {
System.out.println("Test that if an MBean throws RuntimeException " + "it is wrapped in RuntimeMBeanException,");
System.out.println("and if a Standard MBean throws Exception " + "it is wrapped in MBeanException");
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
Object standard = new Except();
ObjectName standardName = new ObjectName(":name=Standard MBean");
Object standardMBean = new StandardMBean(new Except(), ExceptMBean.class);
ObjectName standardMBeanName = new ObjectName(":name=Instance of StandardMBean");
Object dynamic = new DynamicExcept();
ObjectName dynamicName = new ObjectName(":name=Dynamic MBean");
mbs.registerMBean(standard, standardName);
mbs.registerMBean(standardMBean, standardMBeanName);
mbs.registerMBean(dynamic, dynamicName);
int failures = 0;
failures += test(mbs, standardName, true);
failures += test(mbs, standardMBeanName, true);
failures += test(mbs, dynamicName, false);
final boolean[] booleans = { false, true };
for (boolean runtimeX : booleans) {
Class<? extends Exception> excC = runtimeX ? RuntimeMBeanException.class : MBeanException.class;
String excS = runtimeX ? "a RuntimeMBeanException" : "an MBeanException";
String mbsS = "a plain MBeanServer";
System.out.println("Test that, with " + mbsS + ", " + excS + " is wrapped " + "in " + excS);
// is wrapped in an MBeanException".
try {
mbs.createMBean(Except.class.getName(), new ObjectName(":name=Oops"), new Object[] { runtimeX }, new String[] { boolean.class.getName() });
System.out.println("FAIL: createMBean succeeded but should not have");
failures++;
} catch (Exception e) {
if (!excC.isInstance(e)) {
System.out.println("FAIL: expected " + excC.getName() + " from " + "createMBean, got " + e);
failures++;
} else {
Throwable cause = e.getCause();
if (!excC.isInstance(cause)) {
System.out.println("FAIL: expected " + excC.getName() + " as cause of " + excC.getName() + ", got " + e);
failures++;
} else
System.out.println("...ok");
}
}
}
if (failures == 0)
System.out.println("Test passed");
else {
System.out.println("TEST FAILED: " + failures + " failure(s)");
System.exit(1);
}
}
use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.
the class MBeanExceptionTest method test.
private static int test(MBeanServer mbs, ObjectName name, boolean testChecked) throws Exception {
System.out.println("--------" + name + "--------");
int failures = 0;
final String[] ops = { "getAttribute", "setAttribute", "invoke" };
final int GET = 0, SET = 1, INVOKE = 2;
final String[] targets = { "UncheckedException", "CheckedException" };
final int UNCHECKED = 0, CHECKED = 1;
for (int i = 0; i < ops.length; i++) {
for (int j = 0; j < targets.length; j++) {
if (j == CHECKED && !testChecked)
continue;
String target = targets[j];
String what = ops[i] + "/" + target;
System.out.println(what);
try {
switch(i) {
case GET:
mbs.getAttribute(name, target);
break;
case SET:
mbs.setAttribute(name, new Attribute(target, "x"));
break;
case INVOKE:
mbs.invoke(name, target, null, null);
break;
default:
throw new AssertionError();
}
System.out.println("failure: " + what + " returned!");
failures++;
} catch (RuntimeMBeanException e) {
if (j == CHECKED) {
System.out.println("failure: RuntimeMBeanException " + "when checked expected: " + e);
failures++;
} else {
Throwable cause = e.getCause();
if (cause == theUncheckedException)
System.out.println("ok: " + what);
else {
System.out.println("failure: " + what + " wrapped " + cause);
failures++;
}
}
} catch (MBeanException e) {
if (j == UNCHECKED) {
System.out.println("failure: checked exception " + "when unchecked expected: " + e);
failures++;
} else {
Throwable cause = e.getCause();
if (cause == theCheckedException)
System.out.println("ok: " + what);
else {
System.out.println("failure: " + what + " wrapped " + cause);
failures++;
}
}
} catch (Throwable t) {
System.out.println("failure: " + what + " threw: " + t);
while ((t = t.getCause()) != null) System.out.println(" ... " + t);
failures++;
}
}
}
return failures;
}
use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.
the class MBeanInstantiator method instantiate.
/**
* Instantiates an object given its class, the parameters and
* signature of its constructor The call returns a reference to
* the newly created object.
*/
public Object instantiate(Class<?> theClass, Object[] params, String[] signature, ClassLoader loader) throws ReflectionException, MBeanException {
checkMBeanPermission(theClass, null, null, "instantiate");
// Instantiate the new object
// ------------------------------
// ------------------------------
final Class<?>[] tab;
Object moi;
try {
// Build the signature of the method
//
ClassLoader aLoader = theClass.getClassLoader();
// Build the signature of the method
//
tab = ((signature == null) ? null : findSignatureClasses(signature, aLoader));
}// Exception IllegalArgumentException raised in Jdk1.1.8
catch (IllegalArgumentException e) {
throw new ReflectionException(e, "The constructor parameter classes could not be loaded");
}
// Query the metadata service to get the right constructor
Constructor<?> cons = findConstructor(theClass, tab);
if (cons == null) {
throw new ReflectionException(new NoSuchMethodException("No such constructor"));
}
try {
ReflectUtil.checkPackageAccess(theClass);
ensureClassAccess(theClass);
moi = cons.newInstance(params);
} catch (NoSuchMethodError error) {
throw new ReflectionException(new NoSuchMethodException("No such constructor found"), "No such constructor");
} catch (InstantiationException e) {
throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's constructor");
} catch (IllegalAccessException e) {
throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's constructor");
} catch (InvocationTargetException e) {
// Wrap the exception.
Throwable th = e.getTargetException();
if (th instanceof RuntimeException) {
throw new RuntimeMBeanException((RuntimeException) th, "RuntimeException thrown in the MBean's constructor");
} else if (th instanceof Error) {
throw new RuntimeErrorException((Error) th, "Error thrown in the MBean's constructor");
} else {
throw new MBeanException((Exception) th, "Exception thrown in the MBean's constructor");
}
}
return moi;
}
use of javax.management.RuntimeMBeanException in project jdk8u_jdk by JetBrains.
the class MBeanInstantiator method instantiate.
/**
* Instantiates an object given its class, using its empty constructor.
* The call returns a reference to the newly created object.
*/
public Object instantiate(Class<?> theClass) throws ReflectionException, MBeanException {
checkMBeanPermission(theClass, null, null, "instantiate");
Object moi;
// ------------------------------
// ------------------------------
Constructor<?> cons = findConstructor(theClass, null);
if (cons == null) {
throw new ReflectionException(new NoSuchMethodException("No such constructor"));
}
// Instantiate the new object
try {
ReflectUtil.checkPackageAccess(theClass);
ensureClassAccess(theClass);
moi = cons.newInstance();
} catch (InvocationTargetException e) {
// Wrap the exception.
Throwable t = e.getTargetException();
if (t instanceof RuntimeException) {
throw new RuntimeMBeanException((RuntimeException) t, "RuntimeException thrown in the MBean's empty constructor");
} else if (t instanceof Error) {
throw new RuntimeErrorException((Error) t, "Error thrown in the MBean's empty constructor");
} else {
throw new MBeanException((Exception) t, "Exception thrown in the MBean's empty constructor");
}
} catch (NoSuchMethodError error) {
throw new ReflectionException(new NoSuchMethodException("No constructor"), "No such constructor");
} catch (InstantiationException e) {
throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor");
} catch (IllegalAccessException e) {
throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor");
} catch (IllegalArgumentException e) {
throw new ReflectionException(e, "Exception thrown trying to invoke the MBean's empty constructor");
}
return moi;
}
Aggregations