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