From 36c679330d1bc0c6f0dfd7ec3dd1fb631259a8d9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 12 Oct 2016 15:15:57 +0300 Subject: juick-demo: add console --- juick-demo/build.gradle | 3 ++ juick-demo/src/main/java/com/juick/demo/Demo.java | 41 +++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) (limited to 'juick-demo') diff --git a/juick-demo/build.gradle b/juick-demo/build.gradle index 8dfb596b..90206124 100644 --- a/juick-demo/build.gradle +++ b/juick-demo/build.gradle @@ -10,6 +10,7 @@ apply plugin: 'com.github.ben-manes.versions' repositories { mavenCentral() + jcenter() } mainClassName = "com.juick.demo.Demo" @@ -19,5 +20,7 @@ dependencies { compile 'org.apache.commons:commons-lang3:3.4' compile 'org.springframework:spring-jdbc:4.3.2.RELEASE' compile 'mysql:mysql-connector-java:5.1.39' + compile 'ch.vorburger.mariaDB4j:mariaDB4j:2.1.3' + compile 'jline:jline:2.14.2' } compileJava.options.encoding = 'UTF-8' \ No newline at end of file diff --git a/juick-demo/src/main/java/com/juick/demo/Demo.java b/juick-demo/src/main/java/com/juick/demo/Demo.java index 6940bfd0..c3d45b44 100644 --- a/juick-demo/src/main/java/com/juick/demo/Demo.java +++ b/juick-demo/src/main/java/com/juick/demo/Demo.java @@ -1,13 +1,23 @@ package com.juick.demo; +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; import com.juick.Tag; +import com.juick.User; import com.juick.server.MessagesQueries; import com.juick.server.PMQueries; import com.juick.server.TagQueries; import com.juick.server.UserQueries; +import com.juick.server.protocol.JuickProtocol; +import com.juick.server.protocol.ProtocolReply; +import jline.UnsupportedTerminal; +import jline.console.ConsoleReader; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; +import java.io.IOException; +import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.logging.Logger; @@ -16,8 +26,14 @@ import java.util.logging.Logger; */ public class Demo { private static final Logger logger = Logger.getLogger(Demo.class.getName()); - public static void main(String ...args) { - DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:mysql://localhost/juick?user=root"); + public static void main(String ...args) throws ManagedProcessException { + DB db = DB.newEmbeddedDB(33306); + db.start(); + db.createDB("juick"); + db.source("schema.sql"); + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("com.mysql.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root"); JdbcTemplate jdbc = new JdbcTemplate(dataSource); logger.info("initializing demo database"); @@ -35,5 +51,26 @@ public class Demo { MessagesQueries.setMessagePopular(jdbc, ugn_mid, 1); PMQueries.createPM(jdbc, freefd_id, ugnich_id, "Hi ugnich!"); jdbc.execute("UPDATE tags SET top=1"); + jline.TerminalFactory.registerFlavor(jline.TerminalFactory.Flavor.WINDOWS, UnsupportedTerminal.class); + try (ConsoleReader reader = new ConsoleReader()) { + reader.setPrompt("ugnich>"); + PrintWriter out = new PrintWriter(reader.getOutput()); + User ugnich = UserQueries.getUserByUID(jdbc, ugnich_id).get(); + out.println(String.format("logged in as @%s", ugnich.getUName())); + out.flush(); + String input; + JuickProtocol protocol = new JuickProtocol(jdbc, "http://localhost/"); + while ((input = reader.readLine()) != null) { + ProtocolReply reply = protocol.getReply(ugnich, input); + out.println(reply.getDescription()); + out.flush(); + } + } catch (IOException | NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } } } -- cgit v1.2.3