Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jjava-distro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
4 changes: 3 additions & 1 deletion jjava-distro/src/main/java/org/dflib/jjava/distro/JJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public static void main(String[] args) throws Exception {
.version((String) pomProps.getOrDefault("version", ""))

.extensionsEnabled(Env.extensionsEnabled())
.startupSnippets(Env.startupSnippets())
.compilerOpts(Env.compilerOpts())
.timeout(Env.timeout())

Expand All @@ -79,6 +78,9 @@ public static void main(String[] args) throws Exception {
// process custom locations: expand JShell classpath, install extensions from those places (if enabled)
kernel.addToClasspath(Env.extraClasspath());

// run user defined startup snippets explicitly after the default startup
Env.startupSnippets().forEach(kernel::evalRaw);

// connect to Jupyter
kernel.becomeHandlerForConnection(connection);
connection.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Container;

import java.util.Map;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.matchesPattern;

public class KernelStartupIT extends ContainerizedKernelCase {

Expand All @@ -17,4 +20,17 @@ public void startUp() throws Exception {
assertThat(snippetResult.getStderr(), not(containsString("|")));
assertThat(snippetResult.getStdout(), containsString("1001.0"));
}

@Test
void startUp_scriptRequiresClasspath() throws Exception {
Map<String, String> env = Map.of(
Env.JJAVA_CLASSPATH, TEST_CLASSPATH,
Env.JJAVA_STARTUP_SCRIPT, "var obj = new org.dflib.jjava.Dummy()"
);
String snippet = "\"hash = \" + obj.hashCode()";
Container.ExecResult snippetResult = executeInKernel(snippet, env);

assertThat(snippetResult.getStdout(), not(containsString("|")));
assertThat(snippetResult.getStdout(), matchesPattern("(?s).*hash = -?\\d+.*"));
}
}
4 changes: 2 additions & 2 deletions jjava-jupyter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -49,4 +49,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected JShell buildJShell(JJavaExecutionControlProvider jShellExecControlProv
}

protected CodeEvaluator buildCodeEvaluator(JShell jShell, JJavaExecutionControlProvider jShellExecControlProvider) {
return new CodeEvaluator(jShell, jShellExecControlProvider, jShellExecControlID, startupSnippets);
return new CodeEvaluator(jShell, jShellExecControlProvider, jShellExecControlID);
}

protected MagicParser buildMagicParser(MagicTranspiler transpiler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,16 @@ public class CodeEvaluator {
private final JJavaExecutionControlProvider execControlProvider;
private final String execControlID;
private final SourceCodeAnalysis sourceAnalyzer;
private final List<String> startupSnippets;

private volatile boolean initialized;

public CodeEvaluator(
JShell shell,
JJavaExecutionControlProvider execControlProvider,
String execControlID,
List<String> startupSnippets) {
String execControlID) {

this.shell = shell;
this.execControlProvider = execControlProvider;
this.execControlID = execControlID;
this.sourceAnalyzer = shell.sourceCodeAnalysis();
this.startupSnippets = startupSnippets;
}

private SourceCodeAnalysis.CompletionInfo analyzeCompletion(String source) {
Expand Down Expand Up @@ -129,35 +124,6 @@ protected Object evalSingle(String code) {
}

public Object eval(String code) {
initIfNeeded();
return doEval(code);
}

private void initIfNeeded() {

// TODO: should we even bother trying to avoid a race condition with locking? Does Jupyter guarantee serial
// cell execution?
if (!initialized) {
synchronized (this) {
if (!initialized) {

// Runs startup snippets in the shell to initialize the environment. The call is deferred until the
// first user requested evaluation to cleanly return errors when they happen.

for (String s : startupSnippets) {
// call "doEval" to bypass "initIfNeeded" and avoid infinite recursion
doEval(s);
}

startupSnippets.clear();
initialized = true;
}
}
}
}


private Object doEval(String code) {
Object lastEvalResult = null;
SourceCodeAnalysis.CompletionInfo info = this.sourceAnalyzer.analyzeCompletion(code);

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<commons.logging.version>1.3.5</commons.logging.version>

<junit5.version>5.11.2</junit5.version>
<hamcrest.version>1.3</hamcrest.version>
<hamcrest.version>3.0</hamcrest.version>
<jimfs.version>1.3.0</jimfs.version>
<testcontainers.version>1.21.3</testcontainers.version>
<slf4j.version>2.0.17</slf4j.version>
Expand Down Expand Up @@ -131,7 +131,7 @@
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
</dependency>
<dependency>
Expand Down
Loading