+

Search Tips   |   Advanced Search

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.

  1. 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>

  2. 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"' />

  3. 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;

    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 profile

Tasks:

  • Use JNDI binding for dynamic values from the server configuration files

    Reference:
    Java Language Specification, Java SE 7 Edition
    Java literals

    Related information:

  • Naming