diff options
Diffstat (limited to 'integrity-monitor/src/main/java/org/onap/policy/common/im/jmx')
4 files changed, 299 insertions, 309 deletions
diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java index eb1d9f8b..bd7ed7b5 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdmin.java @@ -32,197 +32,197 @@ import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.onap.policy.common.im.IntegrityMonitor; import org.onap.policy.common.im.IntegrityMonitorException; import org.onap.policy.common.im.StateManagement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Base class for component MBeans. */ public class ComponentAdmin implements ComponentAdminMBean { - private static final String STATE_MANAGER = "stateManager"; - - private static final Logger logger = LoggerFactory.getLogger(ComponentAdmin.class.getName()); - - private final String name; - private MBeanServer registeredMBeanServer; - private ObjectName registeredObjectName; - private IntegrityMonitor integrityMonitor = null; - private StateManagement stateManager = null; - - /** - * Constructor. - * @param name the MBean name - * @param integrityMonitor - * @param stateManager - * @throws ComponentAdminException - */ - public ComponentAdmin(String name, IntegrityMonitor integrityMonitor, StateManagement stateManager) throws ComponentAdminException { - if ((name == null) || (integrityMonitor == null) || (stateManager == null)) { - logger.error("Error: ComponentAdmin constructor called with invalid input"); - throw new ComponentAdminException("null input"); - } - - this.name = "ONAP_POLICY_COMP:name=" + name; - this.integrityMonitor = integrityMonitor; - this.stateManager = stateManager; - - try { - register(); - } catch (ComponentAdminException e) { - logger.debug("Failed to register ComponentAdmin MBean"); - throw e; - } - } - - /** - * Registers with the MBean server. - * @throws ComponentAdminException a JMX exception - */ - public synchronized void register() throws ComponentAdminException { - - try { - logger.debug("Registering {} MBean", name); - - MBeanServer mbeanServer = findMBeanServer(); - - if (mbeanServer == null) { - return; - } - - ObjectName objectName = new ObjectName(name); - - if (mbeanServer.isRegistered(objectName)) { - logger.debug("Unregistering a previously registered {} MBean", name); - mbeanServer.unregisterMBean(objectName); - } - - mbeanServer.registerMBean(this, objectName); - registeredMBeanServer = mbeanServer; - registeredObjectName = objectName; - - } catch (MalformedObjectNameException | MBeanRegistrationException | InstanceNotFoundException | InstanceAlreadyExistsException | NotCompliantMBeanException e) { - throw new ComponentAdminException(e); - } - } - - /** - * Checks if this MBean is registered with the MBeanServer. - * @return true if this MBean is registered with the MBeanServer. - */ - public boolean isRegistered() { - return registeredObjectName != null; - } - - /** - * Unregisters with the MBean server. - * @throws ComponentAdminException a JMX exception - */ - public synchronized void unregister() throws ComponentAdminException { - - if (registeredObjectName == null) { - return; - } - - - try { - registeredMBeanServer.unregisterMBean(registeredObjectName); - - } catch (MBeanRegistrationException | InstanceNotFoundException e) { - throw new ComponentAdminException(e); - } - - registeredMBeanServer = null; - registeredObjectName = null; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return ComponentAdmin.class.getSimpleName() + "[" + name + "]"; - } - - /** - * Finds the MBeanServer. - * @return the MBeanServer, or null if it is not found - */ - public static MBeanServer findMBeanServer() { - ArrayList<MBeanServer> mbeanServers = - MBeanServerFactory.findMBeanServer(null); - - Iterator<MBeanServer> iter = mbeanServers.iterator(); - MBeanServer mbeanServer; - - while (iter.hasNext()) { - mbeanServer = iter.next(); - if ("DefaultDomain".equals(mbeanServer.getDefaultDomain())) { - return mbeanServer; - } - } - - return null; - } - - /** - * Creates the MBeanServer (intended for unit testing only). - * @return the MBeanServer - */ - public static MBeanServer createMBeanServer() { - return MBeanServerFactory.createMBeanServer("DefaultDomain"); - } - - /** - * Get the MBean object name for the specified feature name. - * @param componentName component name - * @return the object name - * @throws MalformedObjectNameException a JMX exception - */ - public static ObjectName getObjectName(String componentName) - throws MalformedObjectNameException { - return new ObjectName("ONAP_POLICY_COMP:name=" + componentName); - } - - @Override - public void test() throws IntegrityMonitorException { - // Call evaluateSanity on IntegrityMonitor to run the test - logger.debug("test() called..."); - if (integrityMonitor != null) { - integrityMonitor.evaluateSanity(); - } - else { - logger.error("Unable to invoke test() - state manager instance is null"); - throw new ComponentAdminException(STATE_MANAGER); - } - - } - - @Override - public void lock() throws IntegrityMonitorException { - logger.debug("lock() called..."); - if (stateManager != null) { - stateManager.lock(); - } - else { - logger.error("Unable to invoke lock() - state manager instance is null"); - throw new ComponentAdminException(STATE_MANAGER); - } - } - - @Override - public void unlock() throws IntegrityMonitorException { - logger.debug("unlock() called..."); - if (stateManager != null) { - stateManager.unlock(); - } - else { - logger.error("Unable to invoke unlock() - state manager instance is null"); - throw new ComponentAdminException(STATE_MANAGER); - } - - } + private static final String STATE_MANAGER = "stateManager"; + + private static final Logger logger = LoggerFactory.getLogger(ComponentAdmin.class.getName()); + + private final String name; + private MBeanServer registeredMBeanServer; + private ObjectName registeredObjectName; + private IntegrityMonitor integrityMonitor = null; + private StateManagement stateManager = null; + + /** + * Constructor. + * + * @param name the MBean name + * @param integrityMonitor the integrity monitor + * @param stateManager the state manager + * @throws ComponentAdminException if an error occurs + */ + public ComponentAdmin(String name, IntegrityMonitor integrityMonitor, StateManagement stateManager) + throws ComponentAdminException { + if ((name == null) || (integrityMonitor == null) || (stateManager == null)) { + logger.error("Error: ComponentAdmin constructor called with invalid input"); + throw new ComponentAdminException("null input"); + } + + this.name = "ONAP_POLICY_COMP:name=" + name; + this.integrityMonitor = integrityMonitor; + this.stateManager = stateManager; + + try { + register(); + } catch (ComponentAdminException e) { + logger.debug("Failed to register ComponentAdmin MBean"); + throw e; + } + } + + /** + * Registers with the MBean server. + * + * @throws ComponentAdminException a JMX exception + */ + public synchronized void register() throws ComponentAdminException { + + try { + logger.debug("Registering {} MBean", name); + + MBeanServer mbeanServer = findMBeanServer(); + + if (mbeanServer == null) { + return; + } + + ObjectName objectName = new ObjectName(name); + + if (mbeanServer.isRegistered(objectName)) { + logger.debug("Unregistering a previously registered {} MBean", name); + mbeanServer.unregisterMBean(objectName); + } + + mbeanServer.registerMBean(this, objectName); + registeredMBeanServer = mbeanServer; + registeredObjectName = objectName; + + } catch (MalformedObjectNameException | MBeanRegistrationException | InstanceNotFoundException + | InstanceAlreadyExistsException | NotCompliantMBeanException e) { + throw new ComponentAdminException(e); + } + } + + /** + * Checks if this MBean is registered with the MBeanServer. + * + * @return true if this MBean is registered with the MBeanServer. + */ + public boolean isRegistered() { + return registeredObjectName != null; + } + + /** + * Unregisters with the MBean server. + * + * @throws ComponentAdminException a JMX exception + */ + public synchronized void unregister() throws ComponentAdminException { + + if (registeredObjectName == null) { + return; + } + + + try { + registeredMBeanServer.unregisterMBean(registeredObjectName); + + } catch (MBeanRegistrationException | InstanceNotFoundException e) { + throw new ComponentAdminException(e); + } + + registeredMBeanServer = null; + registeredObjectName = null; + } + + @Override + public String toString() { + return ComponentAdmin.class.getSimpleName() + "[" + name + "]"; + } + + /** + * Finds the MBeanServer. + * + * @return the MBeanServer, or null if it is not found + */ + public static MBeanServer findMBeanServer() { + ArrayList<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer(null); + + Iterator<MBeanServer> iter = mbeanServers.iterator(); + MBeanServer mbeanServer; + + while (iter.hasNext()) { + mbeanServer = iter.next(); + if ("DefaultDomain".equals(mbeanServer.getDefaultDomain())) { + return mbeanServer; + } + } + + return null; + } + + /** + * Creates the MBeanServer (intended for unit testing only). + * + * @return the MBeanServer + */ + public static MBeanServer createMBeanServer() { + return MBeanServerFactory.createMBeanServer("DefaultDomain"); + } + + /** + * Get the MBean object name for the specified feature name. + * + * @param componentName component name + * @return the object name + * @throws MalformedObjectNameException a JMX exception + */ + public static ObjectName getObjectName(String componentName) throws MalformedObjectNameException { + return new ObjectName("ONAP_POLICY_COMP:name=" + componentName); + } + + @Override + public void test() throws IntegrityMonitorException { + // Call evaluateSanity on IntegrityMonitor to run the test + logger.debug("test() called..."); + if (integrityMonitor != null) { + integrityMonitor.evaluateSanity(); + } else { + logger.error("Unable to invoke test() - state manager instance is null"); + throw new ComponentAdminException(STATE_MANAGER); + } + + } + + @Override + public void lock() throws IntegrityMonitorException { + logger.debug("lock() called..."); + if (stateManager != null) { + stateManager.lock(); + } else { + logger.error("Unable to invoke lock() - state manager instance is null"); + throw new ComponentAdminException(STATE_MANAGER); + } + } + + @Override + public void unlock() throws IntegrityMonitorException { + logger.debug("unlock() called..."); + if (stateManager != null) { + stateManager.unlock(); + } else { + logger.error("Unable to invoke unlock() - state manager instance is null"); + throw new ComponentAdminException(STATE_MANAGER); + } + + } } diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminException.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminException.java index 5ada6da0..dcc4da0a 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminException.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminException.java @@ -23,22 +23,22 @@ package org.onap.policy.common.im.jmx; import org.onap.policy.common.im.IntegrityMonitorException; public class ComponentAdminException extends IntegrityMonitorException { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - public ComponentAdminException() { - super(); - } + public ComponentAdminException() { + super(); + } - public ComponentAdminException(String message) { - super(message); - } + public ComponentAdminException(String message) { + super(message); + } - public ComponentAdminException(Throwable cause) { - super(cause); - } + public ComponentAdminException(Throwable cause) { + super(cause); + } - public ComponentAdminException(String message, Throwable cause) { - super(message, cause); - } + public ComponentAdminException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminMBean.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminMBean.java index 2c011935..3276cf63 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminMBean.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/ComponentAdminMBean.java @@ -26,27 +26,24 @@ import org.onap.policy.common.im.IntegrityMonitorException; * Provides operations to test health, lock and unlock components. */ public interface ComponentAdminMBean { - /** - * Test health of component. - * - * @throws IntegrityMonitorException - * if the component fails the health check - */ - void test() throws IntegrityMonitorException; + /** + * Test health of component. + * + * @throws IntegrityMonitorException if the component fails the health check + */ + void test() throws IntegrityMonitorException; - /** - * Administratively lock component. - * - * @throws IntegrityMonitorException - * if the component lock fails - */ - void lock() throws IntegrityMonitorException; - - /** - * Administratively unlock component. - * - * @throws IntegrityMonitorException - * if the component unlock fails - */ - void unlock() throws IntegrityMonitorException; + /** + * Administratively lock component. + * + * @throws IntegrityMonitorException if the component lock fails + */ + void lock() throws IntegrityMonitorException; + + /** + * Administratively unlock component. + * + * @throws IntegrityMonitorException if the component unlock fails + */ + void unlock() throws IntegrityMonitorException; } diff --git a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java index c9a2ce80..b71751f0 100644 --- a/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java +++ b/integrity-monitor/src/main/java/org/onap/policy/common/im/jmx/JmxAgentConnection.java @@ -18,9 +18,6 @@ * ============LICENSE_END========================================================= */ -/** - * - */ package org.onap.policy.common.im.jmx; import java.io.IOException; @@ -43,93 +40,89 @@ import org.onap.policy.common.logging.flexlogger.Logger; * Class to create a JMX RMI connection to the JmxAgent. */ public final class JmxAgentConnection { - + private static final Logger logger = FlexLogger.getLogger(JmxAgentConnection.class); - private static final String DEFAULT_HOST = "localhost"; - private static final String DEFAULT_PORT = "9996"; - - private String host; - private String port; - private JMXConnector connector; - private String jmxUrl = null; - - /** - * Set up the host/port from the properties. Use defaults if missing from the properties. - * @param properties the properties used to look for host and port - */ - public JmxAgentConnection() { - host = DEFAULT_HOST; - port = DEFAULT_PORT; - } - - public JmxAgentConnection(String url) { - jmxUrl = url; - } - - /** - * Generate jmxAgent url. - * service:jmx:rmi:///jndi/rmi://host.domain:9999/jmxAgent - * - * @param host - * host.domain - * @param port - * 9999 - * @return jmxAgent url. - */ - private static String jmxAgentUrl(String host, String port) { - - return "service:jmx:rmi:///jndi/rmi://" + host + ":" + port - + "/jmxrmi"; - } - - /** - * Get a connection to the jmxAgent MBeanServer. - * @return the connection - * @throws IntegrityMonitorException on error - */ - public MBeanServerConnection getMBeanConnection() throws IntegrityMonitorException { - - try { - JMXServiceURL url; - if (jmxUrl == null) { - url = new JMXServiceURL(jmxAgentUrl(host, port)); - } - else { - url = new JMXServiceURL(jmxUrl); - } - Map<String, Object> env = new HashMap<>(); - - connector = JMXConnectorFactory.newJMXConnector(url, env); - connector.connect(); - connector.addConnectionNotificationListener( - new NotificationListener() { - - @Override - public void handleNotification( - Notification notification, Object handback) { - if (notification.getType().equals( - JMXConnectionNotification.FAILED)) { - // handle disconnect - disconnect(); - } - } - }, null, null); - - return connector.getMBeanServerConnection(); - - } catch (IOException e) { - throw new IntegrityMonitorException(e); - } - } - - /** - * Disconnect. - */ - public void disconnect() { - if (connector != null) { - try { connector.close(); } catch (IOException e) { logger.debug(e); } - } - } + private static final String DEFAULT_HOST = "localhost"; + private static final String DEFAULT_PORT = "9996"; + + private String host; + private String port; + private JMXConnector connector; + private String jmxUrl = null; + + /** + * Set up the host/port from the properties. Use defaults if missing from the properties. + */ + public JmxAgentConnection() { + host = DEFAULT_HOST; + port = DEFAULT_PORT; + } + + public JmxAgentConnection(String url) { + jmxUrl = url; + } + + /** + * Generate jmxAgent url. service:jmx:rmi:///jndi/rmi://host.domain:9999/jmxAgent + * + * @param host host.domain + * @param port 9999 + * @return jmxAgent url. + */ + private static String jmxAgentUrl(String host, String port) { + + return "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"; + } + + /** + * Get a connection to the jmxAgent MBeanServer. + * + * @return the connection + * @throws IntegrityMonitorException on error + */ + public MBeanServerConnection getMBeanConnection() throws IntegrityMonitorException { + + try { + JMXServiceURL url; + if (jmxUrl == null) { + url = new JMXServiceURL(jmxAgentUrl(host, port)); + } else { + url = new JMXServiceURL(jmxUrl); + } + Map<String, Object> env = new HashMap<>(); + + connector = JMXConnectorFactory.newJMXConnector(url, env); + connector.connect(); + connector.addConnectionNotificationListener(new NotificationListener() { + + @Override + public void handleNotification(Notification notification, Object handback) { + if (notification.getType().equals(JMXConnectionNotification.FAILED)) { + // handle disconnect + disconnect(); + } + } + }, null, null); + + return connector.getMBeanServerConnection(); + + } catch (IOException e) { + throw new IntegrityMonitorException(e); + } + } + + /** + * Disconnect. + */ + public void disconnect() { + if (connector != null) { + try { + connector.close(); + } catch (IOException e) { + logger.debug(e); + } + } + } } |