diff options
author | Vitaly Takmazov | 2016-07-06 18:48:28 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-07-06 18:48:28 +0300 |
commit | d48e3b71c27d6782a63dda84aced58780b54f309 (patch) | |
tree | 0139968c6c7f297a8e7ff9123df3490519c3367e /src/main/java/com/juick/xmpp | |
parent | 210690e7aefbd4d82e269277d621b5e6efc5c909 (diff) |
JdbcTemplate is thread-safe
Diffstat (limited to 'src/main/java/com/juick/xmpp')
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | 52 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/JuickBot.java | 182 | ||||
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/XMPPComponent.java | 1 |
3 files changed, 100 insertions, 135 deletions
diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 95878edd..0030834a 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -55,15 +55,13 @@ public class ConnectionRouter implements Stream.StreamListener, public void sendJuickMessage(JuickMessage jmsg) { List<String> jids = new ArrayList<>(); - synchronized (XMPPComponent.sqlSync) { - if (jmsg.FriendsOnly) { - jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); - } else { - List<User> users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); - for (User user : users) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { - jids.add(jid); - } + if (jmsg.FriendsOnly) { + jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + } else { + List<User> users = SubscriptionsQueries.getSubscribedUsers(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + for (User user : users) { + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + jids.add(jid); } } } @@ -103,13 +101,11 @@ public class ConnectionRouter implements Stream.StreamListener, String replyQuote; String replyTo; - synchronized (XMPPComponent.sqlSync) { - users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); - com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) - : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); - replyTo = replyMessage.getUser().getUName(); - replyQuote = getReplyQuote(replyMessage); - } + users = SubscriptionsQueries.getUsersSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); + com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo) + : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID()); + replyTo = replyMessage.getUser().getUName(); + replyQuote = getReplyQuote(replyMessage); String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n@" + replyTo + " "; String attachment = jmsg.getAttachmentURL(); @@ -124,11 +120,9 @@ public class ConnectionRouter implements Stream.StreamListener, msg.type = Message.Type.chat; msg.addChild(jmsg); for (User user : users) { - synchronized (XMPPComponent.sqlSync) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); - } + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); } } } @@ -146,11 +140,9 @@ public class ConnectionRouter implements Stream.StreamListener, public void sendJuickRecommendation(JuickMessage recomm) { List<User> users; JuickMessage jmsg; - synchronized (XMPPComponent.sqlSync) { - jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); - users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, - recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); - } + jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); + users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, + recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; txt += "@" + jmsg.getUser().getUName() + ":" + jmsg.getTagsString() + "\n"; @@ -186,11 +178,9 @@ public class ConnectionRouter implements Stream.StreamListener, } for (User user : users) { - synchronized (XMPPComponent.sqlSync) { - for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); - } + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + 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 1172d75f..357b070f 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,10 +171,8 @@ 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) { @@ -193,9 +180,7 @@ public class JuickBot { 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(user_from); - } + jmsg.setUser(user_from); jmsg.setText(msg.body); m.childs.add(jmsg); XMPPComponent.connRouter.router.send(m.toString()); @@ -205,22 +190,20 @@ public class JuickBot { List<String> jids; boolean inroster = false; - synchronized (XMPPComponent.sqlSync) { - jids = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); - for (String jid : jids) { - Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; - inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); - if (inroster) { - mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); - mm.body = msg.body; - } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; - } - XMPPComponent.sendOut(mm); + jids = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); + for (String jid : jids) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + inroster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + if (inroster) { + mm.from = new JID(jmsg.getUser().getUName(), "juick.com", "Juick"); + mm.body = msg.body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + jmsg.getUser().getUName() + ":\n" + msg.body; } + XMPPComponent.sendOut(mm); } } else { Message reply = new Message(msg.to, msg.from, Message.Type.error); @@ -299,77 +282,70 @@ public class JuickBot { List<String> jids_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)) { - jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_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)) { + jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_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); - msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); - JuickMessage jmsg = new JuickMessage(); - jmsg.setUser(user_from); - jmsg.setText(body); - msg.childs.add(jmsg); - XMPPComponent.connRouter.router.send(msg.toString()); - - msg.to.Host = "ws.juick.com"; - XMPPComponent.connRouter.router.send(msg.toString()); - - for (String jid : jids_to) { - Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; - haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); - if (haveInRoster) { - mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); - mm.body = body; - } else { - mm.from = new JID("juick", "juick.com", "Juick"); - mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; - } - XMPPComponent.sendOut(mm); + if (ret == 200) { + Message msg = new Message(); + msg.from = new JID("juick", "juick.com", null); + msg.to = new JID(Integer.toString(uid_to), "push.juick.com", null); + JuickMessage jmsg = new JuickMessage(); + jmsg.setUser(user_from); + jmsg.setText(body); + msg.childs.add(jmsg); + XMPPComponent.connRouter.router.send(msg.toString()); + + msg.to.Host = "ws.juick.com"; + XMPPComponent.connRouter.router.send(msg.toString()); + + for (String jid : jids_to) { + Message mm = new Message(); + mm.to = new JID(jid); + mm.type = Message.Type.chat; + haveInRoster = PMQueries.havePMinRoster(XMPPComponent.sql, user_from.getUID(), jid); + if (haveInRoster) { + mm.from = new JID(user_from.getUName(), "juick.com", "Juick"); + mm.body = body; + } else { + mm.from = new JID("juick", "juick.com", "Juick"); + mm.body = "Private message from @" + user_from.getUName() + ":\n" + body; } + XMPPComponent.sendOut(mm); } + } - Message reply = new Message(m.to, m.from); - if (ret == 200) { - reply.type = m.type; - reply.body = "Private message sent"; - } else { - reply.type = Message.Type.error; - reply.body = "Error " + ret; - } - XMPPComponent.sendOut(reply); + Message reply = new Message(m.to, m.from); + if (ret == 200) { + reply.type = m.type; + reply.body = "Private message sent"; + } else { + reply.type = Message.Type.error; + reply.body = "Error " + ret; } + XMPPComponent.sendOut(reply); } private static void commandBLShow(Message m, User user_from) throws Exception { - List<User> blusers; - List<String> bltags; - - synchronized (XMPPComponent.sqlSync) { - blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); - bltags = TagQueries.getUserBLTags(XMPPComponent.sql, user_from.getUID()); - } + List<User> blusers = UserQueries.getUserBLUsers(XMPPComponent.sql, user_from.getUID()); + List<String> 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 70f01553..bb736b5e 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -43,7 +43,6 @@ public class XMPPComponent implements ServletContextListener { 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 JdbcTemplate sql; final public static HashMap<String, StanzaChild> childParsers = new HashMap<>(); |