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.joran.action;
15  
16  import java.lang.management.ManagementFactory;
17  
18  import javax.management.MBeanServer;
19  import javax.management.ObjectName;
20  
21  import org.xml.sax.Attributes;
22  
23  import ch.qos.logback.classic.LoggerContext;
24  import ch.qos.logback.classic.jmx.JMXConfigurator;
25  import ch.qos.logback.classic.jmx.MBeanUtil;
26  import ch.qos.logback.core.joran.action.Action;
27  import ch.qos.logback.core.joran.spi.ActionException;
28  import ch.qos.logback.core.joran.spi.InterpretationContext;
29  import ch.qos.logback.core.util.OptionHelper;
30  
31  public class JMXConfiguratorAction extends Action {
32  
33      static final String OBJECT_NAME_ATTRIBUTE_NAME = "objectName";
34      static final String CONTEXT_NAME_ATTRIBUTE_NAME = "contextName";
35      static final char JMX_NAME_SEPARATOR = ',';
36  
37      @Override
38      public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
39          addInfo("begin");
40  
41          String contextName = context.getName();
42          String contextNameAttributeVal = attributes.getValue(CONTEXT_NAME_ATTRIBUTE_NAME);
43          if (!OptionHelper.isEmpty(contextNameAttributeVal)) {
44              contextName = contextNameAttributeVal;
45          }
46  
47          String objectNameAsStr;
48          String objectNameAttributeVal = attributes.getValue(OBJECT_NAME_ATTRIBUTE_NAME);
49          if (OptionHelper.isEmpty(objectNameAttributeVal)) {
50              objectNameAsStr = MBeanUtil.getObjectNameFor(contextName, JMXConfigurator.class);
51          } else {
52              objectNameAsStr = objectNameAttributeVal;
53          }
54  
55          ObjectName objectName = MBeanUtil.string2ObjectName(context, this, objectNameAsStr);
56          if (objectName == null) {
57              addError("Failed construct ObjectName for [" + objectNameAsStr + "]");
58              return;
59          }
60  
61          MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
62          if (!MBeanUtil.isRegistered(mbs, objectName)) {
63              // register only of the named JMXConfigurator has not been previously
64              // registered. Unregistering an MBean within invocation of itself
65              // caused jconsole to throw an NPE. (This occurs when the reload* method
66              // unregisters the
67              JMXConfigurator jmxConfigurator = new JMXConfigurator((LoggerContext) context, mbs, objectName);
68              try {
69                  mbs.registerMBean(jmxConfigurator, objectName);
70              } catch (Exception e) {
71                  addError("Failed to create mbean", e);
72              }
73          }
74  
75      }
76  
77      @Override
78      public void end(InterpretationContext ec, String name) throws ActionException {
79  
80      }
81  
82  }