commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 36397] New: - PATCH: conditional directives in property files (commons-configuration)
Date Sun, 28 Aug 2005 02:21:51 GMT
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: bcalmac@gmail.com


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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message