diff options
author | 2016-12-23 11:49:39 +0300 | |
---|---|---|
committer | 2016-12-23 11:59:43 +0300 | |
commit | 1bc50475e5a226ca3467882dc50a8264cd376159 (patch) | |
tree | dbc8a83462d83b6735ee15d9b915082b84815685 /juick-console/src/main/java/com/juick/console/ConsoleApp.java | |
parent | 97f993fad0dea34b25cb5da932920971627fd972 (diff) |
juick-demo -> juick-console
Diffstat (limited to 'juick-console/src/main/java/com/juick/console/ConsoleApp.java')
-rw-r--r-- | juick-console/src/main/java/com/juick/console/ConsoleApp.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/juick-console/src/main/java/com/juick/console/ConsoleApp.java b/juick-console/src/main/java/com/juick/console/ConsoleApp.java new file mode 100644 index 00000000..9a061bb6 --- /dev/null +++ b/juick-console/src/main/java/com/juick/console/ConsoleApp.java @@ -0,0 +1,56 @@ +package com.juick.console; + +import com.juick.User; +import com.juick.server.protocol.JuickProtocol; +import com.juick.server.protocol.ProtocolReply; +import com.juick.service.UserService; +import jline.console.ConsoleReader; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; + +import javax.inject.Inject; +import java.io.IOException; +import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; + +/** + * Created by vitalyster on 19.12.2016. + */ +public class ConsoleApp { + @Inject + JuickProtocol juickProtocol; + @Inject + Environment env; + @Inject + UserService userService; + + public ConsoleApp(ApplicationContext context) { + juickProtocol = context.getBean(JuickProtocol.class); + } + public void start() { + //jline.TerminalFactory.registerFlavor(jline.TerminalFactory.Flavor.WINDOWS, UnsupportedTerminal.class); + try (ConsoleReader reader = new ConsoleReader()) { + PrintWriter out = new PrintWriter(reader.getOutput()); + String uname = env.getProperty("uname", "ugnich"); + User user = userService.getUserByName(uname); + if (user != null) { + reader.setPrompt(String.format("%s>", user.getName())); + out.println(String.format("logged in as @%s", user.getName())); + out.flush(); + String input; + while ((input = reader.readLine()) != null) { + ProtocolReply reply = juickProtocol.getReply(user, input); + out.println(reply.getResult()); + out.flush(); + } + } + out.println("uname missing or invalid"); + } catch (IOException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } +} |