aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/JuickApplication.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/JuickApplication.java')
-rw-r--r--src/main/java/com/juick/JuickApplication.java91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/main/java/com/juick/JuickApplication.java b/src/main/java/com/juick/JuickApplication.java
new file mode 100644
index 00000000..c4a62c6f
--- /dev/null
+++ b/src/main/java/com/juick/JuickApplication.java
@@ -0,0 +1,91 @@
+package com.juick;
+
+import com.juick.xmpp.JID;
+import com.juick.xmpp.Stream;
+import com.juick.xmpp.StreamComponent;
+import com.juick.xmpp.s2s.S2SComponent;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.channels.Channels;
+import java.nio.channels.CompletionHandler;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Created by vt on 03/02/16.
+ */
+public class JuickApplication {
+
+ private static final Logger logger = Logger.getLogger(JuickApplication.class.getName());
+
+ private ExecutorService executorService;
+ private Stream router;
+ private JdbcTemplate sql;
+ private JdbcTemplate sqlSearch;
+ private List<JuickComponent> components = new ArrayList<>();
+
+ public JuickApplication(Properties conf) throws IOException {
+ executorService = Executors.newWorkStealingPool();
+ DriverManagerDataSource dataSource = new DriverManagerDataSource();
+ dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"));
+ dataSource.setUrl(conf.getProperty("datasource_url"));
+ sql = new JdbcTemplate(dataSource);
+ DriverManagerDataSource searchDatasource = new DriverManagerDataSource();
+ searchDatasource.setDriverClassName("com.mysql.jdbc.Driver");
+ searchDatasource.setUrl("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000");
+ sqlSearch = new JdbcTemplate(searchDatasource);
+ AsynchronousSocketChannel socket = AsynchronousSocketChannel.open();
+ socket.connect(new InetSocketAddress("localhost", 5347), socket,
+ new CompletionHandler<Void, AsynchronousSocketChannel>() {
+ @Override
+ public void completed(Void result, AsynchronousSocketChannel attachment) {
+ router = new StreamComponent(new JID("", "www.juick.com", ""), Channels.newInputStream(socket),
+ Channels.newOutputStream(socket), conf.getProperty("xmpp_password"));
+ router.startParsing();
+ }
+
+ @Override
+ public void failed(Throwable exc, AsynchronousSocketChannel attachment) {
+ logger.log(Level.SEVERE, "www router failed", exc);
+ }
+ });
+ addComponent(new S2SComponent(this, conf));
+ addComponent(new CrosspostComponent(sql, conf));
+ addComponent(new PushComponent(sql, conf));
+ }
+
+ public Stream getRouter() {
+ return router;
+ }
+
+ public JdbcTemplate getSql() {
+ return sql;
+ }
+
+ public void addComponent(JuickComponent component) {
+ components.add(component);
+ }
+
+ public void push(com.juick.xmpp.Message msg) {
+ for(JuickComponent c : components) {
+ c.messageReceived(msg);
+ }
+ }
+
+ public ExecutorService getExecutorService() {
+ return executorService;
+ }
+
+ public JdbcTemplate getSqlSearch() {
+ return sqlSearch;
+ }
+}