private static void throwMBeanRegistrationException(Throwable t, String where) throws MBeanRegistrationException { if (t instanceof RuntimeException) { throw new RuntimeMBeanException((RuntimeException)t, "RuntimeException thrown " + where); } else if (t instanceof Error) { throw new RuntimeErrorException((Error)t, "Error thrown " + where); } else if (t instanceof MBeanRegistrationException) { throw (MBeanRegistrationException)t; } else if (t instanceof Exception) { throw new MBeanRegistrationException((Exception)t, "Exception thrown " + where); } else // neither Error nor Exception?? throw new RuntimeException(t); }
private static void postRegister( ObjectName logicalName, DynamicMBean mbean, boolean registrationDone, boolean registerFailed) { if (registerFailed && mbean instanceof DynamicMBean2) ((DynamicMBean2) mbean).registerFailed(); try { if (mbean instanceof MBeanRegistration) ((MBeanRegistration) mbean).postRegister(registrationDone); } catch (RuntimeException e) { MBEANSERVER_LOGGER.fine("While registering MBean ["+logicalName+ "]: " + "Exception thrown by postRegister: " + "rethrowing <"+e+">, but keeping the MBean registered"); throw new RuntimeMBeanException(e, "RuntimeException thrown in postRegister method: "+ "rethrowing <"+e+">, but keeping the MBean registered"); } catch (Error er) { MBEANSERVER_LOGGER.fine("While registering MBean ["+logicalName+ "]: " + "Error thrown by postRegister: " + "rethrowing <"+er+">, but keeping the MBean registered"); throw new RuntimeErrorException(er, "Error thrown in postRegister method: "+ "rethrowing <"+er+">, but keeping the MBean registered"); } }
private static void postDeregisterInvoke(ObjectName mbean, MBeanRegistration moi) { try { moi.postDeregister(); } catch (RuntimeException e) { MBEANSERVER_LOGGER.fine("While unregistering MBean ["+mbean+ "]: " + "Exception thrown by postDeregister: " + "rethrowing <"+e+">, although the MBean is succesfully " + "unregistered"); throw new RuntimeMBeanException(e, "RuntimeException thrown in postDeregister method: "+ "rethrowing <"+e+ ">, although the MBean is sucessfully unregistered"); } catch (Error er) { MBEANSERVER_LOGGER.fine("While unregistering MBean ["+mbean+ "]: " + "Error thrown by postDeregister: " + "rethrowing <"+er+">, although the MBean is succesfully " + "unregistered"); throw new RuntimeErrorException(er, "Error thrown in postDeregister method: "+ "rethrowing <"+er+ ">, although the MBean is sucessfully unregistered"); } }
private static void postRegister( ObjectName logicalName, DynamicMBean mbean, boolean registrationDone, boolean registerFailed) { if (registerFailed && mbean instanceof DynamicMBean2) ((DynamicMBean2) mbean).registerFailed(); try { if (mbean instanceof MBeanRegistration) ((MBeanRegistration) mbean).postRegister(registrationDone); } catch (RuntimeException e) { MBEANSERVER_LOGGER.log(Level.DEBUG, "While registering MBean ["+logicalName+ "]: " + "Exception thrown by postRegister: " + "rethrowing <"+e+">, but keeping the MBean registered"); throw new RuntimeMBeanException(e, "RuntimeException thrown in postRegister method: "+ "rethrowing <"+e+">, but keeping the MBean registered"); } catch (Error er) { MBEANSERVER_LOGGER.log(Level.DEBUG, "While registering MBean ["+logicalName+ "]: " + "Error thrown by postRegister: " + "rethrowing <"+er+">, but keeping the MBean registered"); throw new RuntimeErrorException(er, "Error thrown in postRegister method: "+ "rethrowing <"+er+">, but keeping the MBean registered"); } }
private static void postDeregisterInvoke(ObjectName mbean, MBeanRegistration moi) { try { moi.postDeregister(); } catch (RuntimeException e) { MBEANSERVER_LOGGER.log(Level.DEBUG, "While unregistering MBean ["+mbean+ "]: " + "Exception thrown by postDeregister: " + "rethrowing <"+e+">, although the MBean is succesfully " + "unregistered"); throw new RuntimeMBeanException(e, "RuntimeException thrown in postDeregister method: "+ "rethrowing <"+e+ ">, although the MBean is sucessfully unregistered"); } catch (Error er) { MBEANSERVER_LOGGER.log(Level.DEBUG, "While unregistering MBean ["+mbean+ "]: " + "Error thrown by postDeregister: " + "rethrowing <"+er+">, although the MBean is succesfully " + "unregistered"); throw new RuntimeErrorException(er, "Error thrown in postDeregister method: "+ "rethrowing <"+er+ ">, although the MBean is sucessfully unregistered"); } }
/** * Expand inner exception wrapped by JMX. * * @param innerObject * jmx proxy which will be wrapped and returned */ protected <T> T rethrowCause(final T innerObject) { @SuppressWarnings("unchecked") final T proxy = (T) Proxy.newProxyInstance(innerObject.getClass().getClassLoader(), innerObject.getClass().getInterfaces(), (proxy1, method, args) -> { try { return method.invoke(innerObject, args); } catch (final InvocationTargetException e) { try { throw e.getTargetException(); } catch (final RuntimeMBeanException e2) { throw e2.getTargetException(); } } }); return proxy; }
@Test @BMRules( rules = {@BMRule( name = "checking ActiveMQServerControl methods", targetClass = "org.apache.activemq.artemis.core.postoffice.impl.PostOfficeImpl", targetMethod = "route(org.apache.activemq.artemis.api.core.Message, boolean)", targetLocation = "ENTRY", action = "throw new org.apache.activemq.artemis.api.core.ActiveMQException(\"gotcha\")")}) public void testAddressControl() throws Exception { server.getActiveMQServerControl().createAddress("test.address", "ANYCAST"); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); System.out.println("server is " + mbs); ObjectName objectName = new ObjectName("org.apache.activemq.artemis:broker=\"localhost\",component=addresses,address=\"test.address\""); Object[] params = new Object[] {new HashMap(), 3, "aGVsbG8=", true, null, null}; String[] signature = new String[] {"java.util.Map", "int", "java.lang.String", "boolean", "java.lang.String", "java.lang.String"}; try { mbs.invoke(objectName, "sendMessage", params, signature); fail("test should have gotten an exception!"); } catch (RuntimeMBeanException ex) { assertTrue(ex.getCause() instanceof IllegalStateException); IllegalStateException e = (IllegalStateException) ex.getCause(); assertEquals("gotcha", e.getMessage()); } }