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