diff options
Diffstat (limited to 'juick-ws/src/main/java/com/juick')
5 files changed, 64 insertions, 77 deletions
diff --git a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java index 4a80eec5..430a7d59 100644 --- a/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java +++ b/juick-ws/src/main/java/com/juick/ws/XMPPConnection.java @@ -8,6 +8,7 @@ import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.beans.factory.InitializingBean; import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @@ -26,24 +27,20 @@ import java.util.stream.Collectors; * @author ugnich */ @Component -public class XMPPConnection implements Runnable, Stream.StreamListener, Message.MessageListener { +public class XMPPConnection implements InitializingBean, Stream.StreamListener, Message.MessageListener { private static final Logger logger = Logger.getLogger(XMPPConnection.class.getName()); @Inject JdbcTemplate sql; + @Inject + Environment env; Stream xmpp; String xmppPassword; MessageSerializer ms; WebsocketComponent wsHandler; - @Inject - public XMPPConnection(Environment env, WebsocketComponent wsHandler) { - this.wsHandler = wsHandler; + public void afterPropertiesSet() { xmppPassword = env.getProperty("xmpp_password"); ms = new MessageSerializer(); - } - - @Override - public void run() { try { Socket socket = new Socket("localhost", 5347); xmpp = new StreamComponent(new JID("", "ws.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), xmppPassword); diff --git a/juick-ws/src/main/java/com/juick/ws/components/CrosspostComponent.java b/juick-ws/src/main/java/com/juick/ws/components/CrosspostComponent.java index ec5716ee..1a6bd20e 100644 --- a/juick-ws/src/main/java/com/juick/ws/components/CrosspostComponent.java +++ b/juick-ws/src/main/java/com/juick/ws/components/CrosspostComponent.java @@ -25,32 +25,34 @@ import com.juick.xmpp.StreamComponent; import com.juick.xmpp.extensions.JuickMessage; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.stereotype.Component; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import javax.inject.Inject; import javax.net.ssl.HttpsURLConnection; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; import java.io.*; import java.net.Socket; import java.net.URL; import java.net.URLEncoder; import java.security.Key; -import java.util.Properties; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.logging.Level; -import java.util.logging.LogManager; import java.util.logging.Logger; /** * * @author Ugnich Anton */ -public class CrosspostComponent implements ServletContextListener, Stream.StreamListener, Message.MessageListener { +@Component +public class CrosspostComponent implements InitializingBean, DisposableBean, Stream.StreamListener, Message.MessageListener { private static Logger logger = Logger.getLogger(CrosspostComponent.class.getName()); @@ -63,23 +65,20 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea Stream xmpp; String twitter_consumer_key; String twitter_consumer_secret; + @Inject + Environment env; @Override - public void contextInitialized(final ServletContextEvent sce) { + public void afterPropertiesSet() throws Exception { logger.info("component initialized"); executorService = Executors.newSingleThreadExecutor(); - executorService.submit((Runnable) () -> { + executorService.submit(() -> { try { - Properties conf = new Properties(); - conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); + twitter_consumer_key = env.getProperty("twitter_consumer_key", ""); + twitter_consumer_secret = env.getProperty("twitter_consumer_secret", ""); - LogManager.getLogManager().readConfiguration( - sce.getServletContext().getResourceAsStream("/WEB-INF/logging.properties")); - twitter_consumer_key = conf.getProperty("twitter_consumer_key", ""); - twitter_consumer_secret = conf.getProperty("twitter_consumer_secret", ""); - - setupSql(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), conf.getProperty("datasource_url", "")); - setupXmppComponent(conf.getProperty("xmpp_password", "")); + setupSql(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), env.getProperty("datasource_url", "")); + setupXmppComponent(env.getProperty("xmpp_password", "")); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } @@ -87,7 +86,7 @@ public class CrosspostComponent implements ServletContextListener, Stream.Strea } @Override - public void contextDestroyed(ServletContextEvent sce) { + public void destroy() { executorService.shutdown(); logger.info("component destroyed"); } diff --git a/juick-ws/src/main/java/com/juick/ws/components/PushComponent.java b/juick-ws/src/main/java/com/juick/ws/components/PushComponent.java index eeb27184..bf56fb4b 100644 --- a/juick-ws/src/main/java/com/juick/ws/components/PushComponent.java +++ b/juick-ws/src/main/java/com/juick/ws/components/PushComponent.java @@ -45,21 +45,22 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.http.util.TextUtils; import org.json.JSONObject; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.stereotype.Component; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; +import javax.inject.Inject; import java.io.IOException; import java.net.Socket; import java.util.ArrayList; import java.util.Arrays; 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.LogManager; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -67,7 +68,8 @@ import java.util.stream.Collectors; * * @author Ugnich Anton */ -public class PushComponent implements ServletContextListener, Stream.StreamListener, MessageListener { +@Component +public class PushComponent implements InitializingBean, DisposableBean, Stream.StreamListener, MessageListener { private static Logger logger = Logger.getLogger(PushComponent.class.getName()); @@ -78,32 +80,26 @@ public class PushComponent implements ServletContextListener, Stream.StreamListe Socket socket; Stream xmpp; Sender GCMSender; + @Inject + Environment env; @Override - public void contextInitialized(final ServletContextEvent sce) { + public void afterPropertiesSet() { logger.info("component initialized"); executorService = Executors.newSingleThreadExecutor(); executorService.submit(() -> { - Properties conf = new Properties(); - try { - conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); - LogManager.getLogManager().readConfiguration( - sce.getServletContext().getResourceAsStream("/WEB-INF/logging.properties")); - wns_application_sip = conf.getProperty("wns_application_sip", ""); - wns_client_secret = conf.getProperty("wns_client_secret", ""); - GCMSender = new Sender(conf.getProperty("gcm_key")); + wns_application_sip = env.getProperty("wns_application_sip", ""); + wns_client_secret = env.getProperty("wns_client_secret", ""); + GCMSender = new Sender(env.getProperty("gcm_key")); - setupSql(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), conf.getProperty("datasource_url", "")); - setupXmppComponent(new JID("", conf.getProperty("push_jid"), ""), conf.getProperty("xmpp_host", "localhost"), - Integer.parseInt(conf.getProperty("xmpp_port", "5347")), conf.getProperty("push_xmpp_password", "")); - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } + setupSql(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), env.getProperty("datasource_url", "")); + setupXmppComponent(new JID("", env.getProperty("push_jid"), ""), env.getProperty("xmpp_host", "localhost"), + Integer.parseInt(env.getProperty("xmpp_port", "5347")), env.getProperty("push_xmpp_password", "")); }); } @Override - public void contextDestroyed(ServletContextEvent sce) { + public void destroy() { executorService.shutdown(); logger.info("component destroyed"); } diff --git a/juick-ws/src/main/java/com/juick/ws/components/XMPPComponent.java b/juick-ws/src/main/java/com/juick/ws/components/XMPPComponent.java index 544d9179..327b8e71 100644 --- a/juick-ws/src/main/java/com/juick/ws/components/XMPPComponent.java +++ b/juick-ws/src/main/java/com/juick/ws/components/XMPPComponent.java @@ -4,18 +4,21 @@ import com.juick.User; import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; import com.juick.server.UserQueries; +import com.juick.ws.s2s.*; import com.juick.xmpp.*; import com.juick.xmpp.extensions.JuickMessage; import com.juick.xmpp.extensions.Nickname; import com.juick.xmpp.extensions.XOOB; -import com.juick.ws.s2s.*; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.math.NumberUtils; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; import org.xmlpull.v1.XmlPullParserException; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; +import javax.inject.Inject; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; @@ -34,7 +37,8 @@ import java.util.logging.Logger; * * @author ugnich */ -public class XMPPComponent implements ServletContextListener, Stream.StreamListener, +@Component +public class XMPPComponent implements InitializingBean, DisposableBean, Stream.StreamListener, Message.MessageListener, Iq.IqListener, Presence.PresenceListener { private static final Logger logger = Logger.getLogger(XMPPComponent.class.getName()); @@ -54,6 +58,8 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe private final List<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<>()); private JdbcTemplate sql; final public HashMap<String, StanzaChild> childParsers = new HashMap<>(); + @Inject + Environment env; public void addConnectionIn(ConnectionIn c) { synchronized (getInConnections()) { @@ -172,25 +178,23 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe } @Override - public void contextInitialized(ServletContextEvent sce) { + public void afterPropertiesSet() { logger.info("component initialized"); - Properties conf = new Properties(); try { - conf.load(sce.getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); - HOSTNAME = conf.getProperty("hostname"); - componentName = conf.getProperty("componentname"); - int componentPort = NumberUtils.toInt(conf.getProperty("component_port"), 5347); - int s2sPort = NumberUtils.toInt(conf.getProperty("s2s_port"), 5269); - JID Jid = new JID(conf.getProperty("xmppbot_jid")); - STATSFILE = conf.getProperty("statsfile"); - keystore = conf.getProperty("keystore"); - keystorePassword = conf.getProperty("keystore_password"); - brokenSSLhosts = Arrays.asList(conf.getProperty("broken_ssl_hosts", "").split(",")); - bannedHosts = Arrays.asList(conf.getProperty("banned_hosts", "").split(",")); + HOSTNAME = env.getProperty("hostname"); + componentName = env.getProperty("componentname"); + int componentPort = NumberUtils.toInt(env.getProperty("component_port"), 5347); + int s2sPort = NumberUtils.toInt(env.getProperty("s2s_port"), 5269); + JID Jid = new JID(env.getProperty("xmppbot_jid")); + STATSFILE = env.getProperty("statsfile"); + keystore = env.getProperty("keystore"); + keystorePassword = env.getProperty("keystore_password"); + brokenSSLhosts = Arrays.asList(env.getProperty("broken_ssl_hosts", "").split(",")); + bannedHosts = Arrays.asList(env.getProperty("banned_hosts", "").split(",")); BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(conf.getProperty("datasource_url")); + dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(env.getProperty("datasource_url")); setSql(new JdbcTemplate(dataSource)); bot = new JuickBot(this, Jid); @@ -200,7 +204,7 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe Socket routerSocket = null; try { routerSocket = new Socket("localhost", componentPort); - setRouter(new StreamComponent(new JID("s2s"), routerSocket.getInputStream(), routerSocket.getOutputStream(), conf.getProperty("xmpp_password"))); + setRouter(new StreamComponent(new JID("s2s"), routerSocket.getInputStream(), routerSocket.getOutputStream(), env.getProperty("xmpp_password"))); getRouter().addChildParser(new JuickMessage()); getRouter().addListener((Stream.StreamListener) this); getRouter().addListener((Message.MessageListener) this); @@ -234,7 +238,7 @@ public class XMPPComponent implements ServletContextListener, Stream.StreamListe @Override - public void contextDestroyed(ServletContextEvent sce) { + public void destroy() { synchronized (getOutConnections()) { for (Iterator<ConnectionOut> i = getOutConnections().iterator(); i.hasNext();) { ConnectionOut c = i.next(); diff --git a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java index ecd44908..92b61bc7 100644 --- a/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java +++ b/juick-ws/src/main/java/com/juick/ws/configuration/WebsocketConfiguration.java @@ -3,7 +3,6 @@ package com.juick.ws.configuration; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.juick.ws.WebsocketComponent; -import com.juick.ws.XMPPConnection; import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.loader.Loader; import com.mitchellbosecke.pebble.loader.ServletLoader; @@ -31,8 +30,6 @@ import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry import javax.inject.Inject; import javax.servlet.ServletContext; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; /** * Created by vitalyster on 28.06.2016. @@ -44,17 +41,11 @@ import java.util.concurrent.Executors; public class WebsocketConfiguration extends WebMvcConfigurationSupport implements WebSocketConfigurer { @Inject Environment env; - ExecutorService xmppThread = Executors.newSingleThreadExecutor(); @Bean WebsocketComponent wsHandler() { return new WebsocketComponent(); } - @Bean - XMPPConnection xmpp() { - XMPPConnection xmpp = new XMPPConnection(env, wsHandler()); - xmppThread.submit(xmpp); - return xmpp; - } + @Bean JdbcTemplate jdbc() { BasicDataSource dataSource = new BasicDataSource(); |