WAS v8.5 > Deploy applications > Deploy applications to the Liberty profile

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

  1. 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>
  2. To bind constants into the JNDI namespace, add jndiEntry elements into server.xml by specifying the jndiName and value attributes.

      < jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value='"plato"' /> < jndiEntry jndiName="schoolOfAthens/defaultAdminPassword" value='"republic"' />
  3. 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;

    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':

    • The double-precision floating point number 1.0:

    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

Java literals


Related information:

Naming


|