Monitoring.java

  1. /*
  2.  * Copyright (c) 2019 Matthias Sohn <matthias.sohn@sap.com>
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.util;

  11. import java.io.IOException;
  12. import java.lang.management.ManagementFactory;

  13. import javax.management.InstanceAlreadyExistsException;
  14. import javax.management.InstanceNotFoundException;
  15. import javax.management.MBeanRegistrationException;
  16. import javax.management.MBeanServer;
  17. import javax.management.MalformedObjectNameException;
  18. import javax.management.NotCompliantMBeanException;
  19. import javax.management.ObjectInstance;
  20. import javax.management.ObjectName;

  21. import org.eclipse.jgit.annotations.Nullable;
  22. import org.eclipse.jgit.errors.ConfigInvalidException;
  23. import org.eclipse.jgit.lib.ConfigConstants;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. /**
  27.  * Enables monitoring JGit via JMX
  28.  *
  29.  * @since 5.1.13
  30.  */
  31. public class Monitoring {
  32.     private static final Logger LOG = LoggerFactory.getLogger(Monitoring.class);

  33.     /**
  34.      * Register a MBean with the platform MBean server
  35.      *
  36.      * @param mbean
  37.      *            the mbean object to register
  38.      * @param metricName
  39.      *            name of the JGit metric, will be prefixed with
  40.      *            "org.eclipse.jgit/"
  41.      * @return the registered mbean's object instance
  42.      */
  43.     public static @Nullable ObjectInstance registerMBean(Object mbean,
  44.             String metricName) {
  45.         boolean register = false;
  46.         try {
  47.             Class<?>[] interfaces = mbean.getClass().getInterfaces();
  48.             for (Class<?> i : interfaces) {
  49.                 register = SystemReader.getInstance().getUserConfig()
  50.                         .getBoolean(
  51.                     ConfigConstants.CONFIG_JMX_SECTION,
  52.                                 i.getSimpleName(), false);
  53.                 if (register) {
  54.                     break;
  55.                 }
  56.             }
  57.         } catch (IOException | ConfigInvalidException e) {
  58.             LOG.error(e.getMessage(), e);
  59.             return null;
  60.         }
  61.         if (!register) {
  62.             return null;
  63.         }
  64.         MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  65.         try {
  66.             ObjectName mbeanName = objectName(mbean.getClass(), metricName);
  67.             if (server.isRegistered(mbeanName)) {
  68.                 server.unregisterMBean(mbeanName);
  69.             }
  70.             return server.registerMBean(mbean, mbeanName);
  71.         } catch (MalformedObjectNameException | InstanceAlreadyExistsException
  72.                 | MBeanRegistrationException | NotCompliantMBeanException
  73.                 | InstanceNotFoundException e) {
  74.             LOG.error(e.getMessage(), e);
  75.             return null;
  76.         }
  77.     }

  78.     private static ObjectName objectName(Class mbean, String metricName)
  79.             throws MalformedObjectNameException {
  80.         return new ObjectName(String.format("org.eclipse.jgit/%s:type=%s", //$NON-NLS-1$
  81.                 metricName, mbean.getSimpleName()));
  82.     }
  83. }