1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.management.internal

File DefaultJMXBeanRegistration.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

0
12
2
1
83
44
4
0.33
6
2
2

Classes

Class Line # Actions
DefaultJMXBeanRegistration 42 12 0% 4 7
0.550%
 

Contributing tests

This file is covered by 18 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.management.internal;
21   
22    import java.lang.management.ManagementFactory;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26    import javax.management.MBeanServer;
27    import javax.management.ObjectName;
28   
29    import org.apache.commons.lang3.exception.ExceptionUtils;
30    import org.slf4j.Logger;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.management.JMXBeanRegistration;
33   
34    /**
35    * Registers MBeans against the default platform MBean Server using a default ObjectName domain of "org.xwiki".
36    *
37    * @version $Id: 3cfc1d9f412c959dbe950d6f430cc9c249957339 $
38    * @since 2.4M2
39    */
40    @Component
41    @Singleton
 
42    public class DefaultJMXBeanRegistration implements JMXBeanRegistration
43    {
44    private static final String OBJECTNAME_PREFIX = "org.xwiki:";
45   
46    /**
47    * The logger to use for logging.
48    */
49    @Inject
50    private Logger logger;
51   
 
52  50 toggle @Override
53    public void registerMBean(Object mbean, String name)
54    {
55    // Make sure we never fail since XWiki should execute correctly even if there's no MBean Server running.
56  50 try {
57  50 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
58  50 ObjectName oname = new ObjectName(OBJECTNAME_PREFIX + name);
59  50 mbs.registerMBean(mbean, oname);
60  35 this.logger.debug("Registered resource with name [{}]", name);
61    } catch (Exception e) {
62    // Failed to register the MBean, log a warning
63  15 this.logger.warn("Failed to register resource with name [{}]. Reason = [{}]", name,
64    ExceptionUtils.getMessage(e));
65    }
66    }
67   
 
68  0 toggle @Override
69    public void unregisterMBean(String name)
70    {
71    // Make sure we never fail since XWiki should execute correctly even if it fail to unregister some MBean
72  0 try {
73  0 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
74  0 ObjectName oname = new ObjectName(OBJECTNAME_PREFIX + name);
75  0 mbs.unregisterMBean(oname);
76  0 this.logger.debug("Unregistered resource with name [{}]", name);
77    } catch (Exception e) {
78    // Failed to unregister the MBean, log a warning
79  0 this.logger.warn("Failed to unregister resource with name [{}]. Reason = [{}]", name,
80    ExceptionUtils.getMessage(e));
81    }
82    }
83    }