java/sql-dk/src/info/globalcode/sql/dk/jmx/ManagementUtils.java
branchv_0
changeset 179 236332caeb29
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/jmx/ManagementUtils.java	Thu Sep 25 17:50:40 2014 +0200
     1.3 @@ -0,0 +1,68 @@
     1.4 +/**
     1.5 + * SQL-DK
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package info.globalcode.sql.dk.jmx;
    1.22 +
    1.23 +import java.lang.management.ManagementFactory;
    1.24 +import java.util.Hashtable;
    1.25 +import java.util.logging.Level;
    1.26 +import java.util.logging.Logger;
    1.27 +import javax.management.MBeanServer;
    1.28 +import javax.management.ObjectName;
    1.29 +
    1.30 +/**
    1.31 + *
    1.32 + * @author Ing. František Kučera (frantovo.cz)
    1.33 + */
    1.34 +public class ManagementUtils {
    1.35 +
    1.36 +	private static final Logger log = Logger.getLogger(ManagementUtils.class.getName());
    1.37 +	public static final String DEFAULT_CONNECTION_JMX_NAME = "main";
    1.38 +
    1.39 +	/**
    1.40 +	 * @see #registerMBean(java.lang.String, java.lang.String) with default JMX name
    1.41 +	 */
    1.42 +	public static ConnectionManagement registerMBean(String dbName) {
    1.43 +		return registerMBean(dbName, DEFAULT_CONNECTION_JMX_NAME);
    1.44 +	}
    1.45 +
    1.46 +	/**
    1.47 +	 *
    1.48 +	 * @param dbName database name
    1.49 +	 * @param jmxName name of JMX bean
    1.50 +	 * @return registered JMX bean | or null if registration fails (should not)
    1.51 +	 */
    1.52 +	public static ConnectionManagement registerMBean(String dbName, String jmxName) {
    1.53 +		try {
    1.54 +			ConnectionManagement mbean = new ConnectionManagement(dbName);
    1.55 +			MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    1.56 +			Hashtable<String, String> objectProperties = new Hashtable<>();
    1.57 +			objectProperties.put("type", "Connection");
    1.58 +			objectProperties.put("name", jmxName);
    1.59 +			ObjectName objectName = new ObjectName("info.globalcode.sql.dk", objectProperties);
    1.60 +			mbs.registerMBean(mbean, objectName);
    1.61 +			log.log(Level.FINE, "JMX MBean was registered as: {0}", objectName);
    1.62 +			return mbean;
    1.63 +		} catch (Exception e) {
    1.64 +			log.log(Level.WARNING, "Unable to register JMX MBean", e);
    1.65 +			return null;
    1.66 +		}
    1.67 +	}
    1.68 +
    1.69 +	private ManagementUtils() {
    1.70 +	}
    1.71 +}