com.continuent.tungsten.common.jmx
Class JmxManager

java.lang.Object
  extended by com.continuent.tungsten.common.jmx.JmxManager
All Implemented Interfaces:
java.util.EventListener, javax.management.NotificationListener

public class JmxManager
extends java.lang.Object
implements javax.management.NotificationListener

Encapsulates JMX server start/stop and provides static utility methods to register MBeans on the server side as well as get proxies for them on the client side.

Version:
1.0
Author:
Robert Hodges

Field Summary
static java.lang.String CREATE_MBEAN_HELPER
           
protected  javax.management.remote.JMXConnectorServer jmxConnectorServer
           
protected  java.rmi.registry.Registry rmiRegistry
           
 
Constructor Summary
JmxManager(java.lang.String host, int beanPort, int registryPort, java.lang.String serviceName)
          Creates an instance to manage a JMX service
JmxManager(java.lang.String host, int registryPort, java.lang.String serviceName)
          Creates an instance to manage a JMX service
JmxManager(java.lang.String host, int registryPort, java.lang.String serviceName, TungstenProperties tungstenProperty)
          Creates an instance to manage a JMX service Called when using authentication (and) encryption
 
Method Summary
static void addNotificationListener(javax.management.remote.JMXConnector jmxConnector, java.lang.Class<?> mbeanClass, javax.management.NotificationListener notificationListener)
          Attach NotificationListener that can be used to listen notifications emitted by MBean server.
static void addNotificationListener(javax.management.remote.JMXConnector jmxConnector, java.lang.Class<?> mbeanInterface, java.lang.String mbeanName, javax.management.NotificationListener notificationListener, boolean ignored)
          Attach NotificationListener that can be used to listen notifications emitted by MBean server.
static DynamicMBeanHelper createHelper(java.lang.Class<?> mbeanClass)
           
static DynamicMBeanHelper createHelper(java.lang.Class<?> mbeanClass, java.lang.String alias)
           
protected  void createRegistry(int port)
          Starts the rmi registry.
static javax.management.ObjectName generateMBeanObjectName(java.lang.Class<?> mbeanClass)
           
static javax.management.ObjectName generateMBeanObjectName(java.lang.String mbeanName, java.lang.String typeName)
           
 int getBeanPort()
           
static java.lang.String getHostName()
          Get the hostname from the local host.
static java.lang.Object getMBeanProxy(javax.management.remote.JMXConnector clientConnection, java.lang.Class<?> mbeanClass, boolean notificationBroadcaster)
          Client helper method to obtain a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.
static java.lang.Object getMBeanProxy(javax.management.remote.JMXConnector clientConnection, java.lang.Class<?> mbeanClass, java.lang.Class<?> mbeanInterface, java.lang.String mbeanName, boolean notificationBroadcaster, boolean ignored)
          Client helper method to obtain a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.
static java.lang.Object getMBeanProxy(javax.management.remote.JMXConnector clientConnection, java.lang.Class<?> mbeanClass, java.lang.String mbeanName, boolean notificationBroadcaster, boolean ignored)
          Client helper method to obtain a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.
 java.rmi.registry.Registry getRegistry()
           
static javax.management.remote.JMXConnector getRMIConnector(java.lang.String host, int registryPort, java.lang.String serviceName)
          Client helper method to return an RMI connection.
static javax.management.remote.JMXConnector getRMIConnector(java.lang.String host, int registryPort, java.lang.String serviceName, TungstenProperties jmxProperties)
          Client helper method to return an RMI connection.
static javax.management.MBeanServerConnection getServerConnection(javax.management.remote.JMXConnector jmxConnector)
           
 void handleNotification(javax.management.Notification notification, java.lang.Object handback)
           
protected  java.rmi.registry.Registry locateDefaultRegistry()
           
static void registerMBean(java.lang.Object mbean, java.lang.Class<?> mbeanClass)
          Server helper method to register a JMX MBean.
static void registerMBean(java.lang.Object mbean, java.lang.Class<?> mbeanInterface, java.lang.String mbeanName, boolean ignored)
          Server helper method to register a JMX MBean.
static void removeNotificationListener(javax.management.remote.JMXConnector jmxConnector, java.lang.Class<?> mbeanClass, javax.management.NotificationListener notificationListener)
          Remove NotificationListener from this MBean.
static void removeNotificationListener(javax.management.remote.JMXConnector jmxConnector, java.lang.Class<?> mbeanInterface, java.lang.String mbeanName, javax.management.NotificationListener notificationListener, boolean ignored)
          Remove NotificationListener from this MBean.
 void start()
          Starts the JXM server.
protected  void startJmxConnector()
          Starts the JMX connector for the server.
 void stop()
          Stops the JXM server.
protected  void stopJmxConnector()
          Stops the JMX connector if it is running.
protected  void stopRMI()
          Deallocates the RMI registry.
static void unregisterMBean(java.lang.Class<?> mbeanInterface)
          Server helper method to register a JMX MBean.
static void unregisterMBean(java.lang.Class<?> mbeanInterface, java.lang.String mbeanName)
          Server helper method to register a JMX MBean.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rmiRegistry

protected java.rmi.registry.Registry rmiRegistry

jmxConnectorServer

protected javax.management.remote.JMXConnectorServer jmxConnectorServer

CREATE_MBEAN_HELPER

public static final java.lang.String CREATE_MBEAN_HELPER
See Also:
Constant Field Values
Constructor Detail

JmxManager

public JmxManager(java.lang.String host,
                  int beanPort,
                  int registryPort,
                  java.lang.String serviceName)
Creates an instance to manage a JMX service

Parameters:
host - The host name or IP to use
beanPort - TODO
registryPort - The JMX server RMI registryPort
serviceName - The JMX service name

JmxManager

public JmxManager(java.lang.String host,
                  int registryPort,
                  java.lang.String serviceName)
Creates an instance to manage a JMX service

Parameters:
host - The host name or IP to use
registryPort - The JMX server RMI registryPort
serviceName - The JMX service name

JmxManager

public JmxManager(java.lang.String host,
                  int registryPort,
                  java.lang.String serviceName,
                  TungstenProperties tungstenProperty)
Creates an instance to manage a JMX service Called when using authentication (and) encryption

Parameters:
host -
registryPort -
serviceName -
tungstenProperty -
See Also:
JMX Tutorial on Security
Method Detail

start

public void start()
Starts the JXM server.


stop

public void stop()
Stops the JXM server.


locateDefaultRegistry

protected java.rmi.registry.Registry locateDefaultRegistry()

createRegistry

protected void createRegistry(int port)
Starts the rmi registry.


stopRMI

protected void stopRMI()
Deallocates the RMI registry.


startJmxConnector

protected void startJmxConnector()
Starts the JMX connector for the server.


stopJmxConnector

protected void stopJmxConnector()
Stops the JMX connector if it is running.


registerMBean

public static void registerMBean(java.lang.Object mbean,
                                 java.lang.Class<?> mbeanInterface,
                                 java.lang.String mbeanName,
                                 boolean ignored)
Server helper method to register a JMX MBean. MBeans are registered by a combination of their MBean interface and the custom mbeanName argument. The mbeanName permits multiple mBeans to be registered under the same name.

Parameters:
mbean - The MBean instance that should be registered
mbeanInterface - The MBean interface this instance implements
mbeanName - A custom name for this MBean
Throws:
ServerRuntimeException

registerMBean

public static void registerMBean(java.lang.Object mbean,
                                 java.lang.Class<?> mbeanClass)
Server helper method to register a JMX MBean. MBeans are registered by a combination of their MBean interface and the custom mbeanName argument. The mbeanName permits multiple mBeans to be registered under the same name.

Parameters:
mbean - The MBean instance that should be registered
mbeanClass - The base class for the mbean
Throws:
ServerRuntimeException

unregisterMBean

public static void unregisterMBean(java.lang.Class<?> mbeanInterface,
                                   java.lang.String mbeanName)
Server helper method to register a JMX MBean. MBeans are registered by a combination of their MBean interface and the custom mbeanName argument. The mbeanName permits multiple mBeans to be registered under the same name.

Parameters:
mbeanInterface - The MBean interface this instance implements
mbeanName - A custom name for this MBean
Throws:
ServerRuntimeException

unregisterMBean

public static void unregisterMBean(java.lang.Class<?> mbeanInterface)
Server helper method to register a JMX MBean. MBeans are registered by a combination of their MBean interface and the custom mbeanName argument. The mbeanName permits multiple mBeans to be registered under the same name.

Parameters:
mbeanInterface - The MBean interface this instance implements
Throws:
ServerRuntimeException

getRMIConnector

public static javax.management.remote.JMXConnector getRMIConnector(java.lang.String host,
                                                                   int registryPort,
                                                                   java.lang.String serviceName)
Client helper method to return an RMI connection. The arguments match those used when instantiating the JmxManager class itself.

Parameters:
host - the hostname to bind to in the jmx url
registryPort - the registryPort number to bind to in the jmx url
serviceName - the JMX service name
Returns:
a connection to the server

getRMIConnector

public static javax.management.remote.JMXConnector getRMIConnector(java.lang.String host,
                                                                   int registryPort,
                                                                   java.lang.String serviceName,
                                                                   TungstenProperties jmxProperties)
Client helper method to return an RMI connection. The arguments match those used when instantiating the JmxManager class itself.

Parameters:
host - the hostname to bind to in the jmx url
registryPort - the registryPort number to bind to in the jmx url
serviceName - the JMX service name
jmxProperties - TungstenProperties holding the AuthenticationInfo instance.
Returns:
a connection to the server

getMBeanProxy

public static java.lang.Object getMBeanProxy(javax.management.remote.JMXConnector clientConnection,
                                             java.lang.Class<?> mbeanClass,
                                             java.lang.String mbeanName,
                                             boolean notificationBroadcaster,
                                             boolean ignored)
Client helper method to obtain a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.

Parameters:
clientConnection - the MBean server to forward to
mbeanClass - The MBean interface this instance implements
mbeanName - A custom name for this MBean
notificationBroadcaster - If true make the returned proxy implement NotificationEmitter by forwarding its methods via connection
Returns:
An MBean proxy

getMBeanProxy

public static java.lang.Object getMBeanProxy(javax.management.remote.JMXConnector clientConnection,
                                             java.lang.Class<?> mbeanClass,
                                             boolean notificationBroadcaster)
Client helper method to obtain a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.

Parameters:
clientConnection - the MBean server to forward to
mbeanClass - The class for which an MBean exists
notificationBroadcaster - If true make the returned proxy implement NotificationEmitter by forwarding its methods via connection
Returns:
An MBean proxy

getMBeanProxy

public static java.lang.Object getMBeanProxy(javax.management.remote.JMXConnector clientConnection,
                                             java.lang.Class<?> mbeanClass,
                                             java.lang.Class<?> mbeanInterface,
                                             java.lang.String mbeanName,
                                             boolean notificationBroadcaster,
                                             boolean ignored)
Client helper method to obtain a proxy that implements the given interface by forwarding its methods through the given MBean server to the named MBean.

Parameters:
clientConnection - the MBean server to forward to
mbeanClass - The MBean interface this instance implements
mbeanName - A custom name for this MBean
notificationBroadcaster - If true make the returned proxy implement NotificationEmitter by forwarding its methods via connection
Returns:
An MBean proxy

addNotificationListener

public static void addNotificationListener(javax.management.remote.JMXConnector jmxConnector,
                                           java.lang.Class<?> mbeanInterface,
                                           java.lang.String mbeanName,
                                           javax.management.NotificationListener notificationListener,
                                           boolean ignored)
                                    throws javax.management.InstanceNotFoundException,
                                           java.lang.Exception
Attach NotificationListener that can be used to listen notifications emitted by MBean server.

Parameters:
jmxConnector - The MBean server connector.
mbeanInterface - The MBean interface this instance implements.
mbeanName - A custom name for the MBean.
notificationListener - User provided NotificationListener instance.
Throws:
javax.management.InstanceNotFoundException
java.lang.Exception

getServerConnection

public static javax.management.MBeanServerConnection getServerConnection(javax.management.remote.JMXConnector jmxConnector)
                                                                  throws java.lang.Exception
Throws:
java.lang.Exception

addNotificationListener

public static void addNotificationListener(javax.management.remote.JMXConnector jmxConnector,
                                           java.lang.Class<?> mbeanClass,
                                           javax.management.NotificationListener notificationListener)
                                    throws javax.management.InstanceNotFoundException,
                                           java.lang.Exception
Attach NotificationListener that can be used to listen notifications emitted by MBean server.

Parameters:
jmxConnector - The MBean server connector.
mbeanClass - The class for which an MBean exists.
notificationListener - User provided NotificationListener instance.
Throws:
javax.management.InstanceNotFoundException
java.lang.Exception

removeNotificationListener

public static void removeNotificationListener(javax.management.remote.JMXConnector jmxConnector,
                                              java.lang.Class<?> mbeanInterface,
                                              java.lang.String mbeanName,
                                              javax.management.NotificationListener notificationListener,
                                              boolean ignored)
                                       throws java.lang.Exception
Remove NotificationListener from this MBean.

Parameters:
jmxConnector - The MBean server connector.
mbeanInterface - The MBean interface this instance implements.
mbeanName - A custom name for the MBean.
notificationListener - Previously added NotificationListener instance.
Throws:
java.lang.Exception

removeNotificationListener

public static void removeNotificationListener(javax.management.remote.JMXConnector jmxConnector,
                                              java.lang.Class<?> mbeanClass,
                                              javax.management.NotificationListener notificationListener)
                                       throws java.lang.Exception
Remove NotificationListener from this MBean.

Parameters:
jmxConnector - The MBean server connector.
mbeanClass - The class for which an MBean exists.
notificationListener - Previously added NotificationListener instance.
Throws:
java.lang.Exception

generateMBeanObjectName

public static javax.management.ObjectName generateMBeanObjectName(java.lang.Class<?> mbeanClass)
                                                           throws java.lang.Exception
Throws:
java.lang.Exception

generateMBeanObjectName

public static javax.management.ObjectName generateMBeanObjectName(java.lang.String mbeanName,
                                                                  java.lang.String typeName)
                                                           throws java.lang.Exception
Throws:
java.lang.Exception

handleNotification

public void handleNotification(javax.management.Notification notification,
                               java.lang.Object handback)
Specified by:
handleNotification in interface javax.management.NotificationListener

createHelper

public static DynamicMBeanHelper createHelper(java.lang.Class<?> mbeanClass)
                                       throws java.lang.Exception
Throws:
java.lang.Exception

getHostName

public static java.lang.String getHostName()
Get the hostname from the local host. Returns the IP address, in textual form, if no hostname can be found.

Returns:
the hostname for the local host

getRegistry

public java.rmi.registry.Registry getRegistry()

getBeanPort

public int getBeanPort()

createHelper

public static DynamicMBeanHelper createHelper(java.lang.Class<?> mbeanClass,
                                              java.lang.String alias)
                                       throws java.lang.Exception
Throws:
java.lang.Exception