[Enterprise Extensions only]

Enterprise JavaBeans session bean

The code snippet following suggests how to perform a localized operation using the Internationalization Service within an Enterprise JavaBeans session bean. Note the comments within the ejbCreate() and getExchangeRate() methods.

...

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

final String UserI18nUrl =
                   "java:comp/websphere/UserInternationalization";
...

/**
 * EJB Activate method.
 * @exception java.rmi.RemoteException <EJB Method Requirement>
 */
public void ejbActivate() throws java.rmi.RemoteException {}

/**
 * EJB Create method
 *  <p>Resolve the Internationalization context API.
 * @exception javax.ejb.CreateException whenever this bean cannot
 *  be instantiated.
 * @exception java.rmi.RemoteException <EJB Method Requirement>
 */
public void ejbCreate() 
  throws javax.ejb.CreateException, java.rmi.RemoteException {

  // Resolve the JNDI initial context.
  try {
    Properties properties = new Properties();
    properties.put("java.naming.provider.url", "iiop:///");
    initialContext = new InitialContext(properties);
  } catch(NamingException ne) {
    System.out.println("Cannot instantiate JNDI 
                        initial context: " + ne);
  }

//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Resolve API.
//
// Under the Server-side Internationalization (SSI) context 
// management policy, EJBs have read-only access to invocation 
// context elements. Attempts to set these elements result in an 
// IllegalStateException. And because EJBs “RunAsCaller” 
// under SSI, the invocation and caller contexts are identical. 
// This example resolves only a reference to the 
// InvocationInternationalization interface for getting caller 
// context elements.
// 
// Suggestion: cache all Internationalization context API 
// references once, during instantiation, and use them 
// throughout the EJB lifecycle.
//--------------------------------------------------------------------
  try {
    userI18n = 
        (UserInternationalization)initialContext.lookup(UserI18nUrl);
    invocationI18n = userI18n.getInvocationInternationalization();
  } catch (NamingException ne) {
    System.out.println ("Cannot resolve 
                         UserInternationalization: " + nnfe);
  } catch (IIlegalStateException ise) {
    System.out.println ("Cannot resolve 
                         InvocationInternationalization: " + ise);
  }
}
/**
 * ejbPassivate method.
 * @exception java.rmi.RemoteException <EJB Method Requirement>
 */
public void ejbPassivate() throws java.rmi.RemoteException {}

/**
 * ejbRemove method.
 * @exception java.rmi.RemoteException <EJB Method Requirement>
 */
public void ejbRemove() throws java.rmi.RemoteException {}

/**
 * getSessionContext method.
 * @return javax.ejb.SessionContext
 */
public javax.ejb.SessionContext getSessionContext() {
  return mySessionCtx;
}

/**
 * setSessionContext method.
 * @param ctx the supplied EJB session context.
 * @exception java.rmi.RemoteException <EJB Method Requirement>.   
 */ 
public void setSessionContext(javax.ejb.SessionContext ctx) 
  throws java.rmi.RemoteException {
  mySessionCtx = ctx;
}
...

/**
 * Business method.
 * @return the exchange rate associated with the invocation locale
 *  under which this method is executing.
 * @exception java.rmi.RemoteException <EJB Method Requirement>
 */
public getExchangeRate() throws java.rmi.RemoteException {
  ...

//--------------------------------------------------------------------
// INTERNATIONALIZATION SERVICE: Get caller/invocation context.
// 
// The Internationalization service associates the locale list 
// propagated in the incoming remote bean method request with the 
// current thread. It is accessible within the bean method 
// implementation and within subsequent methods via the 
// Internationalization context API. 
//
// If the incoming HTTP request does not contain Internationalization 
// context, the service associates the server’s default locale 
// and time zone with the current thread. 
//
// All context elements associated with the current thread will 
// propagate over subsequent remote bean methods calls. 
//--------------------------------------------------------------------
Locale         iLocale   = invocationI18n.getLocale();
SimpleTimeZone iTimeZone = 
                   (SimpleTimeZone)invocationI18n.getTimeZone();
...

// Perform a locale-sensitive computation.
float exchangeRate = exchangeRates.get(iLocale);
...

  return exchangeRate;
} // getRate
...

} // CLASS J2eeSessionBean