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.joran.action;
015
016import java.lang.management.ManagementFactory;
017
018import javax.management.MBeanServer;
019import javax.management.ObjectName;
020
021import org.xml.sax.Attributes;
022
023import ch.qos.logback.classic.LoggerContext;
024import ch.qos.logback.classic.jmx.JMXConfigurator;
025import ch.qos.logback.classic.jmx.MBeanUtil;
026import ch.qos.logback.core.joran.action.Action;
027import ch.qos.logback.core.joran.spi.ActionException;
028import ch.qos.logback.core.joran.spi.InterpretationContext;
029import ch.qos.logback.core.util.OptionHelper;
030
031public class JMXConfiguratorAction extends Action {
032
033    static final String OBJECT_NAME_ATTRIBUTE_NAME = "objectName";
034    static final String CONTEXT_NAME_ATTRIBUTE_NAME = "contextName";
035    static final char JMX_NAME_SEPARATOR = ',';
036
037    @Override
038    public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
039        addInfo("begin");
040
041        String contextName = context.getName();
042        String contextNameAttributeVal = attributes.getValue(CONTEXT_NAME_ATTRIBUTE_NAME);
043        if (!OptionHelper.isEmpty(contextNameAttributeVal)) {
044            contextName = contextNameAttributeVal;
045        }
046
047        String objectNameAsStr;
048        String objectNameAttributeVal = attributes.getValue(OBJECT_NAME_ATTRIBUTE_NAME);
049        if (OptionHelper.isEmpty(objectNameAttributeVal)) {
050            objectNameAsStr = MBeanUtil.getObjectNameFor(contextName, JMXConfigurator.class);
051        } else {
052            objectNameAsStr = objectNameAttributeVal;
053        }
054
055        ObjectName objectName = MBeanUtil.string2ObjectName(context, this, objectNameAsStr);
056        if (objectName == null) {
057            addError("Failed construct ObjectName for [" + objectNameAsStr + "]");
058            return;
059        }
060
061        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
062        if (!MBeanUtil.isRegistered(mbs, objectName)) {
063            // register only of the named JMXConfigurator has not been previously
064            // registered. Unregistering an MBean within invocation of itself
065            // caused jconsole to throw an NPE. (This occurs when the reload* method
066            // unregisters the
067            JMXConfigurator jmxConfigurator = new JMXConfigurator((LoggerContext) context, mbs, objectName);
068            try {
069                mbs.registerMBean(jmxConfigurator, objectName);
070            } catch (Exception e) {
071                addError("Failed to create mbean", e);
072            }
073        }
074
075    }
076
077    @Override
078    public void end(InterpretationContext ec, String name) throws ActionException {
079
080    }
081
082}