001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.jmx;
015
016import javax.management.InstanceNotFoundException;
017import javax.management.MBeanRegistrationException;
018import javax.management.MBeanServer;
019import javax.management.MalformedObjectNameException;
020import javax.management.ObjectName;
021
022import ch.qos.logback.classic.LoggerContext;
023import ch.qos.logback.core.Context;
024import ch.qos.logback.core.status.StatusUtil;
025
026public class MBeanUtil {
027
028    static final String DOMAIN = "ch.qos.logback.classic";
029
030    static public String getObjectNameFor(String contextName, Class<?> type) {
031        return DOMAIN + ":Name=" + contextName + ",Type=" + type.getName();
032    }
033
034    public static ObjectName string2ObjectName(Context context, Object caller, String objectNameAsStr) {
035        String msg = "Failed to convert [" + objectNameAsStr + "] to ObjectName";
036
037        StatusUtil statusUtil = new StatusUtil(context);
038        try {
039            return new ObjectName(objectNameAsStr);
040        } catch (MalformedObjectNameException e) {
041            statusUtil.addError(caller, msg, e);
042            return null;
043        } catch (NullPointerException e) {
044            statusUtil.addError(caller, msg, e);
045            return null;
046        }
047    }
048
049    public static boolean isRegistered(MBeanServer mbs, ObjectName objectName) {
050        return mbs.isRegistered(objectName);
051    }
052
053    public static void createAndRegisterJMXConfigurator(MBeanServer mbs, LoggerContext loggerContext,
054            JMXConfigurator jmxConfigurator, ObjectName objectName, Object caller) {
055        try {
056            mbs.registerMBean(jmxConfigurator, objectName);
057        } catch (Exception e) {
058            StatusUtil statusUtil = new StatusUtil(loggerContext);
059            statusUtil.addError(caller, "Failed to create mbean", e);
060        }
061    }
062
063    public static void unregister(LoggerContext loggerContext, MBeanServer mbs, ObjectName objectName, Object caller) {
064
065        StatusUtil statusUtil = new StatusUtil(loggerContext);
066        if (mbs.isRegistered(objectName)) {
067            try {
068                statusUtil.addInfo(caller, "Unregistering mbean [" + objectName + "]");
069                mbs.unregisterMBean(objectName);
070            } catch (InstanceNotFoundException e) {
071                // this is theoretically impossible
072                statusUtil.addError(caller, "Failed to unregister mbean" + objectName, e);
073            } catch (MBeanRegistrationException e) {
074                // this is theoretically impossible
075                statusUtil.addError(caller, "Failed to unregister mbean" + objectName, e);
076            }
077        } else {
078            statusUtil.addInfo(caller, "mbean [" + objectName + "] does not seem to be registered");
079        }
080    }
081
082}