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.

Tuesday, June 29, 2010

Internet connection speeds

Internet connectivity technologies – fastest to slowest

1.       Fiber optics

2.       Cable

3.       DSL

4.       Dish

5.       Dial-up

Friday, June 25, 2010

Unchecking a html radio button

Given the following code,

 

<input type="radio" value="PG" name="courseType" /> Post Graduate </input>

<input type="radio" value="UG" name="courseType" /> Under Graduate </input>

 

To check the "PG" html radio button in jQuery:

 

jQuery(blahBlahBlah).attr('checked', 'true');

 

To uncheck it:

 

jQuery(blahBlahBlah).removeAttr('checked');

 

Note: the following will **NOT WORK**

 

jQuery(blahBlahBlah).attr('checked', 'false');

jQuery(blahBlahBlah).attr('checked', '');

Monday, June 21, 2010

Tomorrow is always fresh with no mistakes in it.

“Tomorrow is always fresh with no mistakes in it.”

- Anne of Green Gables through Julie911

You know, I think you try a little harder when you're scared.

“You know, I think you try a little harder when you’re scared.”

- Rocky Balboa through Julie911

Sunday, June 20, 2010

Knowing to read

Haji Ali...picked up his dog-eared, grease-spotted Koran and held it before the flames. "Do you see how beautiful this Koran is?" Haji Ali asked.

"Yes."

"I can't read it," he said. "I can't read anything. This is the greatest sadness of my life. I'll do anything so the children of my village never have to know this feeling. I'll pay any price so they have education they deserve"

...Mortenson says "...Here was this illiterate man, who'd hardly ever left this little village in the Karakoram,"..."yet he was the wisest man I've ever met."

- From "Three Cups of Tea" by Greg Mortenson and David Oliver Relin

The oppressor benefits for the day, the oppressed - for life

He saw five men approaching. The four burly men walking behind carried clubs made of poplar branches that they smacked against their palms in time with their steps. The leader was a thin, unhealthy looking older man who leaned on his cane as he climbed to Korphe. He stopped, rudely, fifty yards from Haji Ali, and made Korphe's nurmadhar walk out to greet him.

Twaha leaned toward Mortenson. "This man Haji Mehdi. No good." he whispered.

Mortenson was already acquainted with Haji Mehdi, the nurmadhur of Askole. "He made a show of being a devout Muslim," Mortenson says. "But he ran the economy of the whole Braldu Valley like a mafia boss. He took a percentage of every sheep, goat, or chiken the Balti sold, and he ripped off climbers, setting outrageous prices for supplies. If someone sold so much as an egg to an expedition without paying him his cut, Haji Mehdi sent his henchmen to beat them with clubs."

... Mehdi ...said ..."I have heard that an infidel has come to poison Muslim children, boys as well as girls, with his teachings....Allah forbids the education of girls. And I forbid the construction of this school."

"We will finish out school," Haji Ali said evenly. "Whether you forbid it or not."

....

"And you, are you not a muslim?" Medhi said, turning menacingly toward Haji Ali. "There is only one God. So you worship Allah? Or this kafir?"

Haji Ali clapped his hand on Mortenson's shoulder. "No one else has ever come here to help my people. I 've paid you money every year but you have done nothing for my village. This man is a better Muslim then you. He deserves my devotion more than you do."

"If you insist on keeping your kafir school, you must pay a price" Medhi said,..."I demand twelve of your largest rams"

"As you wish," Haji Ali said, turning his back on Mehdi, to emphasize how he had degraded himself by demanding a bribe....

"You have to understand, in these villages, a ram is like a firstborn child, prize cow and family pet all rolled into one"...

....

'It was one of the most humbling things I've ever seen," Mortenson says, "Haji Ali had just handed over half the wealth of the village to this crook, but he was smiling like he'd just won a lottery."

..."Don't be sad...Long after all those rams are dead and eaten this school will still stand. Haji Hehdi has food today. Now our children have education forever"

- From "Three Cups of Tea" by Greg Mortenson and David Oliver Relin

Friday, June 11, 2010

JSF 2.0 - Automatic resource bundle resolution

User message localization is achived by placing messages in resource bundles (userLoginMessages_en_US.properties for example).

In JSF 1.2, these resource bundles had to be configured in faces-config as follows:


...
    <application>
        <resource-bundle>
            <base-name>test.Messages</base-name> 1 References the file Messages.properties anywhere in the classpath
            <var>msgs</var> 2 Messages in Messages.properties will be referenced in xhtml using "msgs" prefix e.g. #{msgs.userName}
        </resource-bundle>
    </application>
...


Messages in Messages.properties could then be accessed as follows in xhtml pages

<h:outputText
    value="#{msgs.userNameLabel}"/>


In JSF 2.0, the faces-config entry has been made optional. To avoid making the faces-config entry,
1. Rename the properties file to match the xhtml file name. For example, if the xhtml file is named userCreate.xhtml, name the properties file as userCreate.properties.
2. Place the .xhtml and .properties files in the same directory. In our example, place userCreate.xhtml and userCreate.properties in the same directory
3. Change the references in .xhtml to #{cc.resourceBundleMap.<symbol_in_properties_file> where <symbol_in_properties_file> is any symbol in the properties file. For example, if the userCreate.properties has a line as "userNameLabel=User Name" then reference this symbol as follows

<h:outputText
    value="#{cc.resourceBundleMap.userNameLabel}"/>


JSF 2.0 Templating

Templating is used to encapsulate (separate interface from implementation) and reuse page layout across. Templating can be achieved using frameworks like Tiles and SiteMesh. 

In JSP, templating is achieved by defining a header.jsp and <jsp:include...> ing the header.jsp in each page. If header.jsp is changed, all pages that jsp:include header.jsp will automatically get that change. Thus the header content has been encapsulated in header.jsp and reused in multiple pages.

In JSF templating, there are 3 elements to templating

Element 1. Template

Defines layout of pages. Layout has markup to construct the structure of page (5, 6 & 7 in the example below) and sectionsnot standard terminology (1 in example below)that can be filled up with contents. Templates can optionally define default contents for sections (2 in example below defines a default, 3 and 4 don't define a default).
Example: template.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets">
    <head> 5 structure of layout
      <link href="styles.css" rel="stylesheet" type="text/css"/>
      <title> 6 structure of layout
        <ui:insert name="title"> 1 Defines a section named "title"
            Default Title        2 Default contents for the "title" section
        </ui:insert>
      </title>
    </head>
 
    <body>
      <div class="heading"> 7 structure of layout
        <ui:insert name="heading"/> 3 A section named "heading" with no default contents
      </div>
 
      <div class="content">
        <ui:insert name="content"/> 4 A section named "content" with no default contents
      </div>
    </body>
</html>


Element 2. Section ContentsNot standard terminology

These are contents that could go into any section defined in the template. For example, there could be one section-contents xhtml file for user login and another for new employee creation.
An user login page could look like this
Example: userLogin.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets">
  <ui::composition>
    <h:form>
      <h:inputText
        value="#{user.name}"/> 1 User name
      <h:inputSecret
        value="#{user.password}"/> 2 Password
      <h:commandButton
        action="#{userController.loginAction}"
        <h:outputText
          value="#{msgs.login}"/> 3 Login button
      </h:commandButton>
    </h:form>
  </ui:composition>
</html>

Element 3. Composition 

The composition brings the template (template.xhtml in this example) and section contents (login.xhtml) together by specifying which section contents (each in its own xhtml) should go into which sections (defined in the template)
Example: login.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets">
  <ui:composition template="/template.xhtml"> 1 This composition will use sections defined in template.xhtml 

    <ui:define name="content"> 2 Child elements of this ui:define is the content of section named "content". The section named "content" is defined at 4 of template.xhtml.
      <ui:include src="userLogin.xhtml"/>
    </ui:define>

    <ui:define name="heading"> 
      User login heading 5 Contents of "heading" section
    </ui:define>
  </ui:composition>
</html> 6 This composition chooses not to override the "title" section (define at 1 of template.xhtml). So the default contents defined by template.xhtml will take effect.

By encapsulating the layout, it can be reused across multiple compositions.
[ Source ]





Wednesday, June 09, 2010

I can choose my thoughts

“There is so much about my fate that I cannot control, but other things do fall under the jurisdiction. I can decide how I spend my time, whom I interact with, whom I share my body and life and money and energy with. I can select what I can read and eat and study. I can choose how I’m going to regard unfortunate circumstances in my life — whether I will see them as curses or opportunities. I can choose my words and the tone of voice in which I speak to others. And most of all, I can choose my thoughts.”

- Elizabeth Gilbert: Eat, Pray, Love through Julie911

other people's way of living

“I’ve learned about other people’s way of living: how you cope with life’s challenges, how you celebrate good times, how you feel about issues, how awesomely goofy some of you are, and how, in the end, we all truly just want the same thing: to love and be loved, to understand and be understood, to leave this world knowing that we mattered and made a difference. We are all different, but so much alike.”

 

-       Julie911

Wednesday, May 26, 2010

Learning

A Chinese saying:

I hear and I forget

I see and I remember

I do and I understand

 

Try to make sure your study is uninterrupted. You will need to find both the time and place where you can study.

 

It is important to remember that learning is not a race – everyone learns at their own rate. So don’t rush your leaning – make sure you get the most from the book.

-          An introduction to derivatives

Friday, May 21, 2010

Searching for more proof is futile. Searching for more determination makes more sense.

“That's the way a friend described someone she had just met. She was sure (just as I'm sure) that he's going places. Once the determination is in his eyes, the learning will take care of itself.

On the other hand, if I can see the fear in your eyes, then I'm not sure that learning alone will take care of the problem. No one can prove that the path you're on is risk free or guaranteed to work. Searching for more proof is futile. Searching for more determination makes more sense.”

-       Seth Godin

Thursday, May 20, 2010

I'd learned long ago how to leave things behind without regret

 I'd learned long ago how to leave things behind without regret – Tony Kahn

Tuesday, May 18, 2010

Multiple tabs in gvim

The following will open all xml files found in the current directory (and its sub-directories) in multiple tabs in gvim

 

$ find . -name '*.xml' | xargs gvim -p

Thursday, May 13, 2010

The ability to choose who judges your work...is the key building block in becoming an artist in whatever you do.

Here's the mistake we make in high school:

We let anyone, just anyone, judge our work (and by extension, judge us.)

Sue, the airheaded but long-legged girl in Spanish class gets the right to judge our appearance.

Bill, the bitter former-poet English teacher gets the power to tell us if we're good at writing.

And on and on.

The cheerleaders are deputized as the Supreme Court of social popularity, and the gym teacher forever has dibs on whether or not we're macho enough to make it in the world. These are patterns we sign up for, and they last forever (or until we tell them to go away).

In high school, some people learn to ship, they learn to do work that matters and most of all, they learn to ignore the critics they can never possibly please. The ability to choose who judges your work--the people who will make it better, use it and reward you--is the key building block in becoming an artist in whatever you do.

-          Seth Godin