JmxRegistration.java

  1. /*******************************************************************************
  2.  * Copyright (c) 2009, 2025 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. import java.lang.management.ManagementFactory;
  15. import java.util.concurrent.Callable;

  16. import javax.management.MBeanServer;
  17. import javax.management.ObjectName;
  18. import javax.management.StandardMBean;

  19. import org.jacoco.agent.rt.IAgent;

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

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

  26.     private final MBeanServer server;
  27.     private final ObjectName name;

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

  33.     /**
  34.      * De-register the agent again.
  35.      */
  36.     public Void call() throws Exception {
  37.         server.unregisterMBean(name);
  38.         return null;
  39.     }

  40. }