DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=36397>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=36397

           Summary: PATCH: conditional directives in property files
                    (commons-configuration)
           Product: Commons
           Version: unspecified
          Platform: Other
        OS/Version: other
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Configuration
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: [EMAIL PROTECTED]


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


## 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


## 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

## JTA transactions
#hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JTATransactionFactory
#jta.UserTransaction = jta/usertransaction
#jta.UserTransaction = javax.transaction.UserTransaction
#jta.UserTransaction = UserTransaction

## JDBC transactions
hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JDBCTransactionFactory



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

## JTA transactions
hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JTATransactionFactory
jta.UserTransaction = jta/usertransaction
jta.UserTransaction = javax.transaction.UserTransaction
jta.UserTransaction = UserTransaction

%else

## JDBC transactions
hibernate.transaction.factory_class =
net.sf.hibernate.transaction.JDBCTransactionFactory

%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

%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

%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.

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to