aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/http/www/Main.java
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/http/www/Main.java
parentf2a2660e753fa11478fd3edadfdff9c000b22cc9 (diff)
spring-jdbc
Diffstat (limited to 'src/main/java/com/juick/http/www/Main.java')
-rw-r--r--src/main/java/com/juick/http/www/Main.java75
1 files changed, 20 insertions, 55 deletions
diff --git a/src/main/java/com/juick/http/www/Main.java b/src/main/java/com/juick/http/www/Main.java
index cabd50af..85abed00 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;
@@ -45,9 +47,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;
- String sqlSearchConnStr = "jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000";
+ JdbcTemplate sql;
+ JdbcTemplate sqlSearch;
Stream xmpp;
Home home = new Home();
Discover discover = new Discover();
@@ -72,10 +73,16 @@ 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"));
+ sql = new JdbcTemplate(dataSource);
+ getServletContext().setAttribute("sql", sql);
+ DriverManagerDataSource searchDatasource = new DriverManagerDataSource();
+ searchDatasource.setDriverClassName("com.mysql.jdbc.Driver");
+ searchDatasource.setUrl("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000");
+ sqlSearch = new JdbcTemplate(searchDatasource);
+ getServletContext().setAttribute("sqlSearch", sqlSearch);
setupXmppComponent(conf.getProperty("xmpp_password"));
twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"),
conf.getProperty("twitter_consumer_secret"));
@@ -84,32 +91,13 @@ 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 {
Socket socket = new Socket("localhost", 5347);
- xmpp = new StreamComponent(new JID("", "www.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password);
+ xmpp = new StreamComponent(new JID("", "www.juick.com", ""), socket.getInputStream(),
+ socket.getOutputStream(), password);
xmpp.addListener(Main.this);
xmpp.startParsing();
} catch (IOException e) {
@@ -127,26 +115,6 @@ public class Main extends HttpServlet implements Stream.StreamListener {
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 +136,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 +193,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 +221,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")) {