1 22 package org.jboss.system.server.jmx; 23 24 import java.io.File ; 25 import java.lang.reflect.Method ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Properties ; 32 import java.util.logging.LogManager ; 33 34 import javax.management.ListenerNotFoundException ; 35 import javax.management.MBeanNotificationInfo ; 36 import javax.management.MBeanServer ; 37 import javax.management.MBeanServerFactory ; 38 import javax.management.NotificationEmitter ; 39 import javax.management.NotificationFilter ; 40 import javax.management.NotificationListener ; 41 import javax.management.ObjectInstance ; 42 import javax.management.ObjectName ; 43 44 import org.jboss.kernel.Kernel; 45 import org.jboss.logging.JBossJDKLogManager; 46 import org.jboss.mx.loading.RepositoryClassLoader; 47 import org.jboss.mx.server.ServerConstants; 48 import org.jboss.mx.util.JMXExceptionDecoder; 49 import org.jboss.mx.util.MBeanServerLocator; 50 import org.jboss.mx.util.ObjectNameFactory; 51 import org.jboss.system.ServiceController; 52 import org.jboss.system.ServiceControllerMBean; 53 import org.jboss.system.server.Server; 54 import org.jboss.system.server.ServerConfig; 55 import org.jboss.system.server.ServerConfigImpl; 56 import org.jboss.system.server.ServerConfigImplMBean; 57 import org.jboss.system.server.ServerImplMBean; 58 import org.jboss.util.JBossObject; 59 import org.jboss.util.file.FileSuffixFilter; 60 61 68 public class JMXKernel extends JBossObject 69 implements JMXKernelMBean, NotificationEmitter 70 { 71 private final static ObjectName DEFAULT_LOADER_NAME = 72 ObjectNameFactory.create(ServerConstants.DEFAULT_LOADER_NAME); 73 74 75 private MBeanServer mbeanServer; 76 private Server serverImpl; 77 private ServiceController controller; 78 private ServerConfig serverConfig; 79 private ServerConfigImplMBean serverConfigMBean; 80 81 private Kernel kernel; 82 83 private NotificationEmitter notificationEmitter; 84 85 86 private ObjectName bootstrapUCLName; 87 private boolean started; 88 89 private boolean isInShutdown; 90 91 public Server getServerImpl() 92 { 93 return serverImpl; 94 } 95 public void setServerImpl(Server serverImpl) 96 { 97 this.serverImpl = serverImpl; 98 this.notificationEmitter = (NotificationEmitter ) serverImpl; 99 } 100 101 public ServiceControllerMBean getServiceController() 102 { 103 return this.controller; 104 } 105 public MBeanServer getMbeanServer() 106 { 107 return mbeanServer; 108 } 109 110 115 public void setKernel(Kernel kernel) 116 { 117 this.kernel = kernel; 118 } 119 120 public void start() throws Exception 121 { 122 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 123 String builder = System.getProperty(ServerConstants.MBEAN_SERVER_BUILDER_CLASS_PROPERTY, 125 ServerConstants.DEFAULT_MBEAN_SERVER_BUILDER_CLASS); 126 System.setProperty(ServerConstants.MBEAN_SERVER_BUILDER_CLASS_PROPERTY, builder); 127 128 serverConfig = serverImpl.getConfig(); 129 serverConfigMBean = new ServerConfigImpl(serverConfig); 130 if (serverConfig.getPlatformMBeanServer() == true) 132 { 133 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 135 Class clazz = cl.loadClass("java.lang.management.ManagementFactory"); 136 Class [] sig = null; 137 Method method = clazz.getMethod("getPlatformMBeanServer", sig); 138 Object [] args = null; 139 mbeanServer = (MBeanServer ) method.invoke(null, args); 140 MBeanServerLocator.setJBoss(mbeanServer); 142 145 mbeanServer = LazyMBeanServer.resetToJBossServer(mbeanServer); 146 } 147 else 148 { 149 mbeanServer = MBeanServerFactory.createMBeanServer("jboss"); 151 } 152 log.debug("Created MBeanServer: " + mbeanServer); 153 154 mbeanServer.registerMBean(this, ServerImplMBean.OBJECT_NAME); 156 mbeanServer.registerMBean(serverConfigMBean, ServerConfigImplMBean.OBJECT_NAME); 157 158 RepositoryClassLoader ucl = initBootLibraries(); 160 bootstrapUCLName = ucl.getObjectName(); 161 mbeanServer.registerMBean(ucl, bootstrapUCLName); 162 163 try 166 { 167 Thread.currentThread().setContextClassLoader(ucl); 168 169 createMBean("org.jboss.system.server.ServerInfo", 171 "jboss.system:type=ServerInfo"); 172 173 controller = new ServiceController(); 175 controller.setKernel(kernel); 176 controller.setMBeanServer(mbeanServer); 177 mbeanServer.registerMBean(controller, new ObjectName ("jboss.system:service=ServiceController")); 178 179 log.info("Legacy JMX core initialized"); 180 started = true; 181 } 182 finally 183 { 184 Thread.currentThread().setContextClassLoader(tcl); 185 } 186 } 187 188 193 public void stop() throws IllegalStateException 194 { 195 if (log.isTraceEnabled()) 196 log.trace("stop caller:", new Throwable ("Here")); 197 198 if (!started) 199 throw new IllegalStateException ("Server not started"); 200 201 isInShutdown = true; 202 new Thread () 205 { 206 public void run() 207 { 208 shutdown(); 211 } 212 }.start(); 213 } 214 215 219 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 220 { 221 notificationEmitter.addNotificationListener(listener, filter, handback); 222 } 223 224 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 225 { 226 notificationEmitter.removeNotificationListener(listener); 227 } 228 229 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 230 throws ListenerNotFoundException 231 { 232 notificationEmitter.removeNotificationListener(listener, filter, handback); 233 } 234 235 public MBeanNotificationInfo [] getNotificationInfo() 236 { 237 return notificationEmitter.getNotificationInfo(); 238 } 239 240 public void init(Properties props) throws Exception 242 { 243 serverImpl.init(props); 244 } 245 246 public void exit() 247 { 248 serverImpl.exit(); 249 } 250 251 public void exit(int exitcode) 252 { 253 serverImpl.exit(exitcode); 254 } 255 256 public ServerConfig getConfig() 257 { 258 return serverConfig; 259 } 260 public String getBuildDate() 261 { 262 return serverImpl.getBuildDate(); 263 } 264 265 public String getBuildID() 266 { 267 return serverImpl.getBuildID(); 268 } 269 270 public String getBuildJVM() 271 { 272 return serverImpl.getBuildJVM(); 273 } 274 275 public String getBuildNumber() 276 { 277 return serverImpl.getBuildNumber(); 278 } 279 280 public String getBuildOS() 281 { 282 return serverImpl.getBuildOS(); 283 } 284 285 public Date getStartDate() 286 { 287 return serverImpl.getStartDate(); 288 } 289 290 public String getVersion() 291 { 292 return serverImpl.getVersion(); 293 } 294 295 public String getVersionName() 296 { 297 return serverImpl.getVersionName(); 298 } 299 300 public void halt() 301 { 302 serverImpl.halt(); 303 } 304 305 public void halt(int exitcode) 306 { 307 serverImpl.halt(exitcode); 308 } 309 310 public boolean isInShutdown() 311 { 312 return serverImpl.isInShutdown(); 313 } 314 315 public boolean isStarted() 316 { 317 return serverImpl.isStarted(); 318 } 319 320 public void runFinalization() 321 { 322 323 } 324 325 public void runGarbageCollector() 326 { 327 328 } 329 330 public void traceInstructions(Boolean flag) 331 { 332 333 } 334 335 public void traceMethodCalls(Boolean flag) 336 { 337 338 } 339 340 public void shutdown() 341 { 342 if (log.isTraceEnabled()) 343 log.trace("Shutdown caller:", new Throwable ("Here")); 344 345 LogManager lm = LogManager.getLogManager(); 347 if (lm instanceof JBossJDKLogManager) 348 { 349 JBossJDKLogManager jbosslm = (JBossJDKLogManager)lm; 350 jbosslm.doReset(); 351 } 352 353 if (isInShutdown) 357 return; 358 else 359 isInShutdown = true; 360 361 log.debug("Shutting down all services"); 363 shutdownServices(); 364 365 removeMBeans(); 367 368 log.info("Legacy kernel shutdown complete"); 370 System.out.println("Legacy kernel shutdown complete"); 371 } 372 373 377 protected void shutdownServices() 378 { 379 try 380 { 381 controller.shutdown(); 383 } 384 catch (Exception e) 385 { 386 Throwable t = JMXExceptionDecoder.decode(e); 387 log.error("Failed to shutdown services", t); 388 } 389 } 390 391 395 protected void removeMBeans() 396 { 397 try 398 { 399 mbeanServer.unregisterMBean(ServiceControllerMBean.OBJECT_NAME); 400 mbeanServer.unregisterMBean(ServerConfigImplMBean.OBJECT_NAME); 401 mbeanServer.unregisterMBean(ServerImplMBean.OBJECT_NAME); 402 } 403 catch (Exception e) 404 { 405 Throwable t = JMXExceptionDecoder.decode(e); 406 log.error("Failed to unregister mbeans", t); 407 } 408 try 409 { 410 MBeanServer registeredServer = mbeanServer; 411 if (serverConfig.getPlatformMBeanServer() == true) 412 registeredServer = LazyMBeanServer.getRegisteredMBeanServer(mbeanServer); 413 MBeanServerFactory.releaseMBeanServer(registeredServer); 414 } 415 catch (Exception e) 416 { 417 Throwable t = JMXExceptionDecoder.decode(e); 418 log.error("Failed to release mbean mbeanServer", t); 419 } 420 } 421 422 423 429 private RepositoryClassLoader initBootLibraries() throws Exception 430 { 431 List <URL > list = new ArrayList <URL >(); 433 434 URL patchURL = serverConfig.getPatchURL(); 437 if (patchURL != null) 438 { 439 if (patchURL.getProtocol().equals("file")) 440 { 441 File dir = new File (patchURL.getFile()); 442 if (dir.exists()) 443 { 444 list.add(dir.toURL()); 446 447 File [] jars = dir.listFiles(new FileSuffixFilter(new String [] { ".jar", ".zip" }, true)); 449 450 for (int j = 0; jars != null && j < jars.length; j++) 451 { 452 list.add(jars[j].getCanonicalFile().toURL()); 453 } 454 } 455 } 456 else 457 { 458 list.add(patchURL); 459 } 460 } 461 462 list.add(serverConfig.getServerConfigURL()); 464 log.debug("Boot url list: " + list); 465 466 RepositoryClassLoader loader = null; 468 for (Iterator iter = list.iterator(); iter.hasNext();) 469 { 470 URL url = (URL )iter.next(); 471 log.debug("Creating loader for URL: " + url); 472 473 Object [] args = {url, Boolean.TRUE}; 475 String [] sig = {"java.net.URL", "boolean"}; 476 loader = (RepositoryClassLoader) mbeanServer.invoke(DEFAULT_LOADER_NAME, "newClassLoader", args, sig); 477 } 478 return loader; 479 } 480 481 489 private ObjectName createMBean(final String classname, String name) 490 throws Exception 491 { 492 ObjectName mbeanName = null; 493 if( name != null ) 494 mbeanName = new ObjectName (name); 495 try 496 { 497 URL xmbeanDD = new URL ("resource:xmdesc/"+classname+"-xmbean.xml"); 499 Object resource = mbeanServer.instantiate(classname); 500 Object [] args = {resource, xmbeanDD}; 502 String [] sig = {Object .class.getName(), URL .class.getName()}; 503 ObjectInstance instance = mbeanServer.createMBean("org.jboss.mx.modelmbean.XMBean", 504 mbeanName, 505 bootstrapUCLName, 506 args, 507 sig); 508 mbeanName = instance.getObjectName(); 509 log.debug("Created system XMBean: "+mbeanName); 510 } 511 catch(Exception e) 512 { 513 log.debug("Failed to create xmbean for: "+classname); 514 mbeanName = mbeanServer.createMBean(classname, mbeanName).getObjectName(); 515 log.debug("Created system MBean: " + mbeanName); 516 } 517 518 return mbeanName; 519 } 520 } 521 | Popular Tags |