Use JNDI binding for constants from the server configuration files
We can bind constants into the default Java Naming and Directory Interface (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.
For more information about the JNDI naming, see Naming.
- Add a constant into the default JNDI namespace by specifying the jndi-1.0 Liberty feature in server.xml of the Liberty profile server.
<featureManager> <feature>jndi-1.0</feature> </featureManager>
- Bind constants into the JNDI namespace by specifying the <jndiEntry> elements with jndiName and value attributes in server.xml.
<jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value='"plato"' /> <jndiEntry jndiName="schoolOfAthens/defaultAdminPassword" value='"republic"' />
- Look up the constants from an application using a JNDI context with 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 Java Language Specification, Java SE 7 Edition, section 3.10. String and character literals might contain unicode escaped sequences (see section 3.3: Unicode Escaped Sequences ), and the octal and character escape sequences ( see section 3.10.6: Escape Sequences for Character and String Literals). Null literals and class literals are not supported; for more information see section 3.10.7: The null literal and section 15.8.2: Class Literals .
See the following examples of Java literals:
- The string "Hello, world" followed by a newline character:
- The integer with a binary value 1010101:
- The single character 'X':
- The double-precision floating point number 1.0:
For more information about <jndiEntry> element, see Configuration elements in server.xml.
Parent topic: Deploy applications to the Liberty profileTasks:
Use JNDI binding for dynamic values from the server configuration files Reference:
Java Language Specification, Java SE 7 Edition
Java literalsRelated information:
Naming