View Javadoc
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  
12  import java.io.IOException;
13  import java.lang.management.ManagementFactory;
14  
15  import javax.management.InstanceAlreadyExistsException;
16  import javax.management.InstanceNotFoundException;
17  import javax.management.MBeanRegistrationException;
18  import javax.management.MBeanServer;
19  import javax.management.MalformedObjectNameException;
20  import javax.management.NotCompliantMBeanException;
21  import javax.management.ObjectInstance;
22  import javax.management.ObjectName;
23  
24  import org.eclipse.jgit.annotations.Nullable;
25  import org.eclipse.jgit.errors.ConfigInvalidException;
26  import org.eclipse.jgit.lib.ConfigConstants;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Enables monitoring JGit via JMX
32   *
33   * @since 5.1.13
34   */
35  public class Monitoring {
36  	private static final Logger LOG = LoggerFactory.getLogger(Monitoring.class);
37  
38  	/**
39  	 * Register a MBean with the platform MBean server
40  	 *
41  	 * @param mbean
42  	 *            the mbean object to register
43  	 * @param metricName
44  	 *            name of the JGit metric, will be prefixed with
45  	 *            "org.eclipse.jgit/"
46  	 * @return the registered mbean's object instance
47  	 */
48  	public static @Nullable ObjectInstance registerMBean(Object mbean,
49  			String metricName) {
50  		boolean register = false;
51  		try {
52  			Class<?>[] interfaces = mbean.getClass().getInterfaces();
53  			for (Class<?> i : interfaces) {
54  				register = SystemReader.getInstance().getUserConfig()
55  						.getBoolean(
56  					ConfigConstants.CONFIG_JMX_SECTION,
57  								i.getSimpleName(), false);
58  				if (register) {
59  					break;
60  				}
61  			}
62  		} catch (IOException | ConfigInvalidException e) {
63  			LOG.error(e.getMessage(), e);
64  			return null;
65  		}
66  		if (!register) {
67  			return null;
68  		}
69  		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
70  		try {
71  			ObjectName mbeanName = objectName(mbean.getClass(), metricName);
72  			if (server.isRegistered(mbeanName)) {
73  				server.unregisterMBean(mbeanName);
74  			}
75  			return server.registerMBean(mbean, mbeanName);
76  		} catch (MalformedObjectNameException | InstanceAlreadyExistsException
77  				| MBeanRegistrationException | NotCompliantMBeanException
78  				| InstanceNotFoundException e) {
79  			LOG.error(e.getMessage(), e);
80  			return null;
81  		}
82  	}
83  
84  	private static ObjectName objectName(Class mbean, String metricName)
85  			throws MalformedObjectNameException {
86  		return new ObjectName(String.format("org.eclipse.jgit/%s:type=%s", //$NON-NLS-1$
87  				metricName, mbean.getSimpleName()));
88  	}
89  }