aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/PushComponent.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/PushComponent.java')
-rw-r--r--src/main/java/com/juick/PushComponent.java69
1 files changed, 7 insertions, 62 deletions
diff --git a/src/main/java/com/juick/PushComponent.java b/src/main/java/com/juick/PushComponent.java
index eb2230cb..3a96ff16 100644
--- a/src/main/java/com/juick/PushComponent.java
+++ b/src/main/java/com/juick/PushComponent.java
@@ -24,10 +24,6 @@ import com.google.android.gcm.server.Sender;
import com.juick.json.MessageSerializer;
import com.juick.server.PushQueries;
import com.juick.server.SubscriptionsQueries;
-import com.juick.xmpp.JID;
-import com.juick.xmpp.Message.MessageListener;
-import com.juick.xmpp.Stream;
-import com.juick.xmpp.StreamComponent;
import com.juick.xmpp.extensions.JuickMessage;
import com.juick.xmpp.utils.XmlUtils;
import com.notnoop.apns.APNS;
@@ -47,18 +43,12 @@ import org.apache.http.util.TextUtils;
import org.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
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;
@@ -66,69 +56,24 @@ import java.util.stream.Collectors;
*
* @author Ugnich Anton
*/
-public class PushComponent implements ServletContextListener, Stream.StreamListener, MessageListener {
+public class PushComponent implements JuickComponent {
private static Logger logger = Logger.getLogger(PushComponent.class.getName());
- private ExecutorService executorService;
String wns_application_sip;
String wns_client_secret;
JdbcTemplate sql;
- Socket socket;
- Stream xmpp;
Sender GCMSender;
- @Override
- public void contextInitialized(final ServletContextEvent sce) {
+ public PushComponent(JdbcTemplate sql, Properties conf) {
logger.info("component initialized");
- executorService = Executors.newSingleThreadExecutor();
- executorService.submit((Runnable) () -> {
- 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"));
- sql = (JdbcTemplate) sce.getServletContext().getAttribute("sql");
- 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);
- }
- });
+ wns_application_sip = conf.getProperty("wns_application_sip", "");
+ wns_client_secret = conf.getProperty("wns_client_secret", "");
+ GCMSender = new Sender(conf.getProperty("gcm_key"));
+ this.sql = sql;
}
-
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- executorService.shutdown();
- logger.info("component destroyed");
- }
-
- public void setupXmppComponent(JID jid, String host, int port, String password) {
- try {
- socket = new Socket(host, port);
- xmpp = new StreamComponent(jid, socket.getInputStream(), socket.getOutputStream(), password);
- xmpp.addChildParser(new JuickMessage());
- xmpp.addListener((Stream.StreamListener) this);
- xmpp.addListener((MessageListener) this);
- xmpp.startParsing();
- } catch (IOException e) {
- logger.log(Level.SEVERE, e.getMessage(), e);
- }
- }
-
- @Override
- public void onStreamReady() {
- logger.info("XMPP STREAM READY");
- }
-
- @Override
- public void onStreamFail(Exception e) {logger.log(Level.SEVERE, "XMPP STREAM FAIL", e);}
-
@Override
- public void onMessage(com.juick.xmpp.Message msg) {
+ public void messageReceived(com.juick.xmpp.Message msg) {
JuickMessage jmsg = (JuickMessage) msg.getChild(JuickMessage.XMLNS);
if (jmsg == null) {
return;