All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.admin.jmx.JMXClientConnection Maven / Gradle / Ivy

/****************************************************************************
 *
 *  Copyright (c) 2012, EBM WebSourcing
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/
 
package org.ow2.petals.admin.jmx;

import org.ow2.petals.jmx.JMXClient;
import org.ow2.petals.jmx.exception.ConnectionErrorException;

/**
 * This class manages the JMX connection to the remote Petals ESB.
* 
* @author Nicolas Oddoux - EBM WebSourcing
*/
public final class JMXClientConnection {

    private static final String NO_CURRENT_CONNECTION_ERROR_MESSAGE = "no current connection to a remote Petals ESB";
    
    public static JMXClient jmxClient;
    
    /**
     * Create a new JMX client connected to the remote Petals ESB. If a connection
     * already exists, it is closed and a new connection with the specified
     * parameters is created.
     * 
     * @param host
     *            the host of the Petals ESB JMX server
     * @param port
     *            the port of the Petals ESB JMX server
     * @param user
     *            the username of the Petals ESB JMX server
     * @param password
     *            the password of the Petals ESB JMX server
     * 
     * @return the JMX client to communicate with the remote Petals ESB
     * 
     * @throws NumberFormatException
     *             if the value of the property "petals.port" cannot be parsed
     *             as an integer
     * @throws ConnectionErrorException
     *             if the client cannot connect to the remote petals ESB
     */
    public static synchronized final JMXClient createJMXClient(String host, int port, String user, String password)
            throws ConnectionErrorException {
            if(jmxClient !=null) {
                jmxClient.disconnect();
            }
            
            jmxClient = new JMXClient(host, port, user, password);
            return jmxClient;
    }
    
    /**
     * Get the JMX current client connected to the remote petals ESB. This
     * method must be called after the creation of the JMX client.
     * 
     * @return the current JMX client connected to the remote Petals ESB
     * 
     * @throws ConnectionErrorException
     *             if there is no current client connection to the remote petals
     *             ESB
     */
	public static final JMXClient getJMXClient() throws ConnectionErrorException {
	    if(jmxClient == null) {
	        throw new ConnectionErrorException(NO_CURRENT_CONNECTION_ERROR_MESSAGE);
	    }
	    
	    return jmxClient;
	}
    
	/**
	 * Disconnect the JMX client.
	 * 
	 * @throws ConnectionErrorException
     *             if there is no current client connection to the remote petals
     *             ESB
	 */
     public static final void disconnectJMXClient() throws ConnectionErrorException {
         if(jmxClient == null) {
             throw new ConnectionErrorException(NO_CURRENT_CONNECTION_ERROR_MESSAGE);
         }
         
         jmxClient.disconnect();
         jmxClient = null;
    }
	
    private JMXClientConnection() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy