/** * Reads the {@link ManagedNotification} metadata from the {@code Class} of the managed resource * and generates and returns the corresponding {@link javax.management.modelmbean.ModelMBeanNotificationInfo} metadata. */ @Override protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) { Class intf = findJmxInterface(beanKey, AopUtils.getTargetClass(managedBean)); ManagedNotification[] notificationAttributes = this.attributeSource.getManagedNotifications(intf); ModelMBeanNotificationInfo[] notificationInfos = new ModelMBeanNotificationInfo[notificationAttributes.length]; for (int i = 0; i < notificationAttributes.length; i++) { ManagedNotification attribute = notificationAttributes[i]; notificationInfos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(attribute); } return notificationInfos; }
public MBean(String className, String description, Collection<MBeanAttribute> attributes, Collection<MBeanOperation> operations) { List<MBeanAttributeInfo> attributeInfos = new ArrayList<MBeanAttributeInfo>(); Map<String, MBeanAttribute> attributesBuilder = new TreeMap<String, MBeanAttribute>(); for (MBeanAttribute attribute : attributes) { attributesBuilder.put(attribute.getName(), attribute); attributeInfos.add(attribute.getInfo()); } this.attributes = Collections.unmodifiableMap(attributesBuilder); Map<Signature, MBeanOperation> operationsBuilder = new HashMap<Signature, MBeanOperation>(); List<MBeanOperationInfo> operationsInfos = new ArrayList<MBeanOperationInfo>(); for (MBeanOperation operation : operations) { operationsBuilder.put(operation.getSignature(), operation); operationsInfos.add(operation.getInfo()); } this.operations = Collections.unmodifiableMap(operationsBuilder); mbeanInfo = new MBeanInfo(className, description, attributeInfos.toArray(new MBeanAttributeInfo[attributeInfos.size()]), new ModelMBeanConstructorInfo[0], operationsInfos.toArray(new MBeanOperationInfo[operationsInfos.size()]), new ModelMBeanNotificationInfo[0]); }
@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); } }
/** * Convert the supplied {@link ManagedNotification} into the corresponding * {@link javax.management.modelmbean.ModelMBeanNotificationInfo}. */ public static ModelMBeanNotificationInfo convertToModelMBeanNotificationInfo(ManagedNotification notificationInfo) { String name = notificationInfo.getName(); if (!StringUtils.hasText(name)) { throw new IllegalArgumentException("Must specify notification name"); } String[] notifTypes = notificationInfo.getNotificationTypes(); if (notifTypes == null || notifTypes.length == 0) { throw new IllegalArgumentException("Must specify at least one notification type"); } String description = notificationInfo.getDescription(); return new ModelMBeanNotificationInfo(notifTypes, name, description); }
/** * Reads the {@link ManagedNotification} metadata from the {@code Class} of the managed resource * and generates and returns the corresponding {@link ModelMBeanNotificationInfo} metadata. */ @Override protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) { ManagedNotification[] notificationAttributes = this.attributeSource.getManagedNotifications(getClassToExpose(managedBean)); ModelMBeanNotificationInfo[] notificationInfos = new ModelMBeanNotificationInfo[notificationAttributes.length]; for (int i = 0; i < notificationAttributes.length; i++) { ManagedNotification attribute = notificationAttributes[i]; notificationInfos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(attribute); } return notificationInfos; }
public void setNotificationInfos(ManagedNotification[] notificationInfos) { ModelMBeanNotificationInfo[] infos = new ModelMBeanNotificationInfo[notificationInfos.length]; for (int i = 0; i < notificationInfos.length; i++) { ManagedNotification notificationInfo = notificationInfos[i]; infos[i] = JmxMetadataUtils.convertToModelMBeanNotificationInfo(notificationInfo); } this.notificationInfos = infos; }
@Override protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) { ModelMBeanNotificationInfo[] result = null; if (StringUtils.hasText(beanKey)) { result = this.notificationInfoMappings.get(beanKey); } if (result == null) { result = this.notificationInfos; } return (result != null ? result : new ModelMBeanNotificationInfo[0]); }
private void extractMbeanNotifications(Object managedBean, Set<ModelMBeanNotificationInfo> mBeanNotifications) { ManagedNotifications notifications = managedBean.getClass().getAnnotation(ManagedNotifications.class); if (notifications != null) { for (ManagedNotification notification : notifications.value()) { ModelMBeanNotificationInfo info = new ModelMBeanNotificationInfo(notification.notificationTypes(), notification.name(), notification.description()); mBeanNotifications.add(info); LOGGER.trace("Assembled notification: {}", info); } } }
private void extractMbeanNotifications(Object managedBean, Set<ModelMBeanNotificationInfo> mBeanNotifications) { ManagedNotifications notifications = managedBean.getClass().getAnnotation(ManagedNotifications.class); if (notifications != null) { for (ManagedNotification notification : notifications.value()) { ModelMBeanNotificationInfo info = new ModelMBeanNotificationInfo(notification.notificationTypes(), notification.name(), notification.description()); mBeanNotifications.add(info); LOG.trace("Assembled notification: {}", info); } } }
/** * Do 1-6 steps. */ private ModelMBeanInfoSupport constractModelMBeanInfoSupport() throws Exception { ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo( "description", class1.getMethod("simpleOperartion", null)); setDescriptor(operationInfo); ModelMBeanConstructorInfo constructorInfo = new ModelMBeanConstructorInfo( "description", class1.getConstructor(null)); setDescriptor(constructorInfo); ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo( "name", "description", class1.getMethod("getH", null), class1 .getMethod("setH", new Class[] { int.class })); setDescriptor(attributeInfo); ModelMBeanNotificationInfo notificationInfo = new ModelMBeanNotificationInfo( new String[] { "specific notification tepes" }, "name", "description"); setDescriptor(notificationInfo); ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", new ModelMBeanAttributeInfo[] { attributeInfo }, new ModelMBeanConstructorInfo[] { constructorInfo }, new ModelMBeanOperationInfo[] { operationInfo }, new ModelMBeanNotificationInfo[] { notificationInfo }); Descriptor descriptor = beanInfoSupport.getMBeanDescriptor(); String[] strings = getSpesific(beanInfoSupport.getClass()); descriptor.setField(strings[0], strings[1]); map.put(beanInfoSupport.getClass().getName(), descriptor); beanInfoSupport.setMBeanDescriptor(descriptor); return beanInfoSupport; }
/** * <ul> * Verify that logs of new notifications, when sendNotification is invoked, * write to file. File name is value of descriptor of * ModelMBeanNotificationInfo. * <li>Create java class, which is not MBean. MBean has 1 getter and 1 * setter methods. * <li>Create ModelMBeanNotificationInfo object for my type with descriptor * with logging. * <li>Create ModelMBeanInfoSupport object. All ModelMBeanXXXInfo except * ModelMBeanNotificationInfo are default. * <li>Create RequiredModelMBean object. * <li>Instance of java class in 1st step sets managed resource for * RequiredModelMBean using setManagedResource method. * <li>Send my notification using sendNotification method. * <li>Verify that logfile was created and size of file > 0. * </ul> */ public Result testLogging() throws Exception { ModelMBeanAttributeInfo attributeInfoForG = new ModelMBeanAttributeInfo( "g", "descr", class1.getMethod("getG", null), class1.getMethod( "setG", new Class[] { String.class })); ModelMBeanAttributeInfo[] attributeInfos = new ModelMBeanAttributeInfo[] { attributeInfoForG }; ModelMBeanNotificationInfo notificationInfo = new ModelMBeanNotificationInfo( new String[] { SimpleNotification.notificationType }, SimpleNotification.notificationType, "description"); File file = File.createTempFile("log", ".txt"); file.deleteOnExit(); Descriptor descriptor = notificationInfo.getDescriptor(); descriptor.setField("log", "true"); descriptor.setField("logfile", file.getAbsolutePath()); log.info("file name: " + file.getAbsolutePath()); notificationInfo.setDescriptor(descriptor); ModelMBeanInfoSupport beanInfoSupport = new ModelMBeanInfoSupport( class1.getName(), "description", attributeInfos, null, null, new ModelMBeanNotificationInfo[] { notificationInfo }); beanInfoSupport.getNotification(new SimpleNotification("src", 1) .getType()); RequiredModelMBean requiredModelMBean = new RequiredModelMBean( beanInfoSupport); beanInfoSupport.getDescriptor(new SimpleNotification("src", 1) .getType(), "notification"); requiredModelMBean.sendNotification(new SimpleNotification("src", 1)); assertTrue(file.length() > 0); file.delete(); return result(); }
/** * Verify default fields of descriptor from ModelMBeanNotificationInfo: * name=nameUsedInConstructor, displayName=nameUsedInConstructor, severity=6 * and descriptorType=notification. */ public Result testModelMBeanNotificationInfo() throws Exception { final String name = "notification name"; ModelMBeanNotificationInfo notificationInfo = new ModelMBeanNotificationInfo( null, name, "description"); descriptor = notificationInfo.getDescriptor(); assertEquals(descriptor.getFieldValue("name"), name); assertEquals(descriptor.getFieldValue("displayName"), name); assertEquals(descriptor.getFieldValue("severity"), "6"); assertEquals(descriptor.getFieldValue("descriptorType"), "notification"); assertEquals(descriptor.getFields().length, 4); commonCheck(); return result(); }
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())) { LOGGER.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<>(); Set<ManagedOperationInfo> operations = new LinkedHashSet<>(); Set<ModelMBeanAttributeInfo> mBeanAttributes = new LinkedHashSet<>(); Set<ModelMBeanOperationInfo> mBeanOperations = new LinkedHashSet<>(); Set<ModelMBeanNotificationInfo> mBeanNotifications = new LinkedHashSet<>(); // 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); LOGGER.trace("Created ModelMBeanInfo {}", info); return info; }
/** * Gets the {@link ModelMBeanInfo} for the given managed bean * * @param defaultManagedBean the default managed bean * @param customManagedBean an optional custom managed bean * @param objectName the object name * @return the model info, or <tt>null</tt> if not possible to create, for example due the managed bean is a proxy class * @throws JMException is thrown if error creating the model info */ public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object customManagedBean, String objectName) throws JMException { // 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; }
protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) { managedBean = AspectUtil.exposeRootBean(managedBean); return super.getNotificationInfo(managedBean, beanKey); }
/** * Get the notification metadata for the MBean resource. Subclasses should implement * this method to return the appropriate metadata for all notifications that should * be exposed in the management interface for the managed resource. * <p>Default implementation returns an empty array of {@code ModelMBeanNotificationInfo}. * @param managedBean the bean instance (might be an AOP proxy) * @param beanKey the key associated with the MBean in the beans map * of the {@code MBeanExporter} * @return the notification metadata * @throws JMException in case of errors */ protected ModelMBeanNotificationInfo[] getNotificationInfo(Object managedBean, String beanKey) throws JMException { return new ModelMBeanNotificationInfo[0]; }