-
Notifications
You must be signed in to change notification settings - Fork 169
Add Web UI to Spring Shell demo #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patrick96
wants to merge
1
commit into
graalvm:master
Choose a base branch
from
patrick96:pz/spring-shell-interactive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...asm-spring-shell/src/main/java/com/example/wasm_spring_shell/web/CustomResultHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.example.wasm_spring_shell.web; | ||
|
|
||
| import org.jline.utils.AttributedCharSequence; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProperty; | ||
| import org.springframework.shell.ResultHandler; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
|
|
||
| @Component | ||
| public class CustomResultHandler implements ResultHandler<Object> { | ||
| @Override | ||
| public void handleResult(Object result) { | ||
| switch (result) { | ||
| case null -> Worker.sendErrorMessage("null result"); | ||
| case AttributedCharSequence attributed -> { | ||
| System.out.println(attributed); | ||
| Worker.sendOutputMessage(attributed.toAnsi()); | ||
| } | ||
| case Throwable throwable -> { | ||
| StringWriter stringWriter = new StringWriter(); | ||
| throwable.printStackTrace(new PrintWriter(stringWriter)); | ||
| Worker.sendErrorMessage(stringWriter.toString()); | ||
| } | ||
| default -> Worker.sendOutputMessage(result.toString()); | ||
| } | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
.../wasm-spring-shell/src/main/java/com/example/wasm_spring_shell/web/DemoInputProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.example.wasm_spring_shell.web; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add copyright header, here and in all other .java files |
||
|
|
||
| import org.graalvm.webimage.api.JS; | ||
| import org.jline.reader.History; | ||
| import org.jline.reader.ParsedLine; | ||
| import org.jline.reader.impl.DefaultParser; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProperty; | ||
| import org.springframework.shell.Input; | ||
| import org.springframework.shell.InputProvider; | ||
| import org.springframework.shell.Utils; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Component | ||
| public class DemoInputProvider implements InputProvider { | ||
| private static final Logger LOG = LoggerFactory.getLogger(DemoInputProvider.class); | ||
|
|
||
| private final History history; | ||
|
|
||
| public DemoInputProvider(History history) { | ||
| this.history = history; | ||
| } | ||
|
|
||
| @Override | ||
| public Input readInput() { | ||
| var lineParser = new DefaultParser(); | ||
| Worker.sendReadyMessage(); | ||
| LOG.info("Waiting for message..."); | ||
| waitForMessage(); | ||
| String line = getMessage(); | ||
| LOG.info("Got message {}", line); | ||
| history.add(line); | ||
| ParsedLine parsedLine = lineParser.parse(line, line.length() + 1); | ||
| return new SimpleInput(parsedLine); | ||
| } | ||
|
|
||
| @JS.Coerce | ||
| @JS(""" | ||
| const length = globalThis.sharedI32[1]; | ||
| const byteArray = new Uint8Array(length); | ||
| byteArray.set(new Uint8Array(globalThis.sharedBuffer, 8, length)) | ||
| return new TextDecoder().decode(byteArray); | ||
| """) | ||
| public static native String getMessage(); | ||
|
|
||
| @JS(""" | ||
| Atomics.wait(globalThis.sharedI32, 0, 0); | ||
| Atomics.store(globalThis.sharedI32, 0, 0); | ||
| """) | ||
| public static native void waitForMessage(); | ||
|
|
||
| public static class SimpleInput implements Input { | ||
| private final ParsedLine parsedLine; | ||
|
|
||
| public SimpleInput(ParsedLine parsedLine) { | ||
| this.parsedLine = parsedLine; | ||
| } | ||
|
|
||
| @Override | ||
| public String rawText() { | ||
| return parsedLine.line(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> words() { | ||
| return Utils.sanitizeInput(parsedLine.words()); | ||
| } | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
...-spring-shell/src/main/java/com/example/wasm_spring_shell/web/MyResultHandlerService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.example.wasm_spring_shell.web; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProperty; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.shell.result.GenericResultHandlerService; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Primary | ||
| @Component | ||
| public class MyResultHandlerService extends GenericResultHandlerService { | ||
| public MyResultHandlerService(CustomResultHandler handler) { | ||
| this.addResultHandler(handler); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
...-image/wasm-spring-shell/src/main/java/com/example/wasm_spring_shell/web/ShellRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.example.wasm_spring_shell.web; | ||
|
|
||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBooleanProperty; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.shell.Shell; | ||
| import org.springframework.shell.context.InteractionMode; | ||
| import org.springframework.shell.context.ShellContext; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class ShellRunner implements CommandLineRunner { | ||
| private final ApplicationContext context; | ||
|
|
||
| public ShellRunner(ApplicationContext context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(String... args) throws Exception { | ||
| context.getBean(ShellContext.class).setInteractionMode(InteractionMode.INTERACTIVE); | ||
| context.getBean(Shell.class).run(context.getBean(DemoInputProvider.class)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think with something like this, you can append
-H:-AutoRunVMto the default list, and not repeat and override it: