ServerDiscoveryComponent.java
上传用户:xfwatch
上传日期:2020-12-14
资源大小:872k
文件大小:5k
源码类别:

中间件编程

开发平台:

Java

  1. /*
  2.  * RHQ Management Platform
  3.  * Copyright (C) 2005-2008 Red Hat, Inc.
  4.  * All rights reserved.
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation version 2 of the License.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19. package org.rhq.plugins.blacktie;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Properties;
  23. import java.util.Set;
  24. import javax.management.MBeanServerConnection;
  25. import javax.management.ObjectName;
  26. import javax.management.remote.JMXConnector;
  27. import javax.management.remote.JMXConnectorFactory;
  28. import javax.management.remote.JMXServiceURL;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.jboss.blacktie.jatmibroker.core.conf.XMLEnvHandler;
  32. import org.jboss.blacktie.jatmibroker.core.conf.XMLParser;
  33. import org.rhq.core.domain.configuration.Configuration;
  34. import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
  35. import org.rhq.core.pluginapi.inventory.ProcessScanResult;
  36. import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
  37. import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
  38. import org.w3c.dom.Element;
  39. /**
  40.  * This can be the start of your own custom plugin's discovery component. Review
  41.  * the javadoc for {@link ResourceDiscoveryComponent}.
  42.  * 
  43.  * @author John Mazzitelli
  44.  */
  45. public class ServerDiscoveryComponent implements ResourceDiscoveryComponent {
  46. private final Log log = LogFactory.getLog(ServerDiscoveryComponent.class);
  47. private MBeanServerConnection beanServerConnection;
  48. private ObjectName blacktieAdmin = null;
  49. /**
  50.  * Review the javadoc for both {@link ResourceDiscoveryComponent} and
  51.  * {@link ResourceDiscoveryContext} to learn what you need to do in this
  52.  * method.
  53.  * 
  54.  * @see ResourceDiscoveryComponent#discoverResources(ResourceDiscoveryContext)
  55.  */
  56. public Set<DiscoveredResourceDetails> discoverResources(
  57. ResourceDiscoveryContext context) {
  58. log.info("Discovering my custom plugin's resources");
  59. // if your plugin descriptor defined one or more <process-scan>s, then
  60. // see if the plugin container
  61. // auto-discovered processes using those process scan definitions.
  62. // Process all those that were found.
  63. List<ProcessScanResult> autoDiscoveryResults = context
  64. .getAutoDiscoveredProcesses();
  65. for (ProcessScanResult autoDiscoveryResult : autoDiscoveryResults) {
  66. // determine if you want to include the result in this method's
  67. // returned set of discovered resources
  68. }
  69. List<Configuration> pluginConfigs = context.getPluginConfigurations();
  70. for (Configuration pluginConfig : pluginConfigs) {
  71. // pluginConfig contains information on a resource that was manually
  72. // discovered/entered by the user
  73. // take it and build a details object that represents that resource
  74. }
  75. // now perform your own discovery mechanism, if you have one. For each
  76. // resource discovered, you need to
  77. // create a details object that describe the resource that you
  78. // discovered.
  79. HashSet<DiscoveredResourceDetails> set = new HashSet<DiscoveredResourceDetails>();
  80. // key = this must be a unique string across all of your resources - see
  81. // docs for uniqueness rules
  82. // name = this is the name you give the new resource; it does not
  83. // necessarily have to be unique
  84. // version = this is any string that corresponds to the resource's
  85. // version
  86. // description = this is any string that you want to assign as the
  87. // default description for your resource
  88. try {
  89. Properties prop = new Properties();
  90. XMLEnvHandler handler = new XMLEnvHandler(prop);
  91. XMLParser xmlenv = new XMLParser(handler, "btconfig.xsd");
  92. xmlenv.parse("btconfig.xml");
  93. JMXServiceURL u = new JMXServiceURL((String) prop.get("JMXURL"));
  94. JMXConnector c = JMXConnectorFactory.connect(u);
  95. beanServerConnection = c.getMBeanServerConnection();
  96. blacktieAdmin = new ObjectName("jboss.blacktie:service=Admin");
  97. // Get this list from the MBean so that we only need one service
  98. // with the list
  99. java.util.List<String> servers = (java.util.List<String>) beanServerConnection
  100. .invoke(blacktieAdmin, "getServerList",
  101. new Object[] {}, new String[] {});
  102. for (String server : servers) {
  103. DiscoveredResourceDetails resource = new DiscoveredResourceDetails(
  104. context.getResourceType(), server, server, null, null,
  105. null, null);
  106. set.add(resource);
  107. }
  108. } catch (Exception e) {
  109. log.equals("get servers error with " + e);
  110. }
  111. return set;
  112. }
  113. }