@Test public void test_invoke() { DiagnosticServiceDescriptor<MyService> descriptor = new DiagnosticServiceDescriptor<>(MyService.class, new MyService() {}, noop, mbean); { DiagnosticResponse<?> response = descriptor.invoke("bar").get(); assertThat(response.hasError(), is(false)); assertThat(response.getBody(), is(nullValue())); } { assertThat(descriptor.invoke("baz").isPresent(), is(false)); } { assertThat( () -> descriptor.invoke("foo"), is(throwing(instanceOf(AssertionError.class)).andMessage(is(equalTo("Method overloading not yet supported: " + MyService.class.getName()))))); } { DiagnosticResponse<?> response = descriptor.invoke("withArgs", 1).get(); assertThat(response.getBody(), is(nullValue())); assertThat(response.getError().get(), containsString(IllegalArgumentException.class.getName() + ": argument type mismatch")); } { DiagnosticResponse<?> response = descriptor.invoke("fail").get(); assertThat(response.getBody(), is(nullValue())); assertThat(response.getError().get(), containsString(IllegalArgumentException.class.getName() + ": failed")); } }
Optional<DiagnosticResponse<?>> invoke(String methodName, Object... arguments) { return findMethod(methodName).map(method -> { try { Object result = method.invoke(serviceImplementation, arguments); return new DiagnosticResponse<>(result); } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), cause.getMessage(), cause); return new DiagnosticResponse<>(null, cause); } catch (Exception e) { LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), e.getMessage(), e); return new DiagnosticResponse<>(null, e); } }); }
DiagnosticServiceDescriptor implements DiagnosticServicesRegistration<T> { Optional<DiagnosticResponse<?>> invoke(String methodName, Object... arguments) { return findMethod(methodName).map(method -> { try { Object result = method.invoke(serviceImplementation, arguments); return new DiagnosticResponse<>(result); } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), cause.getMessage(), cause); return new DiagnosticResponse<>(null, cause); } catch (Exception e) { LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), e.getMessage(), e); return new DiagnosticResponse<>(null, e); } }); } }
DiagnosticServiceDescriptor implements DiagnosticServicesRegistration<T> { Optional<DiagnosticResponse<?>> invoke(String methodName, Object... arguments) { return findMethod(methodName).map(method -> { try { Object result = method.invoke(serviceImplementation, arguments); return new DiagnosticResponse<>(result); } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), cause.getMessage(), cause); return new DiagnosticResponse<>(null, cause); } catch (Exception e) { LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), e.getMessage(), e); return new DiagnosticResponse<>(null, e); } }); } DiagnosticServiceDescriptor(Class<T> serviceInterface, T serviceImplementation, Runnable onClose, Function<String, Boolean> jmxExpose); }
DiagnosticServiceDescriptor implements DiagnosticServicesRegistration<T> { Optional<DiagnosticResponse<?>> invoke(String methodName, Object... arguments) { return findMethod(methodName).map(method -> { try { Object result = method.invoke(serviceImplementation, arguments); return new DiagnosticResponse<>(result); } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), cause.getMessage(), cause); return new DiagnosticResponse<>(null, cause); } catch (Exception e) { LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), e.getMessage(), e); return new DiagnosticResponse<>(null, e); } }); } DiagnosticServiceDescriptor(Class<T> serviceInterface, T serviceImplementation, Runnable onClose, Function<String, Boolean> jmxExpose); @Override Class<T> getServiceInterface(); @Override boolean exposeMBean(String name); @Override void close(); Set<String> getRegisteredMBeans(); }
DiagnosticServiceDescriptor implements DiagnosticServicesRegistration<T> { Optional<DiagnosticResponse<?>> invoke(String methodName, Object... arguments) { return findMethod(methodName).map(method -> { try { Object result = method.invoke(serviceImplementation, arguments); return new DiagnosticResponse<>(result); } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), cause.getMessage(), cause); return new DiagnosticResponse<>(null, cause); } catch (Exception e) { LOGGER.error("Failed invoking method {} on diagnostic service {}: {}", methodName, serviceInterface.getName(), e.getMessage(), e); return new DiagnosticResponse<>(null, e); } }); } DiagnosticServiceDescriptor(Class<T> serviceInterface, T serviceImplementation, Runnable onClose, Function<String, Boolean> jmxExpose); @Override Class<T> getServiceInterface(); @Override boolean exposeMBean(String name); @Override void close(); Set<String> getRegisteredMBeans();  }