001: /*
002:
003: * Copyright (C) The MX4J Contributors.
004:
005: * All rights reserved.
006:
007: *
008:
009: * This software is distributed under the terms of the MX4J License version 1.0.
010:
011: * See the terms of the MX4J License in the documentation provided with this software.
012:
013: */
014:
015: package test.javax.management.support;
016:
017: import javax.management.Attribute;
018: import javax.management.AttributeList;
019: import javax.management.AttributeNotFoundException;
020: import javax.management.DynamicMBean;
021: import javax.management.InvalidAttributeValueException;
022: import javax.management.MBeanAttributeInfo;
023: import javax.management.MBeanConstructorInfo;
024: import javax.management.MBeanException;
025: import javax.management.MBeanInfo;
026: import javax.management.MBeanNotificationInfo;
027: import javax.management.MBeanOperationInfo;
028: import javax.management.MBeanParameterInfo;
029: import javax.management.ReflectionException;
030:
031: /**
032: * @version $Revision: 1.3 $
033: */
034:
035: class PrivateMBeanDynamic implements DynamicMBean
036:
037: {
038:
039: private String m_value1 = "";
040:
041: private String m_value2 = "";
042:
043: public MBeanInfo getMBeanInfo()
044:
045: {
046:
047: MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[2];
048:
049: attrs[0] = new MBeanAttributeInfo("DynamicAttribute1",
050: "java.lang.String", "A first dynamic attribute", true,
051: true, false);
052:
053: attrs[1] = new MBeanAttributeInfo("DynamicAttribute2",
054: "java.lang.String", "A second dynamic attribute", true,
055: true, false);
056:
057: MBeanConstructorInfo[] ctors = new MBeanConstructorInfo[1];
058:
059: ctors[0] = new MBeanConstructorInfo(
060: "Parameterless Constructor", "A dynamic constructor",
061: new MBeanParameterInfo[0]);
062:
063: MBeanOperationInfo[] opers = new MBeanOperationInfo[1];
064:
065: MBeanParameterInfo[] params = new MBeanParameterInfo[1];
066:
067: params[0] = new MBeanParameterInfo("supposedAttributeValue",
068: "java.lang.String",
069: "Checks if the value of the argument is equal to the value of the attribute");
070:
071: opers[0] = new MBeanOperationInfo("dynamicOperation",
072: "A dynamic operation", params, "boolean",
073: MBeanOperationInfo.INFO);
074:
075: MBeanNotificationInfo[] notifs = new MBeanNotificationInfo[0];
076:
077: return new MBeanInfo(getClass().getName(),
078: "A MBeanDynamic MBean", attrs, ctors, opers, notifs);
079:
080: }
081:
082: private String getDynamicAttribute1() {
083: return m_value1;
084: }
085:
086: private void setDynamicAttribute1(String value) {
087: m_value1 = value;
088: }
089:
090: private String getDynamicAttribute2() {
091: return m_value2;
092: }
093:
094: private void setDynamicAttribute2(String value) {
095: m_value2 = value;
096: }
097:
098: private boolean dynamicOperation(String value) {
099: return m_value1.equals(value);
100: }
101:
102: public Object getAttribute(String attribute)
103: throws AttributeNotFoundException, MBeanException,
104: ReflectionException
105:
106: {
107:
108: if (attribute.equals("DynamicAttribute1")) {
109: return getDynamicAttribute1();
110: }
111:
112: else if (attribute.equals("DynamicAttribute2")) {
113: return getDynamicAttribute2();
114: }
115:
116: else
117: throw new AttributeNotFoundException(attribute);
118:
119: }
120:
121: public void setAttribute(Attribute attribute)
122: throws AttributeNotFoundException,
123: InvalidAttributeValueException, MBeanException,
124: ReflectionException
125:
126: {
127:
128: if (attribute.getName().equals("DynamicAttribute1"))
129:
130: {
131:
132: Object val = attribute.getValue();
133:
134: if (val instanceof String) {
135: setDynamicAttribute1((String) val);
136: }
137:
138: else {
139: throw new InvalidAttributeValueException(
140: val == null ? "null" : val.toString());
141: }
142:
143: }
144:
145: else if (attribute.getName().equals("DynamicAttribute2"))
146:
147: {
148:
149: Object val = attribute.getValue();
150:
151: if (val instanceof String) {
152: setDynamicAttribute2((String) val);
153: }
154:
155: else {
156: throw new InvalidAttributeValueException(
157: val == null ? "null" : val.toString());
158: }
159:
160: }
161:
162: else {
163: throw new AttributeNotFoundException(attribute.getName());
164: }
165:
166: }
167:
168: public AttributeList getAttributes(String[] attributes)
169:
170: {
171:
172: AttributeList list = new AttributeList();
173:
174: for (int i = 0; i < attributes.length; ++i)
175:
176: {
177:
178: if (attributes[i].equals("DynamicAttribute1"))
179:
180: {
181:
182: list.add(new Attribute(attributes[i],
183: getDynamicAttribute1()));
184:
185: }
186:
187: else if (attributes[i].equals("DynamicAttribute2"))
188:
189: {
190:
191: list.add(new Attribute(attributes[i],
192: getDynamicAttribute2()));
193:
194: }
195:
196: }
197:
198: return list;
199:
200: }
201:
202: public AttributeList setAttributes(AttributeList attributes)
203:
204: {
205:
206: AttributeList list = new AttributeList();
207:
208: for (int i = 0; i < attributes.size(); ++i)
209:
210: {
211:
212: Attribute attr = (Attribute) attributes.get(i);
213:
214: if (attr.getName().equals("DynamicAttribute1")
215: || attr.getName().equals("DynamicAttribute2"))
216:
217: {
218:
219: try
220:
221: {
222:
223: setAttribute(attr);
224:
225: list.add(attr);
226:
227: }
228:
229: catch (AttributeNotFoundException ignored) {
230: }
231:
232: catch (InvalidAttributeValueException ignored) {
233: }
234:
235: catch (MBeanException ignored) {
236: }
237:
238: catch (ReflectionException ignored) {
239: }
240:
241: }
242:
243: }
244:
245: return list;
246:
247: }
248:
249: public Object invoke(String method, Object[] arguments,
250: String[] params) throws MBeanException, ReflectionException
251:
252: {
253:
254: if (method.equals("dynamicOperation") &&
255:
256: params.length == 1 &&
257:
258: params[0].equals("java.lang.String") &&
259:
260: arguments.length == 1 &&
261:
262: arguments[0] instanceof String)
263:
264: {
265:
266: boolean match = dynamicOperation((String) arguments[0]);
267:
268: return new Boolean(match);
269:
270: }
271:
272: else
273:
274: {
275:
276: throw new MBeanException(new IllegalArgumentException(
277: "Invalid method or arguments for invoke"));
278:
279: }
280:
281: }
282:
283: }
|