1 22 package org.jboss.system.server.jmx; 23 24 import java.io.ObjectInputStream ; 25 import java.lang.reflect.Constructor ; 26 import java.util.Set ; 27 import java.util.HashSet ; 28 import java.util.Collections ; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectInstance ; 31 import javax.management.ObjectName ; 32 import javax.management.ReflectionException ; 33 import javax.management.InstanceAlreadyExistsException ; 34 import javax.management.MBeanRegistrationException ; 35 import javax.management.MBeanException ; 36 import javax.management.NotCompliantMBeanException ; 37 import javax.management.InstanceNotFoundException ; 38 import javax.management.QueryExp ; 39 import javax.management.AttributeNotFoundException ; 40 import javax.management.AttributeList ; 41 import javax.management.Attribute ; 42 import javax.management.InvalidAttributeValueException ; 43 import javax.management.NotificationListener ; 44 import javax.management.NotificationFilter ; 45 import javax.management.ListenerNotFoundException ; 46 import javax.management.MBeanInfo ; 47 import javax.management.IntrospectionException ; 48 import javax.management.OperationsException ; 49 import javax.management.MBeanServerDelegate ; 50 import javax.management.loading.ClassLoaderRepository ; 51 52 61 public class LazyMBeanServer 62 implements MBeanServer 63 { 64 private static MBeanServer registeredServer; 65 private static MBeanServer theServer; 66 private static MBeanServer platformServer; 67 private static String defaultDomain; 68 private static MBeanServerDelegate theDelegate; 69 72 private static Set platformServerDomains = Collections.synchronizedSet(new HashSet ()); 73 76 private static Set serverDomains = Collections.synchronizedSet(new HashSet ()); 77 78 86 public static MBeanServer resetToJBossServer(MBeanServer server) 87 throws Exception 88 { 89 MBeanServer coreServer = server; 90 if( theDelegate != null ) 91 { 92 Class [] sig = {String .class, MBeanServer .class, MBeanServerDelegate .class}; 93 Object [] args = {defaultDomain, null, theDelegate}; 94 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 95 Class c = loader.loadClass("org.jboss.mx.server.MBeanServerImpl"); 96 Constructor ctor = c.getConstructor(sig); 97 theServer = (MBeanServer ) ctor.newInstance(args); 98 coreServer = theServer; 99 String [] domains = theServer.getDomains(); 100 for(int n = 0; n < domains.length; n ++) 101 { 102 serverDomains.add(domains[n]); 103 } 104 } 105 return coreServer; 106 } 107 114 public static MBeanServer getRegisteredMBeanServer(MBeanServer server) 115 { 116 MBeanServer outerServer = server; 117 if( registeredServer != null ) 118 outerServer = registeredServer; 119 return outerServer; 120 } 121 122 126 public static synchronized void reloadDomains() 127 { 128 String [] domains = platformServer.getDomains(); 130 for(int n = 0; n < domains.length; n ++) 131 { 132 platformServerDomains.add(domains[n]); 133 } 134 domains = theServer.getDomains(); 135 for(int n = 0; n < domains.length; n ++) 136 { 137 serverDomains.add(domains[n]); 138 } 139 } 140 LazyMBeanServer(String domain, MBeanServer outer, 141 MBeanServerDelegate delegate) 142 { 143 defaultDomain = domain; 144 platformServer = outer; 145 theServer = outer; 146 registeredServer = this; 147 theDelegate = delegate; 148 String [] domains = platformServer.getDomains(); 150 for(int n = 0; n < domains.length; n ++) 151 { 152 platformServerDomains.add(domains[n]); 153 } 154 } 155 156 public ObjectInstance createMBean(String className, ObjectName name) 157 throws ReflectionException , InstanceAlreadyExistsException , 158 MBeanRegistrationException , MBeanException , 159 NotCompliantMBeanException 160 { 161 try 162 { 163 return createMBean(className, name, null); 164 } 165 catch (InstanceNotFoundException e) 166 { 167 throw new MBeanException (e); 168 } 169 } 170 171 public ObjectInstance createMBean(String className, ObjectName name, 172 ObjectName loaderName) 173 throws ReflectionException , InstanceAlreadyExistsException , 174 MBeanRegistrationException , MBeanException , 175 NotCompliantMBeanException , InstanceNotFoundException 176 { 177 return getServer(name).createMBean(className, name, loaderName); 178 } 179 180 public ObjectInstance createMBean(String className, ObjectName name, 181 Object params[], String signature[]) 182 throws ReflectionException , InstanceAlreadyExistsException , 183 MBeanRegistrationException , MBeanException , 184 NotCompliantMBeanException 185 { 186 return getServer(name).createMBean(className, name, params, signature); 187 } 188 189 public ObjectInstance createMBean(String className, ObjectName name, 190 ObjectName loaderName, Object params[], 191 String signature[]) 192 throws ReflectionException , InstanceAlreadyExistsException , 193 MBeanRegistrationException , MBeanException , 194 NotCompliantMBeanException , InstanceNotFoundException 195 { 196 return getServer(name).createMBean(className, name, loaderName, params, signature); 197 } 198 199 public ObjectInstance registerMBean(Object object, ObjectName name) 200 throws InstanceAlreadyExistsException , MBeanRegistrationException , 201 NotCompliantMBeanException 202 { 203 return theServer.registerMBean(object, name); 204 } 205 206 public void unregisterMBean(ObjectName name) 207 throws InstanceNotFoundException , MBeanRegistrationException 208 { 209 theServer.unregisterMBean(name); 210 } 211 212 public ObjectInstance getObjectInstance(ObjectName name) 213 throws InstanceNotFoundException 214 { 215 ObjectInstance oi = getServer(name).getObjectInstance(name); 216 return oi; 217 } 218 219 public Set queryMBeans(ObjectName name, QueryExp query) 220 { 221 Set beans = platformServer.queryMBeans(name, query); 223 Set beans2 = theServer.queryMBeans(name, query); 224 beans.addAll(beans2); 225 return beans; 226 } 227 228 public Set queryNames(ObjectName name, QueryExp query) 229 { 230 Set names = platformServer.queryNames(name, query); 231 Set names2 = theServer.queryNames(name, query); 232 names.addAll(names2); 233 return names; 234 } 235 236 public boolean isRegistered(ObjectName name) 237 { 238 return getServer(name).isRegistered(name); 239 } 240 241 public Integer getMBeanCount() 242 { 243 return theServer.getMBeanCount(); 244 } 245 246 public Object getAttribute(ObjectName name, String attribute) 247 throws MBeanException , AttributeNotFoundException , 248 InstanceNotFoundException , ReflectionException 249 { 250 return getServer(name).getAttribute(name, attribute); 251 } 252 253 public AttributeList getAttributes(ObjectName name, String [] attributes) 254 throws InstanceNotFoundException , ReflectionException 255 { 256 return getServer(name).getAttributes(name, attributes); 257 } 258 259 public void setAttribute(ObjectName name, Attribute attribute) 260 throws InstanceNotFoundException , AttributeNotFoundException , 261 InvalidAttributeValueException , MBeanException , 262 ReflectionException 263 { 264 getServer(name).setAttribute(name, attribute); 265 } 266 267 public AttributeList setAttributes(ObjectName name, 268 AttributeList attributes) 269 throws InstanceNotFoundException , ReflectionException 270 { 271 return getServer(name).setAttributes(name, attributes); 272 } 273 274 public Object invoke(ObjectName name, String operationName, 275 Object params[], String signature[]) 276 throws InstanceNotFoundException , MBeanException , 277 ReflectionException 278 { 279 Object value = getServer(name).invoke(name, operationName, params, signature); 280 return value; 281 } 282 283 public String getDefaultDomain() 284 { 285 return theServer.getDefaultDomain(); 286 } 287 288 public String [] getDomains() 289 { 290 return theServer.getDomains(); 291 } 292 293 public void addNotificationListener(ObjectName name, 294 NotificationListener listener, 295 NotificationFilter filter, 296 Object handback) 297 throws InstanceNotFoundException 298 { 299 getServer(name).addNotificationListener(name, listener, filter, handback); 300 } 301 302 public void addNotificationListener(ObjectName name, 303 ObjectName listener, 304 NotificationFilter filter, 305 Object handback) 306 throws InstanceNotFoundException 307 { 308 getServer(name).addNotificationListener(name, listener, filter, handback); 309 } 310 311 public void removeNotificationListener(ObjectName name, 312 ObjectName listener) 313 throws InstanceNotFoundException , ListenerNotFoundException 314 { 315 getServer(name).removeNotificationListener(name, listener); 316 } 317 318 public void removeNotificationListener(ObjectName name, 319 ObjectName listener, 320 NotificationFilter filter, 321 Object handback) 322 throws InstanceNotFoundException , ListenerNotFoundException 323 { 324 getServer(name).removeNotificationListener(name, listener, filter, handback); 325 } 326 327 public void removeNotificationListener(ObjectName name, 328 NotificationListener listener) 329 throws InstanceNotFoundException , ListenerNotFoundException 330 { 331 getServer(name).removeNotificationListener(name, listener); 332 } 333 334 public void removeNotificationListener(ObjectName name, 335 NotificationListener listener, 336 NotificationFilter filter, 337 Object handback) 338 throws InstanceNotFoundException , ListenerNotFoundException 339 { 340 getServer(name).removeNotificationListener(name, listener, filter, handback); 341 } 342 343 353 public MBeanInfo getMBeanInfo(ObjectName name) 354 throws InstanceNotFoundException , IntrospectionException , 355 ReflectionException 356 { 357 MBeanInfo info = null; 358 try 359 { 360 info = getServer(name).getMBeanInfo(name); 361 } 362 catch(InstanceNotFoundException e) 363 { 364 reloadDomains(); 365 info = getServer(name).getMBeanInfo(name); 366 } 367 return info; 368 } 369 370 public boolean isInstanceOf(ObjectName name, String className) 371 throws InstanceNotFoundException 372 { 373 return getServer(name).isInstanceOf(name, className); 374 } 375 376 public Object instantiate(String className) 377 throws ReflectionException , MBeanException 378 { 379 return theServer.instantiate(className); 380 } 381 382 public Object instantiate(String className, ObjectName loaderName) 383 throws ReflectionException , MBeanException , 384 InstanceNotFoundException 385 { 386 return theServer.instantiate(className, loaderName); 387 } 388 389 public Object instantiate(String className, Object params[], 390 String signature[]) 391 throws ReflectionException , MBeanException 392 { 393 return theServer.instantiate(className, params, signature); 394 } 395 396 public Object instantiate(String className, ObjectName loaderName, 397 Object params[], String signature[]) 398 throws ReflectionException , MBeanException , 399 InstanceNotFoundException 400 { 401 return theServer.instantiate(className, loaderName, params, signature); 402 } 403 404 public ObjectInputStream deserialize(ObjectName name, byte[] data) 405 throws InstanceNotFoundException , OperationsException 406 { 407 return getServer(name).deserialize(name, data); 408 } 409 410 public ObjectInputStream deserialize(String className, byte[] data) 411 throws OperationsException , ReflectionException 412 { 413 return theServer.deserialize(className, data); 414 } 415 416 public ObjectInputStream deserialize(String className, 417 ObjectName loaderName, 418 byte[] data) 419 throws InstanceNotFoundException , OperationsException , 420 ReflectionException 421 { 422 return theServer.deserialize(className, loaderName, data); 423 } 424 425 public ClassLoader getClassLoaderFor(ObjectName mbeanName) 426 throws InstanceNotFoundException 427 { 428 return getServer(mbeanName).getClassLoaderFor(mbeanName); 429 } 430 431 public ClassLoader getClassLoader(ObjectName loaderName) 432 throws InstanceNotFoundException 433 { 434 return getServer(loaderName).getClassLoader(loaderName); 435 } 436 437 public ClassLoaderRepository getClassLoaderRepository() 438 { 439 return theServer.getClassLoaderRepository(); 440 } 441 442 451 private MBeanServer getServer(ObjectName name) 452 { 453 String domain = name != null ? name.getDomain() : ""; 454 if( serverDomains.contains(domain) ) 455 return theServer; 456 if( platformServerDomains.contains(domain) ) 457 return platformServer; 458 return theServer; 459 } 460 } 461 | Popular Tags |