001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.support;
010:
011: import javax.management.Attribute;
012: import javax.management.AttributeList;
013: import javax.management.AttributeNotFoundException;
014: import javax.management.DynamicMBean;
015: import javax.management.InvalidAttributeValueException;
016: import javax.management.MBeanAttributeInfo;
017: import javax.management.MBeanConstructorInfo;
018: import javax.management.MBeanException;
019: import javax.management.MBeanInfo;
020: import javax.management.MBeanNotificationInfo;
021: import javax.management.MBeanOperationInfo;
022: import javax.management.MBeanParameterInfo;
023: import javax.management.ReflectionException;
024:
025: /**
026: * @version $Revision: 1.4 $
027: */
028: public class MBeanDynamic implements DynamicMBean {
029: private String m_value1 = "";
030: private String m_value2 = "";
031:
032: public MBeanInfo getMBeanInfo() {
033: MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[2];
034: attrs[0] = new MBeanAttributeInfo("DynamicAttribute1",
035: "java.lang.String", "A first dynamic attribute", true,
036: true, false);
037: attrs[1] = new MBeanAttributeInfo("DynamicAttribute2",
038: "java.lang.String", "A second dynamic attribute", true,
039: true, false);
040:
041: MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[1];
042: ctors[0] = new MBeanConstructorInfo("ParameterlessConstructor",
043: "A dynamic constructor", new MBeanParameterInfo[0]);
044:
045: MBeanOperationInfo[] opers = new MBeanOperationInfo[1];
046: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
047: params[0] = new MBeanParameterInfo("supposedAttributeValue",
048: "java.lang.String",
049: "Checks if the value of the argument is equal to the value of the attribute");
050: opers[0] = new MBeanOperationInfo("dynamicOperation",
051: "A dynamic operation", params, "boolean",
052: MBeanOperationInfo.INFO);
053:
054: MBeanNotificationInfo[] notifs = new MBeanNotificationInfo[0];
055:
056: return new MBeanInfo(getClass().getName(),
057: "A MBeanDynamic MBean", attrs, ctors, opers, notifs);
058: }
059:
060: private String getDynamicAttribute1() {
061: return m_value1;
062: }
063:
064: private void setDynamicAttribute1(String value) {
065: m_value1 = value;
066: }
067:
068: private String getDynamicAttribute2() {
069: return m_value2;
070: }
071:
072: private void setDynamicAttribute2(String value) {
073: m_value2 = value;
074: }
075:
076: private boolean dynamicOperation(String value) {
077: return m_value1.equals(value);
078: }
079:
080: public Object getAttribute(String attribute)
081: throws AttributeNotFoundException, MBeanException,
082: ReflectionException {
083: if (attribute.equals("DynamicAttribute1")) {
084: return getDynamicAttribute1();
085: } else if (attribute.equals("DynamicAttribute2")) {
086: return getDynamicAttribute2();
087: } else
088: throw new AttributeNotFoundException(attribute);
089: }
090:
091: public void setAttribute(Attribute attribute)
092: throws AttributeNotFoundException,
093: InvalidAttributeValueException, MBeanException,
094: ReflectionException {
095: if (attribute.getName().equals("DynamicAttribute1")) {
096: Object val = attribute.getValue();
097: if (val instanceof String) {
098: setDynamicAttribute1((String) val);
099: } else {
100: throw new InvalidAttributeValueException(
101: val == null ? "null" : val.toString());
102: }
103: } else if (attribute.getName().equals("DynamicAttribute2")) {
104: Object val = attribute.getValue();
105: if (val instanceof String) {
106: setDynamicAttribute2((String) val);
107: } else {
108: throw new InvalidAttributeValueException(
109: val == null ? "null" : val.toString());
110: }
111: } else {
112: throw new AttributeNotFoundException(attribute.getName());
113: }
114: }
115:
116: public AttributeList getAttributes(String[] attributes) {
117: AttributeList list = new AttributeList();
118: for (int i = 0; i < attributes.length; ++i) {
119: if (attributes[i].equals("DynamicAttribute1")) {
120: list.add(new Attribute(attributes[i],
121: getDynamicAttribute1()));
122: } else if (attributes[i].equals("DynamicAttribute2")) {
123: list.add(new Attribute(attributes[i],
124: getDynamicAttribute2()));
125: }
126: }
127: return list;
128: }
129:
130: public AttributeList setAttributes(AttributeList attributes) {
131: AttributeList list = new AttributeList();
132: for (int i = 0; i < attributes.size(); ++i) {
133: Attribute attr = (Attribute) attributes.get(i);
134: if (attr.getName().equals("DynamicAttribute1")
135: || attr.getName().equals("DynamicAttribute2")) {
136: try {
137: setAttribute(attr);
138: list.add(attr);
139: } catch (AttributeNotFoundException ignored) {
140: } catch (InvalidAttributeValueException ignored) {
141: } catch (MBeanException ignored) {
142: } catch (ReflectionException ignored) {
143: }
144: }
145: }
146: return list;
147: }
148:
149: public Object invoke(String method, Object[] arguments,
150: String[] params) throws MBeanException, ReflectionException {
151: if (method.equals("dynamicOperation") && params.length == 1
152: && params[0].equals("java.lang.String")
153: && arguments.length == 1
154: && arguments[0] instanceof String) {
155: boolean match = dynamicOperation((String) arguments[0]);
156: return new Boolean(match);
157: } else {
158: throw new MBeanException(new IllegalArgumentException(
159: "Invalid method or arguments for invoke"));
160: }
161: }
162: }
|