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  package org.apache.commons.dbcp2;
18  
19  import java.lang.management.ManagementFactory;
20  import java.util.Objects;
21  
22  import javax.management.MBeanServer;
23  import javax.management.MalformedObjectNameException;
24  import javax.management.ObjectName;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * Internal wrapper class that allows JMX to be a noop if absent or disabled.
31   *
32   * @since 2.2.1
33   */
34  final class ObjectNameWrapper {
35  
36      private static final Log log = LogFactory.getLog(ObjectNameWrapper.class);
37  
38      private static final MBeanServer MBEAN_SERVER = getPlatformMBeanServer();
39  
40      private static MBeanServer getPlatformMBeanServer() {
41          try {
42              return ManagementFactory.getPlatformMBeanServer();
43          } catch (final LinkageError | Exception e) {
44              // ignore - JMX not available
45              log.debug("Failed to get platform MBeanServer", e);
46              return null;
47          }
48      }
49  
50      public static ObjectName unwrap(final ObjectNameWrapper wrapper) {
51          return wrapper == null ? null : wrapper.unwrap();
52      }
53  
54      public static ObjectNameWrapper wrap(final ObjectName objectName) {
55          return new ObjectNameWrapper(objectName);
56      }
57  
58      public static ObjectNameWrapper wrap(final String name) throws MalformedObjectNameException {
59          return wrap(new ObjectName(name));
60      }
61  
62      private final ObjectName objectName;
63  
64      public ObjectNameWrapper(final ObjectName objectName) {
65          this.objectName = objectName;
66      }
67  
68      public void registerMBean(final Object object) {
69          if (MBEAN_SERVER == null || objectName == null) {
70              return;
71          }
72          try {
73              MBEAN_SERVER.registerMBean(object, objectName);
74          } catch (final LinkageError | Exception e) {
75              log.warn("Failed to complete JMX registration for " + objectName, e);
76          }
77      }
78  
79      /**
80       * @since 2.7.0
81       */
82      @Override
83      public String toString() {
84          return Objects.toString(objectName);
85      }
86  
87      public void unregisterMBean() {
88          if (MBEAN_SERVER == null || objectName == null) {
89              return;
90          }
91          if (MBEAN_SERVER.isRegistered(objectName)) {
92              try {
93                  MBEAN_SERVER.unregisterMBean(objectName);
94              } catch (final LinkageError | Exception e) {
95                  log.warn("Failed to complete JMX unregistration for " + objectName, e);
96              }
97          }
98      }
99  
100     public ObjectName unwrap() {
101         return objectName;
102     }
103 
104 }