-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FileOutput.java
101 lines (88 loc) · 2.96 KB
/
FileOutput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
* Copyright (c) 2009, 2022 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Brock Janiczak - initial API and implementation
*
*******************************************************************************/
package org.jacoco.agent.rt.internal.output;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.OverlappingFileLockException;
import org.jacoco.core.data.ExecutionDataWriter;
import org.jacoco.core.runtime.AgentOptions;
import org.jacoco.core.runtime.RuntimeData;
/**
* Local only agent output that will write coverage data to the filesystem. This
* controller uses the following agent options:
* <ul>
* <li>destfile</li>
* <li>append</li>
* </ul>
*/
public class FileOutput implements IAgentOutput {
private static final int LOCK_RETRY_COUNT = 30;
private static final long LOCK_RETRY_WAIT_TIME_MS = 100;
private RuntimeData data;
private File destFile;
private boolean append;
public final void startup(final AgentOptions options,
final RuntimeData data) throws IOException {
this.data = data;
this.destFile = new File(options.getDestfile()).getAbsoluteFile();
this.append = options.getAppend();
final File folder = destFile.getParentFile();
if (folder != null) {
folder.mkdirs();
}
// Make sure we can write to the file:
openFile().close();
}
public void writeExecutionData(final boolean reset) throws IOException {
final OutputStream output = openFile();
try {
final ExecutionDataWriter writer = new ExecutionDataWriter(output);
data.collect(writer, writer, reset);
} finally {
output.close();
}
}
public void shutdown() throws IOException {
// Nothing to do
}
private OutputStream openFile() throws IOException {
final FileOutputStream file = new FileOutputStream(destFile, append);
// Avoid concurrent writes from different agents running in parallel:
final FileChannel fc = file.getChannel();
int retries = 0;
while (true) {
try {
// An agent from another JVM might have a lock. In this case
// this method blocks until the lock is freed.
fc.lock();
return file;
} catch (final OverlappingFileLockException e) {
// In the case of multiple class loaders there can be multiple
// JaCoCo runtimes even in the same VM. In this case we get an
// OverlappingFileLockException and retry lock acquisition:
if (retries++ > LOCK_RETRY_COUNT) {
throw e;
}
}
try {
Thread.sleep(LOCK_RETRY_WAIT_TIME_MS);
} catch (final InterruptedException e) {
throw new InterruptedIOException();
}
}
}
}