-
Notifications
You must be signed in to change notification settings - Fork 208
/
IJavaExecutionControl.java
175 lines (154 loc) · 7.05 KB
/
IJavaExecutionControl.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Spencer Park
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.spencerpark.ijava.execution;
import jdk.jshell.EvalException;
import jdk.jshell.execution.DirectExecutionControl;
import jdk.jshell.spi.SPIResolutionException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* An ExecutionControl very similar to {@link jdk.jshell.execution.LocalExecutionControl} but which
* also logs the actual result of an invocation before being serialized.
*/
public class IJavaExecutionControl extends DirectExecutionControl {
/**
* A special "class name" for a {@link jdk.jshell.spi.ExecutionControl.UserException} such that it may be
* identified after serialization into an {@link jdk.jshell.EvalException} via {@link
* EvalException#getExceptionClassName()}.
*/
public static final String EXECUTION_TIMEOUT_NAME = "Execution Timeout"; // Has spaces to not collide with a class name
/**
* A special "class name" for a {@link jdk.jshell.spi.ExecutionControl.UserException} such that it may be
* identified after serialization into an {@link jdk.jshell.EvalException} via {@link
* EvalException#getExceptionClassName()}
*/
public static final String EXECUTION_INTERRUPTED_NAME = "Execution Interrupted";
private static final Object NULL = new Object();
private static final AtomicInteger EXECUTOR_THREAD_ID = new AtomicInteger(0);
private final ExecutorService executor;
private final long timeoutTime;
private final TimeUnit timeoutUnit;
private final ConcurrentMap<String, Future<Object>> running = new ConcurrentHashMap<>();
private final Map<String, Object> results = new ConcurrentHashMap<>();
public IJavaExecutionControl() {
this(-1, TimeUnit.MILLISECONDS);
}
public IJavaExecutionControl(long timeoutTime, TimeUnit timeoutUnit) {
this.timeoutTime = timeoutTime;
this.timeoutUnit = timeoutTime > 0 ? Objects.requireNonNull(timeoutUnit) : TimeUnit.MILLISECONDS;
this.executor = Executors.newCachedThreadPool(r -> new Thread(r, "IJava-executor-" + EXECUTOR_THREAD_ID.getAndIncrement()));
}
public long getTimeoutDuration() {
return timeoutTime;
}
public TimeUnit getTimeoutUnit() {
return timeoutUnit;
}
public Object takeResult(String key) {
Object result = this.results.remove(key);
if (result == null)
throw new IllegalStateException("No result with key: " + key);
return result == NULL ? null : result;
}
private Object execute(String key, Method doitMethod) throws TimeoutException, Exception {
Future<Object> runningTask = this.executor.submit(() -> doitMethod.invoke(null));
this.running.put(key, runningTask);
try {
if (this.timeoutTime > 0)
return runningTask.get(this.timeoutTime, this.timeoutUnit);
return runningTask.get();
} catch (CancellationException e) {
// If canceled this means that stop() or interrupt() was invoked.
if (this.executor.isShutdown())
// If the executor is shutdown, the situation is the former in which
// case the protocol is to throw an ExecutionControl.StoppedException.
throw new StoppedException();
else
// The execution was purposely interrupted.
throw new UserException(
"Execution interrupted.",
EXECUTION_INTERRUPTED_NAME,
e.getStackTrace()
);
} catch (ExecutionException e) {
// The execution threw an exception. The actual exception is the cause of the ExecutionException.
Throwable cause = e.getCause();
if (cause instanceof InvocationTargetException) {
// Unbox further
cause = cause.getCause();
}
if (cause == null)
throw new UserException("null", "Unknown Invocation Exception", e.getStackTrace());
else if (cause instanceof SPIResolutionException)
throw new ResolutionException(((SPIResolutionException) cause).id(), cause.getStackTrace());
else
throw new UserException(String.valueOf(cause.getMessage()), String.valueOf(cause.getClass().getName()), cause.getStackTrace());
} catch (TimeoutException e) {
throw new UserException(
String.format("Execution timed out after configured timeout of %d %s.", this.timeoutTime, this.timeoutUnit.toString().toLowerCase()),
EXECUTION_TIMEOUT_NAME,
e.getStackTrace()
);
} finally {
this.running.remove(key, runningTask);
}
}
/**
* This method was hijacked and actually only returns a key that can be
* later retrieved via {@link #takeResult(String)}. This should be called
* for every invocation as the objects are saved and not taking them will
* leak the memory.
* <p></p>
* {@inheritDoc}
*
* @returns the key to use for {@link #takeResult(String) looking up the result}.
*/
@Override
protected String invoke(Method doitMethod) throws Exception {
String id = UUID.randomUUID().toString();
Object value = this.execute(id, doitMethod);
this.results.put(id, value);
return id;
}
public void interrupt() {
this.running.forEach((id, future) ->
future.cancel(true));
}
@Override
public void stop() throws EngineTerminationException, InternalException {
this.executor.shutdownNow();
}
@Override
public String toString() {
return "IJavaExecutionControl{" +
"timeoutTime=" + timeoutTime +
", timeoutUnit=" + timeoutUnit +
'}';
}
}