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