KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > support > PrivateMBeanDynamic


1 /*
2
3  * Copyright (C) The MX4J Contributors.
4
5  * All rights reserved.
6
7  *
8
9  * This software is distributed under the terms of the MX4J License version 1.0.
10
11  * See the terms of the MX4J License in the documentation provided with this software.
12
13  */

14
15
16 package test.javax.management.support;
17
18
19 import javax.management.Attribute JavaDoc;
20 import javax.management.AttributeList JavaDoc;
21 import javax.management.AttributeNotFoundException JavaDoc;
22 import javax.management.DynamicMBean JavaDoc;
23 import javax.management.InvalidAttributeValueException JavaDoc;
24 import javax.management.MBeanAttributeInfo JavaDoc;
25 import javax.management.MBeanConstructorInfo JavaDoc;
26 import javax.management.MBeanException JavaDoc;
27 import javax.management.MBeanInfo JavaDoc;
28 import javax.management.MBeanNotificationInfo JavaDoc;
29 import javax.management.MBeanOperationInfo JavaDoc;
30 import javax.management.MBeanParameterInfo JavaDoc;
31 import javax.management.ReflectionException JavaDoc;
32
33
34 /**
35  * @version $Revision: 1.3 $
36  */

37
38 class PrivateMBeanDynamic implements DynamicMBean JavaDoc
39
40 {
41
42    private String JavaDoc m_value1 = "";
43
44    private String JavaDoc m_value2 = "";
45
46
47    public MBeanInfo JavaDoc getMBeanInfo()
48
49    {
50
51       MBeanAttributeInfo JavaDoc[] attrs = new MBeanAttributeInfo JavaDoc[2];
52
53       attrs[0] = new MBeanAttributeInfo JavaDoc("DynamicAttribute1", "java.lang.String", "A first dynamic attribute", true, true, false);
54
55       attrs[1] = new MBeanAttributeInfo JavaDoc("DynamicAttribute2", "java.lang.String", "A second dynamic attribute", true, true, false);
56
57
58       MBeanConstructorInfo JavaDoc[] ctors = new MBeanConstructorInfo JavaDoc[1];
59
60       ctors[0] = new MBeanConstructorInfo JavaDoc("Parameterless Constructor", "A dynamic constructor", new MBeanParameterInfo JavaDoc[0]);
61
62
63       MBeanOperationInfo JavaDoc[] opers = new MBeanOperationInfo JavaDoc[1];
64
65       MBeanParameterInfo JavaDoc[] params = new MBeanParameterInfo JavaDoc[1];
66
67       params[0] = new MBeanParameterInfo JavaDoc("supposedAttributeValue", "java.lang.String", "Checks if the value of the argument is equal to the value of the attribute");
68
69       opers[0] = new MBeanOperationInfo JavaDoc("dynamicOperation", "A dynamic operation", params, "boolean", MBeanOperationInfo.INFO);
70
71
72       MBeanNotificationInfo JavaDoc[] notifs = new MBeanNotificationInfo JavaDoc[0];
73
74
75       return new MBeanInfo JavaDoc(getClass().getName(), "A MBeanDynamic MBean", attrs, ctors, opers, notifs);
76
77    }
78
79
80    private String JavaDoc getDynamicAttribute1()
81    {
82       return m_value1;
83    }
84
85    private void setDynamicAttribute1(String JavaDoc value)
86    {
87       m_value1 = value;
88    }
89
90    private String JavaDoc getDynamicAttribute2()
91    {
92       return m_value2;
93    }
94
95    private void setDynamicAttribute2(String JavaDoc value)
96    {
97       m_value2 = value;
98    }
99
100    private boolean dynamicOperation(String JavaDoc value)
101    {
102       return m_value1.equals(value);
103    }
104
105
106    public Object JavaDoc getAttribute(String JavaDoc attribute) throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
107
108    {
109
110       if (attribute.equals("DynamicAttribute1"))
111       {
112          return getDynamicAttribute1();
113       }
114
115       else if (attribute.equals("DynamicAttribute2"))
116       {
117          return getDynamicAttribute2();
118       }
119
120       else
121          throw new AttributeNotFoundException JavaDoc(attribute);
122
123    }
124
125
126    public void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
127
128    {
129
130       if (attribute.getName().equals("DynamicAttribute1"))
131
132       {
133
134          Object JavaDoc val = attribute.getValue();
135
136          if (val instanceof String JavaDoc)
137          {
138             setDynamicAttribute1((String JavaDoc)val);
139          }
140
141          else
142          {
143             throw new InvalidAttributeValueException JavaDoc(val == null ? "null" : val.toString());
144          }
145
146       }
147
148       else if (attribute.getName().equals("DynamicAttribute2"))
149
150       {
151
152          Object JavaDoc val = attribute.getValue();
153
154          if (val instanceof String JavaDoc)
155          {
156             setDynamicAttribute2((String JavaDoc)val);
157          }
158
159          else
160          {
161             throw new InvalidAttributeValueException JavaDoc(val == null ? "null" : val.toString());
162          }
163
164       }
165
166       else
167       {
168          throw new AttributeNotFoundException JavaDoc(attribute.getName());
169       }
170
171    }
172
173
174    public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
175
176    {
177
178       AttributeList JavaDoc list = new AttributeList JavaDoc();
179
180       for (int i = 0; i < attributes.length; ++i)
181
182       {
183
184          if (attributes[i].equals("DynamicAttribute1"))
185
186          {
187
188             list.add(new Attribute JavaDoc(attributes[i], getDynamicAttribute1()));
189
190          }
191
192          else if (attributes[i].equals("DynamicAttribute2"))
193
194          {
195
196             list.add(new Attribute JavaDoc(attributes[i], getDynamicAttribute2()));
197
198          }
199
200       }
201
202       return list;
203
204    }
205
206
207    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
208
209    {
210
211       AttributeList JavaDoc list = new AttributeList JavaDoc();
212
213       for (int i = 0; i < attributes.size(); ++i)
214
215       {
216
217          Attribute JavaDoc attr = (Attribute JavaDoc)attributes.get(i);
218
219          if (attr.getName().equals("DynamicAttribute1") || attr.getName().equals("DynamicAttribute2"))
220
221          {
222
223             try
224
225             {
226
227                setAttribute(attr);
228
229                list.add(attr);
230
231             }
232
233             catch (AttributeNotFoundException JavaDoc ignored)
234             {
235             }
236
237             catch (InvalidAttributeValueException JavaDoc ignored)
238             {
239             }
240
241             catch (MBeanException JavaDoc ignored)
242             {
243             }
244
245             catch (ReflectionException JavaDoc ignored)
246             {
247             }
248
249          }
250
251       }
252
253       return list;
254
255    }
256
257
258    public Object JavaDoc invoke(String JavaDoc method, Object JavaDoc[] arguments, String JavaDoc[] params) throws MBeanException JavaDoc, ReflectionException JavaDoc
259
260    {
261
262       if (method.equals("dynamicOperation") &&
263
264           params.length == 1 &&
265
266           params[0].equals("java.lang.String") &&
267
268           arguments.length == 1 &&
269
270           arguments[0] instanceof String JavaDoc)
271
272       {
273
274          boolean match = dynamicOperation((String JavaDoc)arguments[0]);
275
276          return new Boolean JavaDoc(match);
277
278       }
279
280       else
281
282       {
283
284          throw new MBeanException JavaDoc(new IllegalArgumentException JavaDoc("Invalid method or arguments for invoke"));
285
286       }
287
288    }
289
290 }
291
292
Popular Tags