1 22 package org.jboss.mx.notification; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 28 import javax.management.Notification ; 29 import javax.management.NotificationListener ; 30 import javax.management.ObjectName ; 31 32 42 public class NotificationListenerProxy 43 implements InvocationHandler 44 { 45 47 49 52 private NotificationListener listener; 53 54 57 private ObjectName name; 58 59 64 private static final String METHODNAME = "handleNotification"; 65 66 69 private final Integer hashCode; 70 71 73 public static Object newInstance(ObjectName name, 74 NotificationListener listener) 75 { 76 java.util.HashSet set = new java.util.HashSet (); 78 79 Class currentClass = listener.getClass(); 81 while(currentClass != null) 82 { 83 Class [] classInterfaces = currentClass.getInterfaces(); 84 for(int i = 0; i < classInterfaces.length; i++) 85 { 86 set.add(classInterfaces[i]); 87 } 88 currentClass = currentClass.getSuperclass(); 89 } 90 Class [] interfaces = new Class [set.size()]; 91 interfaces = (Class [])set.toArray(interfaces); 92 93 return Proxy.newProxyInstance(listener.getClass().getClassLoader(), 94 interfaces, 95 new NotificationListenerProxy(name, listener)); 96 } 97 98 100 106 public NotificationListenerProxy(ObjectName name, 107 NotificationListener listener) 108 { 109 this.name = name; 110 this.listener = listener; 111 this.hashCode = new Integer (System.identityHashCode(this)); 112 113 } 119 120 122 public Object invoke(Object proxy, Method method, Object [] args) 124 throws Throwable 125 { 126 String localMethodName = method.getName(); 127 if(localMethodName.equals(METHODNAME)) 129 { 130 for(int x = 0; x < args.length; x++) 131 { 132 if(args[x] instanceof Notification ) 133 { 134 ((Notification )args[x]).setSource(name); 138 } 139 } 140 } 141 else if (localMethodName.equals("hashCode")) 142 { 143 return proxyHashCode(proxy); 144 } 145 else if (localMethodName.equals("equals")) 146 { 147 return proxyEquals(proxy, args[0]); 148 } 149 else if (localMethodName.equals("toString")) 150 { 151 return proxyToString(proxy); 152 } 153 return method.invoke(listener, args); 154 } 155 156 protected Integer proxyHashCode(Object proxy) 157 { 158 return this.hashCode; 159 } 160 161 protected Boolean proxyEquals(Object proxy, Object other) 162 { 163 return (proxy == other ? Boolean.TRUE : Boolean.FALSE); 164 } 165 166 protected String proxyToString(Object proxy) 167 { 168 return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode()); 169 } 170 171 173 175 177 } 179 | Popular Tags |