aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/JmxRegistration.java
blob: 6c49c0c1effb89cdc04baeb68672de4ec8aeb43c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*******************************************************************************
 * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.agent.rt.internal;

import java.lang.management.ManagementFactory;
import java.util.concurrent.Callable;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.StandardMBean;

import org.jacoco.agent.rt.IAgent;

/**
 * Access to JMX APIs are encapsulated in this class to allow the JaCoCo runtime
 * on platforms without JMX support (e.g Android).
 */
class JmxRegistration implements Callable<Void> {

	private static final String JMX_NAME = "org.jacoco:type=Runtime";

	private final MBeanServer server;
	private final ObjectName name;

	JmxRegistration(final IAgent agent) throws Exception {
		server = ManagementFactory.getPlatformMBeanServer();
		name = new ObjectName(JMX_NAME);
		server.registerMBean(new StandardMBean(agent, IAgent.class), name);
	}

	/**
	 * De-register the agent again.
	 */
	public Void call() throws Exception {
		server.unregisterMBean(name);
		return null;
	}

}