use of javax.management.MBeanException in project jetty.project by eclipse.
the class ObjectMBean method getAttribute.
/* ------------------------------------------------------------ */
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
Method getter = (Method) _getters.get(name);
if (getter == null) {
throw new AttributeNotFoundException(name);
}
try {
Object o = _managed;
if (getter.getDeclaringClass().isInstance(this))
// mbean method
o = this;
// get the attribute
Object r = getter.invoke(o, (java.lang.Object[]) null);
// convert to ObjectName if the type has the @ManagedObject annotation
if (r != null) {
if (r.getClass().isArray()) {
if (r.getClass().getComponentType().isAnnotationPresent(ManagedObject.class)) {
ObjectName[] on = new ObjectName[Array.getLength(r)];
for (int i = 0; i < on.length; i++) {
on[i] = _mbeanContainer.findMBean(Array.get(r, i));
}
r = on;
}
} else if (r instanceof Collection<?>) {
@SuppressWarnings("unchecked") Collection<Object> c = (Collection<Object>) r;
if (!c.isEmpty() && c.iterator().next().getClass().isAnnotationPresent(ManagedObject.class)) {
// check the first thing out
ObjectName[] on = new ObjectName[c.size()];
int i = 0;
for (Object obj : c) {
on[i++] = _mbeanContainer.findMBean(obj);
}
r = on;
}
} else {
Class<?> clazz = r.getClass();
while (clazz != null) {
if (clazz.isAnnotationPresent(ManagedObject.class)) {
ObjectName mbean = _mbeanContainer.findMBean(r);
if (mbean != null) {
return mbean;
} else {
return null;
}
}
clazz = clazz.getSuperclass();
}
}
}
return r;
} catch (IllegalAccessException e) {
LOG.warn(Log.EXCEPTION, e);
throw new AttributeNotFoundException(e.toString());
} catch (InvocationTargetException e) {
LOG.warn(Log.EXCEPTION, e);
throw new ReflectionException(new Exception(e.getCause()));
}
}
use of javax.management.MBeanException in project jetty.project by eclipse.
the class ObjectMBean method invoke.
/* ------------------------------------------------------------ */
public Object invoke(String name, Object[] params, String[] signature) throws MBeanException, ReflectionException {
if (LOG.isDebugEnabled())
LOG.debug("ObjectMBean:invoke " + name);
String methodKey = name + "(";
if (signature != null)
for (int i = 0; i < signature.length; i++) methodKey += (i > 0 ? "," : "") + signature[i];
methodKey += ")";
ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(_loader);
Method method = (Method) _methods.get(methodKey);
if (method == null)
throw new NoSuchMethodException(methodKey);
Object o = _managed;
if (method.getDeclaringClass().isInstance(this)) {
o = this;
}
return method.invoke(o, params);
} catch (NoSuchMethodException e) {
LOG.warn(Log.EXCEPTION, e);
throw new ReflectionException(e);
} catch (IllegalAccessException e) {
LOG.warn(Log.EXCEPTION, e);
throw new MBeanException(e);
} catch (InvocationTargetException e) {
LOG.warn(Log.EXCEPTION, e);
throw new ReflectionException(new Exception(e.getCause()));
} finally {
Thread.currentThread().setContextClassLoader(old_loader);
}
}
use of javax.management.MBeanException 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.MBeanException 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.MBeanException in project jdk8u_jdk by JetBrains.
the class MXBeanExceptionHandlingTest method run.
public void run(Map<String, Object> args) {
System.out.println("MXBeanExceptionHandlingTest::run: Start");
int errorCount = 0;
try {
parseArgs(args);
notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
// JMX MbeanServer used inside single VM as if remote.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// ----
System.out.println("Add me as notification listener");
mbsc.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, null, null);
System.out.println("---- OK\n");
// ----
System.out.println("Create and register the MBean");
ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi");
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
System.out.println("---- OK\n");
// ----
System.out.println("Call method throwException on our MXBean");
try {
mbsc.invoke(objName, "throwException", null, null);
errorCount++;
System.out.println("(ERROR) Did not get awaited MBeanException");
} catch (MBeanException mbe) {
System.out.println("(OK) Got awaited MBeanException");
Throwable cause = mbe.getCause();
if (cause instanceof java.lang.Exception) {
System.out.println("(OK) Cause is of the right class");
String mess = cause.getMessage();
if (mess.equals(Basic.EXCEPTION_MESSAGE)) {
System.out.println("(OK) Cause message is fine");
} else {
errorCount++;
System.out.println("(ERROR) Cause has message " + cause.getMessage() + " as we expect " + Basic.EXCEPTION_MESSAGE);
}
} else {
errorCount++;
System.out.println("(ERROR) Cause is of class " + cause.getClass().getName() + " as we expect java.lang.Exception");
}
} catch (Exception e) {
errorCount++;
System.out.println("(ERROR) Did not get awaited MBeanException but " + e);
Utils.printThrowable(e, true);
}
System.out.println("---- DONE\n");
// ----
System.out.println("Call method throwError on our MXBean");
try {
mbsc.invoke(objName, "throwError", null, null);
errorCount++;
System.out.println("(ERROR) Did not get awaited RuntimeErrorException");
} catch (RuntimeErrorException ree) {
System.out.println("(OK) Got awaited RuntimeErrorException");
Throwable cause = ree.getCause();
if (cause instanceof java.lang.InternalError) {
System.out.println("(OK) Cause is of the right class");
String mess = cause.getMessage();
if (mess.equals(Basic.EXCEPTION_MESSAGE)) {
System.out.println("(OK) Cause message is fine");
} else {
errorCount++;
System.out.println("(ERROR) Cause has message " + cause.getMessage() + " as we expect " + Basic.EXCEPTION_MESSAGE);
}
} else {
errorCount++;
System.out.println("(ERROR) Cause is of class " + cause.getClass().getName() + " as we expect java.lang.InternalError");
}
} catch (Exception e) {
errorCount++;
System.out.println("(ERROR) Did not get awaited RuntimeErrorException but " + e);
Utils.printThrowable(e, true);
}
System.out.println("---- DONE\n");
// ----
System.out.println("Unregister the MBean");
mbsc.unregisterMBean(objName);
System.out.println("---- OK\n");
Thread.sleep(timeForNotificationInSeconds * 1000);
int numOfReceivedNotif = notifList.size();
if (numOfReceivedNotif == numOfNotifications) {
System.out.println("(OK) We received " + numOfNotifications + " Notifications");
} else {
errorCount++;
System.out.println("(ERROR) We received " + numOfReceivedNotif + " Notifications in place of " + numOfNotifications);
}
} catch (Exception e) {
Utils.printThrowable(e, true);
throw new RuntimeException(e);
}
if (errorCount == 0) {
System.out.println("MXBeanExceptionHandlingTest::run: Done without any error");
} else {
System.out.println("MXBeanExceptionHandlingTest::run: Done with " + errorCount + " error(s)");
throw new RuntimeException("errorCount = " + errorCount);
}
}
Aggregations