ObjectNameWrapper.java

  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. import java.lang.management.ManagementFactory;
  19. import java.util.Objects;

  20. import javax.management.MBeanServer;
  21. import javax.management.MalformedObjectNameException;
  22. import javax.management.ObjectName;

  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;

  25. /**
  26.  * Internal wrapper class that allows JMX to be a noop if absent or disabled.
  27.  *
  28.  * @since 2.2.1
  29.  */
  30. final class ObjectNameWrapper {

  31.     private static final Log log = LogFactory.getLog(ObjectNameWrapper.class);

  32.     private static final MBeanServer MBEAN_SERVER = getPlatformMBeanServer();

  33.     private static MBeanServer getPlatformMBeanServer() {
  34.         try {
  35.             return ManagementFactory.getPlatformMBeanServer();
  36.         } catch (final LinkageError | Exception e) {
  37.             // ignore - JMX not available
  38.             log.debug("Failed to get platform MBeanServer", e);
  39.             return null;
  40.         }
  41.     }

  42.     public static ObjectName unwrap(final ObjectNameWrapper wrapper) {
  43.         return wrapper == null ? null : wrapper.unwrap();
  44.     }

  45.     public static ObjectNameWrapper wrap(final ObjectName objectName) {
  46.         return new ObjectNameWrapper(objectName);
  47.     }

  48.     public static ObjectNameWrapper wrap(final String name) throws MalformedObjectNameException {
  49.         return wrap(new ObjectName(name));
  50.     }

  51.     private final ObjectName objectName;

  52.     public ObjectNameWrapper(final ObjectName objectName) {
  53.         this.objectName = objectName;
  54.     }

  55.     public void registerMBean(final Object object) {
  56.         if (MBEAN_SERVER == null || objectName == null) {
  57.             return;
  58.         }
  59.         try {
  60.             MBEAN_SERVER.registerMBean(object, objectName);
  61.         } catch (final LinkageError | Exception e) {
  62.             log.warn("Failed to complete JMX registration for " + objectName, e);
  63.         }
  64.     }

  65.     /**
  66.      * @since 2.7.0
  67.      */
  68.     @Override
  69.     public String toString() {
  70.         return Objects.toString(objectName);
  71.     }

  72.     public void unregisterMBean() {
  73.         if (MBEAN_SERVER == null || objectName == null) {
  74.             return;
  75.         }
  76.         if (MBEAN_SERVER.isRegistered(objectName)) {
  77.             try {
  78.                 MBEAN_SERVER.unregisterMBean(objectName);
  79.             } catch (final LinkageError | Exception e) {
  80.                 log.warn("Failed to complete JMX unregistration for " + objectName, e);
  81.             }
  82.         }
  83.     }

  84.     public ObjectName unwrap() {
  85.         return objectName;
  86.     }

  87. }