View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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="
32          + type.getName();
33    }
34  
35    public static ObjectName string2ObjectName(Context context, Object caller,
36        String objectNameAsStr) {
37      String msg = "Failed to convert [" + objectNameAsStr + "] to ObjectName";
38  
39      try {
40        return new ObjectName(objectNameAsStr);
41      } catch (MalformedObjectNameException e) {
42        StatusUtil.addError(context, caller, msg, e);
43        return null;
44      } catch (NullPointerException e) {
45        StatusUtil.addError(context, caller, msg, e);
46        return null;
47      }
48    }
49  
50    public static boolean isRegistered(MBeanServer mbs, ObjectName objectName) {
51      return mbs.isRegistered(objectName);
52    }
53  
54    public static void createAndRegisterJMXConfigurator(
55        MBeanServer mbs, LoggerContext loggerContext,
56        JMXConfigurator jmxConfigurator, ObjectName objectName, Object caller) {
57      try {
58        mbs.registerMBean(jmxConfigurator, objectName);
59      } catch (Exception e) {
60        StatusUtil.addError(loggerContext, caller, "Failed to create mbean", e);
61      }
62    }
63  
64    public static void unregister(LoggerContext loggerContext, MBeanServer mbs,
65        ObjectName objectName, Object caller) {
66      if (mbs.isRegistered(objectName)) {
67        try {
68          StatusUtil.addInfo(loggerContext, caller, "Unregistering mbean ["
69              + objectName + "]");
70          mbs.unregisterMBean(objectName);
71        } catch (InstanceNotFoundException e) {
72          // this is theoretically impossible
73          StatusUtil.addError(loggerContext, caller, "Failed to unregister mbean"
74              + objectName, e);
75        } catch (MBeanRegistrationException e) {
76          // this is theoretically impossible
77          StatusUtil.addError(loggerContext, caller, "Failed to unregister mbean"
78              + objectName, e);
79        }
80      } else {
81        StatusUtil.addInfo(loggerContext, caller, "mbean [" + objectName
82            + "] does not seem to be registered");
83      }
84    }
85  
86  }