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.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)
39        throws ActionException {
40      addInfo("begin");
41  
42  
43      String contextName = context.getName();
44      String contextNameAttributeVal = attributes
45      .getValue(CONTEXT_NAME_ATTRIBUTE_NAME);
46      if(!OptionHelper.isEmpty(contextNameAttributeVal)) {
47        contextName = contextNameAttributeVal;
48      }
49  
50      String objectNameAsStr;
51      String objectNameAttributeVal = attributes
52          .getValue(OBJECT_NAME_ATTRIBUTE_NAME);
53      if (OptionHelper.isEmpty(objectNameAttributeVal)) {
54        objectNameAsStr = MBeanUtil.getObjectNameFor(contextName,
55            JMXConfigurator.class);
56      } else {
57        objectNameAsStr = objectNameAttributeVal;
58      }
59  
60      ObjectName objectName = MBeanUtil.string2ObjectName(context, this,
61          objectNameAsStr);
62      if (objectName == null) {
63        addError("Failed construct ObjectName for ["+objectNameAsStr+"]");
64        return;
65      }
66      
67      MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
68      if(!MBeanUtil.isRegistered(mbs, objectName)) {
69        // register only of the named JMXConfigurator has not been previously
70        // registered. Unregistering an MBean within invocation of itself
71        // caused jconsole to throw an NPE. (This occurs when the reload* method
72        // unregisters the 
73        JMXConfigurator jmxConfigurator = new JMXConfigurator((LoggerContext) context, mbs,
74            objectName);
75        try {     
76          mbs.registerMBean(jmxConfigurator, objectName);
77        } catch (Exception e) {
78          addError("Failed to create mbean", e);
79        }
80      }
81  
82    }
83  
84    @Override
85    public void end(InterpretationContext ec, String name) throws ActionException {
86  
87    }
88  
89  }