From bd3892546b51f0d31e959d45c138b009d35b2692 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 2 Feb 2016 14:36:04 +0300 Subject: spring-jdbc --- .../com/juick/xmpp/extensions/JuickMessage.java | 21 +++-- .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 62 +++++++------- src/main/java/com/juick/xmpp/s2s/JuickBot.java | 97 +++++++++------------- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 43 +--------- 4 files changed, 91 insertions(+), 132 deletions(-) (limited to 'src/main/java/com/juick/xmpp') diff --git a/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/src/main/java/com/juick/xmpp/extensions/JuickMessage.java index 53dd6deb..bbc1568e 100644 --- a/src/main/java/com/juick/xmpp/extensions/JuickMessage.java +++ b/src/main/java/com/juick/xmpp/extensions/JuickMessage.java @@ -20,6 +20,10 @@ package com.juick.xmpp.extensions; import com.juick.xmpp.utils.XmlUtils; import com.juick.xmpp.*; import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.TimeZone; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -32,7 +36,11 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { public final static String XMLNS = "http://juick.com/message"; public final static String TagName = "juick"; + private SimpleDateFormat df; + public JuickMessage() { + df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); } public JuickMessage(com.juick.Message msg) { @@ -45,7 +53,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { } @Override - public JuickMessage parse(XmlPullParser parser) throws XmlPullParserException, IOException { + public JuickMessage parse(XmlPullParser parser) throws XmlPullParserException, IOException, ParseException { JuickMessage jmsg = new JuickMessage(); final String sMID = parser.getAttributeValue(null, "mid"); @@ -72,7 +80,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (sReadOnly != null) { jmsg.ReadOnly = true; } - jmsg.TimestampString = parser.getAttributeValue(null, "ts"); + + jmsg.setDate(df.parse(parser.getAttributeValue(null, "ts"))); jmsg.AttachmentType = parser.getAttributeValue(null, "attach"); while (parser.next() == XmlPullParser.START_TAG) { @@ -112,8 +121,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (ReadOnly) { ret += " readonly=\"1\""; } - if (TimestampString != null) { - ret += " ts=\"" + TimestampString + "\""; + if (getDate() != null) { + ret += " ts=\"" + df.format(getDate()) + "\""; } if (AttachmentType != null) { ret += " attach=\"" + AttachmentType + "\""; @@ -126,8 +135,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { ret += "" + XmlUtils.escape(getText()) + ""; } if (!Tags.isEmpty()) { - for (int i = 0; i < Tags.size(); i++) { - ret += "" + XmlUtils.escape(Tags.get(i)) + ""; + for (String Tag : Tags) { + ret += "" + XmlUtils.escape(Tag) + ""; } } ret += ""; diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 4634ca99..c96a5cce 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -1,7 +1,9 @@ package com.juick.xmpp.s2s; +import com.juick.User; import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; +import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.extensions.JuickMessage; @@ -9,6 +11,7 @@ import com.juick.xmpp.extensions.Nickname; import com.juick.xmpp.extensions.XOOB; import com.juick.xmpp.utils.SHA1; import com.juick.xmpp.utils.XmlUtils; +import org.springframework.jdbc.core.JdbcTemplate; import org.xmlpull.v1.XmlPullParser; import java.io.IOException; @@ -143,16 +146,16 @@ public class ConnectionRouter extends Connection implements Runnable { } public void sendJuickMessage(JuickMessage jmsg) { - List jids; + List users; - synchronized (XMPPComponent.sqlSync) { - if (jmsg.FriendsOnly) { - jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); - } else { - jids = SubscriptionsQueries.getJIDSubscribedToUserAndTags(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); - } + + if (jmsg.FriendsOnly) { + users = SubscriptionsQueries.getUsersSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + } else { + users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); } + String txt = "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; String attachment = jmsg.getAttachmentURL(); if (attachment != null) { @@ -176,21 +179,19 @@ public class ConnectionRouter extends Connection implements Runnable { oob.URL = attachment; msg.addChild(oob); } - - for (int i = 0; i < jids.size(); i++) { - msg.to = new JID(jids.get(i)); - XMPPComponent.sendOut(msg); + for (User user : users) { + for (String jid : UserQueries.getActiveJIDs(XMPPComponent.sql, user)) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); + } } } public void sendJuickComment(JuickMessage jmsg) { - List jids; String replyQuote; - synchronized (XMPPComponent.sqlSync) { - jids = SubscriptionsQueries.getJIDSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); - replyQuote = getReplyQuote(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo); - } + List users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + replyQuote = getReplyQuote(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo); String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n"; String attachment = jmsg.getAttachmentURL(); @@ -204,13 +205,16 @@ public class ConnectionRouter extends Connection implements Runnable { msg.body = txt; msg.type = Message.Type.chat; msg.addChild(jmsg); - for (int i = 0; i < jids.size(); i++) { - msg.to = new JID(jids.get(i)); - XMPPComponent.sendOut(msg); + for (User user : users) { + // TODO: make single query + for (String jid : UserQueries.getActiveJIDs(XMPPComponent.sql, user)) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); + } } } - private String getReplyQuote(java.sql.Connection sql, int MID, int ReplyTo) { + private String getReplyQuote(JdbcTemplate sql, int MID, int ReplyTo) { String quote = ""; if (ReplyTo > 0) { com.juick.Message q = MessagesQueries.getReply(sql, MID, ReplyTo); @@ -232,13 +236,10 @@ public class ConnectionRouter extends Connection implements Runnable { } public void sendJuickRecommendation(JuickMessage recomm) { - List jids; JuickMessage jmsg; - synchronized (XMPPComponent.sqlSync) { - jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); - jids = SubscriptionsQueries.getJIDSubscribedToUserRecommendations(XMPPComponent.sql, - recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); - } + jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); + List users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, + recomm.getMID(), jmsg.getUser().getUID()); String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; txt += "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; @@ -272,10 +273,11 @@ public class ConnectionRouter extends Connection implements Runnable { oob.URL = attachment; msg.addChild(oob); } - - for (int i = 0; i < jids.size(); i++) { - msg.to = new JID(jids.get(i)); - XMPPComponent.sendOut(msg); + for (User user : users) { + for (String jid : UserQueries.getActiveJIDs(XMPPComponent.sql, user)) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); + } } } } diff --git a/src/main/java/com/juick/xmpp/s2s/JuickBot.java b/src/main/java/com/juick/xmpp/s2s/JuickBot.java index 182de10d..d82ed939 100644 --- a/src/main/java/com/juick/xmpp/s2s/JuickBot.java +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -69,9 +69,7 @@ public class JuickBot { } else if (p.type.equals(Presence.Type.probe)) { int uid_to = 0; if (!toJuick) { - synchronized (XMPPComponent.sqlSync) { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - } + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); } if (toJuick || uid_to > 0) { @@ -94,12 +92,10 @@ public class JuickBot { if (toJuick) { canSubscribe = true; } else { - synchronized (XMPPComponent.sqlSync) { - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - if (uid_to > 0) { - PMQueries.addPMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); - canSubscribe = true; - } + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + if (uid_to > 0) { + PMQueries.addPMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); + canSubscribe = true; } } @@ -122,11 +118,9 @@ public class JuickBot { } } else if (p.type.equals(Presence.Type.unsubscribe)) { if (!toJuick) { - synchronized (XMPPComponent.sqlSync) { - int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - if (uid_to > 0) { - PMQueries.removePMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); - } + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); + if (uid_to > 0) { + PMQueries.removePMinRoster(XMPPComponent.sql, uid_to, p.from.Bare()); } } @@ -146,11 +140,9 @@ public class JuickBot { User user_from = null; String signuphash = ""; - synchronized (XMPPComponent.sqlSync) { - user_from = UserQueries.getUserByJID(XMPPComponent.sql, msg.from.Bare()); - if (user_from == null) { - signuphash = UserQueries.getSignUpHashByJID(XMPPComponent.sql, msg.from.Bare()); - } + user_from = UserQueries.getUserByJID(XMPPComponent.sql, msg.from.Bare()); + if (user_from == null) { + signuphash = UserQueries.getSignUpHashByJID(XMPPComponent.sql, msg.from.Bare()); } if (user_from == null) { @@ -168,10 +160,7 @@ public class JuickBot { return incomingMessageJuick(user_from, msg); } - int uid_to = 0; - synchronized (XMPPComponent.sqlSync) { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); - } + int uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, username); if (uid_to == 0) { Message reply = new Message(msg.to, msg.from, Message.Type.error); @@ -182,20 +171,18 @@ public class JuickBot { } boolean success = false; - synchronized (XMPPComponent.sqlSync) { - if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { - success = PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, msg.body); - } + if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { + success = PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, msg.body); } + if (success) { Message m = new Message(); m.from = new JID("juick", "juick.com", null); m.to = new JID(Integer.toString(uid_to), "push.juick.com", null); JuickMessage jmsg = new JuickMessage(); - synchronized (XMPPComponent.sqlSync) { - jmsg.setUser(UserQueries.getUserByUID(XMPPComponent.sql, user_from.getUID())); - } + jmsg.setUser(UserQueries.getUserByUID(XMPPComponent.sql, user_from.getUID())); + jmsg.setText(msg.body); m.childs.add(jmsg); XMPPComponent.connRouter.sendStanza(m.toString()); @@ -205,11 +192,9 @@ public class JuickBot { String jid; boolean inroster = false; - synchronized (XMPPComponent.sqlSync) { - jid = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); - if (jid != null) { - inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); - } + jid = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); + if (jid != null) { + inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); } if (jid != null) { @@ -303,32 +288,31 @@ public class JuickBot { String jid_to = null; boolean haveInRoster = false; - synchronized (XMPPComponent.sqlSync) { - if (user_to.indexOf('@') > 0) { - uid_to = UserQueries.getUIDbyJID(XMPPComponent.sql, user_to); - } else { - uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, user_to); - } + if (user_to.indexOf('@') > 0) { + uid_to = UserQueries.getUIDbyJID(XMPPComponent.sql, user_to); + } else { + uid_to = UserQueries.getUIDbyName(XMPPComponent.sql, user_to); + } - if (uid_to > 0) { - if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { - if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { - jid_to = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); - if (jid_to != null) { - haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid_to); - } - ret = 200; - } else { - ret = 500; + if (uid_to > 0) { + if (!UserQueries.isInBLAny(XMPPComponent.sql, uid_to, user_from.getUID())) { + if (PMQueries.createPM(XMPPComponent.sql, user_from.getUID(), uid_to, body)) { + jid_to = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); + if (jid_to != null) { + haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid_to); } + ret = 200; } else { - ret = 403; + ret = 500; } } else { - ret = 404; + ret = 403; } + } else { + ret = 404; } + if (ret == 200) { Message msg = new Message(); msg.from = new JID("juick", "juick.com", null); @@ -372,10 +356,9 @@ public class JuickBot { List blusers; List bltags; - synchronized (XMPPComponent.sqlSync) { - blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); - bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); - } + blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); + bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); + String txt = ""; if (bltags.size() > 0) { diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 50d2c1e4..1dfdd38d 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -3,6 +3,7 @@ package com.juick.xmpp.s2s; import com.juick.xmpp.Stanza; import com.juick.xmpp.StanzaChild; import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -32,8 +33,7 @@ public class XMPPComponent implements ServletContextListener { static final List inConnections = Collections.synchronizedList(new ArrayList<>()); static final List outConnections = Collections.synchronizedList(new ArrayList<>()); static final List outCache = Collections.synchronizedList(new ArrayList<>()); - static final Integer sqlSync = 0; - static java.sql.Connection sql; + static JdbcTemplate sql; final public static HashMap childParsers = new HashMap<>(); public static void addConnectionIn(ConnectionIn c) { @@ -159,10 +159,7 @@ public class XMPPComponent implements ServletContextListener { HOSTNAME = conf.getProperty("hostname"); String componentName = conf.getProperty("componentname"); STATSFILE = conf.getProperty("statsfile"); - - 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", "")); + sql = (JdbcTemplate) sce.getServletContext().getAttribute("sql"); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); @@ -170,7 +167,7 @@ public class XMPPComponent implements ServletContextListener { executorService.submit(connRouter); executorService.submit(new ConnectionListener()); executorService.submit(new CleaningUp()); - } catch (IOException | ClassNotFoundException | SQLException e) { + } catch (IOException e) { LOGGER.log(Level.SEVERE, "XMPPComponent error", e); } }); @@ -195,39 +192,7 @@ public class XMPPComponent implements ServletContextListener { i.remove(); } } - XMPPComponent.connRouter.closeConnection(); - - synchronized (XMPPComponent.sqlSync) { - if (XMPPComponent.sql != null) { - try { - XMPPComponent.sql.close(); - XMPPComponent.sql = null; - } catch (SQLException e) { - LOGGER.warning("SQL ERROR: " + e); - } - } - } - // Now deregister JDBC drivers in this context's ClassLoader: - // Get the webapp's ClassLoader - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - // Loop through all drivers - Enumeration 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"); } -- cgit v1.2.3