View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.jmx;
15  
16  import javax.management.InstanceNotFoundException;
17  import javax.management.MBeanRegistrationException;
18  import javax.management.MBeanServer;
19  import javax.management.MalformedObjectNameException;
20  import javax.management.ObjectName;
21  
22  import ch.qos.logback.classic.LoggerContext;
23  import ch.qos.logback.core.Context;
24  import ch.qos.logback.core.status.StatusUtil;
25  
26  public class MBeanUtil {
27  
28      static final String DOMAIN = "ch.qos.logback.classic";
29  
30      static public String getObjectNameFor(String contextName, Class<?> type) {
31          return DOMAIN + ":Name=" + contextName + ",Type=" + type.getName();
32      }
33  
34      public static ObjectName string2ObjectName(Context context, Object caller, String objectNameAsStr) {
35          String msg = "Failed to convert [" + objectNameAsStr + "] to ObjectName";
36  
37          StatusUtil statusUtil = new StatusUtil(context);
38          try {
39              return new ObjectName(objectNameAsStr);
40          } catch (MalformedObjectNameException e) {
41              statusUtil.addError(caller, msg, e);
42              return null;
43          } catch (NullPointerException e) {
44              statusUtil.addError(caller, msg, e);
45              return null;
46          }
47      }
48  
49      public static boolean isRegistered(MBeanServer mbs, ObjectName objectName) {
50          return mbs.isRegistered(objectName);
51      }
52  
53      public static void createAndRegisterJMXConfigurator(MBeanServer mbs, LoggerContext loggerContext, JMXConfigurator jmxConfigurator, ObjectName objectName,
54                      Object caller) {
55          try {
56              mbs.registerMBean(jmxConfigurator, objectName);
57          } catch (Exception e) {
58              StatusUtil statusUtil = new StatusUtil(loggerContext);
59              statusUtil.addError(caller, "Failed to create mbean", e);
60          }
61      }
62  
63      public static void unregister(LoggerContext loggerContext, MBeanServer mbs, ObjectName objectName, Object caller) {
64  
65          StatusUtil statusUtil = new StatusUtil(loggerContext);
66          if (mbs.isRegistered(objectName)) {
67              try {
68                  statusUtil.addInfo(caller, "Unregistering mbean [" + objectName + "]");
69                  mbs.unregisterMBean(objectName);
70              } catch (InstanceNotFoundException e) {
71                  // this is theoretically impossible
72                  statusUtil.addError(caller, "Failed to unregister mbean" + objectName, e);
73              } catch (MBeanRegistrationException e) {
74                  // this is theoretically impossible
75                  statusUtil.addError(caller, "Failed to unregister mbean" + objectName, e);
76              }
77          } else {
78              statusUtil.addInfo(caller, "mbean [" + objectName + "] does not seem to be registered");
79          }
80      }
81  
82  }