Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I access FacesContext inside Hibernate event listener?
    primarykey
    data
    text
    <p>Here is my scenario:</p> <p>I have my-jsf-app.war (JSF 1.2 application) and daos-and-entities.har (Hibernate 3.3.1) deployed to a JBOSSAS 5 server. What I want is to have some sort of change log in my application, that contains the information "who made the change". So my idea was to have hibernate event listeners that access the FacesContext (or actually the HttpSession inside the context) to retrieve the currently logged in user. </p> <p>So basically I configured event listeners and added the following method to retrieve the currently logged in user.</p> <pre><code>protected static String tryToFindUserLogin() { try { Class classFacesContext = Class.forName("javax.faces.context.FacesContext"); Class classExternalContext = Class.forName("javax.faces.context.ExternalContext"); Method methodGetCurrentInstance = classFacesContext.getMethod("getCurrentInstance", (Class[])null); Method methodGetExternalContext = classFacesContext.getMethod("getExternalContext", (Class[])null); Method methodGetRemoteUser = classExternalContext.getMethod("getRemoteUser", (Class[])null); // This call always returns null Object currentFacesContext = methodGetCurrentInstance.invoke(null, (Object[])null); Object currentExternalContext = methodGetExternalContext.invoke(currentFacesContext, (Object[])null); String login = (String) methodGetRemoteUser.invoke(currentExternalContext, (Object[])null); logger.debug("Found Weblogin: " + login); return login; } catch (Exception e) { logger.debug(e, e); } return "anonymous"; } </code></pre> <p>I use java reflection to do this, because the HAR file is also used in other non-jsf-projects.</p> <p>The problem is that I never get a currentFacesContext. I understand that FacesContext.getCurrentInstance() is ThreadLocal. But if the hibernate action (e.g. saveOrUpdate()) is directly triggered from within a JSF ManagedBean actionmethod, shouldn't the event listener be executed by the same thread as the actionmethod? </p> <p>Any help is appriciated, maybe there ist also a totally different approach to my scenario, that I'm not aware of. I obviously don't want to add a userContext paramter to all my dao calls though, as this would be a huge amount of work.</p> <p>Thx in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload