diff options
author | Vitaly Takmazov | 2016-12-23 11:49:39 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-12-23 11:59:43 +0300 |
commit | 1bc50475e5a226ca3467882dc50a8264cd376159 (patch) | |
tree | dbc8a83462d83b6735ee15d9b915082b84815685 /juick-console | |
parent | 97f993fad0dea34b25cb5da932920971627fd972 (diff) |
juick-demo -> juick-console
Diffstat (limited to 'juick-console')
4 files changed, 145 insertions, 0 deletions
diff --git a/juick-console/build.gradle b/juick-console/build.gradle new file mode 100644 index 00000000..064493ce --- /dev/null +++ b/juick-console/build.gradle @@ -0,0 +1,28 @@ +buildscript { + repositories { + mavenCentral() + } +} + +apply plugin: 'java' +apply plugin: 'application' +apply plugin: 'com.github.ben-manes.versions' + +repositories { + jcenter() +} + +mainClassName = "com.juick.console.Console" + +dependencies { + compile project(':juick-server') + compile 'mysql:mysql-connector-java:5.1.40' + compile 'ch.vorburger.mariaDB4j:mariaDB4j:2.2.2' + compile 'jline:jline:2.14.2' +} + +compileJava.options.encoding = 'UTF-8' + +configurations { + all*.exclude module: 'commons-logging' +}
\ No newline at end of file diff --git a/juick-console/src/main/java/com/juick/console/Console.java b/juick-console/src/main/java/com/juick/console/Console.java new file mode 100644 index 00000000..9fff298f --- /dev/null +++ b/juick-console/src/main/java/com/juick/console/Console.java @@ -0,0 +1,27 @@ +package com.juick.console; + +import ch.vorburger.exec.ManagedProcessException; +import com.juick.console.configuration.ConsoleAppConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.core.env.SimpleCommandLinePropertySource; + +/** + * Created by vitalyster on 30.08.2016. + */ +public class Console { + + public static void main(String ...args) throws ManagedProcessException { + final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + + // setup configuration + applicationContext.register(ConsoleAppConfiguration.class); + // add CLI property source + applicationContext.getEnvironment().getPropertySources() + .addLast(new SimpleCommandLinePropertySource(args)); + + // setup all the dependencies (refresh) and make them run (start) + applicationContext.refresh(); + applicationContext.start(); + applicationContext.getBean(ConsoleApp.class).start(); + } +} 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(); + } + } +} diff --git a/juick-console/src/main/java/com/juick/console/configuration/ConsoleAppConfiguration.java b/juick-console/src/main/java/com/juick/console/configuration/ConsoleAppConfiguration.java new file mode 100644 index 00000000..11e2609e --- /dev/null +++ b/juick-console/src/main/java/com/juick/console/configuration/ConsoleAppConfiguration.java @@ -0,0 +1,34 @@ +package com.juick.console.configuration; + +import com.juick.configuration.DataConfiguration; +import com.juick.console.ConsoleApp; +import com.juick.server.protocol.JuickProtocol; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +/** + * Created by vitalyster on 19.12.2016. + */ +@Configuration +@ComponentScan(basePackages = {"com.juick.console", "com.juick.service", "com.juick.server.protocol"}) +@Import(DataConfiguration.class) +public class ConsoleAppConfiguration implements ApplicationContextAware { + private ApplicationContext applicationContext; + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + @Bean + public JuickProtocol juickProtocol() { + return new JuickProtocol("https://juick.com/"); + } + @Bean + public ConsoleApp app() { + return new ConsoleApp(applicationContext); + } +} |