1/*******************************************************************************
2 * Copyright (c) 2009, 2017 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 *******************************************************************************/
12package org.jacoco.cli.internal.commands;
13
14import java.io.File;
15import java.io.IOException;
16import java.io.PrintWriter;
17import java.net.InetAddress;
18
19import org.jacoco.cli.internal.Command;
20import org.jacoco.core.runtime.AgentOptions;
21import org.jacoco.core.tools.ExecDumpClient;
22import org.jacoco.core.tools.ExecFileLoader;
23import org.kohsuke.args4j.Option;
24
25/**
26 * The <code>dump</code> command.
27 */
28public class Dump extends Command {
29
30	@Option(name = "-address", usage = "host name or ip address to connect to (default localhost)", metaVar = "<address>")
31	String address = AgentOptions.DEFAULT_ADDRESS;
32
33	@Option(name = "-port", usage = "the port to connect to (default 6300)", metaVar = "<port>")
34	int port = AgentOptions.DEFAULT_PORT;
35
36	@Option(name = "-destfile", usage = "file to write execution data to", metaVar = "<path>", required = true)
37	File destfile;
38
39	@Option(name = "-reset", usage = "reset execution data on test target after dump")
40	boolean reset = false;
41
42	@Option(name = "-retry", usage = "number of retries (default 0)", metaVar = "<count>")
43	int retrycount = 0;
44
45	@Override
46	public String description() {
47		return "Request execution data from a JaCoCo agent running in 'tcpserver' output mode.";
48	}
49
50	@Override
51	public int execute(final PrintWriter out, final PrintWriter err)
52			throws Exception {
53		final ExecDumpClient client = new ExecDumpClient() {
54			@Override
55			protected void onConnecting(final InetAddress address,
56					final int port) {
57				out.printf("[INFO] Connecting to %s:%s.%n", address,
58						Integer.valueOf(port));
59			}
60
61			@Override
62			protected void onConnectionFailure(final IOException exception) {
63				err.printf("[WARN] %s.%n", exception.getMessage());
64			}
65		};
66		client.setReset(reset);
67		client.setRetryCount(retrycount);
68
69		final ExecFileLoader loader = client.dump(address, port);
70		out.printf("[INFO] Writing execution data to %s.%n",
71				destfile.getAbsolutePath());
72		loader.save(destfile, true);
73
74		return 0;
75	}
76
77}
78