From 606f43f50904f7d811cf9b438e0e820e586be900 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 13:16:43 +0300 Subject: spring-jdbc near complete --- .../com/juick/xmpp/extensions/JuickMessage.java | 17 ++- .../java/com/juick/xmpp/s2s/ConnectionRouter.java | 45 +++++--- src/main/java/com/juick/xmpp/s2s/JuickBot.java | 118 ++++++++++----------- .../java/com/juick/xmpp/s2s/XMPPComponent.java | 24 ++--- 4 files changed, 109 insertions(+), 95 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..220caba7 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; @@ -31,12 +35,17 @@ 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) { super(msg); + df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + df.setTimeZone(TimeZone.getTimeZone("UTC")); } @Override @@ -45,7 +54,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 +81,7 @@ 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 + "\""; diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java index 443bfa82..d0b3a93b 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.*; import com.juick.xmpp.extensions.JuickMessage; import com.juick.xmpp.extensions.Nickname; @@ -9,6 +11,8 @@ import com.juick.xmpp.extensions.XOOB; import java.io.IOException; import java.net.Socket; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -45,13 +49,18 @@ public class ConnectionRouter implements Stream.StreamListener, } public void sendJuickMessage(JuickMessage jmsg) { - List jids; + List jids = new ArrayList<>(); 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()); + List 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); + } + } } } @@ -86,14 +95,14 @@ public class ConnectionRouter implements Stream.StreamListener, } public void sendJuickComment(JuickMessage jmsg) { - List jids; + List users; String replyQuote; String replyTo; synchronized (XMPPComponent.sqlSync) { - jids = SubscriptionsQueries.getJIDSubscribedToComments(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()); + 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); } @@ -110,9 +119,13 @@ public class ConnectionRouter implements Stream.StreamListener, msg.body = txt; msg.type = Message.Type.chat; msg.addChild(jmsg); - for (String jid : jids) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + for (User user : users) { + synchronized (XMPPComponent.sqlSync) { + for (String jid : UserQueries.getJIDsbyUID(XMPPComponent.sql, user.getUID())) { + msg.to = new JID(jid); + XMPPComponent.sendOut(msg); + } + } } } @@ -127,11 +140,11 @@ public class ConnectionRouter implements Stream.StreamListener, } public void sendJuickRecommendation(JuickMessage recomm) { - List jids; + List users; JuickMessage jmsg; synchronized (XMPPComponent.sqlSync) { jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); - jids = SubscriptionsQueries.getJIDSubscribedToUserRecommendations(XMPPComponent.sql, + users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(XMPPComponent.sql, recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); } @@ -168,9 +181,13 @@ public class ConnectionRouter implements Stream.StreamListener, msg.addChild(oob); } - for (String jid : jids) { - msg.to = new JID(jid); - XMPPComponent.sendOut(msg); + for (User user : users) { + synchronized (XMPPComponent.sqlSync) { + 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 25c75dfe..1172d75f 100644 --- a/src/main/java/com/juick/xmpp/s2s/JuickBot.java +++ b/src/main/java/com/juick/xmpp/s2s/JuickBot.java @@ -194,7 +194,7 @@ public class JuickBot { 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(user_from); } jmsg.setText(msg.body); m.childs.add(jmsg); @@ -203,29 +203,25 @@ public class JuickBot { m.to.Host = "ws.juick.com"; XMPPComponent.connRouter.router.send(m.toString()); - String jid; + List jids; boolean inroster = false; synchronized (XMPPComponent.sqlSync) { - jid = UserQueries.getJIDbyUID(XMPPComponent.sql, uid_to); - if (jid != null) { + 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); } } - - if (jid != null) { - Message mm = new Message(); - mm.to = new JID(jid); - mm.type = Message.Type.chat; - 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); reply.id = msg.id; @@ -300,7 +296,7 @@ public class JuickBot { int ret = 0; int uid_to = 0; - String jid_to = null; + List jids_to = null; boolean haveInRoster = false; synchronized (XMPPComponent.sqlSync) { @@ -313,10 +309,7 @@ public class JuickBot { 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); - } + jids_to = UserQueries.getJIDsbyUID(XMPPComponent.sql, uid_to); ret = 200; } else { ret = 500; @@ -327,45 +320,46 @@ public class JuickBot { } 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()); - - if (jid_to != null) { - Message mm = new Message(); - mm.to = new JID(jid_to); - mm.type = Message.Type.chat; - 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; + 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); } - 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; + 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); } - XMPPComponent.sendOut(reply); } private static void commandBLShow(Message m, User user_from) throws Exception { @@ -379,8 +373,8 @@ public class JuickBot { String txt = ""; if (bltags.size() > 0) { - for (int i = 0; i < bltags.size(); i++) { - txt += "*" + bltags.get(i) + "\n"; + for (String bltag : bltags) { + txt += "*" + bltag + "\n"; } if (blusers.size() > 0) { @@ -388,8 +382,8 @@ public class JuickBot { } } if (blusers.size() > 0) { - for (int i = 0; i < blusers.size(); i++) { - txt += "@" + blusers.get(i).getUName() + "\n"; + for (User bluser : blusers) { + txt += "@" + bluser.getUName() + "\n"; } } if (txt.isEmpty()) { diff --git a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java index 13df5fd0..70f01553 100644 --- a/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java +++ b/src/main/java/com/juick/xmpp/s2s/XMPPComponent.java @@ -3,6 +3,8 @@ 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 org.springframework.jdbc.datasource.DriverManagerDataSource; import org.xmlpull.v1.XmlPullParserException; import javax.servlet.ServletContextEvent; @@ -42,7 +44,7 @@ public class XMPPComponent implements ServletContextListener { 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) { @@ -176,9 +178,12 @@ public class XMPPComponent implements ServletContextListener { keystore = conf.getProperty("keystore"); keystorePassword = conf.getProperty("keystore_password"); brokenSSLhosts = Arrays.asList(conf.getProperty("broken_ssl_hosts", "").split(",")); - 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", "")); + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(conf.getProperty("datasource_url")); + DriverManagerDataSource dataSourceSearch = new DriverManagerDataSource(); + dataSourceSearch.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + sql = new JdbcTemplate(dataSource); childParsers.put(JuickMessage.XMLNS, new JuickMessage()); executorService.submit(() -> connRouter = new ConnectionRouter(componentName, conf.getProperty("xmpp_password"))); @@ -215,17 +220,6 @@ public class XMPPComponent implements ServletContextListener { } catch (IOException e) { LOGGER.log(Level.WARNING, "router warning", e); } - - 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(); -- cgit v1.2.3