1 /******************************************************************************* 2 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors 3 * This program and the accompanying materials are made available under 4 * the terms of the Eclipse Public License 2.0 which is available at 5 * http://www.eclipse.org/legal/epl-2.0 6 * 7 * SPDX-License-Identifier: EPL-2.0 8 * 9 * Contributors: 10 * Marc R. Hoffmann - initial API and implementation 11 * 12 *******************************************************************************/ 13 package org.jacoco.agent.rt.internal; 14 15 import java.lang.management.ManagementFactory; 16 import java.util.concurrent.Callable; 17 18 import javax.management.MBeanServer; 19 import javax.management.ObjectName; 20 import javax.management.StandardMBean; 21 22 import org.jacoco.agent.rt.IAgent; 23 24 /** 25 * Access to JMX APIs are encapsulated in this class to allow the JaCoCo runtime 26 * on platforms without JMX support (e.g Android). 27 */ 28 class JmxRegistration implements Callable<Void> { 29 30 private static final String JMX_NAME = "org.jacoco:type=Runtime"; 31 32 private final MBeanServer server; 33 private final ObjectName name; 34 JmxRegistration(final IAgent agent)35 JmxRegistration(final IAgent agent) throws Exception { 36 server = ManagementFactory.getPlatformMBeanServer(); 37 name = new ObjectName(JMX_NAME); 38 server.registerMBean(new StandardMBean(agent, IAgent.class), name); 39 } 40 41 /** 42 * De-register the agent again. 43 */ call()44 public Void call() throws Exception { 45 server.unregisterMBean(name); 46 return null; 47 } 48 49 } 50