@@ -59,9 +59,11 @@ public class DB implements Closeable
59
59
{
60
60
Session session ;
61
61
Log log ;
62
+ String configModifier ;
62
63
63
64
private static volatile SessionFactory sessionFactory = null ;
64
65
private static String propFile ;
66
+ private static final String MODULES_CONFIG_PATH = "META-INF/org/jpos/ee/modules/" ;
65
67
66
68
/**
67
69
* Creates DB Object using default Hibernate instance
@@ -71,6 +73,27 @@ public DB()
71
73
super ();
72
74
}
73
75
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
+
74
97
/**
75
98
* Creates DB Object using default Hibernate instance
76
99
*
@@ -82,6 +105,16 @@ public DB(Log log)
82
105
setLog (log );
83
106
}
84
107
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
+
85
118
/**
86
119
* @return Hibernate's session factory
87
120
*/
@@ -113,13 +146,6 @@ public static synchronized void invalidateSessionFactory()
113
146
}
114
147
115
148
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
- }
123
149
return getMetadata ().buildSessionFactory ();
124
150
}
125
151
@@ -416,17 +442,38 @@ public void printStats()
416
442
417
443
private MetadataImplementor getMetadata () throws IOException , ConfigurationException , DocumentException {
418
444
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" );
420
465
Properties dbProps = loadProperties (propFile );
421
466
if (dbProps != null ) {
422
467
for (Map .Entry entry : dbProps .entrySet ()) {
423
468
ssrb .applySetting ((String ) entry .getKey (), entry .getValue ());
424
469
}
425
470
}
426
471
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 );
428
473
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
+ }
430
477
}
431
478
return (MetadataImplementor ) mds .buildMetadata ();
432
479
}
0 commit comments