Friday, October 11, 2013

How to preserve the user session data during activation and passivation of application module in Oracle ADF?

User session specific information, like a queried user name or account data, can be saved in the Oracle ADF Business Components session using a call to getUserData() that returns a java.util.HashMap as a data store.

The user data map can be accessed from Java can be obtained from the user session using the below method call

getDBTransaction().getSession().getUserData()

However, the user specific information in the userData HashMap is not persisted by default when activation / passivation occurs for application modules that have Application Module pooling enabled, which means that custom session data may be lost between requests.

What we need to to is to passivate the session user data together with the other state data stored by the framework and load it back when the AM is requested the next time (when it gets activated again). To do this we have to overwrite the following two methods in the AMImpl class of ADF application module.

@Override protected void activateState(Element aElement) { super.activateState(aElement); if (true) { Hashtable lData = getSession().getUserData(); if (aElement != null) { // 1. Search the element for any <PrivData> elements NodeList nl = aElement.getElementsByTagName(PRIVATEDATA); if (nl != null) { // 2. If any found, loop over the nodes found for (int i = 0, length = nl.getLength(); i < length; i++) { // 3. Get first child node of the <PrivData> element Node child = nl.item(i).getFirstChild(); if (child != null) { // 4. Set the data value to the user data hashmap String lDataString = child.getNodeValue(); String[] lSplitkeyval = lDataString.split(";"); for (int ii = 0; ii < lSplitkeyval.length; ii++) { mLogger.fine("..." + lSplitkeyval[ii]); String[] lSplit = lSplitkeyval[ii].split("="); lData.put(lSplit[0], lSplit[1]); } break; } } } } } }

@Override
protected void passivateState(Document aDocument, Element aElement) {
super.passivateState(aDocument, aElement); if (true) { // 1. Retrieve the value of the user data to save and build a string representation Session lSession = getSession(); Hashtable lData = lSession.getUserData(); String lDataString = ""; Set<String> keyset = lData.keySet(); if (!keyset.isEmpty()) { Iterator<String> keys = keyset.iterator(); while (keys.hasNext()) { String key = keys.next(); mLogger.fine("..." + key + "=" + lData.get(key)); lDataString += key + "=" + lData.get(key) + ";"; } } // 2. Create an XML element to contain the value Node node = aDocument.createElement(PRIVATEDATA); // 3. Create an XML text node to represent the value Node cNode = aDocument.createTextNode(lDataString); // 4. Append the text node as a child of the element node.appendChild(cNode); // 5. Append the element to the parent element passed in aElement.appendChild(node); } }

No comments:

Post a Comment