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 java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.List;
014: import javax.management.Attribute;
015: import javax.management.AttributeList;
016: import javax.management.AttributeNotFoundException;
017: import javax.management.DynamicMBean;
018: import javax.management.InvalidAttributeValueException;
019: import javax.management.MBeanAttributeInfo;
020: import javax.management.MBeanConstructorInfo;
021: import javax.management.MBeanException;
022: import javax.management.MBeanInfo;
023: import javax.management.MBeanNotificationInfo;
024: import javax.management.MBeanOperationInfo;
025: import javax.management.ReflectionException;
026:
027: /**
028: * @version $Revision: 1.5 $
029: */
030: public class QuerySupport {
031: public interface TestMBean {
032: public Integer getNumber();
033:
034: public String getStr();
035:
036: public Boolean getBoolean();
037: }
038:
039: public static class Test implements TestMBean {
040: private Integer n;
041: private String str;
042: private Boolean b;
043:
044: public Test(String str, Integer n, Boolean b) {
045: this .str = str;
046: this .n = n;
047: this .b = b;
048: }
049:
050: public Integer getNumber() {
051: return n;
052: }
053:
054: public String getStr() {
055: return str;
056: }
057:
058: public Boolean getBoolean() {
059: return b;
060: }
061: }
062:
063: public static class DynamicTest implements DynamicMBean {
064: private Boolean boolval;
065: private long numval;
066: private String strval;
067:
068: public DynamicTest(String s, long n, Boolean b) {
069: this .boolval = b;
070: this .numval = n;
071: this .strval = s;
072: }
073:
074: public Boolean getBoolean() {
075: return this .boolval;
076: }
077:
078: public long getNumber() {
079: return this .numval;
080: }
081:
082: public void setNumber(long value) {
083: this .numval = value;
084: }
085:
086: public String getStr() {
087: throw new RuntimeException("Don't call me!");
088: }
089:
090: public Object getAttribute(String attribute)
091: throws AttributeNotFoundException, MBeanException,
092: ReflectionException {
093: Object result;
094: if (attribute.compareTo("Boolean") == 0) {
095: result = getBoolean();
096: } else if (attribute.compareTo("Number") == 0) {
097: result = new Long(getNumber());
098: } else if (attribute.compareTo("Str") == 0) {
099: result = getStr();
100: } else {
101: throw new AttributeNotFoundException("Can't find "
102: + attribute);
103: }
104: return result;
105: }
106:
107: public AttributeList getAttributes(String[] attributes) {
108: List attrnames = new ArrayList();
109: MBeanAttributeInfo[] attrs = getMBeanInfo().getAttributes();
110: for (int i = 0; i < attrs.length; i++) {
111: attrnames.add(attrs[i].getName());
112: }
113: AttributeList result = new AttributeList();
114: for (int i = 0; i < attributes.length; i++) {
115: if (attrnames.contains(attributes[i])) {
116: try {
117: Attribute attr = new Attribute(attributes[i],
118: getAttribute(attributes[i]));
119: result.add(attr);
120: } catch (AttributeNotFoundException e) {
121: // Don't add this attribute
122: } catch (MBeanException e) {
123: // Don't add this attribute
124: } catch (ReflectionException e) {
125: // Don't add this attribute
126: }
127: }
128: }
129: return result;
130: }
131:
132: public MBeanInfo getMBeanInfo() {
133: MBeanInfo result;
134: MBeanAttributeInfo[] attrs;
135: try {
136: attrs = new MBeanAttributeInfo[] {
137: new MBeanAttributeInfo("Number", "A number",
138: DynamicTest.class.getMethod(
139: "getNumber", new Class[0]),
140: DynamicTest.class.getMethod(
141: "setNumber",
142: new Class[] { long.class })),
143: new MBeanAttributeInfo("Str", "A string",
144: DynamicTest.class.getMethod("getStr",
145: new Class[0]), null),
146: new MBeanAttributeInfo("Boolean", "A Boolean",
147: DynamicTest.class.getMethod(
148: "getBoolean", new Class[0]),
149: null) };
150: } catch (Exception x) {
151: attrs = new MBeanAttributeInfo[0];
152: }
153: MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[0];
154: MBeanOperationInfo[] ops = new MBeanOperationInfo[0];
155: MBeanNotificationInfo[] notifs = new MBeanNotificationInfo[0];
156: result = new MBeanInfo(DynamicTest.class.getName(),
157: "DynamicTest MBean", attrs, ctors, ops, notifs);
158: return result;
159: }
160:
161: public Object invoke(String method, Object[] arguments,
162: String[] params) throws MBeanException,
163: ReflectionException {
164: return null;
165: }
166:
167: public void setAttribute(Attribute attribute)
168: throws AttributeNotFoundException,
169: InvalidAttributeValueException, MBeanException,
170: ReflectionException {
171: if (attribute.getName().compareTo("Number") == 0) {
172: setNumber(((Long) attribute.getValue()).longValue());
173: } else {
174: throw new AttributeNotFoundException("Can't find "
175: + attribute.getName());
176: }
177: }
178:
179: public AttributeList setAttributes(AttributeList attributes) {
180: AttributeList result = new AttributeList();
181: Iterator i = attributes.iterator();
182: while (i.hasNext()) {
183: try {
184: Attribute attr = (Attribute) i.next();
185: setAttribute(attr);
186: result.add(attr);
187: } catch (Exception x) {
188: // Don't add this to the result
189: }
190: }
191: return result;
192: }
193:
194: }
195: }
|