use of javax.management.MBeanServerInvocationHandler in project spring-framework by spring-projects.
the class MBeanClientInterceptor method prepare.
/**
* Ensures that an {@code MBeanServerConnection} is configured and attempts
* to detect a local connection if one is not supplied.
*/
public void prepare() {
synchronized (this.preparationMonitor) {
if (this.server != null) {
this.serverToUse = this.server;
} else {
this.serverToUse = null;
this.serverToUse = this.connector.connect(this.serviceUrl, this.environment, this.agentId);
}
this.invocationHandler = null;
if (this.useStrictCasing) {
// Use the JDK's own MBeanServerInvocationHandler, in particular for native MXBean support.
this.invocationHandler = new MBeanServerInvocationHandler(this.serverToUse, this.objectName, (this.managementInterface != null && JMX.isMXBeanInterface(this.managementInterface)));
} else {
// Non-strict casing can only be achieved through custom invocation handling.
// Only partial MXBean support available!
retrieveMBeanInfo();
}
}
}
use of javax.management.MBeanServerInvocationHandler in project jdk8u_jdk by JetBrains.
the class TestUtils method getObjectName.
/**
* Returns the ObjectName of the MBean that a proxy object
* is proxying.
**/
public static ObjectName getObjectName(Object proxy) {
if (!(proxy instanceof Proxy))
throw new IllegalArgumentException("not a " + Proxy.class.getName());
final Proxy p = (Proxy) proxy;
final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
if (handler instanceof MBeanServerInvocationHandler)
return ((MBeanServerInvocationHandler) handler).getObjectName();
throw new IllegalArgumentException("not a JMX Proxy");
}
use of javax.management.MBeanServerInvocationHandler in project masquerade by cuba-platform.
the class JmxCallHandler method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
JMXServiceURL url;
try {
url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostInfo.getAddress() + "/jmxrmi");
} catch (MalformedURLException e) {
throw new RuntimeException("Incorrect service URL", e);
}
Map<String, Object> properties = new HashMap<>();
if (hostInfo.getUser() != null) {
properties.put(JMXConnector.CREDENTIALS, new String[] { hostInfo.getUser(), hostInfo.getPassword() });
}
try (JMXConnector jmxc = JMXConnectorFactory.connect(url, properties)) {
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName mbeanName;
try {
mbeanName = new ObjectName(objectName);
} catch (MalformedObjectNameException e) {
throw new RuntimeException("Incorrect JMX object name", e);
}
MBeanServerInvocationHandler wrappedHandler = new MBeanServerInvocationHandler(mbsc, mbeanName);
if (args != null) {
log.info("Invoke method {} of {} with parameters {}", method.getName(), objectName, args);
} else {
log.info("Invoke method {} of {}", method.getName(), objectName);
}
return wrappedHandler.invoke(proxy, method, args);
} catch (IOException e) {
throw new RuntimeException("Unable to perform JMX call", e);
}
}
use of javax.management.MBeanServerInvocationHandler in project jdk8u_jdk by JetBrains.
the class TestUtils method makeNotificationEmitter.
/**
* Transfroms a proxy implementing T in a proxy implementing T plus
* NotificationEmitter
*
**/
public static <T> T makeNotificationEmitter(T proxy, Class<T> mbeanInterface) {
if (proxy instanceof NotificationEmitter)
return proxy;
if (proxy == null)
return null;
if (!(proxy instanceof Proxy))
throw new IllegalArgumentException("not a " + Proxy.class.getName());
final Proxy p = (Proxy) proxy;
final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
if (!(handler instanceof MBeanServerInvocationHandler))
throw new IllegalArgumentException("not a JMX Proxy");
final MBeanServerInvocationHandler h = (MBeanServerInvocationHandler) handler;
final ObjectName name = h.getObjectName();
final MBeanServerConnection mbs = h.getMBeanServerConnection();
final boolean isMXBean = h.isMXBean();
final T newProxy;
if (isMXBean)
newProxy = JMX.newMXBeanProxy(mbs, name, mbeanInterface, true);
else
newProxy = JMX.newMBeanProxy(mbs, name, mbeanInterface, true);
return newProxy;
}
use of javax.management.MBeanServerInvocationHandler in project jdk8u_jdk by JetBrains.
the class MXBeanRefTest method main.
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
ObjectName productName = new ObjectName("d:type=Product,n=1");
ObjectName product2Name = new ObjectName("d:type=Product,n=2");
ObjectName moduleName = new ObjectName("d:type=Module");
mbs.registerMBean(product, productName);
mbs.registerMBean(product2, product2Name);
mbs.registerMBean(module, moduleName);
ModuleMXBean moduleProxy = JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class);
ObjectName on;
on = (ObjectName) mbs.getAttribute(moduleName, "Product");
check("ObjectName attribute value", on.equals(productName));
ProductMXBean productProxy = moduleProxy.getProduct();
MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(productProxy);
check("ObjectName in proxy", mbsih.getObjectName().equals(productName));
mbs.setAttribute(moduleName, new Attribute("Product", product2Name));
ProductMXBean product2Proxy = module.getProduct();
mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(product2Proxy);
check("Proxy after setAttribute", mbsih.getObjectName().equals(product2Name));
moduleProxy.setProduct(productProxy);
ProductMXBean productProxyAgain = module.getProduct();
mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(productProxyAgain);
check("Proxy after proxied set", mbsih.getObjectName().equals(productName));
MBeanServer mbs2 = MBeanServerFactory.createMBeanServer();
ProductMXBean productProxy2 = JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class);
try {
moduleProxy.setProduct(productProxy2);
check("Proxy for wrong MBeanServer worked but shouldn't", false);
} catch (Exception e) {
if (e instanceof UndeclaredThrowableException && e.getCause() instanceof OpenDataException)
check("Proxy for wrong MBeanServer correctly rejected", true);
else {
e.printStackTrace(System.out);
check("Proxy for wrong MBeanServer got wrong exception", false);
}
}
// Test 6283873
ObjectName dup = new ObjectName("a:b=c");
mbs.registerMBean(new MBeanServerDelegate(), dup);
try {
mbs.registerMBean(new ProductImpl(), dup);
check("Duplicate register succeeded but should fail", false);
} catch (InstanceAlreadyExistsException e) {
check("Got correct exception from duplicate name", true);
} catch (Exception e) {
e.printStackTrace(System.out);
check("Got wrong exception from duplicate name", false);
}
if (failure != null)
throw new Exception("TEST FAILED: " + failure);
System.out.println("TEST PASSED");
}
Aggregations