From 589901b03688e073eb85b5b51a58cec3c8d5d5e9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 7 Aug 2016 23:46:00 +0300 Subject: juick-ws: do not use servlet api directly, refactor components as InitializingBean --- .../com/juick/ws/components/PushComponent.java | 38 ++++++++++------------ 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'juick-ws/src/main/java/com/juick/ws/components/PushComponent.java') 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"); } -- cgit v1.2.3