diff options
-rw-r--r-- | juick-console/build.gradle (renamed from juick-demo/build.gradle) | 2 | ||||
-rw-r--r-- | juick-console/src/main/java/com/juick/console/Console.java (renamed from juick-demo/src/main/java/com/juick/demo/Demo.java) | 11 | ||||
-rw-r--r-- | juick-console/src/main/java/com/juick/console/ConsoleApp.java | 56 | ||||
-rw-r--r-- | juick-console/src/main/java/com/juick/console/configuration/ConsoleAppConfiguration.java | 34 | ||||
-rw-r--r-- | juick-demo/src/main/java/com/juick/demo/DemoApp.java | 76 | ||||
-rw-r--r-- | juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java | 90 | ||||
-rw-r--r-- | settings.gradle | 2 |
7 files changed, 97 insertions, 174 deletions
diff --git a/juick-demo/build.gradle b/juick-console/build.gradle index 25806e3a..064493ce 100644 --- a/juick-demo/build.gradle +++ b/juick-console/build.gradle @@ -12,7 +12,7 @@ repositories { jcenter() } -mainClassName = "com.juick.demo.Demo" +mainClassName = "com.juick.console.Console" dependencies { compile project(':juick-server') diff --git a/juick-demo/src/main/java/com/juick/demo/Demo.java b/juick-console/src/main/java/com/juick/console/Console.java index db03e2e5..9fff298f 100644 --- a/juick-demo/src/main/java/com/juick/demo/Demo.java +++ b/juick-console/src/main/java/com/juick/console/Console.java @@ -1,20 +1,20 @@ -package com.juick.demo; +package com.juick.console; import ch.vorburger.exec.ManagedProcessException; -import com.juick.demo.configuration.DemoAppConfiguration; +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 Demo { +public class Console { public static void main(String ...args) throws ManagedProcessException { final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // setup configuration - applicationContext.register(DemoAppConfiguration.class); + applicationContext.register(ConsoleAppConfiguration.class); // add CLI property source applicationContext.getEnvironment().getPropertySources() .addLast(new SimpleCommandLinePropertySource(args)); @@ -22,7 +22,6 @@ public class Demo { // setup all the dependencies (refresh) and make them run (start) applicationContext.refresh(); applicationContext.start(); - DemoApp demoApp = new DemoApp(applicationContext); - demoApp.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); + } +} diff --git a/juick-demo/src/main/java/com/juick/demo/DemoApp.java b/juick-demo/src/main/java/com/juick/demo/DemoApp.java deleted file mode 100644 index ccc86a40..00000000 --- a/juick-demo/src/main/java/com/juick/demo/DemoApp.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.juick.demo; - -import com.juick.Tag; -import com.juick.User; -import com.juick.server.protocol.JuickProtocol; -import com.juick.server.protocol.ProtocolReply; -import com.juick.service.MessagesService; -import com.juick.service.PMQueriesService; -import com.juick.service.TagService; -import com.juick.service.UserService; -import jline.UnsupportedTerminal; -import jline.console.ConsoleReader; -import org.springframework.context.ApplicationContext; -import org.springframework.jdbc.core.JdbcTemplate; - -import java.io.IOException; -import java.io.PrintWriter; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; - -/** - * Created by vitalyster on 19.12.2016. - */ -public class DemoApp { - UserService userService; - MessagesService messagesService; - PMQueriesService pmQueriesService; - TagService tagService; - JdbcTemplate jdbcTemplate; - JuickProtocol juickProtocol; - - public DemoApp(ApplicationContext context) { - userService = context.getBean(UserService.class); - messagesService = context.getBean(MessagesService.class); - pmQueriesService = context.getBean(PMQueriesService.class); - tagService = context.getBean(TagService.class); - jdbcTemplate = context.getBean(JdbcTemplate.class); - juickProtocol = context.getBean(JuickProtocol.class); - } - public void start() { - Integer ugnich_id = userService.createUser("ugnich", "secret"); - Integer ugn_mid = messagesService.createMessage(ugnich_id, "Hi, I am ugnich", null, - new ArrayList<Tag>() {{add(tagService.getTag("yo", true)); add(tagService.getTag("people", true));}}); - - Integer juick_id = userService.createUser("juick", "secret"); - messagesService.createMessage(juick_id, "New demo juick was created", null, - new ArrayList<Tag>() {{add(tagService.getTag("yo", true)); add(tagService.getTag("readonly", true));}}); - Integer freefd_id = userService.createUser("freefd", "secret"); - messagesService.createReply(ugn_mid, 0, freefd_id, "I am freefd and this is my reply", null); - messagesService.recommendMessage(ugn_mid, freefd_id); - messagesService.recommendMessage(ugn_mid, juick_id); - messagesService.setMessagePopular(ugn_mid, 1); - pmQueriesService.createPM(freefd_id, ugnich_id, "Hi ugnich!"); - jdbcTemplate.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 = userService.getUserByUID(ugnich_id).get(); - out.println(String.format("logged in as @%s", ugnich.getName())); - out.flush(); - String input; - while ((input = reader.readLine()) != null) { - ProtocolReply reply = juickProtocol.getReply(ugnich, input); - out.println(reply.getResult()); - out.flush(); - } - } catch (IOException | NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } -} diff --git a/juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java b/juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java deleted file mode 100644 index 6a7f697a..00000000 --- a/juick-demo/src/main/java/com/juick/demo/configuration/DemoAppConfiguration.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.juick.demo.configuration; - -import ch.vorburger.exec.ManagedProcessException; -import ch.vorburger.mariadb4j.DB; -import com.juick.server.protocol.JuickProtocol; -import com.juick.service.search.SearchService; -import org.apache.commons.dbcp2.BasicDataSource; -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.DependsOn; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.TransactionManagementConfigurer; - -import java.util.Collections; -import java.util.List; - -/** - * Created by vitalyster on 19.12.2016. - */ -@Configuration -@ComponentScan(basePackages = {"com.juick.demo", "com.juick.service", "com.juick.server.protocol"}) -public class DemoAppConfiguration implements ApplicationContextAware, TransactionManagementConfigurer { - private ApplicationContext applicationContext; - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } - @Bean - public BasicDataSource dataSource() { - try { - DB db = DB.newEmbeddedDB(33306); - db.start(); - db.createDB("juick"); - db.source("schema.sql"); - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName("com.mysql.jdbc.Driver"); - dataSource.setUrl("jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root"); - - dataSource.setValidationQuery("select 1"); - - return dataSource; - } catch (ManagedProcessException e) { - return null; - } - } - - @Bean - public PlatformTransactionManager transactionManager() { - return new DataSourceTransactionManager(dataSource()); - } - - @Override - public PlatformTransactionManager annotationDrivenTransactionManager() { - return transactionManager(); - } - - @Bean - @DependsOn("dataSource") - public JdbcTemplate jdbcTemplate() { - return new JdbcTemplate(dataSource()); - } - @Bean - public SearchService emptySearchService() { - return new SearchService() { - @Override - public void setMaxResult(int maxResult) { - } - - @Override - public List<Integer> searchInAllMessages(String searchString, int messageIdBefore) { - return Collections.emptyList(); - } - - @Override - public List<Integer> searchByStringAndUser(String searchString, int userId, int messageIdBefore) { - return Collections.emptyList(); - } - }; - } - @Bean - public JuickProtocol juickProtocol() { - return new JuickProtocol("http://localhost:8080/"); - } -} diff --git a/settings.gradle b/settings.gradle index efc9a711..5b96cb5d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ rootProject.name = "Juick" -include ':juick-core', 'juick-server', ':juick-api', ':juick-www', ':juick-rss', ':juick-ws', ':juick-demo', ':juick-notifications', ':juick-crosspost', ':juick-xmpp', ':juick-xmpp-ft', ':juick-xmpp-bot', ':juick-spring-www' +include ':juick-core', 'juick-server', ':juick-api', ':juick-www', ':juick-rss', ':juick-ws', ':juick-console', ':juick-notifications', ':juick-crosspost', ':juick-xmpp', ':juick-xmpp-ft', ':juick-xmpp-bot', ':juick-spring-www' |