Thursday, July 22, 2010

A friend who cares

“When we honestly ask ourselves which person in our lives mean the most to us, we often find that it is those who, instead of giving advice, solutions, or cures, have chosen rather to share our pain and touch our wounds with a warm and tender hand. The friend who can be silent with us in a moment of despair or confusion, who can stay with us in an hour of grief and bereavement, who can tolerate not knowing, not curing, not healing and face with us the reality of our powerlessness, that is a friend who cares.”

- Henri J.M. Nouwen 

Friday, July 09, 2010

JSF request processing lifecycle in brief

1.      Restore View Phase: For a new/fresh request, this phase creates a component tree starting with the UIViewRoot. For a post back request (a request from a previously generated JSF response), the component tree is restored (when state saving method is client, the tree is de-serialized from the request. When state saving method is server, the tree is restored from the server’s cache).

2.      Apply Request Values Phase: The values held in the request are fetched and set on the appropriate components. At the end of this phase, the component tree has all the value submitted in the request. The server invokes processDecodes() on UIViewRoot and that call trickles down the entire tree invoking the decode() method on each component. ValueHolder and EditableValueHolder components update the component values and AcionSource components queue an ActionEvent for processing in further phases. If immediate attribute is “true”, validation happens at this phase.

3.      Process Validation Phase: All component in the tree convert and validate their values (that was set in step 2). The server invokes processValidators() on the UIViewRoot (and that in-turn recursively invokes processValidators() on each component). Each component’s processValidators() invokes the converter and validators associated with the component.  If a component fails conversion or validation, its valid property is set to false and a FacesMessage is queued (and rendered in render response phase).

4.      Update Model Values Phase: The backing beans are updated with validated values in the component tree. The server invokes processUpdates() on UIViewRoot which invokes processUpdates() on each component. The processUpdates() method of UIInput types will invoke updateModel() which will update the backing bean.

5.      Invoke Application Phase: Any action method or actionListener methods are invoked in this phase. In phase 2 these components would have queued an ActionEvent. The server invokes the processApplication() on UIViewRoot. This method will invoke the broadcast() method on each ActionSource in the component tree for each queued ActionEvent.

6.      Render Response Phase: The encodeXX() method on each component is invoked. Each method uses its renderer to generate response and persist the component state so that it can restored in a future request.

 

Wednesday, July 07, 2010

ui:debug to inspect JSF component tree and UI state

On several occasions, it would be handy to view the component tree and view state.

The <ui:debug..> makes this possible. Using the plain tag with no attributes specified is the most simplest usage:


<ui:debug />


To view the The component tree, load the page holding this tag and press Ctrl+shift+d. This should bring up a new window. But Ctrl+shift+d happens to be a browser shortcut for “Organize bookmarks” in Firefox and the “Bookmark Manager” in Google Chrome. So we need to change the default hotkey which is done as follows:


<ui:debug rendered="true" hotkey="o"/>


Now reload the page and press Ctrl+shift+o. Now a new window opens with the “Component Tree” and “Scoped Variables”.

1.      The component tree gives a inundated lists all UI components starting with UIViewRoot.

2.      The scoped variables lists various beans in various scopes (e.g. Request Parameters, View Attributes, Request Attributes, Session Attributes, Application Attributes)

The rendered attribute can be used to turn off this feature in production.

Tuesday, July 06, 2010

String.split() trivia

The String.split() method converts a given string to an array based on a regular expression.

Examples:


System.out.println( Arrays.toString( "0:1::::".split( "[:]" ) ) );     // Output: [0, 1]

System.out.println( Arrays.toString( "::::4:5".split( "[:]" ) ) );     // Output: [, , , , 4, 5]

System.out.println( Arrays.toString( "0:1::::".split( "[:]", -1 ) ) ); // Output: [0, 1, , , , ]


As the documentation notes, “blah”.split(“:”) is the same as “blah”.split(“:”, 0) which does not include trailing empty strings.