Bulletin Board Server WebApp support for MySQL and H2 engines.
parent
520697d121
commit
975ad340be
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -15,11 +15,23 @@
|
|||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<context-param>
|
||||
<param-name>meerkatdb</param-name>
|
||||
<param-value>meerkatdb</param-value></context-param>
|
||||
<param-name>dbAddress</param-name>
|
||||
<param-value>localhost</param-value></context-param>
|
||||
<context-param>
|
||||
<param-name>dbtype</param-name>
|
||||
<param-value>SQLite</param-value></context-param>
|
||||
<param-name>dbPort</param-name>
|
||||
<param-value>3306</param-value></context-param>
|
||||
<context-param>
|
||||
<param-name>dbName</param-name>
|
||||
<param-value>meerkat</param-value></context-param>
|
||||
<context-param>
|
||||
<param-name>username</param-name>
|
||||
<param-value>arbel</param-value></context-param>
|
||||
<context-param>
|
||||
<param-name>password</param-name>
|
||||
<param-value>mypass</param-value></context-param>
|
||||
<context-param>
|
||||
<param-name>dbType</param-name>
|
||||
<param-value>H2</param-value></context-param>
|
||||
<listener>
|
||||
<listener-class>meerkat.bulletinboard.webapp.BulletinBoardWebApp</listener-class>
|
||||
</listener>
|
||||
|
|
|
@ -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);
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
Loading…
Reference in New Issue