Skip to content

Commit ed8bc8a

Browse files
committed
Added command system
1 parent c78b51a commit ed8bc8a

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>Command</artifactId>
5252
<version>1.0-SNAPSHOT</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>bsh</groupId>
56+
<artifactId>bsh</artifactId>
57+
<version>2.0b4</version>
58+
</dependency>
5459
<dependency>
5560
<groupId>org.junit.jupiter</groupId>
5661
<artifactId>junit-jupiter-engine</artifactId>

src/main/java/org/javawebstack/framework/WebApplication.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.javawebstack.command.CommandSystem;
66
import org.javawebstack.framework.bind.ModelBindParamTransformer;
77
import org.javawebstack.framework.bind.ModelBindTransformer;
8+
import org.javawebstack.framework.command.ShellCommand;
9+
import org.javawebstack.framework.command.StartCommand;
810
import org.javawebstack.framework.config.Config;
911
import org.javawebstack.framework.module.Module;
1012
import org.javawebstack.framework.util.CORSPolicy;
@@ -95,11 +97,8 @@ public WebApplication(){
9597
modules.forEach(m -> m.setupServer(this, server));
9698
setupCommands(commandSystem);
9799
modules.forEach(m -> m.setupCommands(this, commandSystem));
98-
commandSystem.addCommand("start", (args, params) -> {
99-
server.start();
100-
server.join();
101-
return CommandResult.success();
102-
});
100+
commandSystem.addCommand("start", new StartCommand(this));
101+
commandSystem.addCommand("sh", new ShellCommand(this));
103102
}
104103

105104
public WebApplication addModule(Module module){
@@ -153,4 +152,9 @@ public void run(String[] args){
153152
commandSystem.run(args);
154153
}
155154

155+
public void start(){
156+
server.start();
157+
server.join();
158+
}
159+
156160
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.javawebstack.framework.command;
2+
3+
import bsh.EvalError;
4+
import bsh.Interpreter;
5+
import org.javawebstack.command.Command;
6+
import org.javawebstack.command.CommandResult;
7+
import org.javawebstack.command.CommandSystem;
8+
import org.javawebstack.framework.WebApplication;
9+
import org.javawebstack.orm.ORM;
10+
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public class ShellCommand implements Command {
15+
private final WebApplication application;
16+
public ShellCommand(WebApplication application){
17+
this.application = application;
18+
}
19+
public CommandResult execute(CommandSystem system, List<String> list, Map<String, List<String>> map) {
20+
Interpreter interpreter = new Interpreter();
21+
try {
22+
interpreter.set("app", application);
23+
interpreter.getNameSpace().importClass("org.javawebstack.orm.Repo");
24+
ORM.getModels().forEach(m -> interpreter.getNameSpace().importClass(m.getName()));
25+
interpreter.run();
26+
} catch (EvalError error) {
27+
return CommandResult.error(error);
28+
}
29+
return CommandResult.success();
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javawebstack.framework.command;
2+
3+
import org.javawebstack.command.Command;
4+
import org.javawebstack.command.CommandResult;
5+
import org.javawebstack.command.CommandSystem;
6+
import org.javawebstack.framework.WebApplication;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class StartCommand implements Command {
12+
private final WebApplication application;
13+
public StartCommand(WebApplication application){
14+
this.application = application;
15+
}
16+
public CommandResult execute(CommandSystem commandSystem, List<String> list, Map<String, List<String>> map) {
17+
try {
18+
application.start();
19+
}catch (Throwable throwable){
20+
return CommandResult.error(throwable);
21+
}
22+
return CommandResult.success();
23+
}
24+
}

0 commit comments

Comments
 (0)