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 * Evgeny Mandrikov - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.examples; 13 14 import java.io.FileOutputStream; 15 16 import javax.management.MBeanServerConnection; 17 import javax.management.MBeanServerInvocationHandler; 18 import javax.management.ObjectName; 19 import javax.management.remote.JMXConnector; 20 import javax.management.remote.JMXConnectorFactory; 21 import javax.management.remote.JMXServiceURL; 22 23 /** 24 * This example connects to a coverage agent that run in output mode 25 * <code>mbean</code> and requests execution data. The collected data is dumped 26 * to a local file. 27 */ 28 public final class MBeanClient { 29 30 private static final String DESTFILE = "jacoco-client.exec"; 31 32 private static final String SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi"; 33 34 /** 35 * Execute the example. 36 * 37 * @param args 38 * @throws Exception 39 */ main(final String[] args)40 public static void main(final String[] args) throws Exception { 41 // Open connection to the coverage agent: 42 JMXServiceURL url = new JMXServiceURL(SERVICE_URL); 43 JMXConnector jmxc = JMXConnectorFactory.connect(url, null); 44 MBeanServerConnection connection = jmxc.getMBeanServerConnection(); 45 46 IProxy proxy = (IProxy) MBeanServerInvocationHandler.newProxyInstance( 47 connection, new ObjectName("org.jacoco:type=Runtime"), 48 IProxy.class, false); 49 50 // Retrieve JaCoCo version and session id: 51 System.out.println("Version: " + proxy.getVersion()); 52 System.out.println("Session: " + proxy.getSessionId()); 53 54 // Retrieve dump and write to file: 55 byte[] dump = proxy.dump(false); 56 final FileOutputStream localFile = new FileOutputStream(DESTFILE); 57 localFile.write(dump); 58 localFile.close(); 59 60 // Close connection: 61 jmxc.close(); 62 } 63 64 private interface IProxy { getVersion()65 String getVersion(); 66 getSessionId()67 String getSessionId(); 68 setSessionId(String id)69 void setSessionId(String id); 70 dump(boolean reset)71 byte[] dump(boolean reset); 72 reset()73 void reset(); 74 } 75 MBeanClient()76 private MBeanClient() { 77 } 78 } 79