KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package test.javax.management.support;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import javax.management.Attribute JavaDoc;
15 import javax.management.AttributeList JavaDoc;
16 import javax.management.AttributeNotFoundException JavaDoc;
17 import javax.management.DynamicMBean JavaDoc;
18 import javax.management.InvalidAttributeValueException JavaDoc;
19 import javax.management.MBeanAttributeInfo JavaDoc;
20 import javax.management.MBeanConstructorInfo JavaDoc;
21 import javax.management.MBeanException JavaDoc;
22 import javax.management.MBeanInfo JavaDoc;
23 import javax.management.MBeanNotificationInfo JavaDoc;
24 import javax.management.MBeanOperationInfo JavaDoc;
25 import javax.management.ReflectionException JavaDoc;
26
27 /**
28  * @version $Revision: 1.5 $
29  */

30 public class QuerySupport
31 {
32    public interface TestMBean
33    {
34       public Integer JavaDoc getNumber();
35
36       public String JavaDoc getStr();
37
38       public Boolean JavaDoc getBoolean();
39    }
40
41    public static class Test implements TestMBean
42    {
43       private Integer JavaDoc n;
44       private String JavaDoc str;
45       private Boolean JavaDoc b;
46
47       public Test(String JavaDoc str, Integer JavaDoc n, Boolean JavaDoc b)
48       {
49          this.str = str;
50          this.n = n;
51          this.b = b;
52       }
53
54       public Integer JavaDoc getNumber()
55       {
56          return n;
57       }
58
59       public String JavaDoc getStr()
60       {
61          return str;
62       }
63
64       public Boolean JavaDoc getBoolean()
65       {
66          return b;
67       }
68    }
69
70    public static class DynamicTest implements DynamicMBean JavaDoc
71    {
72       private Boolean JavaDoc boolval;
73       private long numval;
74       private String JavaDoc strval;
75
76       public DynamicTest(String JavaDoc s, long n, Boolean JavaDoc b)
77       {
78          this.boolval = b;
79          this.numval = n;
80          this.strval = s;
81       }
82
83       public Boolean JavaDoc getBoolean()
84       {
85          return this.boolval;
86       }
87
88       public long getNumber()
89       {
90          return this.numval;
91       }
92
93       public void setNumber(long value)
94       {
95          this.numval = value;
96       }
97
98       public String JavaDoc getStr()
99       {
100          throw new RuntimeException JavaDoc("Don't call me!");
101       }
102
103       public Object JavaDoc getAttribute(String JavaDoc attribute)
104               throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
105       {
106          Object JavaDoc result;
107          if (attribute.compareTo("Boolean") == 0)
108          {
109             result = getBoolean();
110          }
111          else if (attribute.compareTo("Number") == 0)
112          {
113             result = new Long JavaDoc(getNumber());
114          }
115          else if (attribute.compareTo("Str") == 0)
116          {
117             result = getStr();
118          }
119          else
120          {
121             throw new AttributeNotFoundException JavaDoc("Can't find " + attribute);
122          }
123          return result;
124       }
125
126       public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
127       {
128          List JavaDoc attrnames = new ArrayList JavaDoc();
129          MBeanAttributeInfo JavaDoc[] attrs = getMBeanInfo().getAttributes();
130          for (int i = 0; i < attrs.length; i++)
131          {
132             attrnames.add(attrs[i].getName());
133          }
134          AttributeList JavaDoc result = new AttributeList JavaDoc();
135          for (int i = 0; i < attributes.length; i++)
136          {
137             if (attrnames.contains(attributes[i]))
138             {
139                try
140                {
141                   Attribute JavaDoc attr = new Attribute JavaDoc(attributes[i], getAttribute(attributes[i]));
142                   result.add(attr);
143                }
144                catch (AttributeNotFoundException JavaDoc e)
145                {
146                   // Don't add this attribute
147
}
148                catch (MBeanException JavaDoc e)
149                {
150                   // Don't add this attribute
151
}
152                catch (ReflectionException JavaDoc e)
153                {
154                   // Don't add this attribute
155
}
156             }
157          }
158          return result;
159       }
160
161       public MBeanInfo JavaDoc getMBeanInfo()
162       {
163          MBeanInfo JavaDoc result;
164          MBeanAttributeInfo JavaDoc[] attrs;
165          try
166          {
167             attrs =
168             new MBeanAttributeInfo JavaDoc[]{
169                new MBeanAttributeInfo JavaDoc("Number",
170                                       "A number",
171                                       DynamicTest.class.getMethod("getNumber",
172                                                                   new Class JavaDoc[0]),
173                                       DynamicTest.class.getMethod("setNumber",
174                                                                   new Class JavaDoc[]{long.class})),
175                new MBeanAttributeInfo JavaDoc("Str",
176                                       "A string",
177                                       DynamicTest.class.getMethod("getStr", new Class JavaDoc[0]),
178                                       null),
179                new MBeanAttributeInfo JavaDoc("Boolean",
180                                       "A Boolean",
181                                       DynamicTest.class.getMethod("getBoolean",
182                                                                   new Class JavaDoc[0]),
183                                       null)
184             };
185          }
186          catch (Exception JavaDoc x)
187          {
188             attrs = new MBeanAttributeInfo JavaDoc[0];
189          }
190          MBeanConstructorInfo JavaDoc[] ctors = new MBeanConstructorInfo JavaDoc[0];
191          MBeanOperationInfo JavaDoc[] ops = new MBeanOperationInfo JavaDoc[0];
192          MBeanNotificationInfo JavaDoc[] notifs = new MBeanNotificationInfo JavaDoc[0];
193          result =
194          new MBeanInfo JavaDoc(DynamicTest.class.getName(),
195                        "DynamicTest MBean",
196                        attrs,
197                        ctors,
198                        ops,
199                        notifs);
200          return result;
201       }
202
203       public Object JavaDoc invoke(String JavaDoc method,
204                            Object JavaDoc[] arguments,
205                            String JavaDoc[] params)
206               throws MBeanException JavaDoc, ReflectionException JavaDoc
207       {
208          return null;
209       }
210
211       public void setAttribute(Attribute JavaDoc attribute)
212               throws
213               AttributeNotFoundException JavaDoc,
214               InvalidAttributeValueException JavaDoc,
215               MBeanException JavaDoc,
216               ReflectionException JavaDoc
217       {
218          if (attribute.getName().compareTo("Number") == 0)
219          {
220             setNumber(((Long JavaDoc)attribute.getValue()).longValue());
221          }
222          else
223          {
224             throw new AttributeNotFoundException JavaDoc("Can't find " + attribute.getName());
225          }
226       }
227
228       public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
229       {
230          AttributeList JavaDoc result = new AttributeList JavaDoc();
231          Iterator JavaDoc i = attributes.iterator();
232          while (i.hasNext())
233          {
234             try
235             {
236                Attribute JavaDoc attr = (Attribute JavaDoc)i.next();
237                setAttribute(attr);
238                result.add(attr);
239             }
240             catch (Exception JavaDoc x)
241             {
242                // Don't add this to the result
243
}
244          }
245          return result;
246       }
247
248    }
249 }
250
Popular Tags