Skip to content

Commit 0ffdd3c

Browse files
committed
temporary workaround of Cli Console issue
1 parent 5ce3ba5 commit 0ffdd3c

6 files changed

Lines changed: 65 additions & 8 deletions

File tree

src/main/java/act/app/CliContext.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import act.cli.ascii_table.spec.IASCIITableAware;
77
import act.cli.util.CommandLineParser;
88
import act.util.ActContext;
9+
import jline.Terminal2;
910
import jline.console.ConsoleReader;
1011
import org.osgl.$;
1112
import org.osgl.Osgl;
@@ -18,6 +19,7 @@
1819
import org.osgl.util.S;
1920

2021
import java.io.IOException;
22+
import java.io.OutputStream;
2123
import java.io.PrintWriter;
2224
import java.io.Serializable;
2325
import java.util.List;
@@ -34,6 +36,9 @@ public class CliContext extends ActContext.ActContextBase<CliContext> implements
3436

3537
private ConsoleReader console;
3638

39+
// workaround of issue http://stackoverflow.com/questions/34467383/jline2-print-j-when-it-should-print-n-on-a-telnet-console
40+
private PrintWriter pw;
41+
3742
private CommandLineParser parser;
3843

3944
private IASCIITable asciiTable;
@@ -59,6 +64,9 @@ public Object apply(String s, Serializable serializable) throws NotAppliedExcept
5964
};
6065
evaluatorCache = Osgl.T2(getter, setter);
6166
this.console = $.NPE(console);
67+
Terminal2 t2 = $.cast(console.getTerminal());
68+
t2.setEchoEnabled(false);
69+
this.pw = new PrintWriter(console.getOutput());
6270
}
6371

6472
public Osgl.T2<? extends Osgl.Function<String, Serializable>, ? extends Osgl.Func2<String, Serializable, ?>> evaluatorCache() {
@@ -87,22 +95,31 @@ public H.Format accept() {
8795
throw E.unsupport();
8896
}
8997

90-
public void print(String template, Object... args) {
98+
public void print0(String template, Object... args) {
9199
try {
92100
console.print(S.fmt(template, args));
93101
} catch (IOException e) {
94102
throw E.ioException(e);
95103
}
96104
}
97105

98-
public void println(String template, Object... args) {
106+
public void print(String template, Object ... args) {
107+
pw.printf(template, args);
108+
}
109+
110+
public void println0(String template, Object... args) {
99111
try {
100112
console.println(S.fmt(template, args));
101113
} catch (IOException e) {
102114
throw E.ioException(e);
103115
}
104116
}
105117

118+
public void println(String template, Object... args) {
119+
pw.printf(template, args);
120+
pw.println();
121+
}
122+
106123
@Override
107124
protected void releaseResources() {
108125
super.releaseResources();

src/main/java/act/app/CliServer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,16 @@ public void run() {
6060
socket = serverSocket.accept();
6161
CliSession session = new CliSession(socket, this);
6262
sessions.put(session.id(), session);
63-
session.run();
63+
executor.submit(session);
6464
} catch (Exception e) {
6565
log.error(e, "Error processing CLI session");
6666
stop();
6767
return;
6868
} finally {
69-
IO.close(socket);
69+
// Note we cannot close socket here. The ownership
70+
// of socket has been transferred to the CliSession
71+
// and it is up to the session to manage the socket
72+
// IO.close(socket);
7073
}
7174
}
7275
}

src/main/java/act/app/CliSession.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import act.Act;
44
import act.cli.CliDispatcher;
55
import act.cli.builtin.Exit;
6+
import act.cli.event.CliSessionStart;
7+
import act.cli.event.CliSessionTerminate;
68
import act.handler.CliHandler;
79
import act.util.Banner;
810
import act.util.DestroyableBase;
@@ -12,10 +14,13 @@
1214
import org.osgl.util.S;
1315

1416
import java.io.IOException;
17+
import java.io.InterruptedIOException;
18+
import java.io.OutputStream;
19+
import java.io.PrintWriter;
1520
import java.net.Socket;
1621
import java.util.concurrent.atomic.AtomicInteger;
1722

18-
class CliSession extends DestroyableBase implements Runnable {
23+
public class CliSession extends DestroyableBase implements Runnable {
1924

2025
private static final AtomicInteger ID_GEN = new AtomicInteger();
2126

@@ -60,11 +65,12 @@ protected void releaseResources() {
6065
@Override
6166
public void run() {
6267
runningThread = Thread.currentThread();
68+
app.eventBus().emitSync(new CliSessionStart(this));
6369
try {
6470
String user = username();
65-
66-
ConsoleReader console = new ConsoleReader(socket.getInputStream(), socket.getOutputStream());
67-
console.println(Banner.banner(Act.VERSION));
71+
OutputStream os = socket.getOutputStream();
72+
ConsoleReader console = new ConsoleReader(socket.getInputStream(), os);
73+
new PrintWriter(os).println(Banner.banner(Act.VERSION));
6874
console.setPrompt("act[" + user + "]>");
6975

7076
while (!exit) {
@@ -102,6 +108,8 @@ public void run() {
102108
console.println("Error: " + e.getMessage());
103109
}
104110
}
111+
} catch (InterruptedIOException e) {
112+
logger.info("session thread interrupted");
105113
} catch (Throwable e) {
106114
logger.error(e, "Error processing cli session");
107115
} finally {
@@ -111,6 +119,7 @@ public void run() {
111119
} catch (IOException e) {
112120
logger.warn(e, "Failed to close the socket");
113121
}
122+
app.eventBus().emitSync(new CliSessionTerminate(this));
114123
}
115124
}
116125

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package act.cli.event;
2+
3+
import act.app.CliSession;
4+
import act.event.ActEvent;
5+
6+
/**
7+
* This event is emitted synchronously when a {@link CliSession} begin to run
8+
*/
9+
public class CliSessionStart extends ActEvent<CliSession> {
10+
public CliSessionStart(CliSession source) {
11+
super(source);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package act.cli.event;
2+
3+
import act.app.CliSession;
4+
import act.event.ActEvent;
5+
6+
/**
7+
* This event is emitted synchronously when a {@link CliSession} terminated
8+
*/
9+
public class CliSessionTerminate extends ActEvent<CliSession> {
10+
public CliSessionTerminate(CliSession source) {
11+
super(source);
12+
}
13+
}

src/main/java/act/handler/builtin/UnknownHttpMethodHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import act.app.ActionContext;
44
import act.handler.builtin.controller.FastRequestHandler;
55
import act.handler.builtin.controller.RequestHandlerProxy;
6+
import act.handler.event.BeforeCommit;
67
import org.osgl.http.H;
78
import org.osgl.mvc.result.Result;
89

@@ -17,6 +18,7 @@ public void handle(ActionContext context) {
1718
H.Method method = context.req().method();
1819
Result result = context.config().unknownHttpMethodProcessor().handle(method);
1920
result = RequestHandlerProxy.GLOBAL_AFTER_INTERCEPTOR.apply(result, context);
21+
context.app().eventBus().emit(new BeforeCommit(result, context));
2022
result.apply(context.req(), context.resp());
2123
}
2224

0 commit comments

Comments
 (0)