author | egahlin |
Wed, 26 Jun 2019 16:04:47 +0200 | |
branch | JEP-349-branch |
changeset 57432 | ba454a26d2c1 |
parent 57386 | acdd0dbe37ee |
child 57433 | 83e4343a6984 |
permissions | -rw-r--r-- |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
1 |
/* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
4 |
* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
10 |
* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
15 |
* accompanied this code). |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
16 |
* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
20 |
* |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
23 |
* questions. |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
24 |
*/ |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
25 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
26 |
package jdk.jfr.internal.consumer; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
27 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
28 |
import java.io.IOException; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
29 |
import java.lang.invoke.MethodHandles; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
30 |
import java.lang.invoke.VarHandle; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
31 |
import java.security.AccessControlContext; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
32 |
import java.security.AccessController; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
33 |
import java.security.PrivilegedAction; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
34 |
import java.time.Duration; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
35 |
import java.util.ArrayList; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
36 |
import java.util.Arrays; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
37 |
import java.util.List; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
38 |
import java.util.Objects; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
39 |
import java.util.function.Consumer; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
40 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
41 |
import jdk.jfr.EventType; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
42 |
import jdk.jfr.consumer.RecordedEvent; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
43 |
import jdk.jfr.internal.JVM; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
44 |
import jdk.jfr.internal.LogLevel; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
45 |
import jdk.jfr.internal.LogTag; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
46 |
import jdk.jfr.internal.Logger; |
57373 | 47 |
import jdk.jfr.internal.LongMap; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
48 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
49 |
abstract public class EventConsumer implements Runnable { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
50 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
51 |
private final static class EventDispatcher { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
52 |
public final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0]; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
53 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
54 |
final private String eventName; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
55 |
final Consumer<RecordedEvent> action; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
56 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
57 |
public EventDispatcher(Consumer<RecordedEvent> action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
58 |
this(null, action); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
59 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
60 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
61 |
public EventDispatcher(String eventName, Consumer<RecordedEvent> action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
62 |
this.eventName = eventName; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
63 |
this.action = action; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
64 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
65 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
66 |
public void offer(RecordedEvent event) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
67 |
action.accept(event); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
68 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
69 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
70 |
public boolean accepts(EventType eventType) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
71 |
return (eventName == null || eventType.getName().equals(eventName)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
72 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
73 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
74 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
75 |
private final static JVM jvm = JVM.getJVM(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
76 |
private final static VarHandle closedHandle; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
77 |
private final static VarHandle consumersHandle; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
78 |
private final static VarHandle dispatcherHandle; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
79 |
private final static VarHandle flushActionsHandle; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
80 |
private final static VarHandle closeActionsHandle; |
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
81 |
private final static VarHandle orderedHandle; |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
82 |
private final static VarHandle reuseHandle; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
83 |
static { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
84 |
try { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
85 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
86 |
closedHandle = l.findVarHandle(EventConsumer.class, "closed", boolean.class); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
87 |
consumersHandle = l.findVarHandle(EventConsumer.class, "consumers", EventDispatcher[].class); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
88 |
dispatcherHandle = l.findVarHandle(EventConsumer.class, "dispatcher", LongMap.class); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
89 |
flushActionsHandle = l.findVarHandle(EventConsumer.class, "flushActions", Runnable[].class); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
90 |
closeActionsHandle = l.findVarHandle(EventConsumer.class, "closeActions", Runnable[].class); |
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
91 |
orderedHandle = l.findVarHandle(EventConsumer.class, "ordered", boolean.class); |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
92 |
reuseHandle = l.findVarHandle(EventConsumer.class, "reuse", boolean.class); |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
93 |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
94 |
} catch (ReflectiveOperationException e) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
95 |
throw new InternalError(e); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
96 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
97 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
98 |
// set by VarHandle |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
99 |
private boolean closed; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
100 |
// set by VarHandle |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
101 |
private EventDispatcher[] consumers = new EventDispatcher[0]; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
102 |
// set by VarHandle |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
103 |
private LongMap<EventDispatcher[]> dispatcher = new LongMap<>(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
104 |
// set by VarHandle |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
105 |
private Runnable[] flushActions = new Runnable[0]; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
106 |
// set by VarHandle |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
107 |
private Runnable[] closeActions = new Runnable[0]; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
108 |
|
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
109 |
protected boolean ordered = true; |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
110 |
|
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
111 |
protected boolean reuse = true; |
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
112 |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
113 |
protected InternalEventFilter eventFilter = InternalEventFilter.ACCEPT_ALL; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
114 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
115 |
private final AccessControlContext accessControlContext; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
116 |
private boolean started; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
117 |
private Thread thread; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
118 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
119 |
protected long startNanos; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
120 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
121 |
public EventConsumer(AccessControlContext acc) throws IOException { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
122 |
this.accessControlContext = acc; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
123 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
124 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
125 |
public void run() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
126 |
doPriviliged(() -> execute()); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
127 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
128 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
129 |
void doPriviliged(Runnable r) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
130 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
131 |
@Override |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
132 |
public Void run() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
133 |
r.run(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
134 |
return null; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
135 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
136 |
}, accessControlContext); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
137 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
138 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
139 |
private void execute() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
140 |
jvm.exclude(Thread.currentThread()); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
141 |
try { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
142 |
process(); |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
143 |
} catch (IOException e) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
144 |
if (!isClosed()) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
145 |
logException(e); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
146 |
} |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
147 |
} catch (Exception e) { |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
148 |
logException(e); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
149 |
} finally { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
150 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended."); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
151 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
152 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
153 |
|
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
154 |
private void logException(Exception e) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
155 |
e.printStackTrace(); // for debugging purposes, remove before |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
156 |
// integration |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
157 |
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Unexpected error processing stream. " + e.getMessage()); |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
158 |
} |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
159 |
|
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
160 |
public abstract void process() throws IOException; |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
161 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
162 |
public synchronized boolean remove(Object action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
163 |
boolean remove = false; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
164 |
Runnable[] updatedFlushActions = removeAction(flushActions, action); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
165 |
if (updatedFlushActions != null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
166 |
flushActionsHandle.setVolatile(this, updatedFlushActions); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
167 |
remove = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
168 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
169 |
Runnable[] updatedCloseActions = removeAction(closeActions, action); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
170 |
if (updatedCloseActions != null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
171 |
closeActionsHandle.setVolatile(this, updatedCloseActions); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
172 |
remove = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
173 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
174 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
175 |
boolean removeConsumer = false; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
176 |
List<EventDispatcher> list = new ArrayList<>(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
177 |
for (int i = 0; i < consumers.length; i++) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
178 |
if (consumers[i].action != action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
179 |
list.add(consumers[i]); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
180 |
} else { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
181 |
removeConsumer = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
182 |
remove = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
183 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
184 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
185 |
if (removeConsumer) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
186 |
EventDispatcher[] array = list.toArray(new EventDispatcher[list.size()]); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
187 |
consumersHandle.setVolatile(this, array); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
188 |
dispatcherHandle.setVolatile(this, new LongMap<>()); // will reset |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
189 |
// dispatch |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
190 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
191 |
return remove; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
192 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
193 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
194 |
public void dispatch(RecordedEvent e) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
195 |
EventDispatcher[] consumerDispatch = dispatcher.get(e.getEventType().getId()); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
196 |
if (consumerDispatch == null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
197 |
consumerDispatch = EventDispatcher.NO_DISPATCHERS; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
198 |
for (EventDispatcher ec : consumers.clone()) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
199 |
if (ec.accepts(e.getEventType())) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
200 |
consumerDispatch = merge(consumerDispatch, ec); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
201 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
202 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
203 |
dispatcher.put(e.getEventType().getId(), consumerDispatch); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
204 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
205 |
for (int i = 0; i < consumerDispatch.length; i++) { |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
206 |
try { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
207 |
consumerDispatch[i].offer(e); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
208 |
} catch (Exception exception) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
209 |
// Is this a reasonable behavior for an exception? |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
210 |
// Error will abort the stream. |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57373
diff
changeset
|
211 |
} |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
212 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
213 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
214 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
215 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
216 |
public void onEvent(Consumer<RecordedEvent> action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
217 |
add(new EventDispatcher(action)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
218 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
219 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
220 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
221 |
add(new EventDispatcher(eventName, action)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
222 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
223 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
224 |
private synchronized void add(EventDispatcher e) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
225 |
consumersHandle.setVolatile(this, merge(consumers, e)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
226 |
dispatcherHandle.setVolatile(this, new LongMap<>()); // will reset |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
227 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
228 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
229 |
public synchronized void onFlush(Runnable action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
230 |
flushActionsHandle.setVolatile(this, addAction(flushActions, action)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
231 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
232 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
233 |
public synchronized void addCloseAction(Runnable action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
234 |
closeActionsHandle.setVolatile(this, addAction(closeActions, action)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
235 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
236 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
237 |
public void setClosed(boolean closed) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
238 |
closedHandle.setVolatile(this, closed); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
239 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
240 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
241 |
final public boolean isClosed() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
242 |
return closed; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
243 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
244 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
245 |
public void runCloseActions() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
246 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
247 |
Runnable[] cas = this.closeActions; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
248 |
for (int i = 0; i < cas.length; i++) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
249 |
cas[i].run(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
250 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
251 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
252 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
253 |
public void runFlushActions() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
254 |
Runnable[] fas = this.flushActions; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
255 |
for (int i = 0; i < fas.length; i++) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
256 |
fas[i].run(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
257 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
258 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
259 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
260 |
public synchronized void startAsync(long startNanos) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
261 |
if (started) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
262 |
throw new IllegalStateException("Event stream can only be started once"); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
263 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
264 |
started = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
265 |
setStartNanos(startNanos); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
266 |
thread = new Thread(this); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
267 |
thread.setDaemon(true); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
268 |
thread.start(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
269 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
270 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
271 |
public void start(long startNanos) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
272 |
synchronized (this) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
273 |
if (started) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
274 |
throw new IllegalStateException("Event stream can only be started once"); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
275 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
276 |
started = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
277 |
setStartNanos(startNanos); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
278 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
279 |
run(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
280 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
281 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
282 |
public void awaitTermination(Duration timeout) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
283 |
Objects.requireNonNull(timeout); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
284 |
Thread t = null; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
285 |
synchronized (this) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
286 |
t = thread; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
287 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
288 |
if (t != null && t != Thread.currentThread()) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
289 |
try { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
290 |
t.join(timeout.toMillis()); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
291 |
} catch (InterruptedException e) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
292 |
// ignore |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
293 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
294 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
295 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
296 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
297 |
public void awaitTermination() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
298 |
awaitTermination(Duration.ofMillis(0)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
299 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
300 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
301 |
private void setStartNanos(long startNanos) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
302 |
this.startNanos = startNanos; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
303 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
304 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
305 |
protected static EventDispatcher[] merge(EventDispatcher[] current, EventDispatcher add) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
306 |
EventDispatcher[] array = new EventDispatcher[current.length + 1]; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
307 |
System.arraycopy(current, 0, array, 0, current.length); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
308 |
array[current.length] = add; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
309 |
return array; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
310 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
311 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
312 |
private static Runnable[] removeAction(Runnable[] array, Object action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
313 |
if (array.length == 0) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
314 |
return null; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
315 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
316 |
boolean remove = false; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
317 |
List<Runnable> list = new ArrayList<>(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
318 |
for (int i = 0; i < array.length; i++) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
319 |
if (array[i] != action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
320 |
list.add(array[i]); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
321 |
} else { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
322 |
remove = true; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
323 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
324 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
325 |
if (remove) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
326 |
return list.toArray(new Runnable[list.size()]); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
327 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
328 |
return null; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
329 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
330 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
331 |
private static Runnable[] addAction(Runnable[] array, Runnable action) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
332 |
ArrayList<Runnable> a = new ArrayList<>(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
333 |
a.addAll(Arrays.asList(array)); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
334 |
a.add(action); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
335 |
return a.toArray(new Runnable[0]); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
336 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
337 |
|
57386 | 338 |
abstract public void close(); |
339 |
||
340 |
public void setReuse(boolean reuse) { |
|
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
341 |
reuseHandle.setVolatile(this, reuse); |
57386 | 342 |
} |
343 |
||
344 |
public void setOrdered(boolean ordered) { |
|
57432
ba454a26d2c1
SetOrdered and setReuse now work on a directory stream
egahlin
parents:
57386
diff
changeset
|
345 |
orderedHandle.setVolatile(this, ordered); |
57386 | 346 |
} |
347 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff
changeset
|
348 |
} |