Wednesday, November 17, 2010

Richfaces : ReThink Before ReRender

Sometimes when you go deeper into a framework or a tool , you tend to take beginner stuff for granted. That bit me today for a while before I could get my head around it.  ;-)

When using Ajax, it's a well known fact that for you to manipulate a DOM element, that element needs to be present. Same rule applies when you are using reRender in Richfaces as well. Let's take a look at following code snippet:

<rich:panel header="Add New Application">
   <h:outputText value="App Name " />
   <h:inputText size="20" id="appNameInput" value="#{appConfigBean.currentApp.appName}" />
   <a4j:commandButton id="new_app_submit" action="#{appConfigBean.addNewApp}"     
    value="Add" reRender="appDetails" />
</rich:panel>


<h:panelGrid columns="2" id="appDetails" 
                rendered="#{appConfigBean.renderDetail}">
      <h:outputText value="Some stuff"/>
</h:panelGrid>


The addNewApp method in backing bean sets renderDetail to true which initially was false. So when you click Add button, you would expect reRender to kick in and show the panelGrid with id appDetails. Nope. It does not happen. From second paragraph you might have understood the reason already. But here is a more detailed explanation.

Since initially, renderDetail boolean is set to false, when the page is first loaded, the html for panelGrid never gets rendered hence there does not exist a DOM element with id appDetails and reRender does not know what to update.

To fix this, the easiest and probably the best solution is to surround panelGrid with a4j:outputPanel and you would put this output panel's id for reRender to do its magic.  Here is the updated code:

<a4j:commandbutton id="new_app_submit">
                      action="#{appConfigBean.addNewApp}" value="Add"
                      reRender="appDetailOutputPanel" /&gt; 

<a4j:outputpanel id="appDetailOutputPanel"> 
     <h:panelgrid columns="2" id="appDetails">
                     rendered="#{appConfigBean.renderDetail}"&gt;
          <h:outputtext value="Some stuff"></h:outputtext>
     </h:panelgrid>
</a4j:outputpanel>

Tuesday, November 2, 2010

Tips when using AWS sdk with JBoss AS + GateIn

While working on a really cool portlet for GateIn that uses Quartz scheduler and AWS sdk which I am going to unveil to the rest of the world in few days ;-), I came across a weird problem. :


Caused by: java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.xml.xpath.XPath.setNamespaceContext(Ljavax/xml/namespace/NamespaceContext;)V" the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the current class, org/quartz/xml/XMLSchedulingDataProcessor, and the class loader (instance of ) for resolved class, javax/xml/xpath/XPath, have different Class objects for the type javax/xml/namespace/NamespaceContext used in the signature


This kind of error obviously suggests some conflict with classloader and that was the case in this scenario too. I initially thought it must be something with quartz because I am using Quartz's XMLSchedulingDataProcessorPlugin but looks like that dependency comes from AWS sdk. To get it working, I simply removed stax-api-1.0.1.jar from WAR file and portlet deployed fine. The conflict happens because GateIn 3.0.1.GA + JBoss AS 5 combination has stax-api.jar under GateIn-3.1.0-GA/lib/endorsed which means StAX library is a system-wide library.

Tuesday, September 21, 2010

How to configure GateIn with Amazon RDS !!

Couple of weeks back JBoss Stormgrind project created an AMI (ami-0cda3165) for GateIn  3.1.0.Final . If you have created an Amazon's EC2 instance from existing AMI before, creating a GateIn EC2 instance is no different. When  an instance is running, you can access the default portal at URL which will look something like http://ec2-75-101-229-18.compute-1.amazonaws.com:8080/portal/ Don't try accessing the URL, it will go to /dev/null ;-) If you ssh into your instance, you can see JBoss+GateIn bundle at /opt/gatein-3.1.0.FINAL. By default, GateIn comes bundled with HSQLDB but normally people have a separate instance of DB such as MySQL running on separate server so here I am going to show you how you can configure Amazon RDS aka MySQL 5.1 with your GateIn instance running in cloud.

1. In AWS management console, search for Gatein or you can directly enter ami-0cda3165. In subsequent window pick your availability zone and select instance type as "Small" which is enough to use GateIn for smaller use case and lighter load. Then select your key-pair, security group. Make sure your security group has port 8080 turned on. Ultimately, the details of instance should look similar to following screenshot:


2. Launch your instance and when AWS console says instance is running, you should be able to access the GateIn's landing page at http://ec2-75-101-229-18.compute-1.amazonaws.com:8080/portal/ This verifies that we have GateIn AMI working properly which is running atop HSQL.

3. Now, let's create a RDS instance. Launch DB instance wizard under Amazon RDS tab, select instance class as small, size 5G,  give a name to your DB instance (this is not the schema name in case you are wondering) e.g. gateinmysql and of course master userid and password. In next window of wizard, give a DB name which is schema name. For availability zone, select the same zone you selected in step 1. This means that both GateIn and DB servers are collocated which will give you a better network QoS. Your configuration should look something like following:
Launch the DB, go get a cup of coffee because it takes a while for Amazon to launch a DB instance. When it's runnig, your DB server name should look like  gateinmysql.ck0mw2jbysdo.us-east-1.rds.amazonaws.com

4.  Now we need to hook 1 & 3 together. For this, let's ssh into the GateIn instance and stop GateIn server. We then modify the file, /opt/gatein-3.1.0.FINAL/server/default/conf/gatein/configuration.properties with the details from screenshot in 3. You can see complete detail of configuring GateIn with MySql at GateIn 3.1 reference guide. 

5. Copy (scp or wget) MySQL jdbc driver to /opt/gatein-3.1.0.FINAL/server/default/lib

6. Delete data, log, tmp, work folders from /opt/gatein-3.1.0.FINAL/server/default so that we have a clean server again.

7. Start GateIn server by doing "bash run.sh -c default -b 0.0.0.0". After couple of minutes depending on how good network connection between GateIn & RDS is, you should be able to see your portal running again at the URL you saw in 2. You can also use mysql-administrator in your laptop/desktop to see the tables and data GateIn created during startup.

Unless you are a zombie, you would not want to repeat this process every time you need to run GateIn with RDS. You should create your own GateIn AMI based on all modifications you have done. The feature I liked about RDS is that you can take a snapshot of database and relaunch again. Amazon preserves the RDS public server name which means that configuration changes that you made on 4 would continue to work.

Please let me know if you run into any problem when you to try to do yourself and I will update this blog accordingly. Happy clouding and portaling.
-----------------
Help me in my mission at http://www.eejot.org

Sunday, September 12, 2010

How to debug a portlet in GateIn !!

Lately I have  seen few questions on how to use an IDE to develop and debug a portlet in GateIn user forum. The simplest answer is: it's similar to how you develop and debug a regular (jsp, servlet, jsf etc) web application. But I am going to walk you through one example of doing the same using plain Eclipse (or JBoss Tools ) and GateIn Portal using remote debugging of the upcoming Jasper portlet that is still a work in progress.

1. Import or create a project in eclipse

Nothing much to add here..it's like any standard java project. You can see further details for creating a new project at http://docs.jboss.org/tools/whatsnew/portlet/portlet-news-1.0.0.Alpha1.html In this example, I am going to import the mavenized jasper project from http://anonsvn.jboss.org/repos/qa/people/vrockai/jasper-sample-portlet/

2. Deploy the portlet into GateIn

Since this is a maven project, doing a maven install will create a .war file which we will deploy or copy to $GATEIN_HOME/server/default/deploy.

3. Preapre GateIn for debugging
Uncomment JPDA section for remote socket debugging in $GATEIN_HOME/bin/run.conf

JAVA_OPTS="$JAVA_OPTS -Xrunjdwp:transport=dt_socket, address=8787, server=y,suspend=n"


Please note the port number here. You can change it if you have any conflict on that port.

4. Start GateIn
You simply do "bash run.sh" on Linux/Unix or run.bat on Windows.

5. Hook up Eclipse with GateIn server
 In eclipse, select Run->Debug Configurations->Remote Java application and click on "New Launch Configuration Icon" which should show the following screen. Please change the port number from Step 3.
Click Debug. You should not get an error if eclipse is able to connect to the socket on port 8787.

6. Add portlet to a page
As of GateIn 3.1.0, there is no "simple" way of adding a portlet to a page using XML similar to how it was in JBoss Portal so we are going to use GateIn's user interface to do that. You can see how to do that at http://docs.jboss.com/gatein/portal/3.1.0-FINAL/user-guide/en-US/html_single/index.html#chap-User_Guide-Portal_Administration Please ask in user forum if you need help with this. In my case, I added this to home page. I can see the portlet when I visit the home page. However since we did not put any break point, eclispe does not care at this point

7.  Add a break point

I am going to add a break point on the first line of code in doView method of JasperPortlet class knowing that this method is always going to be called for normal viewing of this portlet.

8. See it in action

Let's refresh the page which should call doView method again and eclipse should prompt me to go to Debug perspective. Here is screenshot.

Happy debugging.
------------------
Please help me in my effort of computer literacy in Nepal at http://www.eejot.org

Thursday, August 19, 2010

Hot eclipse tip of the day

In eclipse, when you hit F3 while your cursor is over some code in .java file, it will jump to its declaration. It's all good except in the case of an interface. For a method in interface, most of time you want to see its implementation not its declaration. There are few ways to work around this:

- Ctrl+mouse over the text will show options to jump to declaration or implementation.

- Ctrl+t will also show you options for both interface and its implementation.

First one requires me to use mouse, second one is not intuitive because I am used to hitting F3 to do similar operation. Ideally, you would like to see some combination of F3 doing its job. The Implementors plugin does exactly that. Now when I hit, ALT+F3 it goes directly to its implementation.

Friday, August 6, 2010

Some new kind of entertainment for your portal

Most of us like and love movies and we have some favourite stars and sometimes we even hang their posters. Even folks at prison get to do that; remember Rita Hayworth in Shaswshank Redemption. Now if you don't know who Rita Hayworth is and which movie I am talking about, you would probably google it and your search would more likely take you to IMDB or Wikipedia - yaaaawn ;-) Now imagine not having to do that and getting those information in your own portal. YE !!

Introducing Netflix portlet. Since picutures are worth thousand words, here are some screenshots with details of Shawshank Redemption. For a movie and TV series, the portlet currently shows its synopsis, cast, ratings, awards, poster if available.



For an actor, it shows his/her biography, filmography and of course a picture if available. Here is a screen shot of portlet with details of one of the actors I like.


Now some interesting details: Portlet consumes data using Netflix's webservices API hence to have this portlet running on your server you need to have proper key which you can get from Neflix's Dev page and when you start your server, you simply add -Dnetflix.consumer.key=blah -Dnetflix.consumer.secret=blahblah as JVM parameters. Portlet is based on JBoss Richfaces 3.3.3.Final, JBoss PortletBridge 2.0.0.Final and has been tested with GateIn Portal and JBoss Enteprise Portal Platform 5. The source code of this portlet can be downloaded from http://anonsvn.jboss.org/repos/qa/prabhat/netflix

Disclaimer: This portlet is not officially supported by JBoss, by Redhat.

Help support my mission: http://www.eejot.org

Friday, June 25, 2010

Richfaces Presentation at Austin JUG

I will be giving a 45-min presentation on JBoss Richfaces at Austin JUG on June 29 starting at 7 PM. More details at http://austinjug.org/index.jsp?p=events-20100629 I hope to see you there if you are in town.