MetaData(Class<?> klass, Constructor<?> constructor, MetaData parent, List<MetaData> interfaces) { _klass = klass; _parent = parent; _interfaces = interfaces; _constructor = constructor; if (_constructor != null) parseMethods(klass, _constructor.getDeclaringClass()); else parseMethods(klass); _info = buildMBeanInfo(klass); }
private void collectMBeanAttributeInfos(Map<String, MBeanAttributeInfo> attributeInfos) { // Start with interfaces, overwrite with superClass, then overwrite with local attributes. for (MetaData intf : _interfaces) intf.collectMBeanAttributeInfos(attributeInfos); if (_parent != null) { MBeanAttributeInfo[] parentAttributes = _parent.getMBeanInfo().getAttributes(); for (MBeanAttributeInfo parentAttribute : parentAttributes) attributeInfos.put(parentAttribute.getName(), parentAttribute); } for (Map.Entry<String, AttributeInfo> entry : _attributes.entrySet()) attributeInfos.put(entry.getKey(), entry.getValue()._info); }
private AttributeInfo findAttribute(String name) { if (name == null) return null; AttributeInfo result = null; for (MetaData intf : _interfaces) { AttributeInfo r = intf.findAttribute(name); if (r != null) result = r; } if (_parent != null) { AttributeInfo r = _parent.findAttribute(name); if (r != null) result = r; } AttributeInfo r = _attributes.get(name); if (r != null) result = r; return result; }
private MBeanInfo buildMBeanInfo(Class<?> klass) { ManagedObject managedObject = klass.getAnnotation(ManagedObject.class); String description = managedObject == null ? "" : managedObject.value(); Map<String, MBeanAttributeInfo> attributeInfos = new HashMap<>(); collectMBeanAttributeInfos(attributeInfos); Map<String, MBeanOperationInfo> operationInfos = new HashMap<>(); collectMBeanOperationInfos(operationInfos); MBeanInfo mbeanInfo = _parent == null ? null : _parent.getMBeanInfo(); MBeanAttributeInfo[] attributes = attributeInfos.values().toArray(NO_ATTRIBUTES); MBeanConstructorInfo[] constructors = mbeanInfo == null ? NO_CONSTRUCTORS : mbeanInfo.getConstructors(); MBeanOperationInfo[] operations = operationInfos.values().toArray(NO_OPERATIONS); MBeanNotificationInfo[] notifications = mbeanInfo == null ? NO_NOTIFICATIONS : mbeanInfo.getNotifications(); return new MBeanInfo(klass.getName(), description, attributes, constructors, operations, notifications); }
private void collectMBeanOperationInfos(Map<String, MBeanOperationInfo> operationInfos) { // Start with interfaces, overwrite with superClass, then overwrite with local operations. for (MetaData intf : _interfaces) intf.collectMBeanOperationInfos(operationInfos); if (_parent != null) { MBeanOperationInfo[] parentOperations = _parent.getMBeanInfo().getOperations(); for (MBeanOperationInfo parentOperation : parentOperations) { String signature = signature(parentOperation.getName(), Arrays.stream(parentOperation.getSignature()).map(MBeanParameterInfo::getType).toArray(String[]::new)); operationInfos.put(signature, parentOperation); } } for (Map.Entry<String, OperationInfo> entry : _operations.entrySet()) operationInfos.put(entry.getKey(), entry.getValue()._info); }
Object invoke(String name, String[] params, Object[] args, ObjectMBean mbean) throws ReflectionException, MBeanException { String signature = signature(name, params); OperationInfo info = findOperation(signature); if (info == null) throw new ReflectionException(new NoSuchMethodException(signature)); return info.invoke(args, mbean); }
Object newInstance(Object bean) { Object mbean; if (_constructor != null) mbean = newInstance(_constructor, bean); else if (_parent != null) mbean = _parent.newInstance(bean); else mbean = new ObjectMBean(bean); return mbean; }
private OperationInfo findOperation(String signature) { OperationInfo result = null; for (MetaData intf : _interfaces) { OperationInfo r = intf.findOperation(signature); if (r != null) result = r; } if (_parent != null) { OperationInfo r = _parent.findOperation(signature); if (r != null) result = r; } OperationInfo r = _operations.get(signature); if (r != null) result = r; return result; }
@Override public MBeanInfo getMBeanInfo() { return metaData().getMBeanInfo(); }
@Override public Object getAttribute(String name) throws AttributeNotFoundException, ReflectionException, MBeanException { ClassLoader prevLoader = Thread.currentThread().getContextClassLoader(); try { return metaData().getAttribute(name, this); } finally { Thread.currentThread().setContextClassLoader(prevLoader); } }
@Override public Object invoke(String name, Object[] params, String[] signature) throws ReflectionException, MBeanException { ClassLoader prevLoader = Thread.currentThread().getContextClassLoader(); try { return metaData().invoke(name, signature, params, this); } finally { Thread.currentThread().setContextClassLoader(prevLoader); } }
private static MetaData newMetaData(MBeanContainer container, Class<?> klass) { if (klass == null) return null; if (klass == Object.class) return new MetaData(klass, null, null, Collections.emptyList()); List<MetaData> interfaces = Arrays.stream(klass.getInterfaces()) .map(intf -> findMetaData(container, intf)) .collect(Collectors.toList()); MetaData metaData = new MetaData(klass, findConstructor(klass), findMetaData(container, klass.getSuperclass()), interfaces); if (container != null) { if (container.isUseCacheForOtherClassLoaders() || klass.getClassLoader() == container.getClass().getClassLoader()) { MetaData existing = container._metaData.putIfAbsent(klass, metaData); if (existing != null) metaData = existing; if (LOG.isDebugEnabled()) LOG.debug("Cached {}", metaData); } } return metaData; }
static Object mbeanFor(MBeanContainer container, Object o) { if (o == null) return null; Object mbean = findMetaData(container, o.getClass()).newInstance(o); if (mbean instanceof ObjectMBean) ((ObjectMBean)mbean).setMBeanContainer(container); if (LOG.isDebugEnabled()) { LOG.debug("MBean for {} is {}", o, mbean); if (mbean instanceof ObjectMBean) { MBeanInfo info = ((ObjectMBean)mbean).getMBeanInfo(); for (Object a : info.getAttributes()) LOG.debug(" {}", a); for (Object a : info.getOperations()) LOG.debug(" {}", a); } } return mbean; }
void setAttribute(Attribute attribute, ObjectMBean mbean) throws AttributeNotFoundException, ReflectionException, MBeanException { if (attribute == null) return; String name = attribute.getName(); AttributeInfo info = findAttribute(name); if (info == null) throw new AttributeNotFoundException(name); info.setAttribute(attribute.getValue(), mbean); }
Object getAttribute(String name, ObjectMBean mbean) throws AttributeNotFoundException, ReflectionException, MBeanException { AttributeInfo info = findAttribute(name); if (info == null) throw new AttributeNotFoundException(name); return info.getAttribute(mbean); }