KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > MBeanHelper


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.admin;
5
6 import java.io.IOException JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import javax.management.AttributeNotFoundException JavaDoc;
10 import javax.management.InstanceNotFoundException JavaDoc;
11 import javax.management.ListenerNotFoundException JavaDoc;
12 import javax.management.MBeanException JavaDoc;
13 import javax.management.MBeanServerConnection JavaDoc;
14 import javax.management.MalformedObjectNameException JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import javax.management.ObjectInstance JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18 import javax.management.ReflectionException JavaDoc;
19
20 public class MBeanHelper {
21   private static final MBeanHelper m_instance = new MBeanHelper();
22
23   protected MBeanHelper() {/**/}
24
25   public static final MBeanHelper getHelper() {
26     return m_instance;
27   }
28
29   public ObjectInstance JavaDoc queryMBean(MBeanServerConnection JavaDoc mbsc, String JavaDoc query)
30     throws IOException JavaDoc,
31            MalformedObjectNameException JavaDoc
32   {
33     ObjectInstance JavaDoc result = null;
34     
35     if(mbsc != null) {
36       Set JavaDoc mbeans = mbsc.queryMBeans(new ObjectName JavaDoc(query), null);
37
38       if(mbeans != null && mbeans.size() > 0) {
39         result = (ObjectInstance JavaDoc)(mbeans.toArray(new ObjectInstance JavaDoc[]{})[0]);
40       }
41     }
42
43     return result;
44   }
45
46   public ObjectName JavaDoc queryName(MBeanServerConnection JavaDoc mbsc, String JavaDoc query)
47     throws MalformedObjectNameException JavaDoc,
48            IOException JavaDoc
49   {
50     ObjectName JavaDoc result = null;
51     
52     if(mbsc != null) {
53       Set JavaDoc names = mbsc.queryNames(new ObjectName JavaDoc(query), null);
54     
55       if(names != null && names.size() > 0) {
56         result = (ObjectName JavaDoc)names.toArray(new ObjectName JavaDoc[]{})[0];
57       }
58     }
59
60     return result;
61   }
62
63   public ObjectName JavaDoc[] queryNames(MBeanServerConnection JavaDoc mbsc, String JavaDoc query)
64     throws MalformedObjectNameException JavaDoc,
65            IOException JavaDoc
66   {
67     ObjectName JavaDoc[] result = null;
68
69     if(mbsc != null) {
70       Set JavaDoc names = mbsc.queryNames(new ObjectName JavaDoc(query), null);
71     
72       if(names != null && names.size() > 0) {
73         result = (ObjectName JavaDoc[])names.toArray(new ObjectName JavaDoc[]{});
74       }
75     }
76     
77     return result;
78   }
79
80   public Object JavaDoc getAttribute(
81     MBeanServerConnection JavaDoc mbsc,
82     ObjectName JavaDoc bean,
83     String JavaDoc attr)
84     throws MBeanException JavaDoc,
85            AttributeNotFoundException JavaDoc,
86            InstanceNotFoundException JavaDoc,
87            ReflectionException JavaDoc,
88            IOException JavaDoc
89   {
90     if(bean == null) {
91       throw new IllegalArgumentException JavaDoc("ObjectName is null");
92     }
93     
94     return mbsc != null ? mbsc.getAttribute(bean, attr) : null;
95   }
96
97   public long getLongAttribute(
98     MBeanServerConnection JavaDoc mbsc,
99     ObjectName JavaDoc bean,
100     String JavaDoc attr)
101     throws MBeanException JavaDoc,
102            AttributeNotFoundException JavaDoc,
103            InstanceNotFoundException JavaDoc,
104            ReflectionException JavaDoc,
105            IOException JavaDoc
106   {
107     if(bean == null) {
108       throw new IllegalArgumentException JavaDoc("ObjectName is null");
109     }
110
111     Object JavaDoc obj = getAttribute(mbsc, bean, attr);
112       
113     if(obj != null && obj instanceof Long JavaDoc) {
114       return ((Long JavaDoc)obj).longValue();
115     }
116
117     return 0L;
118   }
119
120   public String JavaDoc getStringAttribute(
121     MBeanServerConnection JavaDoc mbsc,
122     ObjectName JavaDoc bean,
123     String JavaDoc attr)
124     throws MBeanException JavaDoc,
125            AttributeNotFoundException JavaDoc,
126            InstanceNotFoundException JavaDoc,
127            ReflectionException JavaDoc,
128            IOException JavaDoc
129   {
130     if(bean == null) {
131       throw new IllegalArgumentException JavaDoc("ObjectName is null");
132     }
133
134     Object JavaDoc obj = getAttribute(mbsc, bean, attr);
135     
136     return (obj != null) ? obj.toString() : null;
137   }
138
139   public boolean getBooleanAttribute(
140     MBeanServerConnection JavaDoc mbsc,
141     ObjectName JavaDoc bean,
142     String JavaDoc attr)
143     throws MBeanException JavaDoc,
144            AttributeNotFoundException JavaDoc,
145            InstanceNotFoundException JavaDoc,
146            ReflectionException JavaDoc,
147            IOException JavaDoc
148   {
149     if(bean == null) {
150       throw new IllegalArgumentException JavaDoc("ObjectName is null");
151     }
152
153     Object JavaDoc obj = getAttribute(mbsc, bean, attr);
154     
155     if(obj != null && obj instanceof Boolean JavaDoc) {
156       return ((Boolean JavaDoc)obj).booleanValue();
157     }
158
159     return false;
160   }
161
162   public Object JavaDoc invoke(
163     MBeanServerConnection JavaDoc mbsc,
164     ObjectName JavaDoc bean,
165     String JavaDoc method,
166     Object JavaDoc[] types,
167     String JavaDoc[] args)
168     throws InstanceNotFoundException JavaDoc,
169            MBeanException JavaDoc,
170            ReflectionException JavaDoc,
171            IOException JavaDoc
172   {
173     if(bean == null) {
174       throw new IllegalArgumentException JavaDoc("ObjectName is null");
175     }
176
177     return mbsc != null ? mbsc.invoke(bean, method, types, args) : null;
178   }
179   
180   public void addNotificationListener(
181     MBeanServerConnection JavaDoc mbsc,
182     ObjectName JavaDoc bean,
183     NotificationListener JavaDoc listener)
184     throws InstanceNotFoundException JavaDoc,
185            IOException JavaDoc
186   {
187     if(bean == null) {
188       throw new IllegalArgumentException JavaDoc("ObjectName is null");
189     }
190
191     if(mbsc != null) {
192       mbsc.addNotificationListener(bean, listener, null, null);
193     }
194   }
195
196   public void removeNotificationListener(
197     MBeanServerConnection JavaDoc mbsc,
198     ObjectName JavaDoc bean,
199     NotificationListener JavaDoc listener)
200     throws InstanceNotFoundException JavaDoc,
201            ListenerNotFoundException JavaDoc,
202            IOException JavaDoc
203   {
204     if(bean == null) {
205       throw new IllegalArgumentException JavaDoc("ObjectName is null");
206     }
207
208     if(mbsc != null) {
209       mbsc.removeNotificationListener(bean, listener);
210     }
211   }
212
213   public boolean isRegistered(MBeanServerConnection JavaDoc mbsc, ObjectName JavaDoc bean)
214     throws IOException JavaDoc
215   {
216     return mbsc != null ? mbsc.isRegistered(bean) : false;
217   }
218 }
219
Popular Tags