org.rapaio.jupyter.kernel.install.KernelJsonBuilder Maven / Gradle / Ivy
package org.rapaio.jupyter.kernel.install;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.rapaio.jupyter.kernel.core.RapaioKernel;
public class KernelJsonBuilder {
private static final String SEPARATOR = "/";
private static final String CONNECTION_FILE_MARKER = "{connection_file}";
private static final String LANGUAGE = "java";
private static final String INTERRUPT_MODE = "message";
public static final String DEFAULT_DISPLAY_NAME = "Rapaio Kernel";
public static final String DEFAULT_KERNEL_DIR = "rapaio-jupyter-kernel";
private static final String DEFAULT_JAR_NAME = "rapaio-jupyter-kernel.jar";
private String displayName = DEFAULT_DISPLAY_NAME;
private String kernelDir = DEFAULT_KERNEL_DIR;
private String jarPath = null;
private final Map env = new HashMap<>() {{
put(RapaioKernel.RJK_COMPILER_OPTIONS, RapaioKernel.DEFAULT_COMPILER_OPTIONS);
put(RapaioKernel.RJK_TIMEOUT_MILLIS, RapaioKernel.DEFAULT_RJK_TIMEOUT_MILLIS);
put(RapaioKernel.RJK_INIT_SCRIPT, RapaioKernel.DEFAULT_INIT_SCRIPT);
}};
private void validate() {
Objects.requireNonNull(jarPath);
}
public KernelJsonBuilder withDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
public KernelJsonBuilder withKernelDir(String kernelDir) {
this.kernelDir = kernelDir;
return this;
}
public KernelJsonBuilder withJarPath(String jarPath) {
if (jarPath.endsWith("/")) {
jarPath = jarPath.substring(0, jarPath.length() - 1);
}
this.jarPath = jarPath;
return this;
}
public KernelJsonBuilder withEnvCompilerOptions(String compilerOptions) {
env.put(RapaioKernel.RJK_COMPILER_OPTIONS, compilerOptions);
return this;
}
public KernelJsonBuilder withEnvTimeoutMillis(String timeoutMillis) {
env.put(RapaioKernel.RJK_TIMEOUT_MILLIS, timeoutMillis);
return this;
}
public KernelJsonBuilder withEnvInitScript(String initScript) {
env.put(RapaioKernel.RJK_INIT_SCRIPT, initScript);
return this;
}
public KernelJson build() {
validate();
String jarPath = KernelJsonBuilder.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String jarName = jarPath.substring(jarPath.lastIndexOf('/') + 1);
if (jarName.isEmpty()) {
jarName = DEFAULT_JAR_NAME;
}
List argv = new ArrayList<>();
argv.add("java");
argv.addAll(List.of("--enable-preview", "--add-modules", "jdk.incubator.vector,jdk.incubator.concurrent"));
argv.addAll(List.of("-jar", this.jarPath + SEPARATOR + kernelDir + SEPARATOR + jarName));
argv.add(CONNECTION_FILE_MARKER);
return new KernelJson(argv.toArray(String[]::new), displayName, LANGUAGE, INTERRUPT_MODE, env);
}
}