Skip to content

Commit 631ba70

Browse files
committed
add support form multiple DBs
DB constructor now accepts a modifier that allows it to use an alternate configuration.
1 parent 4020d67 commit 631ba70

File tree

1 file changed

+57
-10
lines changed
  • modules/dbsupport/src/main/java/org/jpos/ee

1 file changed

+57
-10
lines changed

modules/dbsupport/src/main/java/org/jpos/ee/DB.java

+57-10
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ public class DB implements Closeable
5959
{
6060
Session session;
6161
Log log;
62+
String configModifier;
6263

6364
private static volatile SessionFactory sessionFactory = null;
6465
private static String propFile;
66+
private static final String MODULES_CONFIG_PATH = "META-INF/org/jpos/ee/modules/";
6567

6668
/**
6769
* Creates DB Object using default Hibernate instance
@@ -71,6 +73,27 @@ public DB()
7173
super();
7274
}
7375

76+
/**
77+
* Creates DB Object using a config <i>modifier</i>.
78+
*
79+
* The <i>modifier</i> can be either a prefix used to locate the <code>cfg/db.properties</code> file.
80+
* i.e.: "mysql" used as modifier would pick the configuration from <code>cfg/mysql-db.properties</code>
81+
* instead of the default <code>cfg/db.properties</code>
82+
*
83+
* If a colon and a second modifier is present ("mysql:v1") the metadata is picket from
84+
* <code>META-INF/org/jpos/ee/modules/v1-*</code> instead of just
85+
* <code>META-INF/org/jpos/ee/modules/</code>.
86+
*
87+
* Finally, if the modifier ends with <code>.hbm.xml</code> (case insensitive), then all configuration
88+
* is picked from that config file.
89+
*
90+
* @param configModifier modifier
91+
*/
92+
public DB (String configModifier) {
93+
super();
94+
this.configModifier = configModifier;
95+
}
96+
7497
/**
7598
* Creates DB Object using default Hibernate instance
7699
*
@@ -82,6 +105,16 @@ public DB(Log log)
82105
setLog(log);
83106
}
84107

108+
/**
109+
* Creates DB Object using default Hibernate instance
110+
*
111+
* @param log Log object
112+
*/
113+
public DB(Log log, String configModifier) {
114+
this(configModifier);
115+
setLog(log);
116+
}
117+
85118
/**
86119
* @return Hibernate's session factory
87120
*/
@@ -113,13 +146,6 @@ public static synchronized void invalidateSessionFactory()
113146
}
114147

115148
private SessionFactory newSessionFactory() throws IOException, ConfigurationException, DocumentException {
116-
String hibCfg = System.getProperty("HIBERNATE_CFG","/hibernate.cfg.xml");
117-
if (hibCfg != null) {
118-
Configuration configuration = new Configuration().configure(hibCfg);
119-
configureProperties(configuration);
120-
configureMappings(configuration);
121-
return configuration.buildSessionFactory();
122-
}
123149
return getMetadata().buildSessionFactory();
124150
}
125151

@@ -416,17 +442,38 @@ public void printStats()
416442

417443
private MetadataImplementor getMetadata() throws IOException, ConfigurationException, DocumentException {
418444
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
419-
String propFile = System.getProperty("DB_PROPERTIES", "cfg/db.properties");
445+
String propFile;
446+
String dbPropertiesPrefix = "";
447+
String metadataPrefix = "";
448+
if (configModifier != null) {
449+
String[] ss = configModifier.split(":");
450+
if (ss.length > 0)
451+
dbPropertiesPrefix = ss[0] + "-";
452+
if (ss.length > 1)
453+
metadataPrefix = ss[1] + "-";
454+
}
455+
456+
String hibCfg = System.getProperty("HIBERNATE_CFG","/" + dbPropertiesPrefix + "hibernate.cfg.xml");
457+
if (getClass().getClassLoader().getResource(hibCfg) == null)
458+
hibCfg = null;
459+
460+
if (hibCfg == null)
461+
hibCfg = System.getProperty("HIBERNATE_CFG","/hibernate.cfg.xml");
462+
ssrb.configure(hibCfg);
463+
464+
propFile = System.getProperty(dbPropertiesPrefix + "DB_PROPERTIES", "cfg/" + dbPropertiesPrefix + "db.properties");
420465
Properties dbProps = loadProperties(propFile);
421466
if (dbProps != null) {
422467
for (Map.Entry entry : dbProps.entrySet()) {
423468
ssrb.applySetting((String) entry.getKey(), entry.getValue());
424469
}
425470
}
426471
MetadataSources mds = new MetadataSources(ssrb.build());
427-
List<String> moduleConfigs = ModuleUtils.getModuleEntries("META-INF/org/jpos/ee/modules/");
472+
List<String> moduleConfigs = ModuleUtils.getModuleEntries(MODULES_CONFIG_PATH);
428473
for (String moduleConfig : moduleConfigs) {
429-
addMappings(mds, moduleConfig);
474+
if (metadataPrefix.length() == 0 || moduleConfig.substring(MODULES_CONFIG_PATH.length()).startsWith(metadataPrefix)) {
475+
addMappings(mds, moduleConfig);
476+
}
430477
}
431478
return (MetadataImplementor) mds.buildMetadata();
432479
}

0 commit comments

Comments
 (0)