1 8 9 package mx4j.server.interceptor; 10 11 import java.security.PrivilegedAction ; 12 import java.security.AccessController ; 13 14 import javax.management.Attribute ; 15 import javax.management.AttributeList ; 16 import javax.management.AttributeNotFoundException ; 17 import javax.management.InvalidAttributeValueException ; 18 import javax.management.ListenerNotFoundException ; 19 import javax.management.MBeanException ; 20 import javax.management.MBeanInfo ; 21 import javax.management.MBeanRegistrationException ; 22 import javax.management.NotificationFilter ; 23 import javax.management.NotificationListener ; 24 import javax.management.ReflectionException ; 25 26 import mx4j.server.MBeanMetaData; 27 28 36 public class ContextClassLoaderMBeanServerInterceptor extends DefaultMBeanServerInterceptor 37 { 38 public ContextClassLoaderMBeanServerInterceptor() 39 { 40 setEnabled(false); 42 } 43 44 public String getType() 45 { 46 return "contextclassloader"; 47 } 48 49 public void addNotificationListener(MBeanMetaData metadata, NotificationListener listener, NotificationFilter filter, Object handback) 50 { 51 if (isEnabled()) 52 { 53 ClassLoader context = getContextClassLoader(); 54 if (metadata.getClassLoader() != context) 55 { 56 try 57 { 58 setContextClassLoader(metadata.getClassLoader()); 59 super.addNotificationListener(metadata, listener, filter, handback); 60 return; 61 } 62 finally 63 { 64 setContextClassLoader(context); 65 } 66 } 67 } 68 69 super.addNotificationListener(metadata, listener, filter, handback); 70 } 71 72 public void removeNotificationListener(MBeanMetaData metadata, NotificationListener listener) throws ListenerNotFoundException 73 { 74 if (isEnabled()) 75 { 76 ClassLoader context = getContextClassLoader(); 77 if (metadata.getClassLoader() != context) 78 { 79 try 80 { 81 setContextClassLoader(metadata.getClassLoader()); 82 super.removeNotificationListener(metadata, listener); 83 return; 84 } 85 finally 86 { 87 setContextClassLoader(context); 88 } 89 } 90 } 91 92 super.removeNotificationListener(metadata, listener); 93 } 94 95 public void removeNotificationListener(MBeanMetaData metadata, NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 96 { 97 if (isEnabled()) 98 { 99 ClassLoader context = getContextClassLoader(); 100 if (metadata.getClassLoader() != context) 101 { 102 try 103 { 104 setContextClassLoader(metadata.getClassLoader()); 105 super.removeNotificationListener(metadata, listener, filter, handback); 106 return; 107 } 108 finally 109 { 110 setContextClassLoader(context); 111 } 112 } 113 } 114 115 super.removeNotificationListener(metadata, listener, filter, handback); 116 } 117 118 public void instantiate(MBeanMetaData metadata, String className, String [] params, Object [] args) throws ReflectionException , MBeanException 119 { 120 if (isEnabled()) 121 { 122 ClassLoader context = getContextClassLoader(); 123 if (metadata.getClassLoader() != context) 124 { 125 try 126 { 127 setContextClassLoader(metadata.getClassLoader()); 128 super.instantiate(metadata, className, params, args); 129 return; 130 } 131 finally 132 { 133 setContextClassLoader(context); 134 } 135 } 136 } 137 138 super.instantiate(metadata, className, params, args); 139 } 140 141 public void registration(MBeanMetaData metadata, int operation) throws MBeanRegistrationException 142 { 143 if (isEnabled()) 144 { 145 ClassLoader context = getContextClassLoader(); 146 if (metadata.getClassLoader() != context) 147 { 148 try 149 { 150 setContextClassLoader(metadata.getClassLoader()); 151 super.registration(metadata, operation); 152 return; 153 } 154 finally 155 { 156 setContextClassLoader(context); 157 } 158 } 159 } 160 161 super.registration(metadata, operation); 162 } 163 164 public MBeanInfo getMBeanInfo(MBeanMetaData metadata) 165 { 166 if (isEnabled()) 167 { 168 ClassLoader context = getContextClassLoader(); 169 if (metadata.getClassLoader() != context) 170 { 171 try 172 { 173 setContextClassLoader(metadata.getClassLoader()); 174 return super.getMBeanInfo(metadata); 175 } 176 finally 177 { 178 setContextClassLoader(context); 179 } 180 } 181 } 182 183 return super.getMBeanInfo(metadata); 184 } 185 186 public Object invoke(MBeanMetaData metadata, String method, String [] params, Object [] args) throws MBeanException , ReflectionException 187 { 188 if (isEnabled()) 189 { 190 ClassLoader context = getContextClassLoader(); 191 if (metadata.getClassLoader() != context) 192 { 193 try 194 { 195 setContextClassLoader(metadata.getClassLoader()); 196 return super.invoke(metadata, method, params, args); 197 } 198 finally 199 { 200 setContextClassLoader(context); 201 } 202 } 203 } 204 205 return super.invoke(metadata, method, params, args); 206 } 207 208 public AttributeList getAttributes(MBeanMetaData metadata, String [] attributes) 209 { 210 if (isEnabled()) 211 { 212 ClassLoader context = getContextClassLoader(); 213 if (metadata.getClassLoader() != context) 214 { 215 try 216 { 217 setContextClassLoader(metadata.getClassLoader()); 218 return super.getAttributes(metadata, attributes); 219 } 220 finally 221 { 222 setContextClassLoader(context); 223 } 224 } 225 } 226 227 return super.getAttributes(metadata, attributes); 228 } 229 230 public AttributeList setAttributes(MBeanMetaData metadata, AttributeList attributes) 231 { 232 if (isEnabled()) 233 { 234 ClassLoader context = getContextClassLoader(); 235 if (metadata.getClassLoader() != context) 236 { 237 try 238 { 239 setContextClassLoader(metadata.getClassLoader()); 240 return super.setAttributes(metadata, attributes); 241 } 242 finally 243 { 244 setContextClassLoader(context); 245 } 246 } 247 } 248 249 return super.setAttributes(metadata, attributes); 250 } 251 252 public Object getAttribute(MBeanMetaData metadata, String attribute) throws MBeanException , AttributeNotFoundException , ReflectionException 253 { 254 if (isEnabled()) 255 { 256 ClassLoader context = getContextClassLoader(); 257 if (metadata.getClassLoader() != context) 258 { 259 try 260 { 261 setContextClassLoader(metadata.getClassLoader()); 262 return super.getAttribute(metadata, attribute); 263 } 264 finally 265 { 266 setContextClassLoader(context); 267 } 268 } 269 } 270 271 return super.getAttribute(metadata, attribute); 272 } 273 274 public void setAttribute(MBeanMetaData metadata, Attribute attribute) throws MBeanException , AttributeNotFoundException , InvalidAttributeValueException , ReflectionException 275 { 276 if (isEnabled()) 277 { 278 ClassLoader context = getContextClassLoader(); 279 if (metadata.getClassLoader() != context) 280 { 281 try 282 { 283 setContextClassLoader(metadata.getClassLoader()); 284 super.setAttribute(metadata, attribute); 285 return; 286 } 287 finally 288 { 289 setContextClassLoader(context); 290 } 291 } 292 } 293 294 super.setAttribute(metadata, attribute); 295 } 296 297 private ClassLoader getContextClassLoader() 298 { 299 return Thread.currentThread().getContextClassLoader(); 300 } 301 302 private void setContextClassLoader(final ClassLoader cl) 303 { 304 AccessController.doPrivileged(new PrivilegedAction () 305 { 306 public Object run() 307 { 308 Thread.currentThread().setContextClassLoader(cl); 309 return null; 310 } 311 }); 312 } 313 } 314 | Popular Tags |