List of usage examples for javax.management.modelmbean ModelMBeanAttributeInfo getDescriptor
public Descriptor getDescriptor()
From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java
private static ModelMBeanAttributeInfo[] extractAttributeInfo(Object object) { Map<String, Method> getters = new HashMap<String, Method>(); Map<String, Method> setters = new HashMap<String, Method>(); Map<String, String> descriptions = new HashMap<String, String>(); for (Method m : object.getClass().getMethods()) { JmxGetter getter = m.getAnnotation(JmxGetter.class); if (getter != null) { getters.put(getter.name(), m); descriptions.put(getter.name(), getter.description()); }//from www .ja v a 2s . c om JmxSetter setter = m.getAnnotation(JmxSetter.class); if (setter != null) { setters.put(setter.name(), m); descriptions.put(setter.name(), setter.description()); } } Set<String> attributes = new HashSet<String>(getters.keySet()); attributes.addAll(setters.keySet()); List<ModelMBeanAttributeInfo> infos = new ArrayList<ModelMBeanAttributeInfo>(); for (String name : attributes) { try { Method getter = getters.get(name); Method setter = setters.get(name); ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(name, descriptions.get(name), getter, setter); Descriptor descriptor = info.getDescriptor(); if (getter != null) descriptor.setField("getMethod", getter.getName()); if (setter != null) descriptor.setField("setMethod", setter.getName()); info.setDescriptor(descriptor); infos.add(info); } catch (IntrospectionException e) { throw new RuntimeException(e); } } return infos.toArray(new ModelMBeanAttributeInfo[infos.size()]); }
From source file:org.eclipse.smila.management.jmx.AgentMBean.java
/** * {@inheritDoc}/*from ww w . java 2 s . c o m*/ * * @see javax.management.DynamicMBean#getAttribute(java.lang.String) */ public Object getAttribute(final String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException { final ModelMBeanAttributeInfo info = _beanInfo.getAttribute(attribute); final Descriptor descriptor = info.getDescriptor(); final String getterMethod = (String) (descriptor.getFieldValue("getMethod")); return invoke(getterMethod, ClassHelper.EMPTY_PARAMS, ClassHelper.EMPTY_SIGNATURE); }
From source file:org.eclipse.smila.management.jmx.AgentMBean.java
/** * {@inheritDoc}/*from ww w .j av a2 s .co m*/ * * @see javax.management.DynamicMBean#setAttribute(javax.management.Attribute) */ public void setAttribute(final Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { final ModelMBeanAttributeInfo info = _beanInfo.getAttribute(attribute.getName()); if (info == null) { final String msg = "MBean " + _beanInfo.getClassName() + " doesnt contain attribute Information for method " + attribute.getName() + ", please check if you have defined a gettter and setter for this Attriubute!"; _log.error(msg); throw new AttributeNotFoundException(msg); } final Descriptor descriptor = info.getDescriptor(); final String setterMethod = (String) (descriptor.getFieldValue("setMethod")); final String setterSignatureClass = (String) (descriptor.getFieldValue("setterSignatureClass")); invoke(setterMethod, new Object[] { attribute.getValue() }, new String[] { setterSignatureClass }); }
From source file:org.hyperic.hq.product.jmx.ServiceTypeFactory.java
private MeasurementInfo createMeasurementInfo(final ServiceType serviceType, final ProductPlugin productPlugin, ModelMBeanAttributeInfo attribute) { Properties measurementProperties = new Properties(); Descriptor descriptor = attribute.getDescriptor(); String units = (String) descriptor.getFieldValue(MeasurementInfo.ATTR_UNITS); if ("s".equals(units)) { units = MeasurementConstants.UNITS_SECONDS; }/*from w w w . ja va 2 s .c o m*/ if (!VALID_UNITS.contains(units)) { measurementProperties.put(MeasurementInfo.ATTR_UNITS, "none"); } else { measurementProperties.put(MeasurementInfo.ATTR_UNITS, units); } final String displayName = (String) descriptor.getFieldValue("displayName"); // Not likely to be null, as JMX impl currently populates it with // attribute name if not set if (displayName == null) { measurementProperties.put(MeasurementInfo.ATTR_NAME, attribute.getName()); } else { measurementProperties.put(MeasurementInfo.ATTR_NAME, displayName); } measurementProperties.put(MeasurementInfo.ATTR_ALIAS, attribute.getName()); String metricType = (String) descriptor.getFieldValue("metricType"); if (metricType == null || !("COUNTER".equals(metricType.toUpperCase()))) { //GAUGE measurementProperties.put(MeasurementInfo.ATTR_COLLECTION_TYPE, "dynamic"); measurementProperties.put(MeasurementInfo.ATTR_INTERVAL, "300000"); } else { //COUNTER measurementProperties.put(MeasurementInfo.ATTR_COLLECTION_TYPE, "trendsup"); String rate = (String) descriptor.getFieldValue("rate"); if (rate != null) { measurementProperties.put(MeasurementInfo.ATTR_RATE, rate); } else { measurementProperties.put(MeasurementInfo.ATTR_RATE, "none"); } measurementProperties.put(MeasurementInfo.ATTR_INTERVAL, "600000"); } String collectionInterval = (String) descriptor.getFieldValue("collectionInterval"); if (collectionInterval != null) { try { Long.valueOf(collectionInterval); measurementProperties.put(MeasurementInfo.ATTR_INTERVAL, collectionInterval); } catch (NumberFormatException e) { log.warn("Specified collection interval " + collectionInterval + " is not numeric. Default value will be used instead."); } } String category = (String) descriptor.getFieldValue("metricCategory"); if (category == null || !VALID_CATEGORIES.contains(category.toUpperCase())) { measurementProperties.put(MeasurementInfo.ATTR_CATEGORY, MeasurementConstants.CAT_UTILIZATION); } else { measurementProperties.put(MeasurementInfo.ATTR_CATEGORY, category.toUpperCase()); } String indicator = (String) descriptor.getFieldValue("indicator"); if (indicator == null || "true".equals(indicator.toLowerCase())) { //indicator is not in Spring 3.0 @ManagedMetric. Turn measurement on and make indicator by default measurementProperties.put(MeasurementInfo.ATTR_INDICATOR, "true"); measurementProperties.put(MeasurementInfo.ATTR_DEFAULTON, "true"); } else { measurementProperties.put(MeasurementInfo.ATTR_INDICATOR, "false"); measurementProperties.put(MeasurementInfo.ATTR_DEFAULTON, "false"); } String defaultOn = (String) descriptor.getFieldValue("defaultOn"); if (defaultOn != null) { if ("true".equals(defaultOn.toLowerCase()) || "false".equals(defaultOn.toLowerCase())) { measurementProperties.put(MeasurementInfo.ATTR_DEFAULTON, defaultOn.toLowerCase()); } else { log.warn("Invalid value of " + defaultOn + " specified for defaultOn. Default value will be used instead."); } } addMeasurementTemplate(measurementProperties, productPlugin, serviceType); return createMeasurementInfo(measurementProperties); }