diff --git a/Mage.Common/src/mage/db/EntityManager.java b/Mage.Common/src/mage/db/EntityManager.java index 335647ab10a..aea98be0c05 100644 --- a/Mage.Common/src/mage/db/EntityManager.java +++ b/Mage.Common/src/mage/db/EntityManager.java @@ -5,12 +5,14 @@ import com.j256.ormlite.dao.DaoManager; import com.j256.ormlite.jdbc.JdbcConnectionSource; import com.j256.ormlite.support.ConnectionSource; 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.sql.SQLException; import java.util.ArrayList; import java.util.List; -import mage.db.model.Feedback; -import mage.db.model.Log; /** * @author noxx, North @@ -19,9 +21,6 @@ public enum EntityManager { 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 logDao; private Dao feedbackDao; @@ -31,14 +30,15 @@ public enum EntityManager { file.mkdirs(); } try { - ConnectionSource logConnectionSource = new JdbcConnectionSource(LOG_JDBC_URL); + ConnectionSource logConnectionSource = new JdbcConnectionSource(PropertiesUtil.getDBLogUrl()); TableUtils.createTableIfNotExists(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); feedbackDao = DaoManager.createDao(feedbackConnectionSource, Feedback.class); } catch (SQLException ex) { + ex.printStackTrace(); } } diff --git a/Mage.Common/src/mage/utils/properties/PropertiesUtil.java b/Mage.Common/src/mage/utils/properties/PropertiesUtil.java new file mode 100644 index 00000000000..50d1bfdbb1f --- /dev/null +++ b/Mage.Common/src/mage/utils/properties/PropertiesUtil.java @@ -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; + } +} diff --git a/Mage.Common/src/mage/utils/properties/PropertyKeys.java b/Mage.Common/src/mage/utils/properties/PropertyKeys.java new file mode 100644 index 00000000000..01802e5489c --- /dev/null +++ b/Mage.Common/src/mage/utils/properties/PropertyKeys.java @@ -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"; +}