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(); } } }