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.rythmengine.Rythm; 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.*; 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 components = new ArrayList<>(); private String applicationPath; public JuickApplication(Properties conf, String applicationPath) throws IOException { this.applicationPath = applicationPath; executorService = Executors.newWorkStealingPool(); initDataSources(conf); initSockets(conf); initComponents(conf); initRythm(conf); } private void initRythm(Properties conf) { String rythmMode = conf.getProperty("rythm_mode", "prod"); Map map = new HashMap<>(); map.put("home.template", applicationPath+"/WEB-INF/classes/templates"); map.put("rythm.engine.mode", rythmMode); Rythm.init(map); } private void initComponents(Properties conf) { if (!isHttpDevMode()) { addComponent(new S2SComponent(this, conf)); addComponent(new CrosspostComponent(sql, conf)); addComponent(new PushComponent(sql, conf)); } } private void initSockets(final Properties conf) throws IOException { AsynchronousSocketChannel socket = AsynchronousSocketChannel.open(); socket.connect(new InetSocketAddress("localhost", 5347), socket, new CompletionHandler() { @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) { if (!isHttpDevMode()) { logger.log(Level.SEVERE, "www router failed", exc); } } }); } private void initDataSources(Properties conf) { 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); } public static boolean isHttpDevMode() { return "san".equals(System.getenv("USER")); } 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; } }