From 095dee1388919eac7d3a3fa6fc93767f82c16365 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 24 Jun 2016 15:06:58 +0300 Subject: spring-jdbc --- src/main/java/com/juick/jabber/ws/Main.java | 19 +++++----- .../java/com/juick/jabber/ws/WSConnections.java | 44 ---------------------- src/main/java/com/juick/jabber/ws/WSData.java | 7 ++-- src/main/java/com/juick/jabber/ws/WSKeepAlive.java | 7 ++-- .../java/com/juick/jabber/ws/XMPPConnection.java | 41 ++++++-------------- 5 files changed, 29 insertions(+), 89 deletions(-) delete mode 100644 src/main/java/com/juick/jabber/ws/WSConnections.java (limited to 'src/main/java/com/juick/jabber/ws') diff --git a/src/main/java/com/juick/jabber/ws/Main.java b/src/main/java/com/juick/jabber/ws/Main.java index a7aea543..3a26c3ad 100644 --- a/src/main/java/com/juick/jabber/ws/Main.java +++ b/src/main/java/com/juick/jabber/ws/Main.java @@ -17,8 +17,10 @@ */ package com.juick.jabber.ws; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + import java.io.FileInputStream; -import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; @@ -30,7 +32,7 @@ import java.util.Properties; */ public class Main { - Connection sql; + JdbcTemplate sql; XMPPConnection xmpp; public static String STATSFILE = null; public final static ArrayList clients = new ArrayList(); @@ -46,7 +48,7 @@ public class Main { STATSFILE = conf.getProperty("statsfile"); - setupSql(conf.getProperty("mysql_username", ""), conf.getProperty("mysql_password", "")); + setupSql(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver"), conf.getProperty("datasource_url")); xmpp = new XMPPConnection(sql, conf.getProperty("xmpp_password", "")); new Thread(xmpp).start(); @@ -58,11 +60,10 @@ public class Main { } } - public void setupSql(String username, String password) { - try { - sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + username + "&password=" + password); - } catch (SQLException e) { - System.err.println(e); - } + public void setupSql(String driver, String url) { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(driver); + dataSource.setUrl(url); + sql = new JdbcTemplate(dataSource); } } \ No newline at end of file diff --git a/src/main/java/com/juick/jabber/ws/WSConnections.java b/src/main/java/com/juick/jabber/ws/WSConnections.java deleted file mode 100644 index 15fbe4e8..00000000 --- a/src/main/java/com/juick/jabber/ws/WSConnections.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.juick.jabber.ws; - -import java.net.InetSocketAddress; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; -import java.util.Iterator; - -/** - * - * @author ugnich - */ -public class WSConnections implements Runnable { - - Selector sel; - - @Override - public void run() { - try { - sel = Selector.open(); - ServerSocketChannel listensock = ServerSocketChannel.open(); - listensock.configureBlocking(false); - listensock.socket().bind(new InetSocketAddress(8081)); - listensock.register(sel, SelectionKey.OP_ACCEPT); - - while (true) { - sel.select(); - Iterator it = sel.selectedKeys().iterator(); - while (it.hasNext()) { - SelectionKey selKey = it.next(); - it.remove(); - ServerSocketChannel ssChannel = (ServerSocketChannel) selKey.channel(); - SocketChannel sChannel = ssChannel.accept(); - sChannel.configureBlocking(false); - sChannel.register(sel, SelectionKey.OP_READ); - System.out.println(sChannel.socket().getRemoteSocketAddress().toString() + " ACCEPTED"); - } - } - } catch (Exception e) { - System.err.println("WSConnections: " + e); - } - } -} diff --git a/src/main/java/com/juick/jabber/ws/WSData.java b/src/main/java/com/juick/jabber/ws/WSData.java index d77257e6..3abc63d8 100644 --- a/src/main/java/com/juick/jabber/ws/WSData.java +++ b/src/main/java/com/juick/jabber/ws/WSData.java @@ -3,6 +3,8 @@ package com.juick.jabber.ws; import com.juick.server.MessagesQueries; import com.juick.server.UserQueries; import com.juick.xmpp.utils.Base64; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; @@ -14,7 +16,6 @@ import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.sql.Connection; import java.util.Iterator; /** @@ -24,10 +25,10 @@ import java.util.Iterator; public class WSData implements Runnable { static final String WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - Connection sql; + JdbcTemplate sql; public Selector sel; - public WSData(Connection sql) { + public WSData(JdbcTemplate sql) { this.sql = sql; } diff --git a/src/main/java/com/juick/jabber/ws/WSKeepAlive.java b/src/main/java/com/juick/jabber/ws/WSKeepAlive.java index 2deef594..f253439d 100644 --- a/src/main/java/com/juick/jabber/ws/WSKeepAlive.java +++ b/src/main/java/com/juick/jabber/ws/WSKeepAlive.java @@ -1,8 +1,9 @@ package com.juick.jabber.ws; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.PrintWriter; import java.nio.ByteBuffer; -import java.sql.Connection; import java.util.Iterator; /** @@ -11,11 +12,11 @@ import java.util.Iterator; */ public class WSKeepAlive implements Runnable { - Connection sql; + JdbcTemplate sql; ByteBuffer pingBytes; ByteBuffer closeBytes; - public WSKeepAlive(Connection sql) { + public WSKeepAlive(JdbcTemplate sql) { this.sql = sql; //pingBytes = ByteBuffer.allocate(2); diff --git a/src/main/java/com/juick/jabber/ws/XMPPConnection.java b/src/main/java/com/juick/jabber/ws/XMPPConnection.java index df403083..1679ae8f 100644 --- a/src/main/java/com/juick/jabber/ws/XMPPConnection.java +++ b/src/main/java/com/juick/jabber/ws/XMPPConnection.java @@ -1,23 +1,23 @@ package com.juick.jabber.ws; +import com.juick.User; import com.juick.json.MessageSerializer; import com.juick.server.MessagesQueries; -import com.juick.server.Utils; +import com.juick.server.SubscriptionsQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; import com.juick.xmpp.extensions.JuickMessage; +import org.springframework.jdbc.core.JdbcTemplate; + import java.io.IOException; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.charset.Charset; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; /** * @@ -25,11 +25,11 @@ import java.util.Iterator; */ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.MessageListener { - Connection sql; + JdbcTemplate sql; Stream xmpp; String xmppPassword; - public XMPPConnection(Connection sql, String password) { + public XMPPConnection(JdbcTemplate sql, String password) { this.sql = sql; xmppPassword = password; } @@ -54,7 +54,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. } @Override - public void onStreamFail(Exception msg) { + public void onStreamFail(String msg) { System.err.println("Stream failed: " + msg); } @@ -102,27 +102,8 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message. String json = messageSerializer.serialize(jmsg).toString(); ByteBuffer bbMsg = buildTextFrame(json); - ArrayList uids = new ArrayList(); - String query = "SELECT suser_id FROM subscr_users WHERE user_id=" + jmsg.getUser().getUID() + - " AND suser_id NOT IN (SELECT user_id FROM bl_tags " + - "INNER JOIN messages_tags USING(tag_id) WHERE message_id=" + jmsg.getMID() + ")"; - if (jmsg.Privacy < 0) { - query += " AND suser_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=" + jmsg.getUser().getUID() + ")"; - } - Statement stmt = null; - ResultSet rs = null; - try { - stmt = sql.createStatement(); - rs = stmt.executeQuery(query); - rs.beforeFirst(); - while (rs.next()) { - uids.add(rs.getInt(1)); - } - } catch (SQLException e) { - System.err.println("onJuickMessagePost: " + e); - } finally { - Utils.finishSQL(rs, stmt); - } + List uids = SubscriptionsQueries.getSubscribedUsers(sql, jmsg.getUser().getUID(), jmsg.getMID()) + .stream().map(User::getUID).collect(Collectors.toList()); synchronized (Main.clients) { for (Iterator i = Main.clients.iterator(); i.hasNext();) { -- cgit v1.2.3