Configuration Properties File

Good day,

I would like to create a configuration file where i will store connection to an external application (SAP). I have the connection hard coded in my code but read that its not good idea so i am trying to move it to a configuration properties file.

Below is the connection:

Environment.unregisterDestinationDataProvider(provider);

	this.connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "10.0.0.1");
	this.connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "77");
	this.connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "101");
	this.connectProperties.setProperty(DestinationDataProvider.JCO_USER, "user");
	this.connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "pass");
	this.connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "en");
  1. In which directory in my project do i create the configuration properties file?
  2. How do i create the above mentioned connection configuration in the properties file?
  3. Help me understand how i can call the configuration file into a service i created.

Thank you in advance.

Hi,

CUBA has the following concepts:

  1. Application properties.
    Application Properties - CUBA Platform. Developer’s Manual

  2. Configuration interfaces, which you can use to make your properties type-safe:
    Configuration Interfaces - CUBA Platform. Developer’s Manual

  3. You can have different sets of property values for different environments.
    E.g. with help of Spring profiles: Using Spring Profiles - CUBA Platform. Developer’s Manual

Also you can set value of the configuration interface directly on production server, e.g. with help of environment variables.

If you like your properties to be runtime-modifiable - you can choose to store configuration properties in the database. Then you can use SourceType.DATABASE mode for the configuration interface properties.

Thank you @AlexBudarov for your kind response.

I read the documentation, but couldn’t make full understanding out of it. It would be great if you perhaps give me an example, that is creating a template properties file with dummy data and specifying in which directory of the project folder i can add that file.

Thank you.

If you want to move configuration properties to a separate file stored in the project, you can do the following:

  1. Create configuration interface in Studio, with APP source type.
    Code properties like below:
@Source(type = SourceType.APP)
public interface SapConfig extends Config {

    @Property("sap.jco.ashost")
    @DefaultString("10.0.0.1")
    String getHost();

    @Property("sap.jco.sysnr")
    String getSysnr();
}

You can put development-environment default property values to the @DefaultString annotation. Or you skip this annotation.

  1. You can obtain configuration interface in your code two ways:
    In services, Spring beans and screen controllers - just inject the interface:
@Inject
private SapConfig sapConfig;

In other code, use AppBeans and com.haulmont.cuba.core.global.Configuration global bean:

SapConfig sapConfig = AppBeans.get(Configuration.class).getConfig(SapConfig.class);
  1. Use separate Spring profile to store production values.
    Create “prod-app.properties” file nearby to the app.properties file (com/company/projectname) in the core module.
    Put production values to the file, like below:
sap.jco.ashost = 10.0.0.1
sap.jco.sysnr = 77
  1. Enable “prod” Spring profile on a target server to enable using of the prod-app.properties.
    E.g. if you are using Tomcat server on Windows, then you can add this line to the setenv.bat script:
set CATALINA_OPTS=%CATALINA_OPTS% -Dspring.profiles.active=prod

Another option to pass application property values on the target server is to use environment variables.
E.g. to define such environment variables:

SAP_JCO_ASHOST
SAP_JCO_SYSNR