项目作者: d-shap

项目描述 :
RuCon is the application configuration helper
高级语言: Java
项目地址: git://github.com/d-shap/rucon.git
创建时间: 2020-01-08T09:48:16Z
项目社区:https://github.com/d-shap/rucon

开源协议:

下载


RuCon

RuCon is the application configuration helper.

Application configuration can be defined in the properties file, as the system properties (specified as JVM -D arguments), as the system environment variables.
Configuration can be defined in the deployment descriptors (for example, web.xml, ejb-jar.xml).
Configuration can be defined in the framework-specific configuration files (for example, Spring’s applicationContext.xml).

Also, the possibility to override configuration is mandatory.

RuCon is intended to deal with these problems.

For example, application uses JDBC driver.
To connect to the database, some configuration is needed.
This configuration is defined in the jdbc.properties file, that is located in the classpath.
Application is packaged as jar, and the jdbc.properties file is in this jar.

The content of the jdbc.properties file is the following:

  1. # Database name
  2. jdbc.database=tempbase
  3. # Login info
  4. jdbc.user=tempuser
  5. jdbc.pass=tempsecret

The following code reads this configuration:

  1. ConfigurationBuilder configurationBuilder = ConfigurationBuilder.newInstance();
  2. configurationBuilder.addPropertiesResourceLoader("jdbc.properties");
  3. Configuration configuration = configurationBuilder.buildAndLoad();
  4. String database = configuration.getPropertyAsString("jdbc.database", "undefined");
  5. // database == "tempbase"
  6. String username = configuration.getPropertyAsString("jdbc.user", "undefined");
  7. // username == "tempuser"
  8. String password = configuration.getPropertyAsString("jdbc.pass", "undefined");
  9. // password == "tempsecret"

This application is deployed to different environments.
Database configuration may not be the same in each environment.
And to adjust the application configuration to the environment the jdbc.properties file in the jar should be updated.

To avoid this, configuration can be defined in the properties file, located somewhere in the environment.
If this file exists, then configuration is obtained from this file.
If this file does not exist, then configuration is obtained from the jdbc.properties file.

The content of the /somepath/externalJdbc.properties file:

  1. # Login info
  2. jdbc.user=realuser
  3. jdbc.pass=realsecret

The path to this file is specified as the JVM argument -Djdbc.config.file.location=/somepath/externalJdbc.properties.

The following code reads configuration with the external file:

  1. ConfigurationBuilder configurationBuilder = ConfigurationBuilder.newInstance();
  2. configurationBuilder.addPropertiesSystemPropertyFileLoader("jdbc.config.file.location");
  3. configurationBuilder.addPropertiesResourceLoader("jdbc.properties");
  4. Configuration configuration = configurationBuilder.buildAndLoad();
  5. String database = configuration.getPropertyAsString("jdbc.database", "undefined");
  6. // database == "tempbase"
  7. String username = configuration.getPropertyAsString("jdbc.user", "undefined");
  8. // username == "realuser"
  9. String password = configuration.getPropertyAsString("jdbc.pass", "undefined");
  10. // password == "realsecret"

Also, this configuration can be adjusted with the JVM arguments.
The database name can be specified as the JVM argument -Dorg.mycompany.myproject.jdbc.database=realbase

The following code reads configuration with the JVM arguments:

  1. ConfigurationBuilder configurationBuilder = ConfigurationBuilder.newInstance();
  2. configurationBuilder.addSystemPropertiesLoader("org.mycompany.myproject.");
  3. configurationBuilder.addPropertiesSystemPropertyFileLoader("jdbc.config.file.location");
  4. configurationBuilder.addPropertiesResourceLoader("jdbc.properties");
  5. Configuration configuration = configurationBuilder.buildAndLoad();
  6. String database = configuration.getPropertyAsString("jdbc.database", "undefined");
  7. // database == "realbase"
  8. String username = configuration.getPropertyAsString("jdbc.user", "undefined");
  9. // username == "realuser"
  10. String password = configuration.getPropertyAsString("jdbc.pass", "undefined");
  11. // password == "realsecret"

This final code looks first looks for the configuration property in the JVM arguments that starts with org.mycompany.myproject..
If the configuration property is not defined, then the code looks for the configuration property in the /somepath/externalJdbc.properties external file.
Finally, if the configuration property is not defined in the external file, then the code looks for the configuration property in the jdbc.properties file in the classpath.