LocalController.java
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 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 * Brock Janiczak - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.agent.rt.controller;
13
14import java.io.BufferedOutputStream;
15import java.io.File;
16import java.io.FileOutputStream;
17import java.io.IOException;
18import java.io.OutputStream;
19
20import org.jacoco.core.data.ExecutionDataWriter;
21import org.jacoco.core.runtime.AgentOptions;
22import org.jacoco.core.runtime.IRuntime;
23
24/**
25 * Local only agent controller that will write coverage data to the filesystem.
26 * This controller uses the following agent options:
27 * <ul>
28 * <li>destfile</li>
29 * <li>append</li>
30 * </ul>
31 *
32 * @author Brock Janiczak
33 * @version 0.4.1.20101007204400
34 */
35public class LocalController implements IAgentController {
36
37 private IRuntime runtime;
38
39 private OutputStream output;
40
41 public final void startup(final AgentOptions options, final IRuntime runtime)
42 throws IOException {
43 this.runtime = runtime;
44 final File destFile = new File(options.getDestfile()).getAbsoluteFile();
45 final File folder = destFile.getParentFile();
46 if (folder != null) {
47 folder.mkdirs();
48 }
49 output = new BufferedOutputStream(new FileOutputStream(destFile,
50 options.getAppend()));
51 }
52
53 public void writeExecutionData() throws IOException {
54 final ExecutionDataWriter writer = new ExecutionDataWriter(output);
55 runtime.collect(writer, writer, false);
56 }
57
58 public void shutdown() throws IOException {
59 output.close();
60 }
61
62}