Uploaded image for project: 'Commons Configuration'
  1. Commons Configuration
  2. CONFIGURATION-181

[configuration] Conditional directives in property files

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • None
    • 1.4
    • 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.

        1. JNDI Datasource
          #hibernate.connection.datasource = jdbc/test
          #hibernate.connection.username = db2
          #hibernate.connection.password = db2
        1. 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
        1. 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
        1. JTA transactions
          #hibernate.transaction.factory_class =
          net.sf.hibernate.transaction.JTATransactionFactory
          #jta.UserTransaction = jta/usertransaction
          #jta.UserTransaction = javax.transaction.UserTransaction
          #jta.UserTransaction = UserTransaction
        1. 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

        1. JNDI Datasource
          hibernate.connection.datasource = jdbc/test
          hibernate.connection.username = db2
          hibernate.connection.password = db2
        1. JTA transactions
          hibernate.transaction.factory_class =
          net.sf.hibernate.transaction.JTATransactionFactory
          jta.UserTransaction = jta/usertransaction
          jta.UserTransaction = javax.transaction.UserTransaction
          jta.UserTransaction = UserTransaction

      %else

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

      %if db = hypersonic

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

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

      Attachments

        Activity

          People

            Unassigned Unassigned
            bcalmac@gmail.com Bogdan Calmac
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: