001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin;
005:
006: import java.io.IOException;
007: import java.util.Set;
008:
009: import javax.management.AttributeNotFoundException;
010: import javax.management.InstanceNotFoundException;
011: import javax.management.ListenerNotFoundException;
012: import javax.management.MBeanException;
013: import javax.management.MBeanServerConnection;
014: import javax.management.MalformedObjectNameException;
015: import javax.management.NotificationListener;
016: import javax.management.ObjectInstance;
017: import javax.management.ObjectName;
018: import javax.management.ReflectionException;
019:
020: public class MBeanHelper {
021: private static final MBeanHelper m_instance = new MBeanHelper();
022:
023: protected MBeanHelper() {/**/
024: }
025:
026: public static final MBeanHelper getHelper() {
027: return m_instance;
028: }
029:
030: public ObjectInstance queryMBean(MBeanServerConnection mbsc,
031: String query) throws IOException,
032: MalformedObjectNameException {
033: ObjectInstance result = null;
034:
035: if (mbsc != null) {
036: Set mbeans = mbsc.queryMBeans(new ObjectName(query), null);
037:
038: if (mbeans != null && mbeans.size() > 0) {
039: result = (ObjectInstance) (mbeans
040: .toArray(new ObjectInstance[] {})[0]);
041: }
042: }
043:
044: return result;
045: }
046:
047: public ObjectName queryName(MBeanServerConnection mbsc, String query)
048: throws MalformedObjectNameException, IOException {
049: ObjectName result = null;
050:
051: if (mbsc != null) {
052: Set names = mbsc.queryNames(new ObjectName(query), null);
053:
054: if (names != null && names.size() > 0) {
055: result = (ObjectName) names
056: .toArray(new ObjectName[] {})[0];
057: }
058: }
059:
060: return result;
061: }
062:
063: public ObjectName[] queryNames(MBeanServerConnection mbsc,
064: String query) throws MalformedObjectNameException,
065: IOException {
066: ObjectName[] result = null;
067:
068: if (mbsc != null) {
069: Set names = mbsc.queryNames(new ObjectName(query), null);
070:
071: if (names != null && names.size() > 0) {
072: result = (ObjectName[]) names
073: .toArray(new ObjectName[] {});
074: }
075: }
076:
077: return result;
078: }
079:
080: public Object getAttribute(MBeanServerConnection mbsc,
081: ObjectName bean, String attr) throws MBeanException,
082: AttributeNotFoundException, InstanceNotFoundException,
083: ReflectionException, IOException {
084: if (bean == null) {
085: throw new IllegalArgumentException("ObjectName is null");
086: }
087:
088: return mbsc != null ? mbsc.getAttribute(bean, attr) : null;
089: }
090:
091: public long getLongAttribute(MBeanServerConnection mbsc,
092: ObjectName bean, String attr) throws MBeanException,
093: AttributeNotFoundException, InstanceNotFoundException,
094: ReflectionException, IOException {
095: if (bean == null) {
096: throw new IllegalArgumentException("ObjectName is null");
097: }
098:
099: Object obj = getAttribute(mbsc, bean, attr);
100:
101: if (obj != null && obj instanceof Long) {
102: return ((Long) obj).longValue();
103: }
104:
105: return 0L;
106: }
107:
108: public String getStringAttribute(MBeanServerConnection mbsc,
109: ObjectName bean, String attr) throws MBeanException,
110: AttributeNotFoundException, InstanceNotFoundException,
111: ReflectionException, IOException {
112: if (bean == null) {
113: throw new IllegalArgumentException("ObjectName is null");
114: }
115:
116: Object obj = getAttribute(mbsc, bean, attr);
117:
118: return (obj != null) ? obj.toString() : null;
119: }
120:
121: public boolean getBooleanAttribute(MBeanServerConnection mbsc,
122: ObjectName bean, String attr) throws MBeanException,
123: AttributeNotFoundException, InstanceNotFoundException,
124: ReflectionException, IOException {
125: if (bean == null) {
126: throw new IllegalArgumentException("ObjectName is null");
127: }
128:
129: Object obj = getAttribute(mbsc, bean, attr);
130:
131: if (obj != null && obj instanceof Boolean) {
132: return ((Boolean) obj).booleanValue();
133: }
134:
135: return false;
136: }
137:
138: public Object invoke(MBeanServerConnection mbsc, ObjectName bean,
139: String method, Object[] types, String[] args)
140: throws InstanceNotFoundException, MBeanException,
141: ReflectionException, IOException {
142: if (bean == null) {
143: throw new IllegalArgumentException("ObjectName is null");
144: }
145:
146: return mbsc != null ? mbsc.invoke(bean, method, types, args)
147: : null;
148: }
149:
150: public void addNotificationListener(MBeanServerConnection mbsc,
151: ObjectName bean, NotificationListener listener)
152: throws InstanceNotFoundException, IOException {
153: if (bean == null) {
154: throw new IllegalArgumentException("ObjectName is null");
155: }
156:
157: if (mbsc != null) {
158: mbsc.addNotificationListener(bean, listener, null, null);
159: }
160: }
161:
162: public void removeNotificationListener(MBeanServerConnection mbsc,
163: ObjectName bean, NotificationListener listener)
164: throws InstanceNotFoundException,
165: ListenerNotFoundException, IOException {
166: if (bean == null) {
167: throw new IllegalArgumentException("ObjectName is null");
168: }
169:
170: if (mbsc != null) {
171: mbsc.removeNotificationListener(bean, listener);
172: }
173: }
174:
175: public boolean isRegistered(MBeanServerConnection mbsc,
176: ObjectName bean) throws IOException {
177: return mbsc != null ? mbsc.isRegistered(bean) : false;
178: }
179: }
|