use of javax.management.MBeanAttributeInfo in project tomcat by apache.
the class ManagedBean method getMBeanInfo.
/**
* Create and return a <code>ModelMBeanInfo</code> object that
* describes this entire managed bean.
* @return the MBean info
*/
MBeanInfo getMBeanInfo() {
// Return our cached information (if any)
mBeanInfoLock.readLock().lock();
try {
if (info != null) {
return info;
}
} finally {
mBeanInfoLock.readLock().unlock();
}
mBeanInfoLock.writeLock().lock();
try {
if (info == null) {
// Create subordinate information descriptors as required
AttributeInfo[] attrs = getAttributes();
MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[attrs.length];
for (int i = 0; i < attrs.length; i++) attributes[i] = attrs[i].createAttributeInfo();
OperationInfo[] opers = getOperations();
MBeanOperationInfo[] operations = new MBeanOperationInfo[opers.length];
for (int i = 0; i < opers.length; i++) operations[i] = opers[i].createOperationInfo();
NotificationInfo[] notifs = getNotifications();
MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[notifs.length];
for (int i = 0; i < notifs.length; i++) notifications[i] = notifs[i].createNotificationInfo();
// Construct and return a new ModelMBeanInfo object
info = new MBeanInfo(getClassName(), getDescription(), attributes, new MBeanConstructorInfo[] {}, operations, notifications);
}
return info;
} finally {
mBeanInfoLock.writeLock().unlock();
}
}
use of javax.management.MBeanAttributeInfo in project tomcat by apache.
the class MBeanDumper method dumpBeans.
/**
* The following code to dump MBeans has been copied from JMXProxyServlet.
* @param mbeanServer the MBean server
* @param names a set of object names for which to dump the info
* @return a string representation of the MBeans
*/
public static String dumpBeans(MBeanServer mbeanServer, Set<ObjectName> names) {
StringBuilder buf = new StringBuilder();
Iterator<ObjectName> it = names.iterator();
while (it.hasNext()) {
ObjectName oname = it.next();
buf.append("Name: ");
buf.append(oname.toString());
buf.append(CRLF);
try {
MBeanInfo minfo = mbeanServer.getMBeanInfo(oname);
// can't be null - I think
String code = minfo.getClassName();
if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
code = (String) mbeanServer.getAttribute(oname, "modelerType");
}
buf.append("modelerType: ");
buf.append(code);
buf.append(CRLF);
MBeanAttributeInfo[] attrs = minfo.getAttributes();
Object value = null;
for (int i = 0; i < attrs.length; i++) {
if (!attrs[i].isReadable())
continue;
String attName = attrs[i].getName();
if ("modelerType".equals(attName))
continue;
if (attName.indexOf('=') >= 0 || attName.indexOf(':') >= 0 || attName.indexOf(' ') >= 0) {
continue;
}
try {
value = mbeanServer.getAttribute(oname, attName);
} catch (JMRuntimeException rme) {
Throwable cause = rme.getCause();
if (cause instanceof UnsupportedOperationException) {
if (log.isDebugEnabled()) {
log.debug("Error getting attribute " + oname + " " + attName, rme);
}
} else if (cause instanceof NullPointerException) {
if (log.isDebugEnabled()) {
log.debug("Error getting attribute " + oname + " " + attName, rme);
}
} else {
log.error("Error getting attribute " + oname + " " + attName, rme);
}
continue;
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.error("Error getting attribute " + oname + " " + attName, t);
continue;
}
if (value == null)
continue;
String valueString;
try {
Class<?> c = value.getClass();
if (c.isArray()) {
int len = Array.getLength(value);
StringBuilder sb = new StringBuilder("Array[" + c.getComponentType().getName() + "] of length " + len);
if (len > 0) {
sb.append(CRLF);
}
for (int j = 0; j < len; j++) {
sb.append("\t");
Object item = Array.get(value, j);
if (item == null) {
sb.append("NULL VALUE");
} else {
try {
sb.append(escape(item.toString()));
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
sb.append("NON-STRINGABLE VALUE");
}
}
if (j < len - 1) {
sb.append(CRLF);
}
}
valueString = sb.toString();
} else {
valueString = escape(value.toString());
}
buf.append(attName);
buf.append(": ");
buf.append(valueString);
buf.append(CRLF);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
buf.append(CRLF);
}
return buf.toString();
}
use of javax.management.MBeanAttributeInfo in project jvm-tools by aragozin.
the class MBeanHelper method get.
public String get(ObjectName bean, String attr) throws Exception {
MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
if (ai == null) {
throw new IllegalArgumentException("No such attribute '" + attr + "'");
}
if (!ai.isReadable()) {
throw new IllegalArgumentException("Attribute '" + attr + "' is write-only");
}
Object v = mserver.getAttribute(bean, attr);
String type = ai.getType();
String text = format(v, type);
return text;
}
use of javax.management.MBeanAttributeInfo in project jvm-tools by aragozin.
the class MBeanHelper method set.
public void set(ObjectName bean, String attr, String value) throws Exception {
MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
if (ai == null) {
throw new IllegalArgumentException("No such attribute '" + attr + "'");
}
if (!ai.isWritable()) {
throw new IllegalArgumentException("Attribute '" + attr + "' is not writeable");
}
String type = ai.getType();
Object ov = convert(value, type);
mserver.setAttribute(bean, new Attribute(attr, ov));
}
use of javax.management.MBeanAttributeInfo in project jcollectd by collectd.
the class CollectdMBean method getAttributeInfo.
protected MBeanAttributeInfo[] getAttributeInfo() {
MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[_metrics.size()];
int i = 0;
for (String name : _metrics.keySet()) {
attrs[i++] = new MBeanAttributeInfo(name, getAttributeType(name), getAttributeDescription(name), // isReadable
true, // isWritable
false, // isIs
false);
}
return attrs;
}
Aggregations