Skip to content

Commit

Permalink
Honor hibernate.cfg.xml when provided
Browse files Browse the repository at this point in the history
closes #135
  • Loading branch information
ar committed Feb 27, 2020
1 parent 234721b commit 9ec7493
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions modules/dbsupport/src/main/java/org/jpos/ee/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public DB() {
* <code>META-INF/org/jpos/ee/modules/v1-*</code> instead of just
* <code>META-INF/org/jpos/ee/modules/</code> </li>
*
* <li>finally, if the modifier ends with <code>.hbm.xml</code> (case insensitive), then all configuration
* <li>finally, if the modifier ends with <code>.cfg.xml</code> (case insensitive), then all configuration
* is picked from that config file.</li>
* </ul>
*
Expand Down Expand Up @@ -520,9 +520,10 @@ private Metadata getMetadata() throws IOException, ConfigurationException, Docum
String propFile;
String dbPropertiesPrefix = "";
String metadataPrefix = "";
boolean standardHibernateConfig = cm.endsWith(".cfg.xml");

String hibCfg = null;
if (cm.endsWith(".cfg.xml")) {
if (standardHibernateConfig) {
hibCfg = cm;
} else if (configModifier != null) {
String[] ss = configModifier.split(":");
Expand All @@ -539,26 +540,28 @@ private Metadata getMetadata() throws IOException, ConfigurationException, Docum
hibCfg = System.getProperty("HIBERNATE_CFG","/hibernate.cfg.xml");

ssrb.configure(hibCfg);
propFile = System.getProperty(dbPropertiesPrefix + "DB_PROPERTIES", "cfg/" + dbPropertiesPrefix + "db.properties");
Properties dbProps = loadProperties(propFile);
for (Map.Entry entry : dbProps.entrySet()) {
String k = (String) entry.getKey();
String v = Environment.get((String) entry.getValue());
ssrb.applySetting(k,v);
dbProps.setProperty(k,v);
}

// if DBInstantiator has put db user name and/or password in Space, set Hibernate config accordingly
Space sp = SpaceFactory.getSpace("tspace:dbconfig");
String user = (String) sp.inp(dbPropertiesPrefix +"connection.username");
String pass = (String) sp.inp(dbPropertiesPrefix +"connection.password");
if (user != null)
ssrb.applySetting("hibernate.connection.username", user);
if (pass != null)
ssrb.applySetting("hibernate.connection.password", pass);

Properties dbProps = null;
if (!standardHibernateConfig) {
propFile = System.getProperty(dbPropertiesPrefix + "DB_PROPERTIES", "cfg/" + dbPropertiesPrefix + "db.properties");
dbProps = loadProperties(propFile);
for (Map.Entry entry : dbProps.entrySet()) {
String k = (String) entry.getKey();
String v = Environment.get((String) entry.getValue());
ssrb.applySetting(k,v);
dbProps.setProperty(k,v);
}

// if DBInstantiator has put db user name and/or password in Space, set Hibernate config accordingly
Space sp = SpaceFactory.getSpace("tspace:dbconfig");
String user = (String) sp.inp(dbPropertiesPrefix +"connection.username");
String pass = (String) sp.inp(dbPropertiesPrefix +"connection.password");
if (user != null)
ssrb.applySetting("hibernate.connection.username", user);
if (pass != null)
ssrb.applySetting("hibernate.connection.password", pass);
}
MetadataSources mds = new MetadataSources(ssrb.build());

List<String> moduleConfigs = ModuleUtils.getModuleEntries(MODULES_CONFIG_PATH);
for (String moduleConfig : moduleConfigs) {
if (metadataPrefix.length() == 0 || moduleConfig.substring(MODULES_CONFIG_PATH.length()).startsWith(metadataPrefix)) {
Expand All @@ -570,7 +573,8 @@ private Metadata getMetadata() throws IOException, ConfigurationException, Docum
}
md = mds.buildMetadata();
metadatas.put(cm, md);
properties.put(cm, dbProps);
if (dbProps != null)
properties.put(cm, dbProps);
}
} finally {
mdSem.release();
Expand Down

0 comments on commit 9ec7493

Please sign in to comment.