[Enterprise Extensions only]

Gaining access to the Internationalization context API

In the Enterprise JavaBeans environment, the Internationalization Service supplies a JNDI binding to an implementation of the UserInternationalization interface under the java:comp/websphere/UserInternationalization name. Applications requiring access to the service can perform a lookup on that JNDI name, as shown in the following code snippet:

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;
try {
  initCtx = new InitialContext();
  userI18n = (UserInternationalization)initCtx.lookup(
  "java:comp/websphere/UserInternationalization");
}
catch (NamingException nnfe) {
  // UserInternationalization URL is unavailable.
}
catch (NamingException ne) {
  // InitialContext could not be instantiated.
}
...

Once an application component resolves a reference to the UserInternationalization interface, it can use this reference to create references to the Internationalization and InvocationInternationalization interfaces, which afford access to both caller and invocation locales and time zone. See the following code snippet:

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

Suggestion: Internationalization context API references need to be resolved only once over the lifecycle of any application component. Therefore, when developing server-side application components (for example, servlets and Enterprise JavaBeans), resolve the Internationalization context API references within the initialization methods of such components (for example, within the init() method of servlets, or within the ejbCreate() method of Enterprise JavaBeans). See Programming examples for more information.