1 7 8 package javax.management; 9 10 import java.lang.reflect.InvocationHandler ; 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.Proxy ; 13 14 89 public class MBeanServerInvocationHandler implements InvocationHandler { 90 103 public MBeanServerInvocationHandler(MBeanServerConnection connection, 104 ObjectName objectName) { 105 this.connection = connection; 106 this.objectName = objectName; 107 108 } 109 110 137 public static Object newProxyInstance(MBeanServerConnection connection, 138 ObjectName objectName, 139 Class interfaceClass, 140 boolean notificationBroadcaster) { 141 final InvocationHandler handler = 142 new MBeanServerInvocationHandler (connection, objectName); 143 final Class [] interfaces; 144 if (notificationBroadcaster) { 145 interfaces = 146 new Class [] {interfaceClass, NotificationEmitter .class}; 147 } else 148 interfaces = new Class [] {interfaceClass}; 149 return Proxy.newProxyInstance(interfaceClass.getClassLoader(), 150 interfaces, 151 handler); 152 } 153 154 public Object invoke(Object proxy, Method method, Object [] args) 155 throws Throwable { 156 final Class methodClass = method.getDeclaringClass(); 157 158 if (methodClass.equals(NotificationBroadcaster .class) 159 || methodClass.equals(NotificationEmitter .class)) 160 return invokeBroadcasterMethod(proxy, method, args); 161 162 final String methodName = method.getName(); 163 final Class [] paramTypes = method.getParameterTypes(); 164 final Class returnType = method.getReturnType(); 165 166 169 final int nargs = (args == null) ? 0 : args.length; 170 171 if (methodName.startsWith("get") 172 && methodName.length() > 3 173 && nargs == 0 174 && !returnType.equals(Void.TYPE)) { 175 return connection.getAttribute(objectName, 176 methodName.substring(3)); 177 } 178 179 if (methodName.startsWith("is") 180 && methodName.length() > 2 181 && nargs == 0 182 && (returnType.equals(Boolean.TYPE) 183 || returnType.equals(Boolean .class))) { 184 return connection.getAttribute(objectName, 185 methodName.substring(2)); 186 } 187 188 if (methodName.startsWith("set") 189 && methodName.length() > 3 190 && nargs == 1 191 && returnType.equals(Void.TYPE)) { 192 Attribute attr = new Attribute (methodName.substring(3), args[0]); 193 connection.setAttribute(objectName, attr); 194 return null; 195 } 196 197 final String [] signature = new String [paramTypes.length]; 198 for (int i = 0; i < paramTypes.length; i++) 199 signature[i] = paramTypes[i].getName(); 200 try { 201 return connection.invoke(objectName, methodName, args, signature); 202 } catch (MBeanException e) { 203 throw e.getTargetException(); 204 } 205 217 } 218 219 private Object invokeBroadcasterMethod(Object proxy, Method method, 220 Object [] args) throws Exception { 221 final String methodName = method.getName(); 222 final int nargs = (args == null) ? 0 : args.length; 223 224 if (methodName.equals("addNotificationListener")) { 225 229 if (nargs != 3) { 230 final String msg = 231 "Bad arg count to addNotificationListener: " + nargs; 232 throw new IllegalArgumentException (msg); 233 } 234 236 237 NotificationListener listener = (NotificationListener ) args[0]; 238 NotificationFilter filter = (NotificationFilter ) args[1]; 239 Object handback = args[2]; 240 connection.addNotificationListener(objectName, 241 listener, 242 filter, 243 handback); 244 return null; 245 246 } else if (methodName.equals("removeNotificationListener")) { 247 248 250 NotificationListener listener = (NotificationListener ) args[0]; 251 252 switch (nargs) { 253 case 1: 254 connection.removeNotificationListener(objectName, listener); 255 return null; 256 257 case 3: 258 NotificationFilter filter = (NotificationFilter ) args[1]; 259 Object handback = args[2]; 260 connection.removeNotificationListener(objectName, 261 listener, 262 filter, 263 handback); 264 return null; 265 266 default: 267 final String msg = 268 "Bad arg count to removeNotificationListener: " + nargs; 269 throw new IllegalArgumentException (msg); 270 } 271 272 } else if (methodName.equals("getNotificationInfo")) { 273 274 if (args != null) { 275 throw new IllegalArgumentException ("getNotificationInfo has " + 276 "args"); 277 } 278 279 MBeanInfo info = connection.getMBeanInfo(objectName); 280 return info.getNotifications(); 281 282 } else { 283 throw new IllegalArgumentException ("Bad method name: " + 284 methodName); 285 } 286 } 287 288 private final MBeanServerConnection connection; 289 private final ObjectName objectName; 290 } 291 | Popular Tags |