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

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

There is a newer version: 2.2.0
Show newest version
/**
 * Copyright (c) 2012 EBM WebSourcing, 2012-2016 Linagora
 * 
 * This program/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 program/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 program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.admin.jmx;

import org.ow2.petals.admin.api.exception.NoConnectionException;
import org.ow2.petals.jmx.api.api.JMXClient;
import org.ow2.petals.jmx.api.api.PetalsJmxApiFactory;
import org.ow2.petals.jmx.api.api.exception.ConnectionErrorException;
import org.ow2.petals.jmx.api.api.exception.DuplicatedServiceException;
import org.ow2.petals.jmx.api.api.exception.MissingServiceException;

/**
 * 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();
        }

        try {
            jmxClient = PetalsJmxApiFactory.getInstance().createJMXClient(host, port, user,
                    password);
            return jmxClient;
        } catch (final DuplicatedServiceException e) {
            throw new ConnectionErrorException(e);
        } catch (MissingServiceException e) {
            throw new ConnectionErrorException(e);
        }
    }
    
    /**
     * 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;
	}
    
    /**
     * @return true if a {@link JMXClient} exists (ie. a connection
     *         is established to a Petals ESB container, false
     *         otherwise.
     */
    public static final boolean isConnected() {
        return jmxClient != null;
    }

    /**
     * Disconnect the JMX client.
     * 
     * @throws ConnectionErrorException
     *             An error occurs disconnecting the JMX Client.
     * @throws NoConnectionException
     *             There is no current JMX connection to the remote petals ESB.
     */
    public static final void disconnectJMXClient() throws NoConnectionException,
            ConnectionErrorException {
         if(jmxClient == null) {
            throw new NoConnectionException();
         }
         
         jmxClient.disconnect();
         jmxClient = null;
    }
	
    private JMXClientConnection() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy