java/sql-dk/src/main/java/info/globalcode/sql/dk/jmx/ManagementUtils.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 238 4a1864c3e867
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@179
     1
/**
franta-hg@179
     2
 * SQL-DK
franta-hg@179
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@179
     4
 *
franta-hg@179
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@179
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@250
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@179
     8
 *
franta-hg@179
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@179
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@179
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@179
    12
 * GNU General Public License for more details.
franta-hg@179
    13
 *
franta-hg@179
    14
 * You should have received a copy of the GNU General Public License
franta-hg@179
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@179
    16
 */
franta-hg@179
    17
package info.globalcode.sql.dk.jmx;
franta-hg@179
    18
franta-hg@179
    19
import java.lang.management.ManagementFactory;
franta-hg@179
    20
import java.util.Hashtable;
franta-hg@179
    21
import java.util.logging.Level;
franta-hg@179
    22
import java.util.logging.Logger;
franta-hg@179
    23
import javax.management.MBeanServer;
franta-hg@179
    24
import javax.management.ObjectName;
franta-hg@179
    25
franta-hg@179
    26
/**
franta-hg@179
    27
 *
franta-hg@179
    28
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@179
    29
 */
franta-hg@179
    30
public class ManagementUtils {
franta-hg@179
    31
franta-hg@179
    32
	private static final Logger log = Logger.getLogger(ManagementUtils.class.getName());
franta-hg@179
    33
	public static final String DEFAULT_CONNECTION_JMX_NAME = "main";
franta-hg@179
    34
franta-hg@179
    35
	/**
franta-hg@179
    36
	 * @see #registerMBean(java.lang.String, java.lang.String) with default JMX name
franta-hg@179
    37
	 */
franta-hg@179
    38
	public static ConnectionManagement registerMBean(String dbName) {
franta-hg@179
    39
		return registerMBean(dbName, DEFAULT_CONNECTION_JMX_NAME);
franta-hg@179
    40
	}
franta-hg@179
    41
franta-hg@179
    42
	/**
franta-hg@179
    43
	 *
franta-hg@179
    44
	 * @param dbName database name
franta-hg@179
    45
	 * @param jmxName name of JMX bean
franta-hg@179
    46
	 * @return registered JMX bean | or null if registration fails (should not)
franta-hg@179
    47
	 */
franta-hg@179
    48
	public static ConnectionManagement registerMBean(String dbName, String jmxName) {
franta-hg@179
    49
		try {
franta-hg@179
    50
			ConnectionManagement mbean = new ConnectionManagement(dbName);
franta-hg@179
    51
			MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
franta-hg@179
    52
			Hashtable<String, String> objectProperties = new Hashtable<>();
franta-hg@179
    53
			objectProperties.put("type", "Connection");
franta-hg@179
    54
			objectProperties.put("name", jmxName);
franta-hg@179
    55
			ObjectName objectName = new ObjectName("info.globalcode.sql.dk", objectProperties);
franta-hg@179
    56
			mbs.registerMBean(mbean, objectName);
franta-hg@179
    57
			log.log(Level.FINE, "JMX MBean was registered as: {0}", objectName);
franta-hg@179
    58
			return mbean;
franta-hg@179
    59
		} catch (Exception e) {
franta-hg@179
    60
			log.log(Level.WARNING, "Unable to register JMX MBean", e);
franta-hg@179
    61
			return null;
franta-hg@179
    62
		}
franta-hg@179
    63
	}
franta-hg@179
    64
franta-hg@179
    65
	private ManagementUtils() {
franta-hg@179
    66
	}
franta-hg@179
    67
}