Reading db_log_jdbc_url from properties

This commit is contained in:
magenoxx 2014-08-27 02:49:16 +04:00
parent 431877b8b1
commit 78c0d76088
3 changed files with 74 additions and 7 deletions

View file

@ -5,12 +5,14 @@ import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils; import com.j256.ormlite.table.TableUtils;
import mage.db.model.Feedback;
import mage.db.model.Log;
import mage.utils.properties.PropertiesUtil;
import java.io.File; import java.io.File;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import mage.db.model.Feedback;
import mage.db.model.Log;
/** /**
* @author noxx, North * @author noxx, North
@ -19,9 +21,6 @@ public enum EntityManager {
instance; instance;
private static final String LOG_JDBC_URL = "jdbc:h2:file:./db/mage.h2;AUTO_SERVER=TRUE";
private static final String FEEDBACK_JDBC_URL = "jdbc:h2:file:./db/feedback.h2;AUTO_SERVER=TRUE";
private Dao<Log, Object> logDao; private Dao<Log, Object> logDao;
private Dao<Feedback, Object> feedbackDao; private Dao<Feedback, Object> feedbackDao;
@ -31,14 +30,15 @@ public enum EntityManager {
file.mkdirs(); file.mkdirs();
} }
try { try {
ConnectionSource logConnectionSource = new JdbcConnectionSource(LOG_JDBC_URL); ConnectionSource logConnectionSource = new JdbcConnectionSource(PropertiesUtil.getDBLogUrl());
TableUtils.createTableIfNotExists(logConnectionSource, Log.class); TableUtils.createTableIfNotExists(logConnectionSource, Log.class);
logDao = DaoManager.createDao(logConnectionSource, Log.class); logDao = DaoManager.createDao(logConnectionSource, Log.class);
ConnectionSource feedbackConnectionSource = new JdbcConnectionSource(FEEDBACK_JDBC_URL); ConnectionSource feedbackConnectionSource = new JdbcConnectionSource(PropertiesUtil.getDBFeedbackUrl());
TableUtils.createTableIfNotExists(feedbackConnectionSource, Feedback.class); TableUtils.createTableIfNotExists(feedbackConnectionSource, Feedback.class);
feedbackDao = DaoManager.createDao(feedbackConnectionSource, Feedback.class); feedbackDao = DaoManager.createDao(feedbackConnectionSource, Feedback.class);
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace();
} }
} }

View file

@ -0,0 +1,57 @@
package mage.utils.properties;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.logger.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author noxx
*/
public class PropertiesUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static final String LOG_JDBC_URL = "jdbc:h2:file:./db/mage.h2;AUTO_SERVER=TRUE";
private static final String FEEDBACK_JDBC_URL = "jdbc:h2:file:./db/feedback.h2;AUTO_SERVER=TRUE";
private static Properties properties = new Properties();
static {
InputStream in = PropertiesUtil.class.getResourceAsStream("/xmage.properties");
if (in != null) {
try {
properties.load(in);
} catch (IOException e) {
logger.error(e, "Couldn't load properties");
}
} else {
logger.warn("No xmage.properties were found on classpath");
}
}
/**
* Hide constructor
*/
private PropertiesUtil() {
}
public static String getDBLogUrl() {
String url = properties.getProperty(PropertyKeys.KEY_DB_LOG_URL, LOG_JDBC_URL);
if (url != null) {
return url.trim();
}
return null;
}
public static String getDBFeedbackUrl() {
String url = properties.getProperty(PropertyKeys.KEY_DB_FEEDBACK_URL, FEEDBACK_JDBC_URL);
if (url != null) {
return url.trim();
}
return null;
}
}

View file

@ -0,0 +1,10 @@
package mage.utils.properties;
/**
* @author noxx
*/
public class PropertyKeys {
public static final String KEY_DB_LOG_URL = "db.log.url";
public static final String KEY_DB_FEEDBACK_URL = "db.feedback.url";
}