aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/xmpp/s2s
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-02-02 14:36:04 +0300
committerGravatar Vitaly Takmazov2016-02-02 14:36:04 +0300
commitbd3892546b51f0d31e959d45c138b009d35b2692 (patch)
tree3c3945f114472344cc3a8866391fa08f02975143 /src/main/java/com/juick/xmpp/s2s
parentf2a2660e753fa11478fd3edadfdff9c000b22cc9 (diff)
spring-jdbc
Diffstat (limited to 'src/main/java/com/juick/xmpp/s2s')
-rw-r--r--src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java62
-rw-r--r--src/main/java/com/juick/xmpp/s2s/JuickBot.java97
-rw-r--r--src/main/java/com/juick/xmpp/s2s/XMPPComponent.java43
3 files changed, 76 insertions, 126 deletions
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<String> jids;
+ List<User> 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<String> 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<User> 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<String> 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<User> 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<User> blusers;
List<String> 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<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;
+ static JdbcTemplate sql;
final public static HashMap<String, StanzaChild> 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<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");
}