Example: Internationalization context in an EJB client program

The following code example illustrates how to use the internationalization context API within a contained EJB client program or Web service client program.

//------------------------------------------
// Basic Example: J2EE EJB client.
//------------------------------------------
package examples.basic;

//------------------------------------------
// INTERNATIONALIZATION SERVICE: Imports.
//------------------------------------------
import com.ibm.websphere.i18n.context.UserInternationalization;
import com.ibm.websphere.i18n.context.Internationalization;
import com.ibm.websphere.i18n.context.InvocationInternationalization;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.util.Locale;
import java.util.SimpleTimeZone;

public class EjbClient {

  public static void main(String args[]) {

    //--------------------------------------------------
    // INTERNATIONALIZATION SERVICE: API references.
    //--------------------------------------------------
    UserInternationalization userI18n = null;
    Internationalization callerI18n = null;
    InvocationInternationalization invocationI18n = null;
   
    //--------------------------------------------------
    // INTERNATIONALIZATION SERVICE: JNDI name. 
    //--------------------------------------------------
    final String UserI18NUrl =
        "java:comp/websphere/UserInternationalization";

    //--------------------------------------------------
    // INTERNATIONALIZATION SERVICE: Resolve the API.
    //--------------------------------------------------
    try {
     Context initialContext = new InitialContext();    
     userI18n = (UserInternationalization)initialContext.lookup(
         UserI18NUrl);
     callerI18n = userI18n.getCallerInternationalization();
     invI18n = userI18n.getInvocationInternationalization ();
    } catch (NamingException ne) {
      log("Error: Cannot resolve UserInternationalization: Exception: " + ne);
    } catch (IllegalStateException ise) {
      log("Error: UserInternationalization is not available: " + ise);
    }
    ...

    //--------------------------------------------------------------------
    // INTERNATIONALIZATION SERVICE: Set invocation context.
    //
    // Under Application-managed Internationalization (AMI), contained EJB 
    // client programs may set invocation context elements. The following 
    // statements associate the supplied invocation locale and time zone 
    // with the current thread. Subsequent remote bean method calls will 
    // propagate these context elements.
    //--------------------------------------------------------------------
    try {
      invocationI18n.setLocale(new Locale("fr", "FR", ""));
      invocationI18n.setTimeZone("ECT");
    } catch (IllegalStateException ise) {
      log("An anomaly occurred accessing Invocation context: " + ise );
    }
    ...

    //--------------------------------------------------------------------
    // INTERNATIONALIZATION SERVICE: Get locale and time zone.
    //
    // Under AMI, contained EJB client programs can get caller and
    // invocation context elements associated with the current thread. 
    // The next four statements return the invocation locale and time zone 
    // associated above, and the caller locale and time zone associated 
    // internally by the service. Getting a caller context element within 
    // a contained client results in the default element of the JVM.
    //--------------------------------------------------------------------
    Locale invocationLocale = null;
    SimpleTimeZone invocationTimeZone = null;
    Locale callerLocale = null;
    SimpleTimeZone callerTimeZone = null;
    try {
      invocationLocale = invocationI18n.getLocale();
      invocationTimeZone =                           
          (SimpleTimeZone)invocationI18n.getTimeZone();
      callerLocale = callerI18n.getLocale();
      callerTimeZone = SimpleTimeZone)callerI18n.getTimeZone();
    } catch (IllegalStateException ise) {
      log("An anomaly occurred accessing I18n context: " + ise );
    }

    ...
  } // main

  ...
  void log(String s) {
    System.out.println (((s == null) ? "null" : s));
  }
} // EjbClient


 

Related Tasks


Using the internationalization context API