View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp2;
19  
20  import java.lang.management.ManagementFactory;
21  import java.util.Objects;
22  
23  import javax.management.MBeanServer;
24  import javax.management.MalformedObjectNameException;
25  import javax.management.ObjectName;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * Internal wrapper class that allows JMX to be a noop if absent or disabled.
32   *
33   * @since 2.2.1
34   */
35  class ObjectNameWrapper {
36  
37      private static final Log log = LogFactory.getLog(ObjectNameWrapper.class);
38  
39      private static final MBeanServer MBEAN_SERVER = getPlatformMBeanServer();
40  
41      private static MBeanServer getPlatformMBeanServer() {
42          try {
43              return ManagementFactory.getPlatformMBeanServer();
44          } catch (final LinkageError | Exception e) {
45              // ignore - JMX not available
46              log.debug("Failed to get platform MBeanServer", e);
47              return null;
48          }
49      }
50  
51      public static ObjectName unwrap(final ObjectNameWrapper wrapper) {
52          return wrapper == null ? null : wrapper.unwrap();
53      }
54  
55      public static ObjectNameWrapper wrap(final ObjectName objectName) {
56          return new ObjectNameWrapper(objectName);
57      }
58  
59      public static ObjectNameWrapper wrap(final String name) throws MalformedObjectNameException {
60          return wrap(new ObjectName(name));
61      }
62  
63      private final ObjectName objectName;
64  
65      public ObjectNameWrapper(final ObjectName objectName) {
66          this.objectName = objectName;
67      }
68  
69      public void registerMBean(final Object object) {
70          if (MBEAN_SERVER == null || objectName == null) {
71              return;
72          }
73          try {
74              MBEAN_SERVER.registerMBean(object, objectName);
75          } catch (final LinkageError | Exception e) {
76              log.warn("Failed to complete JMX registration for " + objectName, e);
77          }
78      }
79  
80      /**
81       * @since 2.7.0
82       */
83      @Override
84      public String toString() {
85          return Objects.toString(objectName);
86      }
87  
88      public void unregisterMBean() {
89          if (MBEAN_SERVER == null || objectName == null) {
90              return;
91          }
92          if (MBEAN_SERVER.isRegistered(objectName)) {
93              try {
94                  MBEAN_SERVER.unregisterMBean(objectName);
95              } catch (final LinkageError | Exception e) {
96                  log.warn("Failed to complete JMX unregistration for " + objectName, e);
97              }
98          }
99      }
100 
101     public ObjectName unwrap() {
102         return objectName;
103     }
104 
105 }