001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx;
009:
010: import javax.management.DynamicMBean;
011: import javax.management.ObjectName;
012: import javax.management.MBeanInfo;
013: import javax.management.MBeanAttributeInfo;
014: import javax.management.AttributeNotFoundException;
015: import javax.management.MBeanOperationInfo;
016: import javax.management.MBeanException;
017: import javax.management.AttributeList;
018: import javax.management.Attribute;
019: import javax.management.ReflectionException;
020: import javax.management.InvalidAttributeValueException;
021: import javax.management.JMRuntimeException;
022:
023: /**
024: *
025: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
026: */
027:
028: public class DynamicMBeanMetaDataSupport implements MBeanMetaData {
029: private DynamicMBean mbeanObject = null;
030: private ObjectName objectName = null;
031:
032: // private MBeanInfo mbeanInfo = null;
033:
034: public DynamicMBeanMetaDataSupport(ObjectName objectName,
035: DynamicMBean obj) {
036: this .objectName = objectName;
037: mbeanObject = obj;
038: // mbeanInfo = mbeanObject.getMBeanInfo();
039: }
040:
041: public MBeanAttributeInfo[] getAttributes() {
042: return getMBeanInfo().getAttributes();
043: }
044:
045: public MBeanAttributeInfo getAttributeInfo(String attribute) {
046: MBeanAttributeInfo[] attributes = getAttributes();
047: MBeanAttributeInfo attributeInfo = null;
048: for (int i = 0; i < attributes.length; i++) {
049: MBeanAttributeInfo info = attributes[i];
050: if (info.getName().equals(attribute)) {
051: attributeInfo = info;
052: break;
053: }
054: }
055: // if(attributeInfo == null) throw new AttributeNotFoundException(attribute);
056: return attributeInfo;
057:
058: }
059:
060: public MBeanOperationInfo[] getOperations() {
061: return getMBeanInfo().getOperations();
062: }
063:
064: // not cache the mbeanInfo, so can change on runtime
065: public MBeanInfo getMBeanInfo() {
066: return mbeanObject.getMBeanInfo();
067: }
068:
069: public ClassLoader getClassLoader() {
070: return mbeanObject.getClass().getClassLoader();
071: }
072:
073: public String getClassName() {
074: return mbeanObject.getClass().getName();
075: }
076:
077: public ObjectName getObjectName() {
078: return objectName;
079: }
080:
081: public void setObjectName(ObjectName name) {
082: this .objectName = name;
083: }
084:
085: public ExtendedObjectInstance getObjectInstance() {
086: return new ExtendedObjectInstance(objectName, mbeanObject
087: .getClass(), this .getInterfaceClass());
088: }
089:
090: public Object getMBeanObject() {
091: return mbeanObject;
092: }
093:
094: public boolean isDynamic() {
095: return true;
096: }
097:
098: public String getInterfaceClassName() {
099: return DynamicMBean.class.getName();
100: }
101:
102: public Class getInterfaceClass() {
103: return DynamicMBean.class;
104: }
105:
106: public Object getAttribute(String attribute) throws MBeanException,
107: AttributeNotFoundException, ReflectionException {
108:
109: MBeanAttributeInfo attributeInfo = getAttributeInfo(attribute);
110: if (attributeInfo == null)
111: throw new AttributeNotFoundException(attribute);
112: if (!attributeInfo.isReadable()) {
113: throw new MBeanException(new RuntimeException(),
114: "The attribte " + attribute + " is not readable");
115: }
116:
117: return mbeanObject.getAttribute(attribute);
118: }
119:
120: public AttributeList getAttributes(String[] attributes)
121: throws MBeanException, AttributeNotFoundException,
122: ReflectionException {
123: return mbeanObject.getAttributes(attributes);
124: }
125:
126: public void setAttribute(Attribute attribute)
127: throws AttributeNotFoundException,
128: InvalidAttributeValueException, MBeanException,
129: ReflectionException {
130: String attributeName = attribute.getName();
131: MBeanAttributeInfo attributeInfo = getAttributeInfo(attributeName);
132: if (attributeInfo == null)
133: throw new AttributeNotFoundException(attributeName);
134: if (!attributeInfo.isWritable()) {
135: throw new AttributeNotFoundException(attribute.getName()
136: + " is not writeable");
137: }
138:
139: mbeanObject.setAttribute(attribute);
140: }
141:
142: public AttributeList setAttributes(AttributeList attributes)
143: throws AttributeNotFoundException,
144: InvalidAttributeValueException, MBeanException,
145: ReflectionException {
146: return mbeanObject.setAttributes(attributes);
147: }
148:
149: /*
150: * Note: Getters and setters cannot be invoked through the invoke method, since JMX 1.2
151: */
152: public Object invoke(String operationName, Object params[],
153: String[] signatures) throws MBeanException,
154: ReflectionException {
155: // check for use invoke to get/set attribute
156: if (isAttributeMethod(operationName, signatures)) {
157: // System.out.println("is Attribute,break;");
158: throw new JMRuntimeException(
159: operationName
160: + " is attribute getter/setter , use MBeanConnection.getAttribute or MBeanConnection.setAttribute instead.");
161: }
162: return mbeanObject.invoke(operationName, params, signatures);
163: }
164:
165: private boolean isAttributeMethod(String operationName,
166: String[] signatures) {
167: boolean isAttribute = false;
168: if (MethodHelper.maybeGetterMethod(operationName, signatures)
169: || MethodHelper.maybeSetterMethod(operationName,
170: signatures)) {
171: String attribute = "";
172: if (operationName.startsWith("is")) {
173: attribute = operationName.substring(2);
174: } else {
175: attribute = operationName.substring(3);
176: }
177: if (this .getAttributeInfo(attribute) != null) {
178: isAttribute = true;
179: }
180: }
181: return isAttribute;
182: }
183:
184: }
|