Repository navigation
Expand file tree
/
Copy pathCliSession.java
More file actions
194 lines (174 loc) · 5.83 KB
/
Copy pathCliSession.java
File metadata and controls
194 lines (174 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package act.app;
import act.Destroyable;
import act.cli.CliDispatcher;
import act.cli.CommandNameCompleter;
import act.cli.builtin.Exit;
import act.cli.builtin.Help;
import act.cli.event.CliSessionStart;
import act.cli.event.CliSessionTerminate;
import act.cli.util.CommandLineParser;
import act.handler.CliHandler;
import act.util.Banner;
import act.util.DestroyableBase;
import jline.console.ConsoleReader;
import org.osgl.$;
import org.osgl.util.C;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.util.Map;
import static act.app.App.logger;
public class CliSession extends DestroyableBase implements Runnable {
private String id;
private CliServer server;
private CliDispatcher dispatcher;
private App app;
private Socket socket;
private long ts;
private boolean exit;
private Thread runningThread;
private ConsoleReader console;
private CommandNameCompleter commandNameCompleter;
/**
* Allow user command to attach data to the context and fetched for later use.
* <p>
* A typical usage scenario is user command wants to set up a "context" for the
* following commands. However it shall provide a command to exit the "context"
* </p>
*/
private Map<String, Object> attributes = C.newMap();
CliSession(Socket socket, CliServer server) {
this.socket = $.NPE(socket);
this.server = $.NPE(server);
this.app = server.app();
this.dispatcher = app.cliDispatcher();
id = app.cuid();
ts = $.ms();
commandNameCompleter = new CommandNameCompleter(app);
}
public String id() {
return id;
}
void setAttribute(String key, Object val) {
attributes.put(key, val);
}
void removeAttribute(String key) {
attributes.remove(key);
}
<T> T getAttribute(String key) {
return $.cast(attributes.get(key));
}
/**
* Check if this session is expired.
* @param expiration the expiration in seconds
* @return {@code true} if this session is expired
*/
boolean expired(int expiration) {
long l = expiration * 1000;
return l < ($.ms() - ts);
}
@Override
protected void releaseResources() {
stop();
server = null;
Destroyable.Util.tryDestroyAll(attributes.values());
}
@Override
public void run() {
runningThread = Thread.currentThread();
app.eventBus().emitSync(new CliSessionStart(this));
try {
OutputStream os = socket.getOutputStream();
console = new ConsoleReader(socket.getInputStream(), os);
String banner = Banner.cachedBanner();
printBanner(banner, console);
String appName = App.instance().name();
if (S.blank(appName)) {
appName = "act";
}
console.setPrompt(S.fmt("%s[%s]>", appName, id));
console.addCompleter(commandNameCompleter);
while (!exit) {
final String line = console.readLine();
if (exit) {
console.println("session terminated");
console.flush();
return;
}
ts = $.ms();
try {
app.detectChanges();
} catch (RequestRefreshClassLoader refreshRequest) {
refreshRequest.doRefresh(app);
return;
}
if (S.blank(line)) {
continue;
}
CliContext context = new CliContext(line, app, console, this);
//handle the command
final CliHandler handler = dispatcher.handler(context.command());
if (null == handler) {
context.println("Command not recognized: %s", context.command());
continue;
}
if (handler == Exit.INSTANCE) {
console.println("bye");
console.flush();
exit = true;
return;
}
CommandLineParser parser = context.commandLine();
boolean help = parser.getBoolean("-h", "--help");
if (help) {
Help.INSTANCE.showHelp(parser.command(), context);
continue;
}
try {
handler.handle(context);
} catch (Exception e) {
console.println("Error: " + e.getMessage());
}
}
} catch (InterruptedIOException e) {
logger.info("session thread interrupted");
} catch (SocketException e) {
logger.error(e.getMessage());
} catch (Exception e) {
logger.error(e, "Error processing cli session");
} finally {
if (null != server) {
server.remove(this);
}
IO.close(socket);
app.eventBus().emitSync(new CliSessionTerminate(this));
}
}
void stop() {
exit = true;
if (null != runningThread) {
runningThread.interrupt();
}
console = null;
IO.close(socket);
}
void stop(String message) {
if (null != console) {
PrintWriter pw = new PrintWriter(console.getOutput());
pw.println(message);
pw.flush();
}
stop();
}
private static void printBanner(String banner, ConsoleReader console) throws IOException {
String[] lines = banner.split("[\n\r]");
for (String line : lines) {
console.println(line);
}
}
}