use of javax.management.MBeanException in project tomcat by apache.
the class ManagedBean method createMBean.
/**
* Create and return a <code>ModelMBean</code> that has been
* preconfigured with the <code>ModelMBeanInfo</code> information
* for this managed bean, and is associated with the specified
* managed object instance. The returned <code>ModelMBean</code>
* will <strong>NOT</strong> have been registered with our
* <code>MBeanServer</code>.
*
* @param instance Instanced of the managed object, or <code>null</code>
* for no associated instance
* @return the MBean
* @exception InstanceNotFoundException if the managed resource
* object cannot be found
* @exception MBeanException if a problem occurs instantiating the
* <code>ModelMBean</code> instance
* @exception RuntimeOperationsException if a JMX runtime error occurs
*/
public DynamicMBean createMBean(Object instance) throws InstanceNotFoundException, MBeanException, RuntimeOperationsException {
BaseModelMBean mbean = null;
// Load the ModelMBean implementation class
if (getClassName().equals(BASE_MBEAN)) {
// Skip introspection
mbean = new BaseModelMBean();
} else {
Class<?> clazz = null;
Exception ex = null;
try {
clazz = Class.forName(getClassName());
} catch (Exception e) {
}
if (clazz == null) {
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl != null)
clazz = cl.loadClass(getClassName());
} catch (Exception e) {
ex = e;
}
}
if (clazz == null) {
throw new MBeanException(ex, "Cannot load ModelMBean class " + getClassName());
}
try {
// Stupid - this will set the default minfo first....
mbean = (BaseModelMBean) clazz.newInstance();
} catch (RuntimeOperationsException e) {
throw e;
} catch (Exception e) {
throw new MBeanException(e, "Cannot instantiate ModelMBean of class " + getClassName());
}
}
mbean.setManagedBean(this);
// Set the managed resource (if any)
try {
if (instance != null)
mbean.setManagedResource(instance, "ObjectReference");
} catch (InstanceNotFoundException e) {
throw e;
}
return mbean;
}
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 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);
}
}
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);
}
}
Aggregations