From a5c21dc130787519031fe23202fb716e8ce637b7 Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Wed, 9 Mar 2011 11:57:18 -0800 Subject: [PATCH] 1) make JMX port configuration in deverloper build, 2) Add property map based dynamic bean --- build/developer.xml | 10 +- .../utils/mgmt/PropertyMapDynamicBean.java | 111 ++++++++++++++++++ 2 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 utils/src/com/cloud/utils/mgmt/PropertyMapDynamicBean.java diff --git a/build/developer.xml b/build/developer.xml index 89d08d5b7e5..17a5b32fe66 100755 --- a/build/developer.xml +++ b/build/developer.xml @@ -20,6 +20,10 @@ + + + + @@ -69,7 +73,7 @@ - + @@ -90,7 +94,7 @@ - + @@ -103,7 +107,7 @@ - + diff --git a/utils/src/com/cloud/utils/mgmt/PropertyMapDynamicBean.java b/utils/src/com/cloud/utils/mgmt/PropertyMapDynamicBean.java new file mode 100644 index 00000000000..973fa716433 --- /dev/null +++ b/utils/src/com/cloud/utils/mgmt/PropertyMapDynamicBean.java @@ -0,0 +1,111 @@ +package com.cloud.utils.mgmt; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.management.Attribute; +import javax.management.AttributeList; +import javax.management.AttributeNotFoundException; +import javax.management.DynamicMBean; +import javax.management.InvalidAttributeValueException; +import javax.management.MBeanAttributeInfo; +import javax.management.MBeanException; +import javax.management.MBeanInfo; +import javax.management.ReflectionException; + +public class PropertyMapDynamicBean implements DynamicMBean { + + private Map _propMap = new HashMap(); + + public PropertyMapDynamicBean() { + } + + public PropertyMapDynamicBean(Map propMap) { + _propMap = propMap; + } + + @Override + public synchronized Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException { + if(_propMap != null) { + return _propMap.get(name); + } + + throw new AttributeNotFoundException("No such property " + name); + } + + @Override + public synchronized AttributeList getAttributes(String[] names) { + AttributeList list = new AttributeList(); + for (String name : names) { + Object value = _propMap.get(name); + if (value != null) + list.add(new Attribute(name, value)); + } + return list; + } + + @Override + public synchronized MBeanInfo getMBeanInfo() { + SortedSet names = new TreeSet(); + + for (String name : _propMap.keySet()) + names.add(name); + + MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[names.size()]; + Iterator it = names.iterator(); + for (int i = 0; i < attrs.length; i++) { + String name = it.next(); + attrs[i] = new MBeanAttributeInfo( + name, + "java.lang.String", + name, + true, // isReadable + true, // isWritable + false); // isIs + } + + return new MBeanInfo( + this.getClass().getName(), + "Dynamic MBean", + attrs, + null, + null, + null); + } + + @Override + public synchronized Object invoke(String name, Object[] args, String[] sig) throws MBeanException, ReflectionException { + throw new ReflectionException(new NoSuchMethodException(name)); + } + + @Override + public synchronized void setAttribute(Attribute attr) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { + String name = attr.getName(); + if(name != null) + _propMap.put(name, attr.getValue()); + } + + @Override + public synchronized AttributeList setAttributes(AttributeList list) { + Attribute[] attrs = (Attribute[]) list.toArray(new Attribute[0]); + AttributeList retList = new AttributeList(); + for (Attribute attr : attrs) { + String name = attr.getName(); + Object value = attr.getValue(); + _propMap.put(name, value); + retList.add(new Attribute(name, value)); + } + return retList; + } + + public synchronized void addProp(String name, Object value) { + _propMap.put(name, value); + } + + public synchronized Object getProp(String name) { + return _propMap.get(name); + } +}