author | egahlin |
Fri, 24 May 2019 19:39:31 +0200 | |
branch | JEP-349-branch |
changeset 57372 | 50ca040843ea |
parent 57361 | 53dccc90a5be |
child 57374 | 41f0051285e0 |
permissions | -rw-r--r-- |
57361 | 1 |
/* |
2 |
* Copyright (c) 2019, 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.consumer; |
|
27 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
28 |
import java.io.IOException; |
57361 | 29 |
import java.nio.file.Path; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
30 |
import java.security.AccessControlContext; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
31 |
import java.security.AccessController; |
57361 | 32 |
import java.time.Duration; |
33 |
import java.util.Objects; |
|
34 |
import java.util.function.Consumer; |
|
35 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
36 |
import jdk.jfr.internal.consumer.EventConsumer; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
37 |
import jdk.jfr.internal.consumer.RecordingInput; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
38 |
|
57361 | 39 |
/** |
40 |
* Implementation of an event stream that operates against a recording file. |
|
41 |
* |
|
42 |
*/ |
|
43 |
final class EventFileStream implements EventStream { |
|
44 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
45 |
final static class FileEventConsumer extends EventConsumer { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
46 |
private final RecordingInput input; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
47 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
48 |
public FileEventConsumer(AccessControlContext acc, RecordingInput input) throws IOException { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
49 |
super(acc); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
50 |
this.input = input; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
51 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
52 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
53 |
@Override |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
54 |
public void process() throws Exception { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
55 |
// TODO This need more work; filter, multiple chunk etc |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
56 |
ChunkParser cp = new ChunkParser(input); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
57 |
while (true) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
58 |
RecordedEvent e = cp.readEvent(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
59 |
dispatch(e); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
60 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
61 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
62 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
63 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
64 |
private final RecordingInput input; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
65 |
private final FileEventConsumer eventConsumer; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
66 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
67 |
public EventFileStream(Path path) throws IOException { |
57361 | 68 |
Objects.requireNonNull(path); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
69 |
input = new RecordingInput(path.toFile()); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
70 |
eventConsumer = new FileEventConsumer(AccessController.getContext(), input); |
57361 | 71 |
} |
72 |
||
73 |
@Override |
|
74 |
public void onEvent(Consumer<RecordedEvent> action) { |
|
75 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
76 |
eventConsumer.onEvent(action); |
57361 | 77 |
} |
78 |
||
79 |
@Override |
|
80 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
81 |
Objects.requireNonNull(eventName); |
|
82 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
83 |
eventConsumer.onEvent(eventName, action); |
57361 | 84 |
} |
85 |
||
86 |
@Override |
|
87 |
public void onFlush(Runnable action) { |
|
88 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
89 |
eventConsumer.onFlush(action); |
57361 | 90 |
} |
91 |
||
92 |
@Override |
|
93 |
public void onClose(Runnable action) { |
|
94 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
95 |
eventConsumer.addCloseAction(action); |
57361 | 96 |
} |
97 |
||
98 |
@Override |
|
99 |
public void close() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
100 |
eventConsumer.setClosed(true); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
101 |
eventConsumer.runCloseActions(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
102 |
try { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
103 |
input.close(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
104 |
} catch (IOException e) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
105 |
// ignore |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
106 |
} |
57361 | 107 |
} |
108 |
||
109 |
@Override |
|
110 |
public boolean remove(Object action) { |
|
111 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
112 |
return eventConsumer.remove(action); |
57361 | 113 |
} |
114 |
||
115 |
@Override |
|
116 |
public void start() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
117 |
eventConsumer.start(0); |
57361 | 118 |
} |
119 |
||
120 |
@Override |
|
121 |
public void startAsync() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
122 |
eventConsumer.startAsync(0); |
57361 | 123 |
} |
124 |
||
125 |
@Override |
|
126 |
public void awaitTermination(Duration timeout) { |
|
127 |
Objects.requireNonNull(timeout); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
128 |
eventConsumer.awaitTermination(timeout); |
57361 | 129 |
} |
130 |
||
131 |
@Override |
|
132 |
public void awaitTermination() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
133 |
eventConsumer.awaitTermination(); |
57361 | 134 |
} |
135 |
} |