50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr;
|
|
27 |
|
|
28 |
import java.io.Closeable;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.nio.file.Path;
|
|
32 |
import java.time.Duration;
|
|
33 |
import java.time.Instant;
|
|
34 |
import java.util.Collections;
|
|
35 |
import java.util.HashMap;
|
|
36 |
import java.util.Map;
|
|
37 |
import java.util.Objects;
|
|
38 |
|
|
39 |
import jdk.jfr.internal.PlatformRecording;
|
|
40 |
import jdk.jfr.internal.Type;
|
|
41 |
import jdk.jfr.internal.Utils;
|
|
42 |
import jdk.jfr.internal.WriteableUserPath;
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Provides means to configure, start, stop and dump recording data to disk.
|
|
46 |
* <p>
|
|
47 |
* The following example shows how configure, start, stop and dump recording data to disk.
|
|
48 |
*
|
|
49 |
* <pre>
|
|
50 |
* <code>
|
|
51 |
* Configuration c = Configuration.getConfiguration("default");
|
|
52 |
* Recording r = new Recording(c);
|
|
53 |
* r.start();
|
|
54 |
* System.gc();
|
|
55 |
* Thread.sleep(5000);
|
|
56 |
* r.stop();
|
|
57 |
* r.copyTo(Files.createTempFile("my-recording", ".jfr"));
|
|
58 |
* </code>
|
|
59 |
* </pre>
|
|
60 |
*
|
|
61 |
* @since 9
|
|
62 |
*/
|
|
63 |
public final class Recording implements Closeable {
|
|
64 |
|
|
65 |
private static class RecordingSettings extends EventSettings {
|
|
66 |
|
|
67 |
private final Recording recording;
|
|
68 |
private final String identifier;
|
|
69 |
|
|
70 |
RecordingSettings(Recording r, String identifier) {
|
|
71 |
this.recording = r;
|
|
72 |
this.identifier = identifier;
|
|
73 |
}
|
|
74 |
|
|
75 |
RecordingSettings(Recording r, Class<? extends Event> eventClass) {
|
|
76 |
Utils.ensureValidEventSubclass(eventClass);
|
|
77 |
this.recording = r;
|
|
78 |
this.identifier = String.valueOf(Type.getTypeId(eventClass));
|
|
79 |
}
|
|
80 |
|
|
81 |
@Override
|
|
82 |
public EventSettings with(String name, String value) {
|
|
83 |
Objects.requireNonNull(value);
|
|
84 |
recording.setSetting(identifier + "#" + name, value);
|
|
85 |
return this;
|
|
86 |
}
|
|
87 |
|
|
88 |
@Override
|
|
89 |
public Map<String, String> toMap() {
|
|
90 |
return recording.getSettings();
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
private final PlatformRecording internal;
|
|
95 |
|
|
96 |
private Recording(PlatformRecording internal) {
|
|
97 |
this.internal = internal;
|
|
98 |
this.internal.setRecording(this);
|
|
99 |
if (internal.getRecording() != this) {
|
|
100 |
throw new InternalError("Internal recording not properly setup");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Creates a recording without any settings.
|
|
106 |
* <p>
|
|
107 |
* A newly created recording is in the {@link RecordingState#NEW} state. To start
|
|
108 |
* the recording, invoke the {@link Recording#start()} method.
|
|
109 |
*
|
|
110 |
* @throws IllegalStateException if Flight Recorder can't be created (for
|
|
111 |
* example, if the Java Virtual Machine (JVM) lacks Flight Recorder
|
|
112 |
* support, or if the file repository can't be created or accessed)
|
|
113 |
*
|
|
114 |
* @throws SecurityException If a security manager is used and
|
|
115 |
* FlightRecorderPermission "accessFlightRecorder" is not set.
|
|
116 |
*/
|
|
117 |
public Recording() {
|
|
118 |
this(FlightRecorder.getFlightRecorder().newInternalRecording(new HashMap<String, String>()));
|
|
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
* Creates a recording with settings from a configuration.
|
|
123 |
* <p>
|
|
124 |
* The following example shows how create a recording that uses a predefined configuration.
|
|
125 |
*
|
|
126 |
* <pre>
|
|
127 |
* <code>
|
|
128 |
* Recording r = new Recording(Configuration.getConfiguration("default"));
|
|
129 |
* </code>
|
|
130 |
* </pre>
|
|
131 |
*
|
|
132 |
* The newly created recording is in the {@link RecordingState#NEW} state. To
|
|
133 |
* start the recording, invoke the {@link Recording#start()} method.
|
|
134 |
*
|
|
135 |
* @param configuration configuration that contains the settings to be use, not
|
|
136 |
* {@code null}
|
|
137 |
*
|
|
138 |
* @throws IllegalStateException if Flight Recorder can't be created (for
|
|
139 |
* example, if the Java Virtual Machine (JVM) lacks Flight Recorder
|
|
140 |
* support, or if the file repository can't be created or accessed)
|
|
141 |
*
|
|
142 |
* @throws SecurityException if a security manager is used and
|
|
143 |
* FlightRecorderPermission "accessFlightRecorder" is not set.
|
|
144 |
*
|
|
145 |
* @see Configuration
|
|
146 |
*/
|
|
147 |
public Recording(Configuration configuration) {
|
|
148 |
this(FlightRecorder.getFlightRecorder().newInternalRecording(configuration.getSettings()));
|
|
149 |
}
|
|
150 |
|
|
151 |
/**
|
|
152 |
* Starts this recording.
|
|
153 |
* <p>
|
|
154 |
* It's recommended that the recording options and event settings are configured
|
|
155 |
* before calling this method. The benefits of doing so are a more consistent
|
|
156 |
* state when analyzing the recorded data, and improved performance because the
|
|
157 |
* configuration can be applied atomically.
|
|
158 |
* <p>
|
|
159 |
* After a successful invocation of this method, this recording is in the
|
|
160 |
* {@code RUNNING} state.
|
|
161 |
*
|
|
162 |
* @throws IllegalStateException if recording is already started or is in the
|
|
163 |
* {@code CLOSED} state
|
|
164 |
*/
|
|
165 |
public void start() {
|
|
166 |
internal.start();
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Starts this recording after a delay.
|
|
171 |
* <p>
|
|
172 |
* After a successful invocation of this method, this recording is in the
|
|
173 |
* {@code DELAYED} state.
|
|
174 |
*
|
|
175 |
* @param delay the time to wait before starting this recording, not
|
|
176 |
* {@code null}
|
|
177 |
* @throws IllegalStateException if the recording is not it the {@code NEW} state
|
|
178 |
*/
|
|
179 |
public void scheduleStart(Duration delay) {
|
|
180 |
Objects.requireNonNull(delay);
|
|
181 |
internal.scheduleStart(delay);
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* Stops this recording.
|
|
186 |
* <p>
|
|
187 |
* When a recording is stopped it can't be restarted. If this
|
|
188 |
* recording has a destination, data is written to that destination and
|
|
189 |
* the recording is closed. After a recording is closed, the data is no longer
|
|
190 |
* available.
|
|
191 |
* <p>
|
|
192 |
* After a successful invocation of this method, this recording will be
|
|
193 |
* in the {@code STOPPED} state.
|
|
194 |
*
|
|
195 |
* @return {@code true} if recording is stopped, {@code false} otherwise
|
|
196 |
*
|
|
197 |
* @throws IllegalStateException if the recording is not started or is already stopped
|
|
198 |
*
|
|
199 |
* @throws SecurityException if a security manager exists and the caller
|
|
200 |
* doesn't have {@code FilePermission} to write to the destination
|
|
201 |
* path
|
|
202 |
*
|
|
203 |
* @see #setDestination(Path)
|
|
204 |
*
|
|
205 |
*/
|
|
206 |
public boolean stop() {
|
|
207 |
return internal.stop("Stopped by user");
|
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
|
211 |
* Returns settings used by this recording.
|
|
212 |
* <p>
|
|
213 |
* Modifying the returned {@code Map} will not change the settings for this recording.
|
|
214 |
* <p>
|
|
215 |
* If no settings are set for this recording, an empty {@code Map} is
|
|
216 |
* returned.
|
|
217 |
*
|
|
218 |
* @return recording settings, not {@code null}
|
|
219 |
*/
|
|
220 |
public Map<String, String> getSettings() {
|
|
221 |
return new HashMap<>(internal.getSettings());
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Returns the current size of this recording in the disk repository,
|
|
226 |
* measured in bytes.
|
|
227 |
* <p>
|
|
228 |
* The size is updated when recording buffers are flushed. If the recording is
|
|
229 |
* not written to the disk repository the returned size is always {@code 0}.
|
|
230 |
*
|
|
231 |
* @return amount of recorded data, measured in bytes, or {@code 0} if the
|
|
232 |
* recording is not written to the disk repository
|
|
233 |
*/
|
|
234 |
public long getSize() {
|
|
235 |
return internal.getSize();
|
|
236 |
}
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Returns the time when this recording was stopped.
|
|
240 |
*
|
|
241 |
* @return the time, or {@code null} if this recording is not stopped
|
|
242 |
*/
|
|
243 |
public Instant getStopTime() {
|
|
244 |
return internal.getStopTime();
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Returns the time when this recording was started.
|
|
249 |
*
|
|
250 |
* @return the the time, or {@code null} if this recording is not started
|
|
251 |
*/
|
|
252 |
public Instant getStartTime() {
|
|
253 |
return internal.getStartTime();
|
|
254 |
}
|
|
255 |
|
|
256 |
/**
|
|
257 |
* Returns the maximum size, measured in bytes, at which data is no longer kept in the disk repository.
|
|
258 |
*
|
|
259 |
* @return maximum size in bytes, or {@code 0} if no maximum size is set
|
|
260 |
*/
|
|
261 |
public long getMaxSize() {
|
|
262 |
return internal.getMaxSize();
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Returns the length of time that the data is kept in the disk repository
|
|
267 |
* before it is removed.
|
|
268 |
*
|
|
269 |
* @return maximum length of time, or {@code null} if no maximum length of time
|
|
270 |
* has been set
|
|
271 |
*/
|
|
272 |
public Duration getMaxAge() {
|
|
273 |
return internal.getMaxAge();
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Returns the name of this recording.
|
|
278 |
* <p>
|
|
279 |
* By default, the name is the same as the recording ID.
|
|
280 |
*
|
|
281 |
* @return the recording name, not {@code null}
|
|
282 |
*/
|
|
283 |
public String getName() {
|
|
284 |
return internal.getName();
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Replaces all settings for this recording.
|
|
289 |
* <p>
|
|
290 |
* The following example shows how to set event settings for a recording.
|
|
291 |
*
|
|
292 |
* <pre>
|
|
293 |
* <code>
|
|
294 |
* Map{@literal <}String, String{@literal >} settings = new HashMap{@literal <}{@literal >}();
|
|
295 |
* settings.putAll(EventSettings.enabled("jdk.CPUSample").withPeriod(Duration.ofSeconds(2)).toMap());
|
|
296 |
* settings.putAll(EventSettings.enabled(MyEvent.class).withThreshold(Duration.ofSeconds(2)).withoutStackTrace().toMap());
|
|
297 |
* settings.put("jdk.ExecutionSample#period", "10 ms");
|
|
298 |
* recording.setSettings(settings);
|
|
299 |
* </code>
|
|
300 |
* </pre>
|
|
301 |
*
|
|
302 |
* The following example shows how to merge settings.
|
|
303 |
*
|
|
304 |
* <pre>
|
|
305 |
* {@code
|
|
306 |
* Map<String, String> settings = recording.getSettings();
|
|
307 |
* settings.putAll(additionalSettings);
|
|
308 |
* recording.setSettings(settings);
|
|
309 |
* }
|
|
310 |
* </pre>
|
|
311 |
*
|
|
312 |
* @param settings the settings to set, not {@code null}
|
|
313 |
*/
|
|
314 |
public void setSettings(Map<String, String> settings) {
|
|
315 |
Objects.requireNonNull(settings);
|
|
316 |
Map<String, String> sanitized = Utils.sanitizeNullFreeStringMap(settings);
|
|
317 |
internal.setSettings(sanitized);
|
|
318 |
}
|
|
319 |
|
|
320 |
/**
|
|
321 |
* Returns the recording state that this recording is currently in.
|
|
322 |
*
|
|
323 |
* @return the recording state, not {@code null}
|
|
324 |
*
|
|
325 |
* @see RecordingState
|
|
326 |
*/
|
|
327 |
public RecordingState getState() {
|
|
328 |
return internal.getState();
|
|
329 |
}
|
|
330 |
|
|
331 |
/**
|
|
332 |
* Releases all data that is associated with this recording.
|
|
333 |
* <p>
|
|
334 |
* After a successful invocation of this method, this recording is in the
|
|
335 |
* {@code CLOSED} state.
|
|
336 |
*/
|
|
337 |
@Override
|
|
338 |
public void close() {
|
|
339 |
internal.close();
|
|
340 |
}
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Returns a clone of this recording, with a new recording ID and name.
|
|
344 |
*
|
|
345 |
* Clones are useful for dumping data without stopping the recording. After
|
|
346 |
* a clone is created, the amount of data to copy is constrained
|
|
347 |
* with the {@link #setMaxAge(Duration)} method and the {@link #setMaxSize(long)}method.
|
|
348 |
*
|
|
349 |
* @param stop {@code true} if the newly created copy should be stopped
|
|
350 |
* immediately, {@code false} otherwise
|
|
351 |
* @return the recording copy, not {@code null}
|
|
352 |
*/
|
|
353 |
public Recording copy(boolean stop) {
|
|
354 |
return internal.newCopy(stop);
|
|
355 |
}
|
|
356 |
|
|
357 |
/**
|
|
358 |
* Writes recording data to a file.
|
|
359 |
* <p>
|
|
360 |
* Recording must be started, but not necessarily stopped.
|
|
361 |
*
|
|
362 |
* @param destination the location where recording data is written, not
|
|
363 |
* {@code null}
|
|
364 |
*
|
|
365 |
* @throws IOException if the recording can't be copied to the specified
|
|
366 |
* location
|
|
367 |
*
|
|
368 |
* @throws SecurityException if a security manager exists and the caller doesn't
|
|
369 |
* have {@code FilePermission} to write to the destination path
|
|
370 |
*/
|
|
371 |
public void dump(Path destination) throws IOException {
|
|
372 |
Objects.requireNonNull(destination);
|
|
373 |
internal.copyTo(new WriteableUserPath(destination), "Dumped by user", Collections.emptyMap());
|
|
374 |
}
|
|
375 |
|
|
376 |
/**
|
|
377 |
* Returns {@code true} if this recording uses the disk repository, {@code false} otherwise.
|
|
378 |
* <p>
|
|
379 |
* If no value is set, {@code true} is returned.
|
|
380 |
*
|
|
381 |
* @return {@code true} if the recording uses the disk repository, {@code false}
|
|
382 |
* otherwise
|
|
383 |
*/
|
|
384 |
public boolean isToDisk() {
|
|
385 |
return internal.isToDisk();
|
|
386 |
}
|
|
387 |
|
|
388 |
/**
|
|
389 |
* Determines how much data is kept in the disk repository.
|
|
390 |
* <p>
|
|
391 |
* To control the amount of recording data that is stored on disk, the maximum
|
|
392 |
* amount of data to retain can be specified. When the maximum limit is
|
|
393 |
* exceeded, the Java Virtual Machine (JVM) removes the oldest chunk to make
|
|
394 |
* room for a more recent chunk.
|
|
395 |
* <p>
|
|
396 |
* If neither maximum limit or the maximum age is set, the size of the
|
|
397 |
* recording may grow indefinitely.
|
|
398 |
*
|
|
399 |
* @param maxSize the amount of data to retain, {@code 0} if infinite
|
|
400 |
*
|
|
401 |
* @throws IllegalArgumentException if <code>maxSize</code> is negative
|
|
402 |
*
|
|
403 |
* @throws IllegalStateException if the recording is in {@code CLOSED} state
|
|
404 |
*/
|
|
405 |
public void setMaxSize(long maxSize) {
|
|
406 |
if (maxSize < 0) {
|
|
407 |
throw new IllegalArgumentException("Max size of recording can't be negative");
|
|
408 |
}
|
|
409 |
internal.setMaxSize(maxSize);
|
|
410 |
}
|
|
411 |
|
|
412 |
/**
|
|
413 |
* Determines how far back data is kept in the disk repository.
|
|
414 |
* <p>
|
|
415 |
* To control the amount of recording data stored on disk, the maximum length of
|
|
416 |
* time to retain the data can be specified. Data stored on disk that is older
|
|
417 |
* than the specified length of time is removed by the Java Virtual Machine (JVM).
|
|
418 |
* <p>
|
|
419 |
* If neither maximum limit or the maximum age is set, the size of the
|
|
420 |
* recording may grow indefinitely.
|
|
421 |
*
|
|
422 |
* @param maxAge the length of time that data is kept, or {@code null} if infinite
|
|
423 |
*
|
|
424 |
* @throws IllegalArgumentException if <code>maxAge</code> is negative
|
|
425 |
*
|
|
426 |
* @throws IllegalStateException if the recording is in the {@code CLOSED} state
|
|
427 |
*/
|
|
428 |
public void setMaxAge(Duration maxAge) {
|
|
429 |
if (maxAge != null && maxAge.isNegative()) {
|
|
430 |
throw new IllegalArgumentException("Max age of recording can't be negative");
|
|
431 |
}
|
|
432 |
internal.setMaxAge(maxAge);
|
|
433 |
}
|
|
434 |
|
|
435 |
/**
|
|
436 |
* Sets a location where data is written on recording stop, or
|
|
437 |
* {@code null} if data is not to be dumped.
|
|
438 |
* <p>
|
|
439 |
* If a destination is set, this recording is automatically closed
|
|
440 |
* after data is successfully copied to the destination path.
|
|
441 |
* <p>
|
|
442 |
* If a destination is <em>not</em> set, Flight Recorder retains the
|
|
443 |
* recording data until this recording is closed. Use the {@link #dump(Path)} method to
|
|
444 |
* manually write data to a file.
|
|
445 |
*
|
|
446 |
* @param destination the destination path, or {@code null} if recording should
|
|
447 |
* not be dumped at stop
|
|
448 |
*
|
|
449 |
* @throws IllegalStateException if recording is in the {@code STOPPED} or
|
|
450 |
* {@code CLOSED} state.
|
|
451 |
*
|
|
452 |
* @throws SecurityException if a security manager exists and the caller
|
|
453 |
* doesn't have {@code FilePermission} to read, write, and delete the
|
|
454 |
* {@code destination} file
|
|
455 |
*
|
|
456 |
* @throws IOException if the path is not writable
|
|
457 |
*/
|
|
458 |
public void setDestination(Path destination) throws IOException {
|
|
459 |
internal.setDestination(destination != null ? new WriteableUserPath(destination) : null);
|
|
460 |
}
|
|
461 |
|
|
462 |
/**
|
|
463 |
* Returns the destination file, where recording data is written when the
|
|
464 |
* recording stops, or {@code null} if no destination is set.
|
|
465 |
*
|
|
466 |
* @return the destination file, or {@code null} if not set.
|
|
467 |
*/
|
|
468 |
public Path getDestination() {
|
|
469 |
WriteableUserPath usp = internal.getDestination();
|
|
470 |
if (usp == null) {
|
|
471 |
return null;
|
|
472 |
} else {
|
|
473 |
return usp.getPotentiallyMaliciousOriginal();
|
|
474 |
}
|
|
475 |
}
|
|
476 |
|
|
477 |
/**
|
|
478 |
* Returns a unique ID for this recording.
|
|
479 |
*
|
|
480 |
* @return the recording ID
|
|
481 |
*/
|
|
482 |
public long getId() {
|
|
483 |
return internal.getId();
|
|
484 |
}
|
|
485 |
|
|
486 |
/**
|
|
487 |
* Sets a human-readable name (for example, {@code "My Recording"}).
|
|
488 |
*
|
|
489 |
* @param name the recording name, not {@code null}
|
|
490 |
*
|
|
491 |
* @throws IllegalStateException if the recording is in {@code CLOSED} state
|
|
492 |
*/
|
|
493 |
public void setName(String name) {
|
|
494 |
Objects.requireNonNull(name);
|
|
495 |
internal.setName(name);
|
|
496 |
}
|
|
497 |
|
|
498 |
/**
|
|
499 |
* Sets whether this recording is dumped to disk when the JVM exits.
|
|
500 |
*
|
|
501 |
* @param dumpOnExit if this recording should be dumped when the JVM exits
|
|
502 |
*/
|
|
503 |
public void setDumpOnExit(boolean dumpOnExit) {
|
|
504 |
internal.setDumpOnExit(dumpOnExit);
|
|
505 |
}
|
|
506 |
|
|
507 |
/**
|
|
508 |
* Returns whether this recording is dumped to disk when the JVM exits.
|
|
509 |
* <p>
|
|
510 |
* If dump on exit is not set, {@code false} is returned.
|
|
511 |
*
|
|
512 |
* @return {@code true} if the recording is dumped on exit, {@code false}
|
|
513 |
* otherwise.
|
|
514 |
*/
|
|
515 |
public boolean getDumpOnExit() {
|
|
516 |
return internal.getDumpOnExit();
|
|
517 |
}
|
|
518 |
|
|
519 |
/**
|
|
520 |
* Determines whether this recording is continuously flushed to the disk
|
|
521 |
* repository or data is constrained to what is available in memory buffers.
|
|
522 |
*
|
|
523 |
* @param disk {@code true} if this recording is written to disk,
|
|
524 |
* {@code false} if in-memory
|
|
525 |
*
|
|
526 |
*/
|
|
527 |
public void setToDisk(boolean disk) {
|
|
528 |
internal.setToDisk(disk);
|
|
529 |
}
|
|
530 |
|
|
531 |
/**
|
|
532 |
* Creates a data stream for a specified interval.
|
|
533 |
* <p>
|
|
534 |
* The stream may contain some data outside the specified range.
|
|
535 |
*
|
|
536 |
* @param the start start time for the stream, or {@code null} to get data from
|
|
537 |
* start time of the recording
|
|
538 |
*
|
|
539 |
* @param the end end time for the stream, or {@code null} to get data until the
|
|
540 |
* present time.
|
|
541 |
*
|
|
542 |
* @return an input stream, or {@code null} if no data is available in the
|
|
543 |
* interval.
|
|
544 |
*
|
|
545 |
* @throws IllegalArgumentException if {@code end} happens before
|
|
546 |
* {@code start}
|
|
547 |
*
|
|
548 |
* @throws IOException if a stream can't be opened
|
|
549 |
*/
|
|
550 |
public InputStream getStream(Instant start, Instant end) throws IOException {
|
|
551 |
if (start != null && end != null && end.isBefore(start)) {
|
|
552 |
throw new IllegalArgumentException("End time of requested stream must not be before start time");
|
|
553 |
}
|
|
554 |
return internal.open(start, end);
|
|
555 |
}
|
|
556 |
|
|
557 |
/**
|
|
558 |
* Returns the specified duration for this recording, or {@code null} if no
|
|
559 |
* duration is set.
|
|
560 |
* <p>
|
|
561 |
* The duration can be set only when the recording is in the
|
|
562 |
* {@link RecordingState#NEW} state.
|
|
563 |
*
|
|
564 |
* @return the desired duration of the recording, or {@code null} if no duration
|
|
565 |
* has been set.
|
|
566 |
*/
|
|
567 |
public Duration getDuration() {
|
|
568 |
return internal.getDuration();
|
|
569 |
}
|
|
570 |
|
|
571 |
/**
|
|
572 |
* Sets a duration for how long a recording runs before it stops.
|
|
573 |
* <p>
|
|
574 |
* By default, a recording has no duration ({@code null}).
|
|
575 |
*
|
|
576 |
* @param duration the duration, or {@code null} if no duration is set
|
|
577 |
*
|
|
578 |
* @throws IllegalStateException if recording is in the {@code STOPPED} or {@code CLOSED} state
|
|
579 |
*/
|
|
580 |
public void setDuration(Duration duration) {
|
|
581 |
internal.setDuration(duration);
|
|
582 |
}
|
|
583 |
|
|
584 |
/**
|
|
585 |
* Enables the event with the specified name.
|
|
586 |
* <p>
|
|
587 |
* If multiple events have the same name (for example, the same class is loaded
|
|
588 |
* in different class loaders), then all events that match the name are enabled. To
|
|
589 |
* enable a specific class, use the {@link #enable(Class)} method or a {@code String}
|
|
590 |
* representation of the event type ID.
|
|
591 |
*
|
|
592 |
* @param name the settings for the event, not {@code null}
|
|
593 |
*
|
|
594 |
* @return an event setting for further configuration, not {@code null}
|
|
595 |
*
|
|
596 |
* @see EventType
|
|
597 |
*/
|
|
598 |
public EventSettings enable(String name) {
|
|
599 |
Objects.requireNonNull(name);
|
|
600 |
RecordingSettings rs = new RecordingSettings(this, name);
|
|
601 |
rs.with("enabled", "true");
|
|
602 |
return rs;
|
|
603 |
}
|
|
604 |
|
|
605 |
/**
|
|
606 |
* Disables event with the specified name.
|
|
607 |
* <p>
|
|
608 |
* If multiple events with same name (for example, the same class is loaded
|
|
609 |
* in different class loaders), then all events that match the
|
|
610 |
* name is disabled. To disable a specific class, use the
|
|
611 |
* {@link #disable(Class)} method or a {@code String} representation of the event
|
|
612 |
* type ID.
|
|
613 |
*
|
|
614 |
* @param name the settings for the event, not {@code null}
|
|
615 |
*
|
|
616 |
* @return an event setting for further configuration, not {@code null}
|
|
617 |
*
|
|
618 |
*/
|
|
619 |
public EventSettings disable(String name) {
|
|
620 |
Objects.requireNonNull(name);
|
|
621 |
RecordingSettings rs = new RecordingSettings(this, name);
|
|
622 |
rs.with("enabled", "false");
|
|
623 |
return rs;
|
|
624 |
}
|
|
625 |
|
|
626 |
/**
|
|
627 |
* Enables event.
|
|
628 |
*
|
|
629 |
* @param eventClass the event to enable, not {@code null}
|
|
630 |
*
|
|
631 |
* @throws IllegalArgumentException if {@code eventClass} is an abstract
|
|
632 |
* class or not a subclass of {@link Event}
|
|
633 |
*
|
|
634 |
* @return an event setting for further configuration, not {@code null}
|
|
635 |
*/
|
|
636 |
public EventSettings enable(Class<? extends Event> eventClass) {
|
|
637 |
Objects.requireNonNull(eventClass);
|
|
638 |
RecordingSettings rs = new RecordingSettings(this, eventClass);
|
|
639 |
rs.with("enabled", "true");
|
|
640 |
return rs;
|
|
641 |
}
|
|
642 |
|
|
643 |
/**
|
|
644 |
* Disables event.
|
|
645 |
*
|
|
646 |
* @param eventClass the event to enable, not {@code null}
|
|
647 |
*
|
|
648 |
* @throws IllegalArgumentException if {@code eventClass} is an abstract
|
|
649 |
* class or not a subclass of {@link Event}
|
|
650 |
*
|
|
651 |
* @return an event setting for further configuration, not {@code null}
|
|
652 |
*
|
|
653 |
*/
|
|
654 |
public EventSettings disable(Class<? extends Event> eventClass) {
|
|
655 |
Objects.requireNonNull(eventClass);
|
|
656 |
RecordingSettings rs = new RecordingSettings(this, eventClass);
|
|
657 |
rs.with("enabled", "false");
|
|
658 |
return rs;
|
|
659 |
}
|
|
660 |
|
|
661 |
// package private
|
|
662 |
PlatformRecording getInternal() {
|
|
663 |
return internal;
|
|
664 |
}
|
|
665 |
|
|
666 |
private void setSetting(String id, String value) {
|
|
667 |
Objects.requireNonNull(id);
|
|
668 |
Objects.requireNonNull(value);
|
|
669 |
internal.setSetting(id, value);
|
|
670 |
}
|
|
671 |
|
|
672 |
}
|