From 975ad340be5af48896e128534787dbd46fa89b58 Mon Sep 17 00:00:00 2001 From: Arbel Deutsch Peled Date: Sat, 12 Dec 2015 12:36:00 +0200 Subject: [PATCH] Bulletin Board Server WebApp support for MySQL and H2 engines. --- .../sqlserver/H2QueryProvider.java | 23 ++------ .../sqlserver/MySQLQueryProvider.java | 12 +++-- .../webapp/BulletinBoardWebApp.java | 28 +++++++--- .../src/main/webapp/WEB-INF/web.xml | 20 +++++-- ...ulletinBoardSQLServerIntegrationTest.java} | 52 +------------------ .../H2BulletinBoardServerTest.java | 4 +- .../MySQLBulletinBoardServerTest.java | 4 +- 7 files changed, 59 insertions(+), 84 deletions(-) rename bulletin-board-server/src/test/java/meerkat/bulletinboard/{SQLiteServerIntegrationTest.java => BulletinBoardSQLServerIntegrationTest.java} (73%) diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/H2QueryProvider.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/H2QueryProvider.java index cf3fd68..fa2b146 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/H2QueryProvider.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/H2QueryProvider.java @@ -16,14 +16,10 @@ import java.util.List; public class H2QueryProvider implements BulletinBoardSQLServer.SQLQueryProvider { - private String dbAddress; - private String username; - private String password; + private String dbName; - public H2QueryProvider(String dbAddress, String username, String password) { - this.dbAddress = dbAddress; - this.username = username; - this.password = password; + public H2QueryProvider(String dbName) { + this.dbName = dbName; } @@ -116,18 +112,9 @@ public class H2QueryProvider implements BulletinBoardSQLServer.SQLQueryProvider @Override public DataSource getDataSource() { - // TODO: Fix this + JdbcDataSource dataSource = new JdbcDataSource(); - dataSource.setURL("jdbc:h2:~/" + dbAddress + "/meerkat"); // TODO: make this generic - dataSource.setUser(username); - dataSource.setPassword(password); -// Context ctx = null; -// try { -// ctx = new InitialContext(); -// ctx.bind("jdbc/dsName", dataSource); -// } catch (NamingException e) { -// e.printStackTrace(); -// } + dataSource.setURL("jdbc:h2:~/" + dbName); return dataSource; } diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/MySQLQueryProvider.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/MySQLQueryProvider.java index 9956646..c00c044 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/MySQLQueryProvider.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/sqlserver/MySQLQueryProvider.java @@ -15,11 +15,15 @@ import java.util.List; public class MySQLQueryProvider implements SQLQueryProvider { private String dbAddress; + private int dbPort; + private String dbName; private String username; private String password; - public MySQLQueryProvider(String dbAddress, String username, String password) { + public MySQLQueryProvider(String dbAddress, int dbPort, String dbName, String username, String password) { this.dbAddress = dbAddress; + this.dbPort = dbPort; + this.dbName = dbName; this.username = username; this.password = password; } @@ -101,10 +105,12 @@ public class MySQLQueryProvider implements SQLQueryProvider { @Override public DataSource getDataSource() { MysqlDataSource dataSource = new MysqlDataSource(); - dataSource.setDatabaseName("meerkat"); //TODO: Make generic + + dataSource.setServerName(dbAddress); + dataSource.setPort(dbPort); + dataSource.setDatabaseName(dbName); dataSource.setUser(username); dataSource.setPassword(password); - dataSource.setServerName(dbAddress); return dataSource; } diff --git a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java index 1a8abaf..779982a 100644 --- a/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java +++ b/bulletin-board-server/src/main/java/meerkat/bulletinboard/webapp/BulletinBoardWebApp.java @@ -13,6 +13,8 @@ import javax.ws.rs.core.MediaType; import meerkat.bulletinboard.BulletinBoardServer; import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer; +import meerkat.bulletinboard.sqlserver.H2QueryProvider; +import meerkat.bulletinboard.sqlserver.MySQLQueryProvider; import meerkat.bulletinboard.sqlserver.SQLiteQueryProvider; import meerkat.comm.CommunicationException; import meerkat.protobuf.BulletinBoardAPI.BoolMsg; @@ -48,15 +50,29 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL @Override public void contextInitialized(ServletContextEvent servletContextEvent) { ServletContext servletContext = servletContextEvent.getServletContext(); - String meerkatDB = servletContext.getInitParameter("meerkatdb"); - String dbType = servletContext.getInitParameter("dbtype"); - - if (dbType.compareTo("SQLite") == 0){ - bulletinBoard = new BulletinBoardSQLServer(new SQLiteQueryProvider(meerkatDB)); + String dbType = servletContext.getInitParameter("dbType"); + String dbName = servletContext.getInitParameter("dbName"); + + if ("SQLite".compareTo(dbType) == 0){ + + bulletinBoard = new BulletinBoardSQLServer(new SQLiteQueryProvider(dbName)); + + } else if ("H2".compareTo(dbType) == 0) { + + bulletinBoard = new BulletinBoardSQLServer(new H2QueryProvider(dbName)); + + } else if ("MySQL".compareTo(dbType) == 0) { + + String dbAddress = servletContext.getInitParameter("dbAddress"); + int dbPort = Integer.parseInt(servletContext.getInitParameter("dbPort")); + String username = servletContext.getInitParameter("username"); + String password = servletContext.getInitParameter("password"); + + bulletinBoard = new BulletinBoardSQLServer(new MySQLQueryProvider(dbAddress,dbPort,dbName,username,password)); } try { - init(meerkatDB); + init(dbName); servletContext.setAttribute(BULLETIN_BOARD_ATTRIBUTE_NAME, bulletinBoard); } catch (CommunicationException e) { System.err.println(e.getMessage()); diff --git a/bulletin-board-server/src/main/webapp/WEB-INF/web.xml b/bulletin-board-server/src/main/webapp/WEB-INF/web.xml index 5f513e9..226aa3b 100644 --- a/bulletin-board-server/src/main/webapp/WEB-INF/web.xml +++ b/bulletin-board-server/src/main/webapp/WEB-INF/web.xml @@ -15,11 +15,23 @@ /* - meerkatdb - meerkatdb + dbAddress + localhost - dbtype - SQLite + dbPort + 3306 + + dbName + meerkat + + username + arbel + + password + mypass + + dbType + H2 meerkat.bulletinboard.webapp.BulletinBoardWebApp diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java similarity index 73% rename from bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java rename to bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java index 2a4d6d9..838adcc 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/SQLiteServerIntegrationTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/BulletinBoardSQLServerIntegrationTest.java @@ -19,14 +19,13 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; -public class SQLiteServerIntegrationTest { +public class BulletinBoardSQLServerIntegrationTest { private static String PROP_GETTY_URL = "gretty.httpBaseURI"; private static String DEFAULT_BASE_URL = "http://localhost:8081"; private static String BASE_URL = System.getProperty(PROP_GETTY_URL, DEFAULT_BASE_URL); Client client; -// Connection connection; @Before public void setup() throws Exception { @@ -53,13 +52,6 @@ public class SQLiteServerIntegrationTest { MessageFilterList filterList; BulletinBoardMessageList msgList; -// try{ -// connection = DriverManager.getConnection("jdbc:sqlite:d:/arbel/projects/meerkat-java/bulletin-board-server/local-instances/meerkat.db"); -// } catch (SQLException e) { -// System.err.println(e.getMessage()); -// assert false; -// } - // Test writing mechanism System.err.println("******** Testing: " + Constants.POST_MESSAGE_PATH); @@ -119,47 +111,7 @@ public class SQLiteServerIntegrationTest { .build() ) .build(); - -// String sql = "SELECT MsgTable.EntryNum, MsgTable.Msg FROM MsgTable INNER JOIN SignatureTable ON SignatureTable.EntryNum = MsgTable.EntryNum WHERE SignatureTable.SignerId = ?"; -// PreparedStatement pstmt = connection.prepareStatement(sql); -// int i=1; -// for (MessageFilter filter : filterList.getFilterList()){ -// -// switch (filter.getType().getNumber()){ -// -// case FilterType.EXACT_ENTRY_VALUE: // Go through. -// case FilterType.MAX_ENTRY_VALUE: -// pstmt.setLong(i, filter.getEntry()); -// i++; -// break; -// -// case FilterType.MSG_ID_VALUE: // Go through. -// case FilterType.SIGNER_ID_VALUE: -// pstmt.setBytes(i, filter.getId().toByteArray()); -// i++; -// break; -// -// case FilterType.TAG_VALUE: -// pstmt.setString(i, filter.getTag()); -// break; -// -// // The max-messages condition is applied as a suffix. Therefore, it is treated differently. -// case FilterType.MAX_MESSAGES_VALUE: -// pstmt.setLong(filterList.getFilterList().size(), filter.getMaxMessages()); -// break; -// -// } -// } -// ResultSet rs = pstmt.executeQuery(); -// -// i = 0; -// while (rs.next()){ -// i++; -// assert rs.getBytes(2) -// } -// System.err.println("Local DB size = " + i); -// pstmt.close(); - + response = webTarget.request(Constants.MEDIATYPE_PROTOBUF).post(Entity.entity(filterList, Constants.MEDIATYPE_PROTOBUF)); System.err.println(response); msgList = response.readEntity(BulletinBoardMessageList.class); diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/H2BulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/H2BulletinBoardServerTest.java index fc02b7b..ef19310 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/H2BulletinBoardServerTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/H2BulletinBoardServerTest.java @@ -21,7 +21,7 @@ import static org.junit.Assert.fail; */ public class H2BulletinBoardServerTest { - private final String dbAddress = "meerkatTest"; + private final String dbName = "meerkatTest"; private GenericBulletinBoardServerTest serverTest; @@ -35,7 +35,7 @@ public class H2BulletinBoardServerTest { System.err.println("Starting to initialize H2BulletinBoardServerTest"); long start = threadBean.getCurrentThreadCpuTime(); - queryProvider = new H2QueryProvider(dbAddress, "", ""); + queryProvider = new H2QueryProvider(dbName); try { diff --git a/bulletin-board-server/src/test/java/meerkat/bulletinboard/MySQLBulletinBoardServerTest.java b/bulletin-board-server/src/test/java/meerkat/bulletinboard/MySQLBulletinBoardServerTest.java index 6f0b1a3..e473931 100644 --- a/bulletin-board-server/src/test/java/meerkat/bulletinboard/MySQLBulletinBoardServerTest.java +++ b/bulletin-board-server/src/test/java/meerkat/bulletinboard/MySQLBulletinBoardServerTest.java @@ -24,6 +24,8 @@ import static org.junit.Assert.fail; public class MySQLBulletinBoardServerTest { private final String dbAddress = "localhost"; + private final int dbPort = 3306; + private final String dbName = "meerkat"; private final String username = "arbel"; private final String password = "mypass"; @@ -37,7 +39,7 @@ public class MySQLBulletinBoardServerTest { System.err.println("Starting to initialize MySQLBulletinBoardServerTest"); long start = threadBean.getCurrentThreadCpuTime(); - SQLQueryProvider queryProvider = new MySQLQueryProvider(dbAddress,username,password); + SQLQueryProvider queryProvider = new MySQLQueryProvider(dbAddress,dbPort,dbName,username,password); try {