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