下面列出了怎么用javax.management.modelmbean.ModelMBeanOperationInfo的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testRegistrationOnInterface() throws Exception {
Object bean = getContext().getBean("testInterfaceBean");
ModelMBeanInfo inf = getAssembler().getMBeanInfo(bean, "bean:name=interfaceTestBean");
assertNotNull(inf);
assertEquals("My Managed Bean", inf.getDescription());
ModelMBeanOperationInfo op = inf.getOperation("foo");
assertNotNull("foo operation not exposed", op);
assertEquals("invoke foo", op.getDescription());
assertNull("doNotExpose operation should not be exposed", inf.getOperation("doNotExpose"));
ModelMBeanAttributeInfo attr = inf.getAttribute("Bar");
assertNotNull("bar attribute not exposed", attr);
assertEquals("Bar description", attr.getDescription());
ModelMBeanAttributeInfo attr2 = inf.getAttribute("CacheEntries");
assertNotNull("cacheEntries attribute not exposed", attr2);
assertEquals("Metric Type should be COUNTER", "COUNTER",
attr2.getDescriptor().getFieldValue("metricType"));
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo get = info.getOperation("getName");
assertNotNull("get operation should not be null", get);
assertEquals("get operation should have visibility of four",
get.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role"));
ModelMBeanOperationInfo set = info.getOperation("setName");
assertNotNull("set operation should not be null", set);
assertEquals("set operation should have visibility of four",
set.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role"));
}
@Test
public void testRegistrationOnInterface() throws Exception {
Object bean = getContext().getBean("testInterfaceBean");
ModelMBeanInfo inf = getAssembler().getMBeanInfo(bean, "bean:name=interfaceTestBean");
assertNotNull(inf);
assertEquals("My Managed Bean", inf.getDescription());
ModelMBeanOperationInfo op = inf.getOperation("foo");
assertNotNull("foo operation not exposed", op);
assertEquals("invoke foo", op.getDescription());
assertNull("doNotExpose operation should not be exposed", inf.getOperation("doNotExpose"));
ModelMBeanAttributeInfo attr = inf.getAttribute("Bar");
assertNotNull("bar attribute not exposed", attr);
assertEquals("Bar description", attr.getDescription());
ModelMBeanAttributeInfo attr2 = inf.getAttribute("CacheEntries");
assertNotNull("cacheEntries attribute not exposed", attr2);
assertEquals("Metric Type should be COUNTER", "COUNTER",
attr2.getDescriptor().getFieldValue("metricType"));
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo get = info.getOperation("getName");
assertNotNull("get operation should not be null", get);
assertEquals("get operation should have visibility of four",
get.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role"));
ModelMBeanOperationInfo set = info.getOperation("setName");
assertNotNull("set operation should not be null", set);
assertEquals("set operation should have visibility of four",
set.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role"));
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo get = info.getOperation("getName");
assertNotNull("get operation should not be null", get);
assertEquals("get operation should have visibility of four",
get.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role"));
ModelMBeanOperationInfo set = info.getOperation("setName");
assertNotNull("set operation should not be null", set);
assertEquals("set operation should have visibility of four",
set.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role"));
}
@Override
public MBeanInfo getMBeanInfo() {
try {
ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[0];
ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[] {
new ModelMBeanConstructorInfo("-", this.getClass().getConstructor())
};
ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[] {
new ModelMBeanOperationInfo("info", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.INFO),
new ModelMBeanOperationInfo("action", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION),
new ModelMBeanOperationInfo("actionInfo", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION_INFO),
new ModelMBeanOperationInfo("unknown", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.UNKNOWN)
};
ModelMBeanNotificationInfo[] notifications = new ModelMBeanNotificationInfo[0];
return new ModelMBeanInfoSupport(this.getClass().getName(), "-", attributes, constructors, operations, notifications);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void addModelMBeanMethod(String name,
String[] paramTypes,
String[] paramNames,
String[] paramDescs,
String description,
String rtype,
Descriptor desc) {
MBeanParameterInfo[] params = null;
if (paramTypes != null) {
params = new MBeanParameterInfo[ paramTypes.length ];
for (int i = 0; i < paramTypes.length; i++) {
params[i] = new MBeanParameterInfo(paramNames[i],
paramTypes[i], paramDescs[i]);
}
}
operations.put(name,
new ModelMBeanOperationInfo(name,
description,
params,
rtype,
MBeanOperationInfo.ACTION,
desc));
}
public ModelMBeanInfo buildModelMBeanInfo(Descriptor desc) {
ModelMBeanOperationInfo[] ops =
operations.values().toArray(new ModelMBeanOperationInfo[operations.values().size()]);
ModelMBeanAttributeInfo[] atts =
attributes.values().toArray(new ModelMBeanAttributeInfo[attributes.values().size()]);
ModelMBeanConstructorInfo[] cons =
constructors.values().toArray(new ModelMBeanConstructorInfo[constructors.values().size()]);
ModelMBeanNotificationInfo[] notifs =
notifications.values().toArray(new ModelMBeanNotificationInfo[notifications.values().size()]);
return new ModelMBeanInfoSupport("javax.management.modelmbean.ModelMBeanInfo",
"description",
atts,
cons,
ops,
notifs, desc);
}
/**
* Builds an operationInfor for getter purposes.
*/
public static ModelMBeanOperationInfo buildGetterModelMBeanOperationInfo(
String name,
String klass,
String description,
String signature) {
Descriptor theDescriptor = buildGetterDescriptor(name, klass);
return new ModelMBeanOperationInfo(
name,
description,
null,
signature,
ModelMBeanOperationInfo.INFO,
theDescriptor);
}
/**
* Creates an instance of {@code ModelMBeanOperationInfo} for the
* given method. Populates the parameter info for the operation.
* @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for
* @param name the logical name for the operation (method name or property name);
* not used by the default implementation but possibly by subclasses
* @param beanKey the key associated with the MBean in the beans map
* of the {@code MBeanExporter}
* @return the {@code ModelMBeanOperationInfo}
*/
protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
MBeanParameterInfo[] params = getOperationParameters(method, beanKey);
if (params.length == 0) {
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
}
else {
return new ModelMBeanOperationInfo(method.getName(),
getOperationDescription(method, beanKey),
getOperationParameters(method, beanKey),
method.getReturnType().getName(),
MBeanOperationInfo.UNKNOWN);
}
}
@Test
public void testOperationParameterMetadata() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo oper = info.getOperation("add");
MBeanParameterInfo[] params = oper.getSignature();
assertEquals("Invalid number of params", 2, params.length);
assertEquals("Incorrect name for x param", "x", params[0].getName());
assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType());
assertEquals("Incorrect name for y param", "y", params[1].getName());
assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType());
}
@Test
public void testMetricDescription() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo metric = inf.getAttribute(QUEUE_SIZE_METRIC);
ModelMBeanOperationInfo operation = inf.getOperation("getQueueSize");
assertEquals("The description for the queue size metric is incorrect",
"The QueueSize metric", metric.getDescription());
assertEquals("The description for the getter operation of the queue size metric is incorrect",
"The QueueSize metric", operation.getDescription());
}
/**
* Creates an instance of {@code ModelMBeanOperationInfo} for the
* given method. Populates the parameter info for the operation.
* @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for
* @param name the logical name for the operation (method name or property name);
* not used by the default implementation but possibly by subclasses
* @param beanKey the key associated with the MBean in the beans map
* of the {@code MBeanExporter}
* @return the {@code ModelMBeanOperationInfo}
*/
protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
MBeanParameterInfo[] params = getOperationParameters(method, beanKey);
if (params.length == 0) {
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
}
else {
return new ModelMBeanOperationInfo(method.getName(),
getOperationDescription(method, beanKey),
getOperationParameters(method, beanKey),
method.getReturnType().getName(),
MBeanOperationInfo.UNKNOWN);
}
}
@Test
public void testOperationParameterMetadata() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo oper = info.getOperation("add");
MBeanParameterInfo[] params = oper.getSignature();
assertEquals("Invalid number of params", 2, params.length);
assertEquals("Incorrect name for x param", "x", params[0].getName());
assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType());
assertEquals("Incorrect name for y param", "y", params[1].getName());
assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType());
}
@Test
public void testMetricDescription() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo metric = inf.getAttribute(QUEUE_SIZE_METRIC);
ModelMBeanOperationInfo operation = inf.getOperation("getQueueSize");
assertEquals("The description for the queue size metric is incorrect",
"The QueueSize metric", metric.getDescription());
assertEquals("The description for the getter operation of the queue size metric is incorrect",
"The QueueSize metric", operation.getDescription());
}
private void extractMbeanOperations(Object managedBean, Set<ManagedOperationInfo> operations, Set<ModelMBeanOperationInfo> mBeanOperations) {
for (ManagedOperationInfo info : operations) {
ModelMBeanOperationInfo mbean = new ModelMBeanOperationInfo(info.getDescription(), info.getOperation());
Descriptor opDesc = mbean.getDescriptor();
mbean.setDescriptor(opDesc);
mBeanOperations.add(mbean);
LOG.trace("Assembled operation: {}", mbean);
}
}
/**
* Creates an instance of {@code ModelMBeanOperationInfo} for the
* given method. Populates the parameter info for the operation.
* @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for
* @param name the logical name for the operation (method name or property name);
* not used by the default implementation but possibly by subclasses
* @param beanKey the key associated with the MBean in the beans map
* of the {@code MBeanExporter}
* @return the {@code ModelMBeanOperationInfo}
*/
protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
MBeanParameterInfo[] params = getOperationParameters(method, beanKey);
if (params.length == 0) {
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
}
else {
return new ModelMBeanOperationInfo(method.getName(),
getOperationDescription(method, beanKey),
getOperationParameters(method, beanKey),
method.getReturnType().getName(),
MBeanOperationInfo.UNKNOWN);
}
}
/**
* Creates an instance of {@code ModelMBeanOperationInfo} for the
* given method. Populates the parameter info for the operation.
* @param method the {@code Method} to create a {@code ModelMBeanOperationInfo} for
* @param name the logical name for the operation (method name or property name);
* not used by the default implementation but possibly by subclasses
* @param beanKey the key associated with the MBean in the beans map
* of the {@code MBeanExporter}
* @return the {@code ModelMBeanOperationInfo}
*/
protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
MBeanParameterInfo[] params = getOperationParameters(method, beanKey);
if (params.length == 0) {
return new ModelMBeanOperationInfo(getOperationDescription(method, beanKey), method);
}
else {
return new ModelMBeanOperationInfo(method.getName(),
getOperationDescription(method, beanKey),
getOperationParameters(method, beanKey),
method.getReturnType().getName(),
MBeanOperationInfo.UNKNOWN);
}
}
@Test
public void testOperationParameterMetadata() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo oper = info.getOperation("add");
MBeanParameterInfo[] params = oper.getSignature();
assertEquals("Invalid number of params", 2, params.length);
assertEquals("Incorrect name for x param", "x", params[0].getName());
assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType());
assertEquals("Incorrect name for y param", "y", params[1].getName());
assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType());
}
@Test
public void testMetricDescription() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo metric = inf.getAttribute(QUEUE_SIZE_METRIC);
ModelMBeanOperationInfo operation = inf.getOperation("getQueueSize");
assertEquals("The description for the queue size metric is incorrect",
"The QueueSize metric", metric.getDescription());
assertEquals("The description for the getter operation of the queue size metric is incorrect",
"The QueueSize metric", operation.getDescription());
}
private void extractMbeanOperations(Object managedBean, Set<ManagedOperationInfo> operations, Set<ModelMBeanOperationInfo> mBeanOperations) {
for (ManagedOperationInfo info : operations) {
ModelMBeanOperationInfo mbean = new ModelMBeanOperationInfo(info.getDescription(), info.getOperation());
Descriptor opDesc = mbean.getDescriptor();
mbean.setDescriptor(opDesc);
mBeanOperations.add(mbean);
LOGGER.trace("Assembled operation: {}", mbean);
}
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo myOperation = info.getOperation("myOperation");
assertNotNull("get operation should not be null", myOperation);
assertEquals("Incorrect myOperation return type", "long", myOperation.getReturnType());
ModelMBeanOperationInfo add = info.getOperation("add");
assertNotNull("set operation should not be null", add);
assertEquals("Incorrect add method description", "Add Two Numbers Together", add.getDescription());
}
@Test
public void testOperationFromInterface() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo op = inf.getOperation("fromInterface");
assertNotNull(op);
}
@Test
public void testOperationOnGetter() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo op = inf.getOperation("getExpensiveToCalculate");
assertNotNull(op);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
@Test
public void testOperationFromInterface() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo op = inf.getOperation("fromInterface");
assertNotNull(op);
}
@Test
public void testOperationOnGetter() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo op = inf.getOperation("getExpensiveToCalculate");
assertNotNull(op);
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
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();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException {
if ((defaultManagedBean == null && customManagedBean == null) || objectName == null)
return null;
// skip proxy classes
if (defaultManagedBean != null && Proxy.isProxyClass(defaultManagedBean.getClass())) {
LOG.trace("Skip creating ModelMBeanInfo due proxy class {}", defaultManagedBean.getClass());
return null;
}
// maps and lists to contain information about attributes and operations
Map<String, ManagedAttributeInfo> attributes = new LinkedHashMap<String, ManagedAttributeInfo>();
Set<ManagedOperationInfo> operations = new LinkedHashSet<ManagedOperationInfo>();
Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<ModelMBeanAttributeInfo>();
Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<ModelMBeanOperationInfo>();
Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<ModelMBeanNotificationInfo>();
// extract details from default managed bean
if (defaultManagedBean != null) {
extractAttributesAndOperations(defaultManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(defaultManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(defaultManagedBean, operations, mBeanOperations);
extractMbeanNotifications(defaultManagedBean, mBeanNotifications);
}
// extract details from custom managed bean
if (customManagedBean != null) {
extractAttributesAndOperations(customManagedBean.getClass(), attributes, operations);
extractMbeanAttributes(customManagedBean, attributes, mBeanAttributes, mBeanOperations);
extractMbeanOperations(customManagedBean, operations, mBeanOperations);
extractMbeanNotifications(customManagedBean, mBeanNotifications);
}
// create the ModelMBeanInfo
String name = getName(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
String description = getDescription(customManagedBean != null ? customManagedBean : defaultManagedBean, objectName);
ModelMBeanAttributeInfo[] arrayAttributes = mBeanAttributes.toArray(new ModelMBeanAttributeInfo[mBeanAttributes.size()]);
ModelMBeanOperationInfo[] arrayOperations = mBeanOperations.toArray(new ModelMBeanOperationInfo[mBeanOperations.size()]);
ModelMBeanNotificationInfo[] arrayNotifications = mBeanNotifications.toArray(new ModelMBeanNotificationInfo[mBeanNotifications.size()]);
ModelMBeanInfo info = new ModelMBeanInfoSupport(name, description, arrayAttributes, null, arrayOperations, arrayNotifications);
LOG.trace("Created ModelMBeanInfo {}", info);
return info;
}