Configuring the Behavior of SAP Commerce
SAP Commerce has a wide array of configuration parameters that allow specifying runtime settings for various parts of the solution. This includes the database, the application server, and more.
The behavior of SAP Commerce is highly customizable. You can configure it in one of two ways:
-
During runtime, by modifying parameters.
-
Before startup, modifying configuration files.
Parameters changed during runtime affect SAP Commerce behavior immediately. However, changes made in this way are stored in memory only and are volatile. Once you restart the application server, all values are reloaded from the configuration files. Changes are then lost, unless made persistent in a configuration file. To modify the behavior of SAP Commerce during runtime, use the SAP Commerce Administration Console.
Changes made to configuration files are persistent, and are effective after an SAP Commerce restart.
Setting Up Persistent Values in Configuration Files
Defining Properties
A property definition looks like the following:
propertyname=value
For example:
db.url=jdbc:hsqldb:file:<at:var at:name="EXPLODED_EAR" />/mydb;hsqldb.cache_scale=16;shutdown=true db.driver=org.hsqldb.jdbcDriver db.username=sa db.password= db.tableprefix= hsqldb.usecachedtables=false
Defining Comments
A hash (#) at the beginning of a line marks that line as a comment. Comments are ignored when properties are parsed. If a comment line contains a property, that property is ignored and therefore is not effective.
#property.name=value
For example, in the following example the first entry property.activated=true is active, while the second one #property.activated=false is commented out and is therefore not effective:
property.activated=true #property.activated=false
As the parsing mechanism reads in a complete line at once and uses any characters after the equals sign (=) as value for the property, make sure that you avoid two possible sources of problems:
-
Remove all blanks and spaces at the end of lines.
-
Do not add comments to a line that defines a propery value as follows:
this.activate=true # this property is activated
This causes the value for the this.activate property to be read as true # this property is activated. Therefore, be sure to put the comment on an individual line:
this.activate=true # this property is activated
To set a property value that contains a hash, you need to precede the hash with a double backslash, as follows:
myproperty=contains a hash \\#
Configuration Templates
SAP Commerce comes with sets of configuration parameters pre-optimized for development and for production operation, referred to as Configuration Templates. Every configuration template can have different default values.
Where to Define Property Values
There are different places in SAP Commerce where a value can be set for a property. These places have a different priority. If you specify a value in more than one of these places, the value with the highest priority overrides the other values and is therefore effective.
Those places are in order of priority, from high to low:
-
The local.properties file
-
Extension-specific project.properties files
-
The global project.properties file (that is, the project.properties file in the <${HYBRIS_BIN_DIR}/platform> directory)
-
The call to retrieve the value in the application code. To retrieve values in the application code, call the Config class' getString( String propertyName, String defaultValue) method. Via the defaultValue parameter, pass a default value in your application code in case the property has not been set by either the local.properties, an extension-specific project.properties file, or the project.properties file.
|
Place of Definition |
Location |
Priority |
Overrides |
Overridden by |
|---|---|---|---|---|
|
local.properties file |
<${HYBRIS_CONFIG_DIR}> |
Highest |
|
(N/A) |
|
extension-specific project.properties file |
<${EXTENSION_DIR}> |
High |
|
|
|
Platform project.properties file |
<${HYBRIS_BIN_DIR}>/platform |
Medium |
|
|
|
Application code |
Application code When a property is not configured, a default value from the application code is used. |
Low |
(N/A) |
|
For example: if both the project.properties and the local.properties files define websphere.home, it is the setting of the local.properties file that is effective. However, if only the project.properties file specifies a value for a certain property, this setting is effective as the local.properties contains no overriding value.
The following table gives an overview of value definition in different locations and the effective value resulting:
|
local.properties |
Extension-specific project.properties |
Platform project.properties |
Default value passed in application code |
Effective value |
|---|---|---|---|---|
|
ABC |
DEF |
GHI |
JKL |
ABC |
|
(none) |
DEF |
GHI |
JKL |
DEF |
|
(none) |
(none) |
GHI |
JKL |
GHI |
|
(none) |
(none) |
(none) |
JKL |
JKL |
|
ABC |
(none) |
(none) |
JKL |
ABC |
|
(none) |
DEF |
(none) |
null |
DEF |
|
(none) |
(none) |
(none) |
null |
(null) |
Why not to Modify the project.properties File
SAP Commerce provides a set of default property values in the project.properties file. These values are based on project experience.
Technically, you could edit the project.properties file in the <${HYBRIS_BIN_DIR}/platform> directory. However, editing the project.properties file is discouraged for several reasons:
-
Upgrade aspects
Updates or upgrades for SAP Commerce are likely to contain revised versions of the project.properties file that would require you to manually merge the changes or cause the changes to be lost.
-
Clustering aspects
In an SAP Commerce cluster, it is recommended that you assign an individual number to each cluster node. Assigning a cluster node requires setting a property. Across a cluster of five machines, you would have five instances of the project.properties file that would only differ in one single entry: the cluster node number.
-
Value Persistence
The values in the project.properties file are subject to change between SAP Commerce versions to reflect project experience. If you override the project.properties file, these values are overwritten.
-
File Access Rights Aspects
SAP Commerce structure puts the project.properties into a directory different from the local.properties directory. Especially on a production system, you may simply have insufficient access rights to modify the project.properties file.
-
Overview Aspects
The project.properties file contains a number of properties. You may find it rather difficult to keep track of modifications in such a large file.
Instead, add all configuration settings to the local.properties file.
Making Property Value Changes Take Effect
The steps required to make configuration property value changes take effect depends on the way SAP Commerce is deployed.
-
The SAP Commerce Server does not require an SAP Commerce build or deployment to use modified configuration property values. During start-up, the SAP Commerce Server reads in all the configuration property values in the current state automatically. It is enough to restart the server for the properties to take effect.
-
On other application servers, you need to re-deploy SAP Commerce for configuration property value modifications to take effect.
Reading Configuration Values
To get a value of a property, you may use a dedicated API in your code. Alternatively, you can get a value of a property by getting the value injected to your Spring-configured object instance. It provides a more easy to test solution and is more loosely coupled.
Accessing by Spring
SAP Commerce properties are provided in the global ApplicationContext. With this, you can inject a specific value by using the usual Spring notation:
<bean class="Foo">
<property name="bar" value="${key_of_property}"/>
</bean>
The properties are provided by PropertyPlaceholderConfigurer with hybrisPropertiesConfigurer id, which gets the hybrisProperties bean injected. This bean fulfills the java.util.Properties class and holds all properties. You can use this bean in other scenarios too, especially if you want to use the SAP Commerce properties in your WebApplicationContext, having the global ApplicationContext as a parent. For this, define:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="hybrisProperties"/> </bean>
Be aware that this mechanism is not tenant aware. You always get the properties of the leading tenant and you do not recognize changes to properties done at runtime. As the configurator is a BeanFactoryPostProcessor, it gets applied once at a startup of a context.
Adding a Custom Property Placeholder Configurer to Your Extension
Let's assume you want to use custom property placeholders in your training extension.
In this example you configure a simple spring bean named testBean with one property value defined using the property placeholder ${custom.property}.
The property placeholder is resolved using CustomPropertyPlaceholderConfigurer bean. In following example, you simply return a "custom.value" string when "custom.property" placeholder is met.
To define the testBean and property placeholder configurer, add the following to training-spring.xml:
training-spring.xml
<bean id="testBean" class="org.training.TestBean">
<property name="value" value="${custom.property}"/>
</bean>
<bean id="customPropertyPlaceholderConfigurer" class="org.training.CustomPropertyPlaceholderConfigurer">
<property name="properties" ref="hybrisProperties"/>
<property name="order" value="1000"/>
</bean>
To define a custom property placeholder configurer, extend Spring's PropertyPlaceholderConfigurer class and override its resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) method like this:
CustomPropertyPlaceholderConfigurer.java
package org.training;
import java.util.Properties;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer
{
@Override
protected String resolvePlaceholder(final String placeholder, final Properties props, final int systemPropertiesMode)
{
if("custom.property".equals(placeholder)){
return "custom.value";
}
return null;
}
}
Accessing by API
To match SAP Commerce configuration property values, you need to be able to retrieve the values by runtime.
On the ServiceLayer, SAP Commerce configuration is reachable using the ConfigurationService. Calling the getConfiguration() method returns a Configuration object. By factory default implementation, the returned object is a HybrisConfiguration object, as shown in the UML diagram.
The following code sample gives a number of examples of how to use the ConfigurationService and the Configuration object.
//fetch the commons configuration instance
Configuration cfg = configurationService.getConfiguration();
//there are various convenience methods
String strgValue = cfg.getString("key");
boolean boolValue = cfg.getBoolean("key");
int intValue = cfg.getInt("key");
BigDecimal bd = cfg.getBigDecimal("key");
//optional default values can be passed as a second parameter
strgValue = cfg.getString("key", "defaultValue");
//listeners can be registered
final ConfigurationListener listener = new ConfigurationListener()
{
public void configurationChanged(final ConfigurationEvent event)
{
//listener gets invoked before and after the value gets changed
boolean isBefore = event.isBeforeUpdate();
Object value = event.getPropertyValue();
String key = event.getPropertyName();
}
};
((HybrisConfiguration)cfg).addConfigurationListener(listener);
The property value getter methods optionally allow passing a second parameter such as defaultValue in the code sample above. This parameter value is the default value used for the case that the configuration property does not have a value.
Tips on Configuring SAP Commerce
-
Changes in property values have a global effect. If you specify erroneous or invalid property values, parts of SAP Commerce may fail to function properly in part or completely. In other words: only change property values if you know what the values will do.
-
A common practice to override property values is to copy individual properties from the project.properties and to paste them into the local.properties file with an edited value.
-
Default property values are subject to change between SAP Commerce versions. Therefore, be sure to explicitly specify any property value you want to be effective in the local.properties file. That way, property values you define rank highest in the priority hierarchy and therefore override any other property value setting.
-
You could create a full copy of the project.properties file and use this copy as the local.properties file. However, there are good reasons to not do this and to keep the local.properties file as small as possible:
-
Ease of updates
For example, project.properties values might change between updates to reflect project experience. You would not profit from such updated values if your local.properties file is a full-scale copy and effectively overrides every single property.
-
Ease of structure
If your local.properties only consists of five or six settings, it is easier to understand what those settings do. Not only for you, but possibly for other administrators or SAP Support.
-
Specifying Property Values at Start-Up
When starting a Java Virtual Machine, you can pass runtime parameters to the Virtual Machine as command line options to the call of the Java application launcher java. Please refer to the documentation by Sun Microsystems for a list of command line switches ( Windows
-related version and Linux / Solaris
-related version of java). The Java Virtual Machine allows specifying system properties via the -D command line switch, such as
java -Dfoo="some string" SomeClass
Via the -D command line switch, you can specify property values that override property values defined in the local.properties and project.properties files. In essence, you can specify a runtime configuration for the SAP Commerce. For example, you may want to use a database different from the database specified in the local.properties file for testing purposes.
You can use this function whenever you can specify parameters for the Java Virtual Machine, for example:
-
When you run Junit tests from Eclipse
-
When you run SAP Commerce in stand-alone mode.
This does not work with the SAP Commerce Server as the server is launched indirectly using a start-up file and you do not have access to the call of the Java Virtual Machine.
Example: The VM arguments field contains connection parameters for a MySQL database: -Ddb.url=jdbc:mysql://localhost/trunk?useConfigs=maxPerformance&characterEncoding=utf8 -Ddb.driver=com.mysql.jdbc.Driver -Ddb.username=root -Ddb.password=root.
Figure: The VM arguments text box in Eclipse displays the example.
This is the equivalent to adding the following lines to the local.properties file, but the VM parameters are runtime only:
db.url=jdbc:mysql://localhost/trunk?useConfigs=maxPerformance&characterEncoding=utf8 db.driver=com.mysql.jdbc.Driver db.username=root db.password=root
Using Environment Variables instead of Files for Secure Settings
Some settings may not be sufficiently safe when placed inside the mentioned properties files. For example, all settings containing passwords, or other secrets necessary to access external resources or remote systems could be attacked by reading the *.properties directly from the server file system.
Example - Securing the database account and connection
# security: make the platform *abort startup* if no one overrides the settings below db.url=<CHANGE_ME> db.username=<CHANGE_ME> db.password=<CHANGE_ME> # security: end
The special property value <CHANGE_ME> has been introduced exactly for such use cases where administrators want to ensure that: a) files do not contain sensible settings, and b) the system doesn't start up if those settings haven't been overridden.
foo$ export y_db_url=jdbc:mysql://my.secret-db-host.com/AlfavaMetraxis?useConfigs=maxPerformance&characterEncoding=utf8 foo$ export y_db_username=Amy foo$ export y_db_password=Rory foo$ ./hybrisserver.sh start ...
|
Property |
Environment Variable |
Explanation |
|---|---|---|
|
<someSettingWithoutDot> |
<y_someSettingWithoutDot> |
Always add the required prefix (it can be configured via <env.properties.prefix>). |
|
<some.setting.with.dots> |
<y_some_setting_with_dots> |
Dots must be changed into underlines. |
|
<special.setting.with_underline.and.dots> |
<y_special_setting_with__underline_and_dots> |
Properties including underlines must double those underlines to work. |