Gaining access to the internationalization context API

 

Overview

This topic describes how to access the internationalization service by resolving a reference to the internationalization context API.

Resolve internationalization context API references once over the life cycle of an application component, within the initialization method of that component (for example, within the init method of servlets, or within the SetXxxContext method of enterprise beans). For Web service client programs, resolve a reference to the internationalization context API during initialization. For stateless session beans enabled for Web services, resolve the reference in the setSessionContext method.

 

Procedure

  1. Resolve a reference to the UserInternationalization interface by performing a lookup on the Java Naming and Directory Interface (JNDI) name java:comp/websphere/UserInternationalization. For example

    //--------------------------------------------------------------------
    // Internationalization context imports. 
    //--------------------------------------------------------------------
    import com.ibm.websphere.i18n.context.*;
    import javax.naming.*;
    ...
    
    public class MyApplication {
      ...
    
      //--------------------------------------------------------------------
      // Resolve a reference to the UserInternationalization interface.
      //--------------------------------------------------------------------
      InitialContext initCtx = null;
      UserInternationalization userI18n = null;
      final String UserI18nUrl = "java:comp/websphere/UserInternationalization"; 
      try {
        initCtx = new InitialContext();
        userI18n = (UserInternationalization)initCtx.lookup(UserI18nUrl);
      }
      catch (NamingException ne) {
        // UserInternationalization URL is unavailable.
      }
    
    

    If the UserInternationalization object is unavailable because of an anomaly or a restriction, the JNDI lookup invocation issues a javax.naming.NameNotFoundException exception that contains the java.lang.IllegalStateException instance.

  2. Use the UserInternationalization reference to create references to the CallerInternationalization or InvocationInternationalization objects, which provide access to elements of the Caller or Invocation internationalization contexts, respectively. The CallerInternationalization reference can be bound to the Internationalization interface only; the InvocationInternationalization reference can be bound to either the Internationalization or the InvocationInternationalization interfaces, depending on whether the application requires read-only or read-write access to the invocation context. For example

      ...
      //--------------------------------------------------------------------
      // Resolve references to the Internationalization and
      // InvocationInternationalization interfaces.
      //--------------------------------------------------------------------
      Internationalization callerI18n = null;
      InvocationInternationalization invocationI18n = null;
      try {
        callerI18n = userI18n.getCallerInternationalization();
        invocationI18n = userI18n.getInvocationInternationalization();
      }
      catch (IllegalStateException ise) {
        // An Internationalization interface(s) is unavailable.
      }
    
    


 

See Also


Internationalization context

 

Related Tasks


Accessing caller locales and time zones
Accessing invocation locales and time zones

 

See Also


Internationalization context API: Programming reference
Example: Internationalization context in an EJB client program
Example: Internationalization context in a servlet
Example: Internationalization context in a session bean