diff options
author | Vitaly Takmazov | 2016-01-26 20:22:59 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-01-26 20:22:59 +0300 |
commit | d5b637c358a05965435716ba366f141eb1f61142 (patch) | |
tree | 8ebeea4b7bfda3a70592a41c149615486cc46ea5 /src/main/java/com/juick/xmpp/s2s | |
parent | 75d80dc433348435c1f8e11fafb0d1deda1ca793 (diff) |
register listener
Diffstat (limited to 'src/main/java/com/juick/xmpp/s2s')
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 110 |
1 files changed, 65 insertions, 45 deletions
diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index d2504c25..ff4ec3e6 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -8,13 +8,14 @@ import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.io.FileInputStream; import java.io.IOException; +import java.sql.Driver; import java.sql.DriverManager; -import java.util.Collections; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; +import java.sql.SQLException; +import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -22,21 +23,21 @@ import java.util.Properties; */ public class XMPPComponent implements ServletContextListener { + private static final Logger LOGGER = Logger.getLogger(XMPPComponent.class.getName()); + + ExecutorService executorService; + public static String HOSTNAME = null; public static String COMPONENTNAME = null; public static String LOGFILE = null; public static String STATSFILE = null; public static ConnectionRouter connRouter; - static final List<ConnectionIn> inConnections = Collections.synchronizedList(new ArrayList<ConnectionIn>()); - static final List<ConnectionOut> outConnections = Collections.synchronizedList(new ArrayList<ConnectionOut>()); - static final List<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<CacheEntry>()); + static final List<ConnectionIn> inConnections = Collections.synchronizedList(new ArrayList<>()); + static final List<ConnectionOut> outConnections = Collections.synchronizedList(new ArrayList<>()); + static final List<CacheEntry> outCache = Collections.synchronizedList(new ArrayList<>()); static final Integer sqlSync = 0; static java.sql.Connection sql; - final public static HashMap<String, StanzaChild> childParsers = new HashMap<String, StanzaChild>(); - - public static void main(String[] args) { - new XMPPComponent().start(); - } + final public static HashMap<String, StanzaChild> childParsers = new HashMap<>(); public static void addConnectionIn(ConnectionIn c) { synchronized (inConnections) { @@ -79,8 +80,7 @@ public class XMPPComponent implements ServletContextListener { public static ConnectionOut getConnectionOut(String hostname, boolean needReady) { synchronized (outConnections) { - for (Iterator<ConnectionOut> i = outConnections.iterator(); i.hasNext();) { - ConnectionOut c = i.next(); + for (ConnectionOut c : outConnections) { if (c.to != null && c.to.equals(hostname) && (!needReady || c.streamReady)) { return c; } @@ -91,8 +91,7 @@ public class XMPPComponent implements ServletContextListener { public static ConnectionIn getConnectionIn(String streamID) { synchronized (inConnections) { - for (Iterator<ConnectionIn> i = inConnections.iterator(); i.hasNext();) { - ConnectionIn c = i.next(); + for (ConnectionIn c : inConnections) { if (c.streamID != null && c.streamID.equals(streamID)) { return c; } @@ -110,8 +109,7 @@ public class XMPPComponent implements ServletContextListener { ConnectionOut connOut = null; synchronized (outConnections) { - for (Iterator<ConnectionOut> i = outConnections.iterator(); i.hasNext();) { - ConnectionOut c = i.next(); + for (ConnectionOut c : outConnections) { if (c.to != null && c.to.equals(hostname)) { if (c.streamReady) { connOut = c; @@ -134,8 +132,7 @@ public class XMPPComponent implements ServletContextListener { boolean haveCache = false; synchronized (outCache) { - for (Iterator<CacheEntry> i = outCache.iterator(); i.hasNext();) { - CacheEntry c = i.next(); + for (CacheEntry c : outCache) { if (c.hostname != null && c.hostname.equals(hostname)) { c.xml += xml; c.tsUpdated = System.currentTimeMillis(); @@ -153,39 +150,62 @@ public class XMPPComponent implements ServletContextListener { } } - public void start() { - try { - Properties conf = new Properties(); - conf.load(new FileInputStream("/etc/juick/s2s.conf")); - HOSTNAME = conf.getProperty("hostname"); - COMPONENTNAME = conf.getProperty("componentname"); - LOGFILE = conf.getProperty("logfile"); - STATSFILE = conf.getProperty("statsfile"); + @Override + public void contextInitialized(ServletContextEvent sce) { - Class.forName("com.mysql.jdbc.Driver"); - sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); + LOGGER.info("component initialized"); + executorService = Executors.newSingleThreadExecutor(); + executorService.submit(() -> { + Properties conf = new Properties(); + try { + conf.load(new FileInputStream("/etc/juick/s2s.conf")); + HOSTNAME = conf.getProperty("hostname"); + COMPONENTNAME = conf.getProperty("componentname"); + LOGFILE = conf.getProperty("logfile"); + STATSFILE = conf.getProperty("statsfile"); - Runtime.getRuntime().addShutdownHook(new Shutdown()); + Class.forName("com.mysql.jdbc.Driver"); + sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); - childParsers.put(JuickMessage.XMLNS, new JuickMessage()); + Runtime.getRuntime().addShutdownHook(new Shutdown()); - connRouter = new ConnectionRouter(); - new Thread(connRouter).start(); - new Thread(new ConnectionListener()).start(); - new Thread(new CleaningUp()).start(); + childParsers.put(JuickMessage.XMLNS, new JuickMessage()); - } catch (Exception e) { - System.err.println(e); - } + connRouter = new ConnectionRouter(); + new Thread(connRouter).start(); + new Thread(new ConnectionListener()).start(); + new Thread(new CleaningUp()).start(); + } catch (IOException | ClassNotFoundException | SQLException e) { + LOGGER.log(Level.SEVERE, "XMPPComponent error", e); + } + }); } - @Override - public void contextInitialized(ServletContextEvent sce) { - } @Override public void contextDestroyed(ServletContextEvent sce) { - + // Now deregister JDBC drivers in this context's ClassLoader: + // Get the webapp's ClassLoader + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + // Loop through all drivers + Enumeration<Driver> drivers = DriverManager.getDrivers(); + while (drivers.hasMoreElements()) { + Driver driver = drivers.nextElement(); + if (driver.getClass().getClassLoader() == cl) { + // This driver was registered by the webapp's ClassLoader, so deregister it: + try { + LOGGER.info(String.format("Deregistering JDBC driver %s", driver.toString())); + DriverManager.deregisterDriver(driver); + } catch (SQLException ex) { + LOGGER.log(Level.SEVERE, String.format("Error deregistering JDBC driver %s", driver), ex); + } + } else { + // driver was not registered by the webapp's ClassLoader and may be in use elsewhere + LOGGER.log(Level.SEVERE, String.format("Not deregistering JDBC driver %s as it does not belong to this webapp's ClassLoader", driver)); + } + } + executorService.shutdown(); + LOGGER.info("component destroyed"); } } |