franta-hg@179: /**
franta-hg@179: * SQL-DK
franta-hg@179: * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@179: *
franta-hg@179: * This program is free software: you can redistribute it and/or modify
franta-hg@179: * it under the terms of the GNU General Public License as published by
franta-hg@179: * the Free Software Foundation, either version 3 of the License, or
franta-hg@179: * (at your option) any later version.
franta-hg@179: *
franta-hg@179: * This program is distributed in the hope that it will be useful,
franta-hg@179: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@179: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@179: * GNU General Public License for more details.
franta-hg@179: *
franta-hg@179: * You should have received a copy of the GNU General Public License
franta-hg@179: * along with this program. If not, see .
franta-hg@179: */
franta-hg@179: package info.globalcode.sql.dk.jmx;
franta-hg@179:
franta-hg@179: import java.lang.management.ManagementFactory;
franta-hg@179: import java.util.Hashtable;
franta-hg@179: import java.util.logging.Level;
franta-hg@179: import java.util.logging.Logger;
franta-hg@179: import javax.management.MBeanServer;
franta-hg@179: import javax.management.ObjectName;
franta-hg@179:
franta-hg@179: /**
franta-hg@179: *
franta-hg@179: * @author Ing. František Kučera (frantovo.cz)
franta-hg@179: */
franta-hg@179: public class ManagementUtils {
franta-hg@179:
franta-hg@179: private static final Logger log = Logger.getLogger(ManagementUtils.class.getName());
franta-hg@179: public static final String DEFAULT_CONNECTION_JMX_NAME = "main";
franta-hg@179:
franta-hg@179: /**
franta-hg@179: * @see #registerMBean(java.lang.String, java.lang.String) with default JMX name
franta-hg@179: */
franta-hg@179: public static ConnectionManagement registerMBean(String dbName) {
franta-hg@179: return registerMBean(dbName, DEFAULT_CONNECTION_JMX_NAME);
franta-hg@179: }
franta-hg@179:
franta-hg@179: /**
franta-hg@179: *
franta-hg@179: * @param dbName database name
franta-hg@179: * @param jmxName name of JMX bean
franta-hg@179: * @return registered JMX bean | or null if registration fails (should not)
franta-hg@179: */
franta-hg@179: public static ConnectionManagement registerMBean(String dbName, String jmxName) {
franta-hg@179: try {
franta-hg@179: ConnectionManagement mbean = new ConnectionManagement(dbName);
franta-hg@179: MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
franta-hg@179: Hashtable objectProperties = new Hashtable<>();
franta-hg@179: objectProperties.put("type", "Connection");
franta-hg@179: objectProperties.put("name", jmxName);
franta-hg@179: ObjectName objectName = new ObjectName("info.globalcode.sql.dk", objectProperties);
franta-hg@179: mbs.registerMBean(mbean, objectName);
franta-hg@179: log.log(Level.FINE, "JMX MBean was registered as: {0}", objectName);
franta-hg@179: return mbean;
franta-hg@179: } catch (Exception e) {
franta-hg@179: log.log(Level.WARNING, "Unable to register JMX MBean", e);
franta-hg@179: return null;
franta-hg@179: }
franta-hg@179: }
franta-hg@179:
franta-hg@179: private ManagementUtils() {
franta-hg@179: }
franta-hg@179: }