use of javax.management.MBeanAttributeInfo in project jetty.project by eclipse.
the class ObjectMBean method defineAttribute.
/* ------------------------------------------------------------ */
/**
* TODO update to new behavior
*
* Define an attribute on the managed object. The meta data is defined by looking for standard
* getter and setter methods. Descriptions are obtained with a call to findDescription with the
* attribute name.
*
* @param method the method to define
* @param attributeAnnotation "description" or "access:description" or "type:access:description" where type is
* one of: <ul>
* <li>"Object" The field/method is on the managed object.
* <li>"MBean" The field/method is on the mbean proxy object
* <li>"MObject" The field/method is on the managed object and value should be converted to MBean reference
* <li>"MMBean" The field/method is on the mbean proxy object and value should be converted to MBean reference
* </ul>
* the access is either "RW" or "RO".
* @return the mbean attribute info for the method
*/
public MBeanAttributeInfo defineAttribute(Method method, ManagedAttribute attributeAnnotation) {
// determine the name of the managed attribute
String name = attributeAnnotation.name();
if ("".equals(name)) {
name = toVariableName(method.getName());
}
if (_attributes.contains(name)) {
// we have an attribute named this already
return null;
}
String description = attributeAnnotation.value();
boolean readonly = attributeAnnotation.readonly();
boolean onMBean = attributeAnnotation.proxied();
boolean convert = false;
// determine if we should convert
Class<?> return_type = method.getReturnType();
// get the component type
Class<?> component_type = return_type;
while (component_type.isArray()) {
component_type = component_type.getComponentType();
}
// Test to see if the returnType or any of its super classes are managed objects
convert = isAnnotationPresent(component_type, ManagedObject.class);
String uName = name.substring(0, 1).toUpperCase(Locale.ENGLISH) + name.substring(1);
Class<?> oClass = onMBean ? this.getClass() : _managed.getClass();
if (LOG.isDebugEnabled())
LOG.debug("defineAttribute {} {}:{}:{}:{}", name, onMBean, readonly, oClass, description);
Method setter = null;
// dig out a setter if one exists
if (!readonly) {
String declaredSetter = attributeAnnotation.setter();
if (LOG.isDebugEnabled())
LOG.debug("DeclaredSetter: {}", declaredSetter);
Method[] methods = oClass.getMethods();
for (int m = 0; m < methods.length; m++) {
if ((methods[m].getModifiers() & Modifier.PUBLIC) == 0)
continue;
if (!"".equals(declaredSetter)) {
// look for a declared setter
if (methods[m].getName().equals(declaredSetter) && methods[m].getParameterCount() == 1) {
if (setter != null) {
LOG.warn("Multiple setters for mbean attr {} in {}", name, oClass);
continue;
}
setter = methods[m];
if (!component_type.equals(methods[m].getParameterTypes()[0])) {
LOG.warn("Type conflict for mbean attr {} in {}", name, oClass);
continue;
}
if (LOG.isDebugEnabled())
LOG.debug("Declared Setter: " + declaredSetter);
}
}
// look for a setter
if (methods[m].getName().equals("set" + uName) && methods[m].getParameterCount() == 1) {
if (setter != null) {
LOG.warn("Multiple setters for mbean attr {} in {}", name, oClass);
continue;
}
setter = methods[m];
if (!return_type.equals(methods[m].getParameterTypes()[0])) {
LOG.warn("Type conflict for mbean attr {} in {}", name, oClass);
continue;
}
}
}
}
if (convert) {
if (component_type == null) {
LOG.warn("No mbean type for {} on {}", name, _managed.getClass());
return null;
}
if (component_type.isPrimitive() && !component_type.isArray()) {
LOG.warn("Cannot convert mbean primative {}", name);
return null;
}
if (LOG.isDebugEnabled())
LOG.debug("passed convert checks {} for type {}", name, component_type);
}
try {
// Remember the methods
_getters.put(name, method);
_setters.put(name, setter);
MBeanAttributeInfo info = null;
if (convert) {
_convert.add(name);
if (component_type.isArray()) {
info = new MBeanAttributeInfo(name, OBJECT_NAME_ARRAY_CLASS, description, true, setter != null, method.getName().startsWith("is"));
} else {
info = new MBeanAttributeInfo(name, OBJECT_NAME_CLASS, description, true, setter != null, method.getName().startsWith("is"));
}
} else {
info = new MBeanAttributeInfo(name, description, method, setter);
}
_attributes.add(name);
return info;
} catch (Exception e) {
LOG.warn(e);
throw new IllegalArgumentException(e.toString());
}
}
use of javax.management.MBeanAttributeInfo in project jetty.project by eclipse.
the class ObjectMBean method getMBeanInfo.
public MBeanInfo getMBeanInfo() {
try {
if (_info == null) {
// Start with blank lazy lists attributes etc.
String desc = null;
List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
List<MBeanConstructorInfo> constructors = new ArrayList<MBeanConstructorInfo>();
List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
List<MBeanNotificationInfo> notifications = new ArrayList<MBeanNotificationInfo>();
// Find list of classes that can influence the mbean
Class<?> o_class = _managed.getClass();
List<Class<?>> influences = new ArrayList<Class<?>>();
// always add MBean itself
influences.add(this.getClass());
influences = findInfluences(influences, _managed.getClass());
if (LOG.isDebugEnabled())
LOG.debug("Influence Count: {}", influences.size());
// Process Type Annotations
ManagedObject primary = o_class.getAnnotation(ManagedObject.class);
if (primary != null) {
desc = primary.value();
} else {
if (LOG.isDebugEnabled())
LOG.debug("No @ManagedObject declared on {}", _managed.getClass());
}
// For each influence
for (int i = 0; i < influences.size(); i++) {
Class<?> oClass = influences.get(i);
ManagedObject typeAnnotation = oClass.getAnnotation(ManagedObject.class);
if (LOG.isDebugEnabled())
LOG.debug("Influenced by: " + oClass.getCanonicalName());
if (typeAnnotation == null) {
if (LOG.isDebugEnabled())
LOG.debug("Annotations not found for: {}", oClass.getCanonicalName());
continue;
}
for (Method method : oClass.getDeclaredMethods()) {
ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
if (methodAttributeAnnotation != null) {
// TODO sort out how a proper name could get here, its a method name as an attribute at this point.
if (LOG.isDebugEnabled())
LOG.debug("Attribute Annotation found for: {}", method.getName());
MBeanAttributeInfo mai = defineAttribute(method, methodAttributeAnnotation);
if (mai != null) {
attributes.add(mai);
}
}
ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
if (methodOperationAnnotation != null) {
if (LOG.isDebugEnabled())
LOG.debug("Method Annotation found for: {}", method.getName());
MBeanOperationInfo oi = defineOperation(method, methodOperationAnnotation);
if (oi != null) {
operations.add(oi);
}
}
}
}
_info = new MBeanInfo(o_class.getName(), desc, (MBeanAttributeInfo[]) attributes.toArray(new MBeanAttributeInfo[attributes.size()]), (MBeanConstructorInfo[]) constructors.toArray(new MBeanConstructorInfo[constructors.size()]), (MBeanOperationInfo[]) operations.toArray(new MBeanOperationInfo[operations.size()]), (MBeanNotificationInfo[]) notifications.toArray(new MBeanNotificationInfo[notifications.size()]));
}
} catch (RuntimeException e) {
LOG.warn(e);
throw e;
}
return _info;
}
use of javax.management.MBeanAttributeInfo in project hazelcast by hazelcast.
the class HazelcastMBean method attributeInfos.
private MBeanAttributeInfo[] attributeInfos() {
MBeanAttributeInfo[] array = new MBeanAttributeInfo[attributeMap.size()];
int i = 0;
for (BeanInfo beanInfo : attributeMap.values()) {
array[i++] = beanInfo.getAttributeInfo();
}
return array;
}
use of javax.management.MBeanAttributeInfo in project jmxtrans by jmxtrans.
the class TreeWalker2 method walkTree.
public void walkTree(MBeanServerConnection connection, Server server) throws Exception {
// key here is null, null returns everything!
Set<ObjectName> mbeans = connection.queryNames(null, null);
for (ObjectName name : mbeans) {
MBeanInfo info = connection.getMBeanInfo(name);
MBeanAttributeInfo[] attrs = info.getAttributes();
Query.Builder queryBuilder = Query.builder().setObj(name.getCanonicalName()).addOutputWriterFactory(new StdOutWriter(ImmutableList.<String>of(), false, false, Collections.<String, Object>emptyMap()));
for (MBeanAttributeInfo attrInfo : attrs) {
queryBuilder.addAttr(attrInfo.getName());
}
Query query = queryBuilder.build();
try {
Iterable<Result> results = server.execute(query);
query.runOutputWritersForQuery(server, results);
} catch (AttributeNotFoundException anfe) {
log.error("Error", anfe);
}
}
}
use of javax.management.MBeanAttributeInfo in project jmxtrans by jmxtrans.
the class Query method fetchResults.
public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
MBeanInfo info = mbeanServer.getMBeanInfo(queryName);
ObjectInstance oi = mbeanServer.getObjectInstance(queryName);
List<String> attributes;
if (attr.isEmpty()) {
attributes = new ArrayList<>();
for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
attributes.add(attrInfo.getName());
}
} else {
attributes = attr;
}
try {
if (!attributes.isEmpty()) {
logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this);
AttributeList al = mbeanServer.getAttributes(queryName, attributes.toArray(new String[attributes.size()]));
return new JmxResultProcessor(this, oi, al.asList(), info.getClassName(), queryName.getDomain()).getResults();
}
} catch (UnmarshalException ue) {
if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) {
logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: " + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52", ue.getMessage());
} else {
throw ue;
}
}
return ImmutableList.of();
}
Aggregations