6 Replies Latest reply: Mar 2, 2012 6:25 AM by John Markakis RSS

How to set user locale on session start

Lydia Ares Newbie

Hi,
based on the documentation in the Seam International module I'm trying to set the user locale on session start. The locale depends on a server-side configuration file and cannot be changed any more once the webapp has started.
I tried the following code:




@ApplicationScoped
public class UserLocaleSetter {

     private Locale configuredUserLocale;
     
     @Inject
     @Client
     @Alter
     private Event<java.util.Locale> userLocaleEvent;
     
     public void onStartup(@Observes @Initialized WebApplication webapp) {
          // read config file and set configuredUserLocale
          ...
     }
     
     public void onSessionStart(@Observes @Initialized HttpSession session) {
          userLocaleEvent.fire(configuredUserLocale);
     }
}


which results in an infinite loop (stack overflow) - the onSessionStart() observer method is called over and over again.


So it seems that the event which is fired to change the locale, causes the HttpSession to start again?


What would be the preferred way to implement a fixed user locale then?


Thanks for any help!

  • 1. Re: How to set user locale on session start
    Jason Porter Master

    Thinking quickly here, you could create a new locale variable in the session and store and use that in your application. Anything more than that I'd have to think about for a while.

  • 2. Re: How to set user locale on session start
    Ken Finnigan Master

    I'll check the documentation for how this is worded, but the initial locale for a user session is set to that of the application (ie. default locale).


    If you set the default application locale, then every user will pick up that locale without you needing to code anything

  • 3. Re: How to set user locale on session start
    John Markakis Newbie

    Hi,

     

    did you perhaps find a solution to that problem? I am facing the exact same problem. I don't know how to initialize the locale for each individual user session (eg from the request Accept-Language)

  • 4. Re: How to set user locale on session start
    Jason Porter Master

    How about using a javax.servlet.http. HttpSessionListener?

  • 5. Re: How to set user locale on session start
    John Markakis Newbie

    Unfortunately the following trace repeats and there is a Stack Overflow

     

          at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:305) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:299) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.introspector.jlr.WeldMethodImpl.invokeOnInstance(WeldMethodImpl.java:188) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.introspector.ForwardingWeldMethod.invokeOnInstance(ForwardingWeldMethod.java:59) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:198) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:282) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:265) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:234) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.manager.BeanManagerImpl.notifyObservers(BeanManagerImpl.java:635) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.manager.BeanManagerImpl.fireEvent(BeanManagerImpl.java:628) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.event.EventImpl.fire(EventImpl.java:75) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at gr.adev.marka.i18n.UserLocaleSessionListener.sessionCreated(UserLocaleSessionListener.java:23) [classes:]

          at org.apache.catalina.session.StandardSession.tellNew(StandardSession.java:374) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.apache.catalina.session.StandardSession.setId(StandardSession.java:344) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.apache.catalina.session.ManagerBase.createSession(ManagerBase.java:506) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.apache.catalina.session.StandardManager.createSession(StandardManager.java:299) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.apache.catalina.connector.Request.doGetSession(Request.java:2665) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.apache.catalina.connector.Request.getSession(Request.java:2375) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:841) [jbossweb-7.0.1.Final.jar:7.0.1.Final]

          at org.jboss.weld.context.beanstore.http.LazySessionBeanStore.getSession(LazySessionBeanStore.java:68) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.context.beanstore.http.AbstractSessionBeanStore.setAttribute(AbstractSessionBeanStore.java:72) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.context.beanstore.AttributeBeanStore.put(AttributeBeanStore.java:148) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:126) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:87) [weld-core-1.1.2.Final.jar:2011-07-26 15:02]

          at org.jboss.seam.international.locale.UserLocaleProducer$Proxy$_$$_WeldClientProxy.changeLocale(UserLocaleProducer$Proxy$_$$_WeldClientProxy.java) [seam-international.jar:]

          at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source) [:1.6.0_30]

          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_30]

          at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_30]

     

    The code was:

     

    public class UserLocaleSessionListener implements HttpSessionListener {
    
      @Inject
      @Alter
      @Client
      private Event<Locale> localeEvent;
    
      @Override
      public void sessionCreated(HttpSessionEvent arg0) {
        localeEvent.fire(FacesContext.getCurrentInstance().getViewRoot().getLocale());
        System.out.println("Initializing session locale to " + FacesContext.getCurrentInstance().getViewRoot().getLocale());    
      }
    
      @Override
      public void sessionDestroyed(HttpSessionEvent arg0) {
         
      }
    
    }
    
  • 6. Re: How to set user locale on session start
    John Markakis Newbie

    It seems that the code is affected by the WELD-892 issue.

     

    You can also look at a workaround:

     

    https://community.jboss.org/message/717858#717858