Permalink
Browse files

pass command over query param. Tidied up console interface and hidden…

… context. Console status now more verbose.
  • Loading branch information...
1 parent e1bee8e commit 4002d574841971e81d1bcb97bcf3a40bba88e4e3 @albertlatacz committed Oct 18, 2013
@@ -70,8 +70,8 @@
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
-import javarepl.client.ExpressionTemplate;
import javarepl.client.JavaREPLClient;
+import javarepl.rendering.ExpressionTemplate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -67,7 +67,7 @@ private static JavaREPLClient clientFor(Option<String> hostname, Option<Integer>
private static JavaREPLClient connectToRemoteInstance(String hostname, Integer port) {
JavaREPLClient replClient = new JavaREPLClient(hostname, port);
- if (!replClient.isAlive()) {
+ if (!replClient.status().isRunning()) {
console.printError("ERROR: Could not connect to remote REPL instance at http://" + hostname + ":" + port);
System.exit(0);
} else {
@@ -112,9 +112,9 @@ public void run() {
}
private static boolean waitUntilInstanceStarted(JavaREPLClient client) throws Exception {
- for (int i = 0; i < 50; i++) {
- Thread.sleep(100);
- if (client.isAlive())
+ for (int i = 0; i < 500; i++) {
+ Thread.sleep(10);
+ if (client.status().isRunning())
return true;
}
@@ -63,9 +63,7 @@ public static void main(String... args) throws Exception {
if (sandboxed)
sandboxApplication();
- for (String expression : consoleConfig.expressions) {
- console.execute(expression);
- }
+ console.start();
do {
console.execute(reader.readExpression().getOrNull());
@@ -7,6 +7,8 @@
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;
import javarepl.completion.CompletionResult;
+import javarepl.console.ConsoleStatus;
+import javarepl.rendering.ExpressionTemplate;
import static com.googlecode.funclate.Model.persistent.parse;
import static com.googlecode.totallylazy.Option.none;
@@ -15,6 +17,7 @@
import static com.googlecode.utterlyidle.RequestBuilder.get;
import static com.googlecode.utterlyidle.RequestBuilder.post;
import static javarepl.client.EvaluationLog.Type;
+import static javarepl.console.ConsoleStatus.Idle;
public final class JavaREPLClient {
private final String hostname;
@@ -53,11 +56,11 @@ public synchronized CompletionResult completions(String expr) throws Exception {
sequence(model.getValues("candidates", String.class)));
}
- public synchronized boolean isAlive() {
+ public synchronized ConsoleStatus status() {
try {
- return parse(client.handle(get(url("status")).build()).entity().toString()).get("isAlive", Boolean.class);
+ return ConsoleStatus.valueOf(parse(client.handle(get(url("status")).build()).entity().toString()).get("status", String.class));
} catch (Exception e) {
- return false;
+ return Idle;
}
}
@@ -1,11 +1,22 @@
package javarepl.console;
-import com.googlecode.yadic.Container;
+import javarepl.completion.CompletionResult;
+import javarepl.rendering.ExpressionTemplate;
public interface Console {
+
ConsoleResult execute(String expression);
- Container context();
+ CompletionResult completion(String expression);
+
+ ExpressionTemplate template(String expression);
+
+ ConsoleStatus status();
+
+ ConsoleHistory history();
+
+ void start();
void shutdown();
+
}
@@ -0,0 +1,9 @@
+package javarepl.console;
+
+public enum ConsoleStatus {
+ Idle, Starting, Running, Terminating, Terminated;
+
+ public Boolean isRunning() {
+ return this == Running;
+ }
+}
@@ -1,6 +1,7 @@
package javarepl.console;
-import com.googlecode.yadic.Container;
+import javarepl.completion.CompletionResult;
+import javarepl.rendering.ExpressionTemplate;
public abstract class DelegatingConsole implements Console {
private final Console delegate;
@@ -13,11 +14,27 @@ public ConsoleResult execute(String expression) {
return delegate.execute(expression);
}
- public Container context() {
- return delegate.context();
+ public CompletionResult completion(String expression) {
+ return delegate.completion(expression);
+ }
+
+ public ExpressionTemplate template(String expression) {
+ return delegate.template(expression);
+ }
+
+ public ConsoleStatus status() {
+ return delegate.status();
+ }
+
+ public ConsoleHistory history() {
+ return delegate.history();
}
public void shutdown() {
delegate.shutdown();
}
+
+ public void start() {
+ delegate.start();
+ }
}
@@ -10,19 +10,28 @@
import javarepl.completion.*;
import javarepl.console.commands.Command;
import javarepl.console.commands.Commands;
+import javarepl.expressions.Expression;
+import javarepl.rendering.ExpressionTemplate;
import static com.googlecode.totallylazy.Predicates.always;
import static com.googlecode.totallylazy.Predicates.notNullValue;
+import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.totallylazy.Strings.blank;
import static com.googlecode.totallylazy.Strings.startsWith;
+import static javarepl.Utils.randomIdentifier;
import static javarepl.completion.Completers.javaKeywordCompleter;
import static javarepl.completion.TypeResolver.functions.defaultPackageResolver;
import static javarepl.console.ConsoleHistory.historyFromFile;
+import static javarepl.console.ConsoleLog.Type.ERROR;
import static javarepl.console.ConsoleResult.emptyResult;
+import static javarepl.console.ConsoleStatus.*;
import static javarepl.console.commands.Command.functions.completer;
+import static javarepl.rendering.EvaluationClassRenderer.renderExpressionClass;
+import static javarepl.rendering.ExpressionTokenRenderer.EXPRESSION_TOKEN;
public final class SimpleConsole implements Console {
private final Container context;
+ private ConsoleStatus status = Idle;
public SimpleConsole(ConsoleConfig config) {
registerShutdownHook();
@@ -45,10 +54,29 @@ public SimpleConsole(ConsoleConfig config) {
));
- evaluator().addResults(config.results);
+ context.get(Evaluator.class).addResults(config.results);
}
public ConsoleResult execute(String expression) {
+ if (status == Running) {
+ return executeExpression(expression);
+ } else {
+ return new ConsoleResult(expression, sequence(new ConsoleLog(ERROR, "Console is not running (" + status + ")")));
+ }
+ }
+
+ public CompletionResult completion(String expression) {
+ return context.get(Completer.class).apply(expression);
+ }
+
+ public ExpressionTemplate template(String expression) {
+ Evaluator evaluator = context.get(Evaluator.class);
+ Expression parsedExpression = evaluator.parseExpression(expression);
+
+ return new ExpressionTemplate(renderExpressionClass(evaluator.context(), randomIdentifier("Evaluation"), parsedExpression), EXPRESSION_TOKEN);
+ }
+
+ private ConsoleResult executeExpression(String expression) {
context.get(ConsoleHistory.class).add(expression);
ConsoleResult result = evaluationRules().apply(expression);
return result;
@@ -58,8 +86,24 @@ public Container context() {
return context;
}
- public Evaluator evaluator() {
- return context.get(Evaluator.class);
+ public ConsoleStatus status() {
+ return status;
+ }
+
+ public ConsoleHistory history() {
+ return context.get(ConsoleHistory.class);
+ }
+
+ public void start() {
+ if (status() == Idle) {
+ status = Starting;
+
+ for (String expression : context.get(ConsoleConfig.class).expressions) {
+ execute(expression);
+ }
+
+ status = Running;
+ }
}
private void registerShutdownHook() {
@@ -71,8 +115,10 @@ public void run() {
}
public void shutdown() {
+ status = Terminating;
context.get(ConsoleHistory.class).save();
context.get(Evaluator.class).clearOutputDirectory();
+ status = Terminated;
}
private Rules<String, ConsoleResult> evaluationRules() {
@@ -5,19 +5,13 @@
import com.googlecode.totallylazy.Option;
import com.googlecode.utterlyidle.MediaType;
import com.googlecode.utterlyidle.annotations.*;
-import javarepl.Evaluator;
-import javarepl.completion.Completer;
import javarepl.completion.CompletionResult;
-import javarepl.console.ConsoleHistory;
import javarepl.console.ConsoleLog;
import javarepl.console.ConsoleResult;
-import javarepl.expressions.Expression;
+import javarepl.rendering.ExpressionTemplate;
import static com.googlecode.funclate.Model.persistent.model;
import static javarepl.Utils.applicationVersion;
-import static javarepl.Utils.randomIdentifier;
-import static javarepl.rendering.EvaluationClassRenderer.renderExpressionClass;
-import static javarepl.rendering.ExpressionTokenRenderer.EXPRESSION_TOKEN;
public class RestConsoleResource {
private final RestConsole console;
@@ -42,7 +36,7 @@ public Model version() {
@Produces(MediaType.APPLICATION_JSON)
public Model status() {
return model()
- .add("isAlive", true);
+ .add("status", console.status());
}
@POST
@@ -72,19 +66,17 @@ public Model call(String expression) throws Exception {
@Path("template")
@Produces(MediaType.APPLICATION_JSON)
public Model template(@QueryParam("expression") String expr) {
- Evaluator evaluator = console.context().get(Evaluator.class);
- Expression expression = evaluator.parseExpression(expr);
-
+ ExpressionTemplate template = console.template(expr);
return model()
- .add("template", renderExpressionClass(evaluator.context(), randomIdentifier("Evaluation"), expression))
- .add("token", EXPRESSION_TOKEN);
+ .add("template", template.template())
+ .add("token", template.token());
}
@GET
@Path("completions")
@Produces(MediaType.APPLICATION_JSON)
public Model completions(@QueryParam("expression") String expr) {
- CompletionResult result = console.context().get(Completer.class).apply(expr);
+ CompletionResult result = console.completion(expr);
return model()
.add("expression", result.expression())
.add("position", result.position().toString())
@@ -95,7 +87,7 @@ public Model completions(@QueryParam("expression") String expr) {
@Path("history")
@Produces(MediaType.APPLICATION_JSON)
public Model history() {
- return model().add("history", console.context().get(ConsoleHistory.class).items().toList());
+ return model().add("history", console.history().items().toList());
}
private static Mapper<ConsoleLog, Model> commandResultToModel() {
@@ -1,10 +1,10 @@
-package javarepl.client;
+package javarepl.rendering;
public final class ExpressionTemplate {
private final String template;
private final String token;
- ExpressionTemplate(String template, String token) {
+ public ExpressionTemplate(String template, String token) {
this.template = template;
this.token = token;
}
@@ -31,8 +31,8 @@ public void run() {
};
}
- public synchronized Option<WebConsoleClientHandler> createClient() {
- WebConsoleClientHandler clientHandler = new WebConsoleClientHandler();
+ public synchronized Option<WebConsoleClientHandler> createClient(Option<String> command) {
+ WebConsoleClientHandler clientHandler = new WebConsoleClientHandler(command);
clients = clients.insert(clientHandler.id(), clientHandler);
return some(clientHandler);
}
Oops, something went wrong.

0 comments on commit 4002d57

Please sign in to comment.