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