WAS v8.5 > Deploy applications > Deploy applications to the Liberty profileUse JNDI binding for constants from the server configuration files
We can bind constants into the default JNDI namespace from the server configuration files using the jndiEntry element on the Liberty profile.
The default JNDI namespace is available in the Liberty profile to provide bindings to miscellaneous objects required by applications. Any data sources declared in the server configuration files are available in the default JNDI namespace. Additionally, we can bind Java™ strings and primitive data types in the configuration file into JNDI namespace. These constants are then made available to an application at run time, providing a simple and portable way to pass configuration values into the application.
See Naming for more information about JNDI.
The following steps show how to bind the constants and use them in the application.
- To add a constant into the default JNDI namespace, the jndi-1.0 Liberty feature must be specified in server.xml of the Liberty profile server.
< featureManager> <feature>jndi-1.0</feature> </featureManager>- To bind constants into the JNDI namespace, add jndiEntry elements into server.xml by specifying the jndiName and value attributes.
- To look up the constants from an application using a JNDI context, use the following code:
Object jndiConstant = new InitialContext().lookup("schoolOfAthens/defaultAdminUserName"); String defaultAdmin = (String) jndiConstant;
- The lookup() method returns an object to the application. The type of the object is determined by interpreting the value stored in the jndiEntry element as a Java literal string or primitive data type. If the parsing fails, the exact value is provided as an unmodified string.
- The jndiEntry element supports the integer, floating-point, boolean, character, and string literals as described in the Java Language Specification, Java SE 7 Edition, section 3.10. String and character literals might contain unicode escaped sequences (section 3.3), and the octal and character escape sequences (section 3.10.6). The null literal (section 3.10.7) and class literals (section 15.8.2) are not supported.
See the following examples of Java literals:
- The string "Hello, world" followed by a newline:
< jndiEntry jndiName="a" value='"Hello, world.\n"' />
- The integer with a binary value 1010101:
< jndiEntry jndiName="b" value="0b1010101" />
- The single character 'X':
< jndiEntry jndiName="c" value="'X'" />
- The double-precision floating point number 1.0:
< jndiEntry jndiName="d" value="1.0D" />
See Liberty profile: Configuration elements in server.xml for more information about jndiEntry element.
Parent topic: Deploy applications to the Liberty profile
Reference:
Java Language Specification, Java SE 7 Edition
Related information:
Naming
|