Wednesday, December 23, 2009

Book Review: Packt's JBoss Richfaces 3.3

Packt folks sent me a copy of "JBoss Richfaces 3.3" so that I could do a review of the book in my blog. I am almost done with the book and probably would have started on writing review over the holidays break but another review came up which pretty much is in line with what I have in mind. You can read it at http://edemmorny.wordpress.com/2009/12/22/book-review-packts-jboss-richfaces-3-3/ . Thanks Edem for saving me some typing. I would still like to summarize the book in few bullet points:

Good Points:
  • A complete application from start to finish along with Seam and JPA integration.
  • Good explanation of some core Richfaces concepts such as placeholder, ajaxSingle, region, process
  • Skinning support and how you can create yours
  • Good use case for Poll, Push, data iteration component and its partial update
Not so good points:
  • First five chapters of book is equally split between Richfaces, Seam and JPA. Given that this book is for beginner, these other frameworks add to the complexity and more than likely will confuse reader.
  • Seam is a great framework but it's not a must have to use Richfaces. The book gives you an impression that you can not get easily started with Richfaces without Seam and seamgen.
How would I have written this book:

Given that the book is tailored towards readers who are familiar with JSF 1.2 ,I would have started with a simple JSF project and added Richfaces feature
on top of the project. Then describe how Richfaces lifecycle fits into JSF lifecyle and so on. This way transition is smooth and helps a reader see how Richfaces fits well with JSF. Most of users who post questions in Richfaces user forum fall into the group who know JSF and want to know more about Richfaces.

I wish you happy holidays and a great new year.

Tuesday, August 25, 2009

Richfaces: immediate vs bypassUpdates attributes

If you are familiar with JSF then you know when to use immediate attribute. If not, here is a quick refresher. JSF has six different phases:
  1. Restore View - Creates or restores the previous page.

  2. Apply Request Values - Set component submitted values to request values.

  3. Process Validations - Convert and validate component values. Set component values to submitted values if valid.

  4. Update Model Values - Set backing bean values to component values.

  5. Invoke Application - Execute actionListeners and actions.

  6. Render Response - Return response.

When immediate is set to true, action components call navigation handler just after 2nd phase and skip 3-5 phases. The most common usecase is with cancel button on a form i.e. when user clicks on Cancel button, you don't want to force user to enter correct values.

Now with bypassUpdates attribute, only 4-5 phases are skipped; validation happens as usual. To credit Nick Belaevski for giving a concrete example of a sign up form, you can have one button in form to cancel (set immediate=true) and another checks whether screenname is already taken (set bypassUpdates=true). Obviously, setting both to true for same component does not make sense. If you are still not clear, here is code snippet from org. ajax4jsf.component.AjaxActionComponent
if (event instanceof ActionEvent) {
if (event.getComponent() == this) {
if (isImmediate()) {
event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
} else if (isBypassUpdates()) {
event.setPhaseId(PhaseId.PROCESS_VALIDATIONS);
} else {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
}
}
// UICommand set Phase ID for all ActionEvents - bypass it.
getParent().queueEvent(event);
}
It should be crystal clear now...at least it was for me.

Tuesday, August 4, 2009

Now you can tweet from JBoss Portal

Introducing Twitter portlet for JBoss Portal and Enterprise Portal platform. It's based on twitter4j, Richfaces, JBoss Portlet Bridge.

Using this portlet, you can
  • Update your status
  • Get messages that are sent directly to you (e.g. @prabhatjha in my case)
  • Get list of folks who you are following and get their latest status. List is sortable.
I would say that this is basic but my goal was to have something for folks to get started with. twitter4j exposes various twitter api which you can use to customize this portlet for your need. I will leave that as an exercise for reader (I always wanted to say that ;-) ) Here are some screen shots:





You can grab the code from http://anonsvn.jboss.org/repos/qa/prabhat/twitter-portlet/ But this portlet can very well run on other portal servers as well if you know what deployment descriptors you need to modify.

Wednesday, July 8, 2009

From JSF to Richfaces to Richfaces Portlet

We are going to start with a very simple JSF web application then convert that to a very simple Richfaces web application and then ultimately convert that Richfaces web application to Richfaces Portlet. This tutorial assumes that you have basic knowlege of JSF. All code used in this tutorial are at http://anonsvn.jboss.org/repos/qa/prabhat/. This application simply gives you some financial advice based on your age and your current savings. We all need some financial advice these days, don't we? ;-)

Converting a JSF application to Richfaces is a 3 step process:

1. Add Richfaces filter and some default parameters:

<!-- Plugging the "Blue Sky" skin into the project -->
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>

<!-- Making the RichFaces skin spread to standard HTML controls -->
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>

<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>

2. Add Richfaces binaries to WEB-INF/lib:
You need three jars:

richfaces-api.jars
richfaces-ui.jar
richfaces-impl.jar

3. Add Richfaces components to your page:
You need to import two tag libraries. For a JSP page, you would need

<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>

For an XHTMl page, you would need

<xmlns:a4j="http://richfaces.org/a4j">
<xmlns:rich="http://richfaces.org/rich">

Just doing these changes will make your page look pretty i.e. Rich.


From






To




Note:
1. If you deploy these app into JBoss AS, you do not need to bundle jsf jars in your war but you need them when deploying in tomcat.
2. Adding Richfaces config and binaries to an existing JSF project is very simple using JBoss Tools or JBDS. You simply add "Richfaces Capabilities".

Converting a Richfaces application to Richfaces Portlet

Since Portlets run on portal server, converting a web app to portlet is not that straightforward. I guess that's why there is no add "Portlet Capabilities" in JBoss Tools/JBDS. But it's equally easy to get started using Maven archetypes. Portlet based on JSF/Richfaces/Struts run on a portal server through portlet bridge. There is a separate project called JBoss Portlet Bridge which allows you to do that that for JSF/Richfaces/Seam based portlets. This project provides different maven archetypes to get you quickly started with these portlets. For richfaces based portlet, you would need to do


mvn archetype:generate -DarchetypeGroupId=org.jboss.portletbridge.archetypes -DarchetypeArtifactId=richfaces-basic -DarchetypeVersion=1.0.0.CR2 -DgroupId=org.jboss.test -DartifactId=richfaces-portlet -DarchetypeRepository=http://repository.jboss.org/maven2/

The archetype will create all required deployment descriptors portlet.xml,portlet-instances.xml, jboss-portlet.xml which you of course would need to update. We are going to import richfaces pages we created previously to this project and modify deployment descriptors accordingly. The important changes are portlet and portal server specific deployment descriptors and hooking up bridge with JSF.

1. Portlet Bridge's view handler and state manager on top of JSF/Richfaces
Modify faces-config.xml so that it has

<application>
<view-handler>org.jboss.portletbridge.application.PortletViewHandler</view-handler>
<state-manager>org.jboss.portletbridge.application.PortletStateManager</state-manager>
</application>

2. The Bridge Portlet to wrap xhtml as a view state of portlet

<portlet>
<portlet-name>SimpleRichfacesPortlet</portlet-name>
<portlet-class>javax.portlet.faces.GenericFacesPortlet</portlet-class>
<init-param>
<name>javax.portlet.faces.defaultViewId.view</name>
<value>/introduction.xhtml</value>
</init-param>
<init-param>
<name>javax.portlet.faces.preserveActionParams</name>
<value>true</value>
</init-param>
<expiration-cache>-0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<portlet-info>
<title>Simple Richfaces Portlet</title>
</portlet-info>
</portlet>

We are configuring that GenericFacesPortlet is the portlet, it supports view mode and for default view, we are going to use introduction.xhtml that we created in JSF and Richfaces.

3. portlet-instances.xml does not change in this context i.e it's same as that of any java portlet

4. Inject needed Richfaces javascripts using jboss-portlet.xml

<portlet>
<portlet-name>SimpleRichfacesPortlet</portlet-name>
<header-content>
<script src="/faces/rfRes/org/ajax4jsf/framework.pack.js" type="text/javascript"></script>
<script src="/faces/rfRes/org/richfaces/ui.pack.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/faces/rfRes/org/richfaces/skin.xcss">
</header-content>
</portlet>


This portlet when deployed to JBoss Portal looks like:

Wednesday, June 3, 2009

Dual monitor setup with Fedora 10 and ATI graphics card

I have a Lenovo T60 with ATI graphics card. Here is the detail

[prabhat@eejot ~]$ lspci | grep VGA
01:00.0 VGA compatible controller: ATI Technologies Inc Radeon Mobility X1400


I installed Fedora 10 (Cambridge) and everything worked out of box except dual monitor setup. I can not function well without dual monitor. I usually put Eclipse/JBDS on one screen and the rest (email, chat, browser etc) on the other screen. Now dual monitor not working out of the box is not unusual given that ATI driver is proprietary and Fedora does not bundle it. But installing the driver should not be hard, right? I was in for a surprise. It took me more than a day and help of my colleague who also uses similar environment to figure this out. I have seen that lots of other folks have this problem but I could not find the complete solution in one place so here is my attempt to help others and possibly myself in future.

The driver you get from ATI is crap. If you follow the document (which makes you feel like you are going to setup a rocket for launch), it will act as if everything was installed but when you restart your machine, all you get is a black screen. What you need is a fglrx driver. Here are the details:

1. Get RPMFusion Repos

rpm -Uvh http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm

2. Install Fglrx

yum install akmod-fglrx xorg-x11-drv-fglrx xorg-x11-drv-fglrx-libs -y

3. Blacklist Radeon
Add the line "blacklist radeon" in the file /etc/modprobe.d/blacklist

4. Reboot

5. Make initrd with fglrx
While booting at grub, press 'a' and append 'single' to the command.
cd /boot/
mv initrd-`uname -r`.img initrd-`uname -r`.img.backup
mkinitrd initrd-`uname -r`.img `uname -r` --with=fglrx
6. Reboot

This installs fglrx driver. Now if you do
 aticonfig --initial -f
It will create a basic xorg.conf at /etc/X11. At this stage your two monitors are clone to each other. For dual monitor, aticonfig --help says to do 'aticonfig --dtop=horizontal' or 'aticonfig --initial=dual-head --screen-layout=right'. Doing the latter approach changed the xorg.conf but on restart, it did not give me a dual monitor and it messed up lots of other stuff like mouse pointer would only show up on one monitor etc. Then I tried 'aticonfig --dtop=horizontal' and I got the error Error: Options, e.g. --dtop and --desktop-setup, are not supported when RandR 1.2 is enabled!. So I had to disable RandR and you do that by:

7.1. Edit /etc/ati/amdpcsdb and in the section labeled [AMDPCSROOT/SYSTEM/DDX] add :

EnableRandR12=Sfalse
7.2. In the Device section under /etc/X11/xorg.conf, add:
Option      "EnableRandR12" "false" 
8. Reboot. This should give you a dual monitor which are not clone of each other.

Now, I was not able to drag windows across monitors and this was because of Xinerama was not enabled.

9. Enable Xinerama
In ServerLayout section in xorg.conf, add

Option     "Xinerama" "on"
Reboot and you are done. From now I would think twice before I get a laptop with ATI graphics card.

Note: For changes in xorg.conf, you don't have to literally reboot. You just have to restart xwindow which you can do by Ctrl+Alt+Backspace.

References:
http://djkaos.wordpress.com/2009/01/26/fedora-10-fglrx/
http://ubuntuforums.org/showthread.php?t=1137576
https://www.linuxquestions.org/questions/linux-hardware-18/dual-monitor-cant-drag-windows-between-monitors-what-gives-328647/

Tuesday, May 19, 2009

Vagaries of Nepali Politics

When Maoists became a cleaner winner of constituent assembly election of Nepal in 2008, I had written in my blog on what could have caused it. But at the same time I also felt sad that despite being clear winner, Maoists had hard time forming the government and getting the prime minister position. If a member from Maoists had not been a prime minister, it would have been a big slap on the whole election process.

Unfortunately and not surprisingly there is still no change in status quo as far as writing constitution goes but there is a change in government. And in most likely hood Mr. Madhav Nepal of UML is going to be prime minister. It's the same Nepal who lost both constituents from which he had filed his nominations. I have no insight into politics but should not prime minister be a candidate who has been elected by people? IMO, it's even a bigger slap on constituent assembly election process. What it shows definitely though that Nepali politicians do not learn from their mistakes. They have lots of fun forming government and then bringing it down. I don't have problem with UML leading the government and I don't have problem with Mr. Nepal as prime minister if he had been elected. In current situation, I wonder how Mr. Nepal's moral allows him to even think about being a prime minister?

Saturday, May 16, 2009

Maven archetype for testng + selenium

Lately I have been working a lot on front end stuffs such as JBoss Portal and Richfaces although my preference has always been to work on back end. Well life is full of these conflicting choices, isn't it?

I write sample applications for these JBoss Web Interface projects and testing them are included as part of continuous integration testsuite. To help with automation and to give easier ramp up time for new team members, I created a maven archetype for writing test cases using TestNG and Selenium. I purposely decided not to add cargo stuff as most of time we have preconfigured application and web servers. To use this for your own project, here is what you need to do


mvn archetype:generate -DarchetypeGroupId=org.jboss.maven.archetypes -DarchetypeArtifactId=selenium-testng -DarchetypeVersion=1.0 -DgroupId=org.whatever.project -DartifactId=myproject -DarchetypeRepository=http://repository.jboss.org/maven2/


This has .classpath and .project for Eclipse/JBDS projects as well so you can import the generated project into Eclipse/JBDS and your project build path will automatically be set correctly. This was my first real foray into maven and I still think that maven needs to mature more in terms of usability. I hear good things about Maven 3 so I am looking forward to that.

Sunday, April 19, 2009

Bug fixing: Two Steps Forward and One Step Backward

Currently I am leading the testing effort of couple of software frameworks which have fairly good user base. They are frameworks because they allow software developers to build a software product as opposed to provide an out of box software product for end users. While fixing bugs and testing them, all of sudden, I realized that bug fixing overall does not necessarily improve the inherent quality of software whether it's a software framework or software product. This self realization some how had a theoretical familiarity to me.

Going down the memory lane, I had read few good books on software design and development as well as software project management as part of my preparation for job interviews when I was about to graduate. One such book was The Mythical Man-Month by Mr. Frederick Brooks, JR. Back then it had made some sense but it it was one of many books that I had read. Its significance and relevance might have fizzled in elegance of other good books that I read notably GOF book on design patterns. I revisited Mr. Brooks literary creation and behold, it talked about exactly what I felt in chapter 11. ( I did not think Mr. Brooks had any pun intended when he talked about bug fixing and maintenance cost being 40% of development cost. These days I hear too many chapter 11 instances in financial news ;-) ).

In this chapter, Mr. Brooks describes bug fixes as two steps forward and one step backward. Here are the main points on why:
  • Fixing a defect has 20-25% of change of introducing another.
  • Whole set of regression testing is very costly.
  • All bug fixes tend to destroy the structure, to increase the entropy and disorder of the system. Less and less effort is spent on fixing flaws introduced by earlier fixes.
  • Things are always at their best in the beginning. Bug fixes increases disorder of the system and users get more sophisticated
  • Software Development is an entropy decreasing process, hence inherently metastable. But maintenance is entropy increasing process, and even its most skillful execution only delays the subsidence of the system into unfixable obsolescence.
Given that Mr. Brooks and many others observed these so many years back and I observe it as well, I wonder how much improvement we have made in terms of software project development as a whole. May be it's because of the fact that the only constancy is change itself and designing a system for change is more widely discussed in the literature than practiced. Mr. Brooks, you definitely make more sense to me now than what you did four years back. Not your fault though.

Tuesday, February 24, 2009

JSR-286 talk at Austin JUG

I will be giving a talk/presentation at Austin JUG about new features in JSR-286. The details of talk at http://www.austinjug.org/index.jsp?p=events-20090224 .

Saturday, February 7, 2009

Technical Review of a book on JBoss Portal


I had the pleasure of being one of the two technical reviewers of a book on JBoss Portal. The details of book is at http://www.packtpub.com/jboss-portal-server-development/book. This book is another example of world being flat.

I was electronically contacted by publisher who till date I have not met and don't even know where exactly it is located. I electronically signed the contract. I got book chapters one by one through a mediator who was liaison between me and the author. I have not met the author yet and book is published.

I hear that if publishers get break even with technical books then it's usually considered a good thing. This also means that a technical reviewer does not usually have a monetary benefit and my case is no exception. I did get two free copies of the book and most importantly a very brief bio which is printed in "golden" letters. And, I am grateful to publisher and author for that. Now back to book.

I believe that book is a good introduction on Portal servers in general and when to use them. You will also get a good feel of different features such as CMS, Security, Management of JBoss Portal server. It has working examples of how to use JSP, JSF, AJAX in a portal environment. The book ends with a good introduction of JSR-286 aka Portlet 2.0 specification.

So, if you are curious about Portal world and looking for a spec compliant open source portal server, then this book is a good to have item in your bookshelf. If you find something not working as explained in book then feel free to comment over here. I will hit myself for a while but make sure its errata is updated accordingly. If you find everything working as it should, I wont mind seeing few words of appreciation. ;-)

Monday, January 19, 2009

We got electricity. Finally !!

Most of the world is agog with upcoming Obama's inauguration and I also belong to that part of the world. But I have something else to cheer for which depending on how you look at is equally exciting.




Sisautiya, the village in Nepal where I was born, finally has electricity. Yes. Yes and Yes. This is the village where I spent the first 16 years of life and went to a nearby high school which also did not have electricity. I hope my grand parents have saved the lantern that I used.