PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jboss-5.1.0/management/src/main/org/jboss/management/j2ee/LocalJBossServerDomain.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 776 lines | 422 code | 62 blank | 292 comment | 27 complexity | 66876c603647bbb2c5537e3ebf1d8b52 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.management.j2ee;
  23. import org.jboss.deployment.DeploymentInfo;
  24. import org.jboss.deployment.MainDeployerConstants;
  25. import org.jboss.deployment.SubDeployer;
  26. import org.jboss.logging.Logger;
  27. import org.jboss.management.j2ee.factory.DefaultManagedObjectFactoryMap;
  28. import org.jboss.management.j2ee.factory.ManagedObjectFactory;
  29. import org.jboss.management.j2ee.factory.ManagedObjectFactoryMap;
  30. import org.jboss.system.ServiceControllerMBean;
  31. import org.jboss.system.ServiceMBean;
  32. import org.jboss.system.ServiceMBeanSupport;
  33. import javax.management.JMException;
  34. import javax.management.MBeanException;
  35. import javax.management.MBeanServer;
  36. import javax.management.Notification;
  37. import javax.management.NotificationListener;
  38. import javax.management.ObjectName;
  39. import java.net.InetAddress;
  40. import java.util.Collection;
  41. import java.util.Iterator;
  42. import java.util.Set;
  43. /**
  44. * The integration MBean for the local JBoss server management domain. This
  45. * bridges between the core JBoss JSR-77 agnostic code to the JSR-77
  46. * managed object interfaces.
  47. *
  48. * @author <a href="mailto:andreas@jboss.org">Andreas Schaefer</a>
  49. * @author <a href="mailto:scott.stark@jboss.org">Scott Stark</a>
  50. * @version $Revision: 81025 $
  51. */
  52. public class LocalJBossServerDomain extends ServiceMBeanSupport
  53. implements NotificationListener, LocalJBossServerDomainMBean
  54. {
  55. /**
  56. * Class logger.
  57. */
  58. private static final Logger log = Logger.getLogger(LocalJBossServerDomain.class);
  59. private J2EEDomain serverDomain;
  60. /**
  61. * The name of the MainDeployer
  62. */
  63. private ObjectName mainDeployer;
  64. /**
  65. * The name of the SARDeployer
  66. */
  67. private ObjectName sarDeployer;
  68. /**
  69. * The name of the EARDeployer
  70. */
  71. private ObjectName earDeployer;
  72. /**
  73. * The name of the EJBDeployer
  74. */
  75. private ObjectName ejbDeployer;
  76. /**
  77. * The name of the RARDeployer
  78. */
  79. private ObjectName rarDeployer;
  80. /**
  81. * The name of the JCA connection manager deployer
  82. */
  83. private ObjectName jcaCMDeployer;
  84. /**
  85. * The name of the WARDeployer
  86. */
  87. private ObjectName warDeployer;
  88. /**
  89. * The client application deployer
  90. */
  91. private ObjectName carDeployer;
  92. /**
  93. * The name of the JMS service
  94. */
  95. private ObjectName jmsService;
  96. /**
  97. * The name of the JNDI service
  98. */
  99. private ObjectName jndiService;
  100. /**
  101. * The name of the JTA service
  102. */
  103. private ObjectName jtaService;
  104. /**
  105. * The name of the UserTransaction service
  106. */
  107. private ObjectName userTxService;
  108. /**
  109. * The name of the JavaMail service
  110. */
  111. private ObjectName mailService;
  112. /**
  113. * The name of the RMI_IIOP service
  114. */
  115. private ObjectName rmiiiopService;
  116. /**
  117. * The name of the service which emites URL binding events
  118. */
  119. private ObjectName jndiBindingService;
  120. /**
  121. * A mapping of JMX notifications to ManagedObjectFactory instances
  122. */
  123. private ManagedObjectFactoryMap managedObjFactoryMap;
  124. private Class managedObjFactoryMapClass = DefaultManagedObjectFactoryMap.class;
  125. // -------------------------------------------------------------------------
  126. // Constructors
  127. // -------------------------------------------------------------------------
  128. public LocalJBossServerDomain()
  129. {
  130. }
  131. // -------------------------------------------------------------------------
  132. // Properties (Getters/Setters)
  133. // -------------------------------------------------------------------------
  134. /**
  135. * @return The MainDeployer mbean name
  136. * @jmx:managed-attribute
  137. */
  138. public ObjectName getMainDeployer()
  139. {
  140. return mainDeployer;
  141. }
  142. /**
  143. * @param name The MainDeployer mbean name
  144. * @jmx:managed-attribute
  145. */
  146. public void setMainDeployer(ObjectName name)
  147. {
  148. this.mainDeployer = name;
  149. }
  150. /**
  151. * @return The SARDeployer mbean name
  152. * @jmx:managed-attribute
  153. */
  154. public ObjectName getSARDeployer()
  155. {
  156. return sarDeployer;
  157. }
  158. /**
  159. * @param name The SARDeployer mbean name
  160. * @jmx:managed-attribute
  161. */
  162. public void setSARDeployer(ObjectName name)
  163. {
  164. this.sarDeployer = name;
  165. }
  166. /**
  167. * @return The EARDeployer mbean name
  168. * @jmx:managed-attribute
  169. */
  170. public ObjectName getEARDeployer()
  171. {
  172. return earDeployer;
  173. }
  174. /**
  175. * @param name The EARDeployer mbean name
  176. * @jmx:managed-attribute
  177. */
  178. public void setEARDeployer(ObjectName name)
  179. {
  180. this.earDeployer = name;
  181. }
  182. /**
  183. * @return The EJBDeployer mbean name
  184. * @jmx:managed-attribute
  185. */
  186. public ObjectName getEJBDeployer()
  187. {
  188. return ejbDeployer;
  189. }
  190. /**
  191. * @param name The EJBDeployer mbean name
  192. * @jmx:managed-attribute
  193. */
  194. public void setEJBDeployer(ObjectName name)
  195. {
  196. this.ejbDeployer = name;
  197. }
  198. /**
  199. * @return The RARDeployer mbean name
  200. * @jmx:managed-attribute
  201. */
  202. public ObjectName getRARDeployer()
  203. {
  204. return rarDeployer;
  205. }
  206. /**
  207. * @param name The RARDeployer mbean name
  208. * @jmx:managed-attribute
  209. */
  210. public void setRARDeployer(ObjectName name)
  211. {
  212. this.rarDeployer = name;
  213. }
  214. /**
  215. * @return The JCA Connection manager deployer name
  216. * @jmx:managed-attribute
  217. */
  218. public ObjectName getCMDeployer()
  219. {
  220. return jcaCMDeployer;
  221. }
  222. /**
  223. * @param name The JCA Connection manager deployer name
  224. * @jmx:managed-attribute
  225. */
  226. public void setCMDeployer(ObjectName name)
  227. {
  228. this.jcaCMDeployer = name;
  229. }
  230. /**
  231. * @return The WARDeployer mbean name
  232. * @jmx:managed-attribute
  233. */
  234. public ObjectName getWARDeployer()
  235. {
  236. return warDeployer;
  237. }
  238. /**
  239. * @param name The WARDeployer mbean name
  240. * @jmx:managed-attribute
  241. */
  242. public void setWARDeployer(ObjectName name)
  243. {
  244. this.warDeployer = name;
  245. }
  246. /**
  247. * @return The CARDeployer mbean name
  248. * @jmx:managed-attribute
  249. */
  250. public ObjectName getCARDeployer()
  251. {
  252. return carDeployer;
  253. }
  254. /**
  255. * @param name The CARDeployer mbean name
  256. * @jmx:managed-attribute
  257. */
  258. public void setCARDeployer(ObjectName name)
  259. {
  260. this.carDeployer = name;
  261. }
  262. /**
  263. * @return The JMS service mbean name
  264. * @jmx:managed-attribute
  265. */
  266. public ObjectName getJMSService()
  267. {
  268. return jmsService;
  269. }
  270. /**
  271. * @param name The JMS service mbean name
  272. * @jmx:managed-attribute
  273. */
  274. public void setJMSService(ObjectName name)
  275. {
  276. this.jmsService = name;
  277. }
  278. /**
  279. * @return The JNDI service mbean name
  280. * @jmx:managed-attribute
  281. */
  282. public ObjectName getJNDIService()
  283. {
  284. return jndiService;
  285. }
  286. /**
  287. * @param name The JNDI service mbean name
  288. * @jmx:managed-attribute
  289. */
  290. public void setJNDIService(ObjectName name)
  291. {
  292. this.jndiService = name;
  293. }
  294. /**
  295. * @return The JTA service mbean name
  296. * @jmx:managed-attribute
  297. */
  298. public ObjectName getJTAService()
  299. {
  300. return jtaService;
  301. }
  302. /**
  303. * @param name The JTA service mbean name
  304. * @jmx:managed-attribute
  305. */
  306. public void setJTAService(ObjectName name)
  307. {
  308. this.jtaService = name;
  309. }
  310. /**
  311. * @return The JavaMail service mbean name
  312. * @jmx:managed-attribute
  313. */
  314. public ObjectName getMailService()
  315. {
  316. return mailService;
  317. }
  318. /**
  319. * @param name The JavaMail service mbean name
  320. * @jmx:managed-attribute
  321. */
  322. public void setMailService(ObjectName name)
  323. {
  324. this.mailService = name;
  325. }
  326. /**
  327. * @return The UserTransaction service mbean name
  328. * @jmx:managed-attribute
  329. */
  330. public ObjectName getUserTransactionService()
  331. {
  332. return userTxService;
  333. }
  334. /**
  335. * @param name The UserTransaction service mbean name
  336. * @jmx:managed-attribute
  337. */
  338. public void setUserTransactionService(ObjectName name)
  339. {
  340. this.userTxService = name;
  341. }
  342. /**
  343. * @return The RMI/IIOP service mbean name
  344. * @jmx:managed-attribute
  345. */
  346. public ObjectName getRMI_IIOPService()
  347. {
  348. return rmiiiopService;
  349. }
  350. /**
  351. * @param name The RMI/IIOP service mbean name
  352. * @jmx:managed-attribute
  353. */
  354. public void setRMI_IIOPService(ObjectName name)
  355. {
  356. this.rmiiiopService = name;
  357. }
  358. /**
  359. * @return The Jndi binding service mbean name
  360. * @jmx:managed-attribute
  361. */
  362. public ObjectName getJndiBindingService()
  363. {
  364. return jndiBindingService;
  365. }
  366. /**
  367. * @param name The Jndi binding service mbean name
  368. * @jmx:managed-attribute
  369. */
  370. public void setJndiBindingService(ObjectName name)
  371. {
  372. this.jndiBindingService = name;
  373. }
  374. /**
  375. * @return The ManagementObjFactoryMap class
  376. * @jmx:managed-attribute
  377. */
  378. public Class getManagementObjFactoryMapClass()
  379. {
  380. return managedObjFactoryMapClass;
  381. }
  382. /**
  383. * @param cls The ManagementObjFactoryMap class
  384. * @jmx:managed-attribute
  385. */
  386. public void setManagementObjFactoryMapClass(Class cls)
  387. {
  388. this.managedObjFactoryMapClass = cls;
  389. }
  390. /**
  391. * The JMX nofication callback. Here we create/destroy JSR77 MBeans based
  392. * on the create/destory notifications.
  393. *
  394. * @param msg the notification msg
  395. * @param handback currently unused
  396. */
  397. public void handleNotification(Notification msg, Object handback)
  398. {
  399. MBeanServer mbeanServer = getServer();
  400. if (managedObjFactoryMap == null || mbeanServer == null)
  401. {
  402. return;
  403. }
  404. log.debug("handleNotification: " + msg);
  405. String type = msg.getType();
  406. Object userData = msg.getUserData();
  407. try
  408. {
  409. /* As this section keeps growing I should change this to a command
  410. dispatch pattern as well. An issue here is that there is a choice
  411. made about what 'data' to pass the ManagedObjectFactory based on
  412. the event type that probably should be hidden in the factory map.
  413. */
  414. if (type.equals(ServiceMBean.CREATE_EVENT))
  415. {
  416. ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
  417. if (factory != null)
  418. {
  419. factory.create(mbeanServer, userData);
  420. }
  421. }
  422. else if (type.equals(ServiceMBean.DESTROY_EVENT))
  423. {
  424. ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
  425. if (factory != null)
  426. {
  427. factory.destroy(mbeanServer, userData);
  428. }
  429. }
  430. else if (type.equals(SubDeployer.START_NOTIFICATION))
  431. {
  432. ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
  433. if (factory != null)
  434. {
  435. factory.create(mbeanServer, userData);
  436. }
  437. }
  438. else if (type.equals(SubDeployer.DESTROY_NOTIFICATION))
  439. {
  440. ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
  441. if (factory != null)
  442. {
  443. DeploymentInfo di = (DeploymentInfo) msg.getUserData();
  444. factory.destroy(mbeanServer, di);
  445. }
  446. }
  447. else if (type.equals(MainDeployerConstants.ADD_DEPLOYER))
  448. {
  449. ObjectName deployerName = (ObjectName) msg.getUserData();
  450. registerWithDeployer(deployerName);
  451. }
  452. else if (type.equals(MainDeployerConstants.REMOVE_DEPLOYER))
  453. {
  454. ObjectName deployerName = (ObjectName) msg.getUserData();
  455. unregisterWithDeployer(deployerName);
  456. }
  457. }
  458. catch (Throwable t)
  459. {
  460. log.debug("Failed to handle event", t);
  461. }
  462. }
  463. public String toString()
  464. {
  465. return "LocalJBossServerDomain { " + super.toString() + " } []";
  466. }
  467. public void createService() throws Exception
  468. {
  469. MBeanServer server = getServer();
  470. setupJ2EEMBeans(server);
  471. registerWithController(server);
  472. populateFactoryMap();
  473. registerWithCurrentDeployers(server);
  474. }
  475. /**
  476. * Called to destroy the service. This unregisters with all deployers and
  477. * then removes all MBeans in this services domain to remove all JSR77
  478. * beans.
  479. *
  480. * @throws Exception
  481. */
  482. protected void destroyService() throws Exception
  483. {
  484. MBeanServer server = getServer();
  485. unregisterWithCurrentDeployers(server);
  486. cleanupLeftoverMBeans(server);
  487. unregisterWithController(server);
  488. }
  489. /**
  490. * Register as a listener of the given deployer.
  491. *
  492. * @param deployerName
  493. */
  494. protected void registerWithDeployer(ObjectName deployerName)
  495. {
  496. log.debug("Registering as listener of deployer: " + deployerName);
  497. try
  498. {
  499. getServer().addNotificationListener(deployerName, this, null, null);
  500. }
  501. catch (Exception e)
  502. {
  503. log.debug("Failed to register with deployer: " + deployerName, e);
  504. }
  505. }
  506. /**
  507. * Unregister as a listener of the given deployer.
  508. *
  509. * @param deployerName
  510. */
  511. protected void unregisterWithDeployer(ObjectName deployerName)
  512. {
  513. log.debug("Unregistering as listener of deployer: " + deployerName);
  514. try
  515. {
  516. getServer().removeNotificationListener(deployerName, this);
  517. }
  518. catch (Exception e)
  519. {
  520. log.debug("Failed to unregister with deployer: " + deployerName, e);
  521. }
  522. }
  523. /**
  524. * Build the ManagedObjectFactoryMap used to obtain the ManagedObjectFactory
  525. * instances from notification msgs.
  526. *
  527. * @throws Exception
  528. */
  529. private void populateFactoryMap() throws Exception
  530. {
  531. // Create the ManagedObjectFactoryMap
  532. managedObjFactoryMap = (ManagedObjectFactoryMap) managedObjFactoryMapClass.newInstance();
  533. managedObjFactoryMap.setSARDeployer(sarDeployer);
  534. managedObjFactoryMap.setEARDeployer(earDeployer);
  535. managedObjFactoryMap.setEJBDeployer(ejbDeployer);
  536. managedObjFactoryMap.setRARDeployer(rarDeployer);
  537. managedObjFactoryMap.setCMDeployer(jcaCMDeployer);
  538. managedObjFactoryMap.setWARDeployer(warDeployer);
  539. managedObjFactoryMap.setJMSResource(jmsService);
  540. managedObjFactoryMap.setJNDIResource(jndiService);
  541. managedObjFactoryMap.setJTAResource(jtaService);
  542. managedObjFactoryMap.setJTAResource(userTxService);
  543. managedObjFactoryMap.setJavaMailResource(mailService);
  544. managedObjFactoryMap.setRMI_IIOPResource(rmiiiopService);
  545. }
  546. /**
  547. * Create the J2EEServer and JVM MBeans.
  548. *
  549. * @param mbeanServer the MBeanServer to register the mbeans with.
  550. */
  551. private void setupJ2EEMBeans(MBeanServer mbeanServer)
  552. {
  553. // Create Server Component
  554. try
  555. {
  556. log.debug("setupJ2EEMBeans(), create J2EEServer instance");
  557. Package pkg = Package.getPackage("org.jboss");
  558. String vendor = pkg.getSpecificationVendor();
  559. String version = pkg.getImplementationVersion();
  560. // Create the createService
  561. serverDomain = new J2EEDomain(serviceName.getDomain());
  562. ObjectName domain = serverDomain.getObjectName();
  563. mbeanServer.registerMBean(serverDomain, domain);
  564. // Create single Local J2EEServer MBean
  565. J2EEServer j2eeServer = new J2EEServer("Local", domain, vendor, version);
  566. ObjectName lServer = j2eeServer.getObjectName();
  567. mbeanServer.registerMBean(j2eeServer, lServer);
  568. // Create the JVM MBean
  569. String hostName = "localhost";
  570. try
  571. {
  572. InetAddress lLocalHost = InetAddress.getLocalHost();
  573. hostName = lLocalHost.getHostName();
  574. }
  575. catch (Exception e)
  576. {
  577. // Ignore when host address is not accessible (localhost is used instead)
  578. }
  579. String vmVendor = System.getProperty("java.vendor");
  580. String vmVersion = System.getProperty("java.version");
  581. String name = vmVendor + " " + vmVersion;
  582. JVM jvm = new JVM(name, lServer, vmVersion, vmVendor, hostName);
  583. ObjectName jvmName = jvm.getObjectName();
  584. mbeanServer.registerMBean(jvm, jvmName);
  585. }
  586. catch (JMException jme)
  587. {
  588. log.debug("setupJ2EEMBeans - unexpected JMException", jme);
  589. }
  590. catch (Exception e)
  591. {
  592. log.debug("setupJ2EEMBeans - unexpected exception", e);
  593. }
  594. }
  595. /**
  596. * Register as a listener of the ServiceControllerMBean
  597. *
  598. * @param mbeanServer
  599. */
  600. private void registerWithController(MBeanServer mbeanServer)
  601. {
  602. try
  603. {
  604. mbeanServer.addNotificationListener(ServiceControllerMBean.OBJECT_NAME,
  605. this, null, null);
  606. log.debug("Registered as listener of: " + ServiceControllerMBean.OBJECT_NAME);
  607. }
  608. catch (JMException jme)
  609. {
  610. log.debug("unexpected exception", jme);
  611. }
  612. catch (Exception e)
  613. {
  614. log.debug("unexpected exception", e);
  615. }
  616. }
  617. /**
  618. * Unregister as a listener of the ServiceControllerMBean.
  619. *
  620. * @param mbeanServer
  621. */
  622. private void unregisterWithController(MBeanServer mbeanServer)
  623. {
  624. try
  625. {
  626. mbeanServer.removeNotificationListener(ServiceControllerMBean.OBJECT_NAME, this);
  627. log.debug("UNRegistered as listener of: " + ServiceControllerMBean.OBJECT_NAME);
  628. }
  629. catch (JMException jme)
  630. {
  631. log.debug("unexpected exception", jme);
  632. }
  633. catch (Exception e)
  634. {
  635. log.debug("unexpected exception", e);
  636. }
  637. }
  638. /**
  639. * Register with deployers known to the MainDeployer
  640. *
  641. * @param mbeanServer
  642. * @throws Exception thrown on failure to register as a listener of the
  643. * MainDeployer or to obtain the list of deployers
  644. */
  645. private void registerWithCurrentDeployers(MBeanServer mbeanServer)
  646. throws Exception
  647. {
  648. log.debug("Registering with all deployers, mainDeployer=" + mainDeployer);
  649. mbeanServer.addNotificationListener(mainDeployer, this, null, null);
  650. // Obtain the deployers list
  651. log.debug("Getting current deployers");
  652. Object[] args = {};
  653. String[] sig = {};
  654. Collection deployers = (Collection) mbeanServer.invoke(mainDeployer,
  655. "listDeployers", args, sig);
  656. Iterator iter = deployers.iterator();
  657. while (iter.hasNext())
  658. {
  659. ObjectName name = (ObjectName) iter.next();
  660. registerWithDeployer(name);
  661. }
  662. }
  663. /**
  664. * Unregister with all deployers known to the MainDeployer
  665. *
  666. * @param mbeanServer
  667. * @throws Exception thrown on failure to unregister as a listener of the
  668. * MainDeployer or to obtain the list of deployers
  669. */
  670. private void unregisterWithCurrentDeployers(MBeanServer mbeanServer)
  671. throws Exception
  672. {
  673. log.debug("Unregistering with all deployers, mainDeployer=" + mainDeployer);
  674. mbeanServer.removeNotificationListener(mainDeployer, this);
  675. // Obtain the deployers list
  676. log.debug("Getting current deployers");
  677. Object[] args = {};
  678. String[] sig = {};
  679. Collection deployers = (Collection) mbeanServer.invoke(mainDeployer,
  680. "listDeployers", args, sig);
  681. Iterator iter = deployers.iterator();
  682. while (iter.hasNext())
  683. {
  684. ObjectName name = (ObjectName) iter.next();
  685. unregisterWithDeployer(name);
  686. }
  687. }
  688. /**
  689. * Query for all mbeans in this services domain and unregisters them.
  690. *
  691. * @throws Exception if the domain query fails
  692. */
  693. private void cleanupLeftoverMBeans(MBeanServer mbeanServer) throws Exception
  694. {
  695. String domain = serviceName.getDomain();
  696. ObjectName domainName = new ObjectName(domain + ":*");
  697. Set domainNames = mbeanServer.queryNames(domainName, null);
  698. log.debug("Found " + domainNames.size() + " domain mbeans");
  699. Iterator domainIter = domainNames.iterator();
  700. while (domainIter.hasNext())
  701. {
  702. try
  703. {
  704. ObjectName name = (ObjectName) domainIter.next();
  705. if (name.equals(serviceName))
  706. continue;
  707. server.unregisterMBean(name);
  708. }
  709. catch (MBeanException ignore)
  710. {
  711. }
  712. }
  713. }
  714. }