Skip to content

Commit 3083705

Browse files
committed
Configurable recipe classloader for PrepareRecipe
1 parent 6844072 commit 3083705

3 files changed

Lines changed: 20 additions & 27 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/rpc/RewriteRpc.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public class RewriteRpc {
6767
private final AtomicReference<PrintStream> log = new AtomicReference<>();
6868
private final AtomicReference<TraceGetObject> traceGetObject = new AtomicReference<>(
6969
new TraceGetObject(false, false));
70+
private final AtomicReference<ClassLoader> recipeClassLoader = new AtomicReference<>(
71+
RewriteRpc.class.getClassLoader());
7072

7173
final PreparedRecipeCache preparedRecipes = new PreparedRecipeCache();
7274

@@ -142,7 +144,7 @@ protected Object handle(Void noParams) {
142144
}
143145
}
144146
});
145-
jsonRpc.rpc("PrepareRecipe", new PrepareRecipe.Handler(preparedRecipes));
147+
jsonRpc.rpc("PrepareRecipe", new PrepareRecipe.Handler(preparedRecipes, recipeClassLoader));
146148
jsonRpc.rpc("Print", new Print.Handler(this::getObject));
147149

148150
jsonRpc.bind();
@@ -169,6 +171,11 @@ public RewriteRpc log(@Nullable PrintStream logFile) {
169171
return this;
170172
}
171173

174+
public RewriteRpc recipeClassLoader(ClassLoader recipeClassLoader) {
175+
this.recipeClassLoader.set(recipeClassLoader);
176+
return this;
177+
}
178+
172179
public void shutdown() {
173180
//noinspection ConstantValue
174181
if (log.get() != null) {

rewrite-core/src/main/java/org/openrewrite/rpc/request/PrepareRecipe.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.rpc.internal.PreparedRecipeCache;
2626

2727
import java.util.Map;
28+
import java.util.concurrent.atomic.AtomicReference;
2829

2930
import static java.util.Collections.emptyList;
3031

@@ -36,10 +37,11 @@ public class PrepareRecipe implements RpcRequest {
3637
@RequiredArgsConstructor
3738
public static class Handler extends JsonRpcMethod<PrepareRecipe> {
3839
private final PreparedRecipeCache preparedRecipes;
40+
private final AtomicReference<ClassLoader> recipeClassLoader;
3941

4042
@Override
4143
protected Object handle(PrepareRecipe request) throws Exception {
42-
Recipe recipe = new RecipeLoader(null).load(request.getId(), request.getOptions());
44+
Recipe recipe = new RecipeLoader(recipeClassLoader.get()).load(request.getId(), request.getOptions());
4345
String instanceId = SnowflakeId.generateId();
4446
preparedRecipes.getInstantiated().put(instanceId, recipe);
4547
return new PrepareRecipeResponse(

rewrite-javascript/src/main/java/org/openrewrite/javascript/rpc/JavaScriptRewriteRpc.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public static class Builder implements Supplier<JavaScriptRewriteRpc> {
107107
private Duration timeout = Duration.ofSeconds(30);
108108
private boolean traceRpcMessages;
109109
private @Nullable Integer inspectBrk;
110-
private boolean profiler;
111110
private @Nullable Integer maxHeapSize;
112111
private @Nullable Path workingDirectory;
112+
private ClassLoader recipeClassLoader = JavaScriptRewriteRpc.class.getClassLoader();
113113

114114
public Builder marketplace(Environment marketplace) {
115115
this.marketplace = marketplace;
@@ -181,26 +181,6 @@ public Builder inspectBrk() {
181181
return inspectBrk(9229);
182182
}
183183

184-
/**
185-
* Enable V8 CPU profiling for performance analysis. When enabled, the process will
186-
* generate a CPU profile that can be analyzed to identify performance bottlenecks.
187-
* The profile is saved in Chrome DevTools format (.cpuprofile) on shutdown.
188-
* <p>
189-
* Profiling uses the V8 Inspector API and works with both npx and direct node execution modes.
190-
* The profile file can be loaded in Chrome DevTools for analysis.
191-
*
192-
* @param profiler Whether to enable profiling
193-
* @return This builder
194-
*/
195-
public Builder profiler(boolean profiler) {
196-
this.profiler = profiler;
197-
return this;
198-
}
199-
200-
public Builder profiler() {
201-
return profiler(true);
202-
}
203-
204184
/**
205185
* Set the maximum heap size for the Node.js process in megabytes.
206186
* Default V8 heap size is approximately 1.5-2 GB on 64-bit systems.
@@ -227,6 +207,11 @@ public Builder workingDirectory(@Nullable Path workingDirectory) {
227207
return this;
228208
}
229209

210+
public Builder recipeClassLoader(ClassLoader recipeClassLoader) {
211+
this.recipeClassLoader = recipeClassLoader;
212+
return this;
213+
}
214+
230215
@Override
231216
public JavaScriptRewriteRpc get() {
232217
Stream<@Nullable String> cmd;
@@ -245,7 +230,6 @@ public JavaScriptRewriteRpc get() {
245230
"node",
246231
"--enable-source-maps",
247232
"--inspect-brk=" + inspectBrk,
248-
profiler ? "--prof" : null,
249233
maxHeapSize != null ? "--max-old-space-size=" + maxHeapSize : null,
250234
serverJs.toAbsolutePath().normalize().toString(),
251235
log == null ? null : "--log-file=" + log.toAbsolutePath().normalize(),
@@ -262,8 +246,7 @@ public JavaScriptRewriteRpc get() {
262246
log == null ? null : "--log-file=" + log.toAbsolutePath().normalize(),
263247
metricsCsv == null ? null : "--metrics-csv=" + metricsCsv.toAbsolutePath().normalize(),
264248
traceRpcMessages ? "--trace-rpc-messages" : null,
265-
recipeInstallDir == null ? null : "--recipe-install-dir=" + recipeInstallDir.toAbsolutePath().normalize(),
266-
profiler ? "--profile" : null
249+
recipeInstallDir == null ? null : "--recipe-install-dir=" + recipeInstallDir.toAbsolutePath().normalize()
267250
);
268251
}
269252

@@ -293,7 +276,8 @@ public JavaScriptRewriteRpc get() {
293276
String.join(" ", cmdArr), process.environment())
294277
.livenessCheck(process::getLivenessCheck)
295278
.timeout(timeout)
296-
.log(log == null ? null : new PrintStream(Files.newOutputStream(log, StandardOpenOption.APPEND, StandardOpenOption.CREATE)));
279+
.log(log == null ? null : new PrintStream(Files.newOutputStream(log, StandardOpenOption.APPEND, StandardOpenOption.CREATE)))
280+
.recipeClassLoader(recipeClassLoader);
297281
} catch (IOException e) {
298282
throw new UncheckedIOException(e);
299283
}

0 commit comments

Comments
 (0)