[Enterprise Extensions only]

Enterprise JavaBeans Java client (contained)

The code sample following illustrates how to utilize the Internationalization context API within a contained Enterprise JavaBeans Java client program.

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

import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

//--------------------------------------------------------------------
// 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 java.util.Locale;
import java.util.SimpleTimeZone;

public class EjbClient {

  public static void main(String args[]) {
    InitialContext initCtx = null;

//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: API references.
//--------------------------------------------------------------------
UserInternationalization       userI18n       = null;
Internationalization           callerI18n     = null;
InvocationInternationalization invocationI18n = null;

// Obtain a reference to the JNDI initial context.
try {
 initCtx= new InitialContext();
    } catch (Exception e) {
// Error resolving the JNDI initial context.
    }
...

//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Resolve API.
//
// Gain access to the Internationalization context API by resolving 
// a reference to the UserInternationalization interface. 
// UserInternationalization is a factory for other interfaces that 
// provide access to different Internationalization context types.
//
// Next, obtain references to the Internationalization and/or
// InvocationInternationalization interfaces. Interface 
// Internationalization provides read access to context elements. 
// Call UserInternationalization method 
// getCallerInternationalization() to obtain a reference for getting 
// CALLER context within all EJB application components. 
// Interface InvocationInternationalization provides read/write 
// access to context elements. Call UserInternationalization method 
// getInvocationInternationalization() to obtain a reference for 
// getting/setting invocation context within EJB Clients, and for 
// getting invocation context within Servlets and EJB 
// implementations.
//--------------------------------------------------------------------
final String UserI18nUrl = 
  "java:comp/websphere/UserInternationalization";
try {
  userI18n = (UserInternationalization)initCtx.lookup(UserI18nUrl);
  callerI18n = userI18n.getCallerInternationalization();
  invocationI18n = userI18n.getInvocationInternationalization();
    } catch (NamingException e) {
  // Error resolving the UserInternationalization object.
    } catch (IllegalStateException e) {
  // Error creating an Internationalization interface reference.
    }
    ...

//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Set invocation locale and time zone.
//
// Under the Client-side Internationalization (CSI) context management
// policy, 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. 
//--------------------------------------------------------------------
invocationI18n.setLocale(new Locale("fr", "FR", ""));
invocationI18n.setTimeZone("ECT");
...

//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Get locale and time zone.
//
// Under CSI, contained EJB client programs may get caller and 
// invocation Internationaliztion 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.
//--------------------------------------------------------------------
Locale         invocationLocale   = 
                       invocationI18n.getLocale();
SimpleTimeZone invocationTimeZone = 
                       (SimpleTimeZone)invocationI18n.getTimeZone();
Locale         callerLocale       = 
                       callerI18n.getLocale();
SimpleTimeZone callerTimeZone     = 
                       (SimpleTimeZone)callerI18n.getTimeZone();
...

  } // main
  ...

} // EjbClient