Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Won't Fix
-
None
-
None
-
None
-
Operating System: other
Platform: Other
-
36397
Description
This patch adds conditional directives to property files similar to C macros:
%if, %elseif, %else, %end.
To illustrate the necessity and the syntax I will take as example a section of
my hibernate configuration file. Switching between databases normally requires
to comment/uncomment a lot of properties, and more importantly, cannot be done
automatically at runtime.
-
- JNDI Datasource
#hibernate.connection.datasource = jdbc/test
#hibernate.connection.username = db2
#hibernate.connection.password = db2
- JNDI Datasource
-
- HypersonicSQL
hibernate.dialect = net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class = org.hsqldb.jdbcDriver
hibernate.connection.username = sa
hibernate.connection.url = jdbc:hsqldb:hsql://localhost
- HypersonicSQL
-
- PostgreSQL
#hibernate.dialect = net.sf.hibernate.dialect.PostgreSQLDialect
#hibernate.connection.driver_class = org.postgresql.Driver
#hibernate.connection.url = jdbc:postgresql:template1
#hibernate.connection.username = pg
#hibernate.connection.password
- PostgreSQL
-
- JTA transactions
#hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JTATransactionFactory
#jta.UserTransaction = jta/usertransaction
#jta.UserTransaction = javax.transaction.UserTransaction
#jta.UserTransaction = UserTransaction
- JTA transactions
-
- JDBC transactions
hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JDBCTransactionFactory
- JDBC transactions
Here is now how it would look like with conditional directives. The only thing
to change now is the property at the top of the file (which could be defined as
a system property if you don't want to touch the file at all). Note that it uses
nested directives.
db = jndi
%if db = jndi
-
- JNDI Datasource
hibernate.connection.datasource = jdbc/test
hibernate.connection.username = db2
hibernate.connection.password = db2
- JNDI Datasource
-
- JTA transactions
hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JTATransactionFactory
jta.UserTransaction = jta/usertransaction
jta.UserTransaction = javax.transaction.UserTransaction
jta.UserTransaction = UserTransaction
- JTA transactions
%else
-
- JDBC transactions
hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JDBCTransactionFactory
- JDBC transactions
%if db = hypersonic
-
- HypersonicSQL
hibernate.dialect = net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class = org.hsqldb.jdbcDriver
hibernate.connection.username = sa
hibernate.connection.url = jdbc:hsqldb:hsql://localhost
- HypersonicSQL
%elseif db = postgres
-
- PostgreSQL
hibernate.dialect = net.sf.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql:template1
hibernate.connection.username = pg
hibernate.connection.password
- PostgreSQL
%end
%end
The directives used are:
%if condition
%elseif condition
%else
%end
The condition can be:
property (true if the property is defined)
property = value (values are interpolated)
property != value
For the implementation, I've replaced in PropertiesConfiguration.java the call
to read a line from the file with a new getNextLine() method which processes
the conditional directives and returns the next property line. The method is
noop unless the line starts with '%', so it should work just fine with files
that do not use the directives.
I've also added the TestPropertiesConfiguration.testConditionalDirectives() unit
test which uses properties added at the end of test.properties.