diff options
author | Vitaly Takmazov | 2016-06-27 10:39:24 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-06-27 10:39:24 +0300 |
commit | 0d93073c6988a22246db61a59b97c2fd7c9f462b (patch) | |
tree | d5f27cdb9a4fe51690f7af067608a9bee62087e3 /src/main/java/com/juick/http/www/Main.java | |
parent | 2d7f61242de54b0c0670bac1953e45ee18d9ecec (diff) | |
parent | 53fb0b3e0e790df007a198deef21c115ed236b3e (diff) |
merge
Diffstat (limited to 'src/main/java/com/juick/http/www/Main.java')
-rw-r--r-- | src/main/java/com/juick/http/www/Main.java | 73 |
1 files changed, 17 insertions, 56 deletions
diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java index 7896c207..604ccf9a 100644 --- a/src/main/java/com/juick/http/www/Main.java +++ b/src/main/java/com/juick/http/www/Main.java @@ -21,6 +21,8 @@ import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; import ru.sape.Sape; import javax.servlet.ServletException; @@ -32,9 +34,6 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.Socket; import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.util.Properties; /** @@ -45,8 +44,8 @@ import java.util.Properties; @MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10) public class Main extends HttpServlet implements Stream.StreamListener { - Connection sql; - Connection sqlSearch; + JdbcTemplate sql; + JdbcTemplate sqlSearch; String sqlSearchConnStr = "jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000"; Stream xmpp; Home home = new Home(); @@ -72,9 +71,14 @@ public class Main extends HttpServlet implements Stream.StreamListener { Properties conf = new Properties(); conf.load(getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); - 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", "")); - sqlSearch = null; // init this on search, timeout is too low + 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")); + dataSourceSearch.setUrl(sqlSearchConnStr); + sql = new JdbcTemplate(dataSource); + sqlSearch = new JdbcTemplate(dataSourceSearch); setupXmppComponent(conf.getProperty("xmpp_password")); twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"), @@ -84,27 +88,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { log(null, e); } } - public void closeSqlSearch() { - if (sqlSearch != null) { - try { - sqlSearch.close(); - sqlSearch = null; - } catch (SQLException e) { - log("An error on closing sql search connection", e); - } - } - }; - public Connection getSqlSearch() { - if (sqlSearch == null) { - try { - sqlSearch = DriverManager.getConnection(sqlSearchConnStr, "", ""); - } - catch (Exception e) { - log("Couldn't open sqlSearch connection",e); - } - } - return sqlSearch; - } + public void setupXmppComponent(final String password) { Thread thr = new Thread(() -> { try { @@ -120,33 +104,13 @@ public class Main extends HttpServlet implements Stream.StreamListener { } @Override - public void onStreamFail(Exception e) {log("XMPP STREAM FAIL", e);} + public void onStreamFail(String e) {log("XMPP STREAM FAIL:" + e);} @Override public void onStreamReady() { log("XMPP STREAM READY"); } - @Override - public void destroy() { - super.destroy(); - if (sql != null) { - try { - sql.close(); - sql = null; - } catch (SQLException e) { - log(null, e); - } - } - if (sqlSearch != null) { - try { - sqlSearch.close(); - sqlSearch = null; - } catch (SQLException e) { - log(null, e); - } - } - } /** * Handles the HTTP <code>GET</code> method. @@ -168,8 +132,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, "UTF-8")); } else { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - home.doGet(sql, getSqlSearch(), request, response, visitor); - closeSqlSearch(); + home.doGet(sql, sqlSearch, request, response, visitor); } } else if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); @@ -226,8 +189,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else if (uri.startsWith("/help/")) { help.doGetHelp(sql, request, response); } else if (uri.startsWith("/tag/")) { - discover.doGet(sql, getSqlSearch(), request, response); - closeSqlSearch(); + discover.doGet(sql, sqlSearch, request, response); } else if (uri.matches("^/\\d+$")) { String strID = request.getRequestURI().substring(1); int mid = 0; @@ -255,8 +217,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, uriparts[1]); if (user != null && user.getUName().equals(uriparts[1]) && !user.Banned) { if (uriparts.length == 2) { // http://juick.com/username/ - pagesUser.doGetBlog(sql, getSqlSearch(), request, response, user); - closeSqlSearch(); + pagesUser.doGetBlog(sql, sqlSearch, request, response, user); } else if (uriparts[2].equals("tags")) { pagesUser.doGetTags(sql, request, response, user); } else if (uriparts[2].equals("friends")) { |