use of javax.management.MBeanOperationInfo in project Payara by payara.
the class ConfigBeanJMXSupport method _getMBeanInfo.
private MBeanInfo _getMBeanInfo() {
final List<MBeanAttributeInfo> attrsList = new ArrayList<MBeanAttributeInfo>();
for (final AttributeMethodInfo info : mAttrInfos) {
attrsList.add(attributeToMBeanAttributeInfo(info));
}
for (final ElementMethodInfo e : mElementInfos) {
final MBeanAttributeInfo attrInfo = elementToMBeanAttributeInfo(e.method());
if (attrInfo != null) {
attrsList.add(attrInfo);
}
}
final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[attrsList.size()];
attrsList.toArray(attrs);
final String classname = mIntf.getName();
final String description = "ConfigBean " + mIntf.getName();
final MBeanOperationInfo[] operations = toMBeanOperationInfos();
final Descriptor descriptor = descriptor();
final MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { ATTRIBUTE_CHANGE_NOTIF_INFO };
final MBeanInfo info = new MBeanInfo(classname, description, attrs, null, operations, notifications, descriptor);
return info;
}
use of javax.management.MBeanOperationInfo in project tomee by apache.
the class LocalJMXCommand method invoke.
private void invoke(final String value) {
if (!value.contains("(") || !value.contains(")")) {
streamManager.writeErr("method should follow the format: <methoName>(<arg1>,<arg2>,...)");
return;
}
int open = value.indexOf("(");
int close = value.lastIndexOf(")");
final String name = value.substring(0, open).trim();
final String rawArgs = value.substring(open + 1, close).trim();
final ObjectName on;
try {
on = new ObjectName(value.substring(close + 1).trim());
} catch (MalformedObjectNameException e) {
streamManager.writeErr(e);
return;
}
final MBeanServer server = LocalMBeanServer.get();
final String[] args;
if (rawArgs == null || rawArgs.isEmpty()) {
args = new String[0];
} else {
args = rawArgs.split(",");
}
try {
final MBeanInfo minfo = server.getMBeanInfo(on);
final MBeanOperationInfo[] methods = minfo.getOperations();
MBeanOperationInfo operation = null;
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(name)) {
operation = methods[i];
break;
}
}
if (operation == null) {
streamManager.writeErr("can't find operation '" + name + "'");
return;
}
final Object[] passedArgs = new Object[args.length];
final String[] passedArgTypes = new String[args.length];
for (int i = 0; i < passedArgs.length; i++) {
final String expected = operation.getSignature()[i].getType();
if (!String.class.getName().equals(expected)) {
passedArgs[i] = PropertyEditors.getValue(expected, args[i], Thread.currentThread().getContextClassLoader());
} else {
passedArgs[i] = args[i];
}
passedArgTypes[i] = expected;
}
streamManager.writeOut(stringify(server.invoke(on, name, passedArgs, passedArgTypes)));
} catch (Exception e) {
streamManager.writeErr(e);
return;
}
}
use of javax.management.MBeanOperationInfo in project tomee by apache.
the class ManagedMBean method getMBeanInfo.
public MBeanInfo getMBeanInfo() {
final List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>(this.attributes);
final List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>(this.operations);
for (final Member member : dynamic) {
try {
final ManagedCollection managedCollection = member.getAnnotation(ManagedCollection.class);
final Collection collection = (Collection) member.get();
for (final Object o : collection) {
try {
final Field field = o.getClass().getDeclaredField(managedCollection.key());
field.setAccessible(true);
final Object key = field.get(o);
final ManagedMBean bean = new ManagedMBean(o, key.toString());
Collections.addAll(attributes, bean.getMBeanInfo().getAttributes());
Collections.addAll(operations, bean.getMBeanInfo().getOperations());
attributesMap.putAll(bean.attributesMap);
operationsMap.putAll(bean.operationsMap);
} catch (final Exception e) {
e.printStackTrace();
}
}
} catch (final Exception e) {
e.printStackTrace();
}
}
sort(operations, MBeanFeatureInfoComparator.INSTANCE);
sort(attributes, MBeanFeatureInfoComparator.INSTANCE);
if (filterAttributes) {
final Iterator<MBeanAttributeInfo> iterator = attributes.iterator();
while (iterator.hasNext()) {
final MBeanAttributeInfo info = iterator.next();
if (includes.matcher(info.getName()).matches()) {
continue;
}
if (excludes.matcher(info.getName()).matches()) {
iterator.remove();
}
}
}
return new MBeanInfo(this.getClass().getName(), "", attributes.toArray(new MBeanAttributeInfo[attributes.size()]), new MBeanConstructorInfo[0], operations.toArray(new MBeanOperationInfo[operations.size()]), EMPTY_NOTIFICATIONS);
}
use of javax.management.MBeanOperationInfo in project jetty.project by eclipse.
the class ObjectMBean method defineOperation.
/* ------------------------------------------------------------ */
/**
* TODO update to new behavior
*
* Define an operation on the managed object. Defines an operation with parameters. Refection is
* used to determine find the method and it's return type. The description of the method is
* found with a call to findDescription on "name(signature)". The name and description of each
* parameter is found with a call to findDescription with "name(signature)[n]", the returned
* description is for the last parameter of the partial signature and is assumed to start with
* the parameter name, followed by a colon.
*
* @param metaData "description" or "impact:description" or "type:impact:description", type is
* the "Object","MBean", "MMBean" or "MObject" to indicate the method is on the object, the MBean or on the
* object but converted to an MBean reference, and impact is either "ACTION","INFO","ACTION_INFO" or "UNKNOWN".
*/
private MBeanOperationInfo defineOperation(Method method, ManagedOperation methodAnnotation) {
String description = methodAnnotation.value();
boolean onMBean = methodAnnotation.proxied();
boolean convert = false;
// determine if we should convert
Class<?> returnType = method.getReturnType();
if (returnType.isArray()) {
if (LOG.isDebugEnabled())
LOG.debug("returnType is array, get component type");
returnType = returnType.getComponentType();
}
if (returnType.isAnnotationPresent(ManagedObject.class)) {
convert = true;
}
String impactName = methodAnnotation.impact();
if (LOG.isDebugEnabled())
LOG.debug("defineOperation {} {}:{}:{}", method.getName(), onMBean, impactName, description);
String signature = method.getName();
try {
// Resolve the impact
int impact = MBeanOperationInfo.UNKNOWN;
if (impactName == null || impactName.equals("UNKNOWN"))
impact = MBeanOperationInfo.UNKNOWN;
else if (impactName.equals("ACTION"))
impact = MBeanOperationInfo.ACTION;
else if (impactName.equals("INFO"))
impact = MBeanOperationInfo.INFO;
else if (impactName.equals("ACTION_INFO"))
impact = MBeanOperationInfo.ACTION_INFO;
else
LOG.warn("Unknown impact '" + impactName + "' for " + signature);
Annotation[][] allParameterAnnotations = method.getParameterAnnotations();
Class<?>[] methodTypes = method.getParameterTypes();
MBeanParameterInfo[] pInfo = new MBeanParameterInfo[allParameterAnnotations.length];
for (int i = 0; i < allParameterAnnotations.length; ++i) {
Annotation[] parameterAnnotations = allParameterAnnotations[i];
for (Annotation anno : parameterAnnotations) {
if (anno instanceof Name) {
Name nameAnnotation = (Name) anno;
pInfo[i] = new MBeanParameterInfo(nameAnnotation.value(), methodTypes[i].getName(), nameAnnotation.description());
}
}
}
signature += "(";
for (int i = 0; i < methodTypes.length; ++i) {
signature += methodTypes[i].getName();
if (i != methodTypes.length - 1) {
signature += ",";
}
}
signature += ")";
Class<?> returnClass = method.getReturnType();
if (LOG.isDebugEnabled())
LOG.debug("Method Cache: " + signature);
if (_methods.containsKey(signature)) {
// we have an operation for this already
return null;
}
_methods.put(signature, method);
if (convert)
_convert.add(signature);
return new MBeanOperationInfo(method.getName(), description, pInfo, returnClass.isPrimitive() ? TypeUtil.toName(returnClass) : (returnClass.getName()), impact);
} catch (Exception e) {
LOG.warn("Operation '" + signature + "'", e);
throw new IllegalArgumentException(e.toString());
}
}
use of javax.management.MBeanOperationInfo 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;
}
Aggregations