Archive

Archive for the ‘Development’ Category

How will you force reload a page in Seam when user clicks Back button

February 6th, 2010 Karthikeyan C No comments

In Seam, to force reload a page when user clicks Back button add the following in pages.xml. That is to make the page expire immediately once it is rendered. If the user clicks on back button, the page is reloaded again by hitting the server.


<page view-id="/pathtoview/yourviewname.xhtml">
 <header name="Cache-Control">no-cache, no-store, max-age=0, must-revalidate</header>
 </page>
  • Share/Bookmark
Categories: Development Tags:

How To ReRender PrimeFaces growl after Ajax request from RichFaces component

January 17th, 2010 Karthikeyan C 2 comments

The trick is to embed p:growl within a4j:outputPanel (else for some reason PrimeFaces growl is not rerendered even after assigning an id to it)


<a4j:outputPanel id="pgrowl">
 <p:growl  showDetail="true"  sticky="true"/>
 </a4j:outputPanel>

The usage is like


<a4j:commandButton reRender="pgrowl" ..........
  • Share/Bookmark
Categories: Development Tags:

Tutorial – Your first CXF web service in 10 minutes

December 16th, 2009 Karthikeyan C 1 comment

This post is for Java developers who wish to start developing web services using CXF.It is assumed you have Tomcat 6.0.x installed on your machine and an IDE that supports Maven projects.

There are 2 Maven projects involved.

  • Project 1 is the web application (deployed in Tomcat) which contains the CXF web service which accepts time in milliseconds and returns the related java.util.Date object.
  • Project 2 is the client side standalone Java code invoking this web service.

Please click here to browse/checkout the source code of the web application is available in Google code.

Please click here to browse/checkout the source code of the standalone Java client.

We start with the web service interface provided below which has the method to convert time in milli seconds to Date format.


package com.karthik;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface DateConversionService {
 java.util.Date convertDate(@WebParam(name="timeInMilliSecs") Long timeInMilliSecs);
}

The implementation class is as below.


package com.karthik;

import java.util.Date;
import javax.jws.WebService;

@WebService(endpointInterface="com.karthik.DateConversionService")
public class DateConversionServiceImpl implements DateConversionService{

 @Override
 public Date convertDate(Long timeInMilliSecs) {
 return new Date(timeInMilliSecs);
 }

}

We have to inform CXF to deploy the web service by providing the necessary details in a Spring Bean configuration file. The file name is cxf.xml and contains the following code (Few XML name spaces are not needed but provided so that we can reuse the same for future posts in CXF series).


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:jaxws="http://cxf.apache.org/jaxws"
 xmlns:cxf="http://cxf.apache.org/core"
 xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
 xsi:schemaLocation
 ="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://cxf.apache.org/jaxws
 http://cxf.apache.org/schemas/jaxws.xsd
 http://cxf.apache.org/core
 http://cxf.apache.org/schemas/core.xsd
 http://cxf.apache.org/transports/http/configuration
 http://cxf.apache.org/schemas/configuration/http-conf.xsd">

 <!-- Load CXF modules from cxf.jar -->
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

 <jaxws:endpoint id="dateconversion"  address="/DateConversionWS" implementor="com.karthik.DateConversionServiceImpl"/>

</beans>

Now in web.xml we configure the URL at which CXF servlet will be listening and also inform the location of the configuration file (that is the above cxf.xml).


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <displayname>karthikeyanc.com CXF example</displayname>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:cxf.xml</param-value>
 </context-param>
 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
 <servlet>
 <servlet-name>CXFServlet</servlet-name>
 <servlet-class>
 org.apache.cxf.transport.servlet.CXFServlet
 </servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>CXFServlet</servlet-name>
 <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
 </web-app>

Note: The pom.xml of this web application uses Maven assembly plugin to pack the web service interface which will  be referred as a dependency from the standalone Java client. By doing this we do not duplicate the web service interface file.

Please navigate to the project root folder and run mvn install. Under Tomcat_Home/conf/Catalina/localhost, create file called convertdate.xml to configure the context for this web application (As convertdate) and point to the target directory of the maven project as below. (Please change the docBase accordingly). Then start Tomcat. The WSDL can be obtained by typing the following in the URL http://localhost:8080/convertdate/services/DateConversionWS?wsdl


<Context path="/convertdate" docBase="/home/pathtoprojectroot/cxfwebappdateconverter/target/cxfdate"/>

Running the standalone Java client:

Now you may open the stand alone client project in an IDE (I use NetBeans 6.8) and run DateConversionServiceClient.java. This is the only Java file in the client project. The web service interface comes from the distribution jar ( created when running mvn install command for the web application Maven project). The source code of DateConversionServiceClient.java is as below. (Please change port number accordingly).


package com.karthik.client;

import com.karthik.DateConversionService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class DateConversionServiceClient {

 private static final String ADDRESS="http://localhost:8080/convertdate/services/DateConversionWS";
 public static void main(String[] args) {
 JaxWsProxyFactoryBean fb=new JaxWsProxyFactoryBean();
 fb.setAddress(ADDRESS);
 fb.setServiceClass(DateConversionService.class);
 DateConversionService service=(DateConversionService) fb.create();
 System.out.println("The date converted is :"+service.convertDate(System.currentTimeMillis()));

 }
}
  • Share/Bookmark
Categories: Development Tags: ,

JBoss Tricks – How to deploy WAR JAR EAR in an external folder (directory)

December 11th, 2009 Karthikeyan C No comments

With JBoss 6.0.0.M1 out to try out the new JSRs the following may be helpful in reducing time to deploy.

The usual way in which we deploy an application (war , jar or ear ) is to drop the application archive ( or in expanded mode with the folder name ending with .war or .ear) under deploy folder of the server.

But how to deploy an application archive or a folder when it is not residing under deploy folder but resides in some other external folder?

Let’s say the application ROOT folder is E:\javaapps\myapptodeploy\webapplication.war, then in jboss-service.xml (under conf folder of the JBoss server profile (like default, all)),  we mention the following. This is very helpful when we build the project using Maven as we can mention the target directory directly without copying the files after each build.


<attribute name="URLs">
 deploy/,file:///E:/javaapps/myapptodeploy
</attribute>
  • Share/Bookmark

Step By Step Guide – Setup Seam 2.2.0.GA based Integration testing with Maven TestNG Cobertura

December 7th, 2009 Karthikeyan C No comments

This step by step guide explains / contains the following

  • Configure a Seam 2.2.0.GA, Hibernate (as the JPA implementation provider), RichFaces, PrimeFaces based project to be built by Maven (tested on 2.0.9 plus).
  • A simple Seam based web application which allows us to save (create and update) Employee details (just name and email to keep it simple) in MySQL database.
  • Write Seam based Integration test cases that run in JBoss Embedded container that run with TestNG.
  • Code coverage report generation using Cobertura.
  • The given project in this post is deployed on Tomcat 6.0.x.

The executable project code is available in Google code and can be obtained by downloading  here. (Please note that I have excluded files that ship with embedded JBoss download. I have provided screen shots of the final folder structure to avoid any confusion)

  1. Choose a folder of your choice. This will be known as ROOT folder for the project. Create a folder called src under it. Also create an empty file called pom.xml
  2. Under this src folder, create two folders called main and test.
  3. Under main folder, we will create the following folders – java, resources, sql and webapp.
  4. Under test folder, we will create the following folders – java, resources and a empty file called testng.xml.

What have we done so far ?:

- We have almost created a Seam based Maven archetype. The main folder contains files that are bundled in the deployable web application. test folder contains files used in various testing (Unit, Integration etc).

The final folder structure is provided as screen shots.

mainfoldertestfolder

Let us start with the employee table in database.


CREATE TABLE employee (
 id INTEGER NOT NULL AUTO_INCREMENT,
 name varchar(100) NOT NULL,
 email varchar(70) NOT NULL UNIQUE,
 PRIMARY KEY  (id)
);

The related JPA entity class is as below. The package is com.karthik.entity (full source code available in the zip file that can be downloaded).[This file is under src/main/java/com/karthik/entity]


@Entity
@Table(name = "employee")
@NamedQueries({
 @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")})
public class Employee implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Basic(optional = false)
 @Column(name = "id")
 private Integer id;
 @Basic(optional = false)
 @Column(name = "name")
 private String name;
 @Basic(optional = false)
 @Column(name = "email")
 private String email;

Now we need a Seam backing bean to save a new Employee data, update existing Employee data and retrieve all employees in database. The source code of the backing bean EmployeeSaveAction.java is provided below. [This file is under src/main/java/com/karthik/action]


package com.karthik.action;

import com.karthik.entity.Employee;
import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;

@Name("employeeaction")
public class EmployeeSaveAction implements Serializable{

 @In
 EntityManager em;
 private Employee employee;
 @DataModel
 List<Employee> employeeList;
 @DataModelSelection("employeeList")
 Employee selectedEmployee;

 @Create
 public void init() {
 employee = new Employee();
 }

 @Factory("employeeList")
 public void populateEmployeeList(){
 employeeList=em.createNamedQuery("Employee.findAll").getResultList();
 }

 public void createEmployee() {
 em.persist(employee);
 //clear UI values
 employee = new Employee();
 populateEmployeeList();
 }

 public void update(){
 selectedEmployee=em.merge(selectedEmployee);
 populateEmployeeList();
 }

 public Employee getEmployee() {
 return employee;
 }

 public void setEmployee(Employee employee) {
 this.employee = employee;
 }

}

Now we focus on configuring Seam to do the necessary injection in the above backing bean. First comes components.xml (this is under src/main/webapp/WEB-INF)


<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
 xmlns:core="http://jboss.com/products/seam/core"
 xmlns:persistence="http://jboss.com/products/seam/persistence"
 xmlns:transaction="http://jboss.com/products/seam/transaction"
 xmlns:web="http://jboss.com/products/seam/web"
 xmlns:security="http://jboss.com/products/seam/security"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:ui="http://jboss.com/products/seam/ui"
 xmlns:mail="http://jboss.com/products/seam/mail"
 xsi:schemaLocation=
 "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd
 http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd
 http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd
 http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd
 http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd">

 <core:manager conversation-timeout="120000"
 concurrent-request-timeout="2000"
 conversation-id-parameter="cid"
 />
  <transaction:entity-transaction entity-manager="#{em}"/>
  <persistence:entity-manager-factory name="exampleDatabase"/>
  <persistence:managed-persistence-context name="em"
 auto-create="true"
 entity-manager-factory="#{exampleDatabase}"/>
 </components>

Now we need a UI to create, update values. Part of the source code of employee.xhtml is provided below [The file is under src/main/webapp]. Complete source code is provided in the zip file that can be downloaded  from Google code.


  <h:form>
            <rich:messages/>
            <s:validateAll>

                <rich:panel id="homepanel">
                    <f:facet name="header">
                       Seam Example
                    </f:facet>
                    <h:panelGrid columns="2">
                        <h:outputText value="Employee Name:"/>
                        <h:inputText value="#{employeeaction.employee.name}" required="true"/>

                        <h:outputText value="Email:"/>
                        <h:inputText value="#{employeeaction.employee.email}" required="true"/>

                        <h:commandButton action="#{employeeaction.createEmployee}" value="Create Employee"/>
                    </h:panelGrid>
                    <br/>
                    List of Existing Employees
                    <br/>
                    <rich:dataTable value="#{employeeList}" var="emp">
                        <rich:column>
                            <f:facet name="header">
                               Name
                            </f:facet>
                            <h:inputText value="#{emp.name}"/>
                        </rich:column>
                        <rich:column>
                            <f:facet name="header">
                                Email
                            </f:facet>
                            <h:inputText value="#{emp.email}"/>
                        </rich:column>
                        <rich:column>
                            <f:facet name="header">
                                Update
                            </f:facet>
                            <h:commandButton action="#{employeeaction.update}" value="Update"/>
                        </rich:column>

                    </rich:dataTable>
                </rich:panel>
            </s:validateAll>
        </h:form>

The faces-config.xml file is provided below (File is under src/main/webapp/WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

    <!-- Facelets support -->
    <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    </application>

</faces-config>

We do not utilize pages.xml defining page navigation in Seam but for the sake of completeness it is provided below. (File is under src/main/webapp/WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<pages xmlns="http://jboss.com/products/seam/pages"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.1.xsd">
</pages>

And here comes web.xml (The file is under src/main/webapp/WEB-INF].

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- Seam   -->

    <listener>
        <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
    </listener>

    <filter>
        <filter-name>Seam Filter</filter-name>
        <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Seam Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>richfaces</filter-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
        <init-param>
            <param-name>forceparser</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>enable-cache</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>richfaces</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <url-pattern>/seam/resource/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Document Store Servlet</servlet-name>
        <servlet-class>org.jboss.seam.document.DocumentStoreServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Document Store Servlet</servlet-name>
        <url-pattern>*.csv</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Document Store Servlet</servlet-name>
        <url-pattern>*.xls</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-mapping>
     <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
     <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.seam</url-pattern>
    </servlet-mapping>
     <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <!-- JSF parameters -->
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.richfaces.SKIN</param-name>
        <param-value>DEFAULT</param-value>
    </context-param>
    <context-param>
        <param-name>org.ajax4jsf.COMPRESS_SCRIPT</param-name>
        <param-value>true</param-value>
    </context-param>
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
  </web-app>

Dont get bored :) , there are only two more files left under main folder that are part of our web application.
We need to have a marker file called seam.properties under src/main/resources folder (that will eventually be under WEB-INF/classes of your web application).
Seam will scan and register our Seam components only if this file is detected.

And the last one is persistence.xml (The file is under src/main/resources/META-INF)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="exampleDatabase" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!--Uncomment the below when you use Tomcat-->
   <jta-data-source>java:comp/env/jdbc/exampleDatabase</jta-data-source>
    <!--Uncomment the below when you run test cases in embedded server-->
    <!--<non-jta-data-source>java:/exampleDatabase</non-jta-data-source>-->

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="validate"/>
      <property name="hibernate.show_sql" value="false"/>
    </properties>
  </persistence-unit>
</persistence>

Now we shift our focus to test folder. Let us start with testng.xml (TestNG configuration file which is under src/test)

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="KarthiksExampleSuite">
    <parameter name="datasourceJndiName" value="java:/exampleDatabase"/>
    <parameter name="database" value="MySQL" />
    <parameter name="binaryDir" value="img/" />
    <test name="Group Test">
        <packages>
            <package name="com.karthik.test" />
        </packages>
   </test>
</suite>

Next file is the Seam based TestNG test case which runs test cases to verify the creation of a new Employee in database. The file is under src/test/java/com/karthik/test
and the source code is provided below.


package com.karthik.test;

import com.karthik.entity.Employee;
import javax.faces.model.DataModel;
import org.jboss.seam.mock.DBUnitSeamTest;
import org.testng.annotations.Test;

public class EmployeeTest extends DBUnitSeamTest {

    private int numOfEmpBeforeAdd;
    private String name;
    private String email;

    @Test(groups = "embeddedcontainer")
    public void testEmployee() throws Exception {

        new FacesRequest("/employee.xhtml") {

            @Override
            protected void updateModelValues() throws Exception {
                name = "Name of employee" + System.nanoTime();
                email = "abc" + System.nanoTime() + "@xmail.com";

                setValue("#{employeeaction.employee.name}", name);
                setValue("#{employeeaction.employee.email}", email);
            }

            @Override
            protected void invokeApplication() {
                DataModel empList = (DataModel) getValue("#{employeeList}");
                numOfEmpBeforeAdd = empList.getRowCount();
                assert invokeAction("#{employeeaction.createEmployee}") == null;

            }

            @Override
            protected void renderResponse() {

                DataModel empList = (DataModel) getValue("#{employeeList}");
                assert empList.getRowCount() == (numOfEmpBeforeAdd + 1);
                empList.setRowIndex(numOfEmpBeforeAdd + 1);
                boolean isEmployeeAvailable = false;
                for (int ctr = 0; ctr < empList.getRowCount(); ctr++) {
                    empList.setRowIndex(ctr);
                    Employee employee = (Employee) empList.getRowData();

                    if (employee.getName().equals(name) && employee.getEmail().equals(email)) {
                        isEmployeeAvailable = true;
                    }

                }//for loop ends
                assert isEmployeeAvailable;
            }
        }.run();
    }

    @Override
    protected void prepareDBUnitOperations() {
    }
}

Now comes the most important part of configuring the embedded JBoss container. The configuration is involves 3 easy steps.
Step 1: Please download embedded-jboss-beta3.SP10.zip. Extract it to any temporary folder and copy the content of bootstrap folder (do not
copy bootstrap but only the content in it) and paste it under src/test/resources
Step 2: Configure the datasource for the embedded JBoss container. The file testmysql-ds.xml (The file is placed under src/test/resources/deploy)
is as below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.com/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
   <local-tx-datasource>
      <jndi-name>exampleDatabase</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/ck</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>root</user-name>
      <password>password</password>
   </local-tx-datasource>
</datasources>

Step 3: In the above testng.xml configuration we had mentioned the binary folder as img. So create a folder called img under src/test/resources
and place an empty marker file called marker.txt (An empty marker file is needed so that Maven copies the empty folder also).

With that we complete the files under test folder. And now the most important file pom.xml which is under ROOT folder parallel to src folder.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<seam.version>2.2.0.GA</seam.version>
<richfaces.version>3.3.2.GA</richfaces.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.karthik</groupId>
<artifactId>seamtest</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>karthiks Seam TestNG Example</name>
<build>
<finalName>seamtest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<configuration>
<artifactItems>
<artifactItem>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.testOutputDirectory}/endorsed</outputDirectory>
</configuration>
<id>download-jaxb-api</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<copy todir="${project.build.testOutputDirectory}">
<fileset dir="${basedir}/src/main/webapp"/>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/testng.xml</suiteXmlFile>
</suiteXmlFiles>

<childDelegation>true</childDelegation>
<useSystemClassLoader>true</useSystemClassLoader>
<argLine>-Djava.endorsed.dirs=${project.build.testOutputDirectory}/endorsed -Dsun.lang.ClassLoader.allowArraySyntax=true</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<check>
<haltOnFailure>false</haltOnFailure>
<regexes>
<regex>
<pattern>com.karthik.*</pattern>
<branchRate>70</branchRate>
<lineRate>70</lineRate>
</regex>

</regexes>
</check>
<instrumentation>
<includes>
<include>com/**/*.class</include>

</includes>
</instrumentation>
</configuration>
<executions>
<execution>
<id>clean</id>
<phase>pre-site</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
<execution>
<id>instrument</id>
<phase>site</phase>
<goals>
<goal>instrument</goal>
<goal>cobertura</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repository.jboss.org</id>
<url>http://repository.jboss.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>repository.central.org</id>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>primefaces.repo</id>
<name>Prime Technology Maven Repository</name>
<url>http://repository.prime.com.tr</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<!--Start of dependencies for embedded JBoss container required for Integration test cases-->
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded-all</artifactId>
<version>beta3.SP10</version>
<exclusions>
<exclusion>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded</artifactId>
</exclusion>

<exclusion>
<groupId>org.jboss.microcontainer</groupId>
<artifactId>jboss-deployers-client-spi</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.microcontainer</groupId>
<artifactId>jboss-deployers-core-spi</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>thirdparty-all</artifactId>
<version>beta3.SP10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.embedded</groupId>
<artifactId>jboss-embedded</artifactId>
<version>beta3.SP10</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.microcontainer</groupId>
<artifactId>jboss-deployers-client-spi</artifactId>
</exclusion>

</exclusions>
</dependency>
<!--End of dependencies for embedded JBoss container required for Integration test cases-->

<!--Start of TestNG dependency-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.10</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
<!--el is required to parse the SeamTest setValue and invokeMethod stuff-->
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
<!-- If you use any database other than MySQL please change the below dependency accordingly-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xmlParserAPIs</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--End of TestNG dependency-->
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam</artifactId>
<version>${seam.version}</version>
<exclusions>
<exclusion>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-ui</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-pdf</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-mail</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-ioc</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-remoting</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-rss</artifactId>
<version>${seam.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.seam</groupId>
<artifactId>jboss-seam-excel</artifactId>
<version>${seam.version}</version>
</dependency>

<!-- Seam makes use of Hibernate which  depends on JTA and JPA-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>3.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.1.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>3.1.0.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>

<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_12</version>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-api</artifactId>
<version>${richfaces.version}</version>
</dependency>
<dependency>
<groupId>org.richfaces.framework</groupId>
<artifactId>richfaces-impl</artifactId>
<version>${richfaces.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-ui</artifactId>
<version>${richfaces.version}</version>

</dependency>

<!--Java Mail Related dependencies-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>

<!--Log4j-->
<dependency>
<groupId>apache-log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.8a</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>1.0.0.RC</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
</project>

Things to consider:

To run the Integration test cases comment the Tomcat specific datasource configuration and uncomment JBoss specific datasource configuration as shown in the comment section of persistence.xml reproduced below. (You can avoid this ugly way of comment and uncomment by using Maven profiles – one profile for testing and the other for a deployable in Tomcat).


<!--Uncomment the below when you use Tomcat-->
<!--jta-data-source>java:comp/env/jdbc/exampleDatabase</jta-data-source-->
<!--Uncomment the below when you run test cases in embedded server-->
<non-jta-data-source>java:/exampleDatabase</non-jta-data-source>

The test cases are executed when we run a command like mvn install

To run cobertura and generate reports, the command is mvn cobertura:cobertura (The cobertura reports will be under target/site/cobertura. Please click on the index.html to view the report.)

coberturareport

To create a deployable, make the required change in persistence.xml (as explained in Things to consider) and run the command like mvn install -Dmaven.test.skip (As the test cases will not execute with a Tomcat specific data source configuration in persistence.xml. I will try to do more analysis on this and check if it is possible to pick up different files one for test and the other to be packaged in web application deployable).

Hope this post is of some use. Please provide your suggestions (to improve this configuration) as a comment.

Seam Example By Karthikeyan C Seam Example
List of Existing Employees
Name Email Update
  • Share/Bookmark

Tutorial – How to create a simple Firefox plugin from scratch in 10 minutes

December 4th, 2009 Karthikeyan C 2 comments

In this post we will create a Firefox plugin from scratch in 10 minutes. Navigate to any folder of your choice and we will call this as ROOT folder for the plugin.

  • Create an empty folder called chrome under ROOT folder
  • Create two empty files called install.rdf and chrome.manifest under ROOT folder.

Now let us decide what we want to do with the plugin. The plugin will contain links to various social networking sites like Twitter, Orkut (we limit to 2 as it is just an example).

  • Now under chrome folder  create two empty folders called content and skin. The below screen shot shows the folder structure so far.

firefoxpluginfolders

Explanation for the folders/files created so far:

skin will contain the images and css files used in the plugin (if any). content folder will contain Javascript , XUL files involved in the plugin. chrome.manifest is self explanative and provides the information about the skin, overlay, content location to Firefox. install.rdf contains the information like plugin creator, minimum and maximum compatible versions of Firefox for the plugin.

Let us create a file called social.xul under content folder with the below lines of code.

<?xml version="1.0"?>
<overlay id="social_karthikeyanc_com"
 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml" orient="vertical">

 <script type="application/x-javascript"
 src="chrome://social/content/social.js" />
<toolbox id="navigator-toolbox">
<toolbar id="Social-Toolbar" toolbarname="Social Toolbar" accesskey="H"
 context="toolbar-context-menu"
 hidden="false" persist="hidden">
<toolbarbutton id="Orkut-Button" tooltiptext="Orkut Home"
 label="Go To Orkut" oncommand="loadURL('http://www.orkut.com')"/>
<toolbarseparator />
<toolbarbutton id="Twitter-Button" tooltiptext="Twitter Home"
 label="Go To Twitter" oncommand="loadURL('http://www.twitter.com')"/>

</toolbar>
</toolbox>
</overlay>

Now create a file called social.js under content folder with the below code.


function loadURL(url)
{
 // Set the browser window's location to the incoming URL
 window._content.document.location = url;
 // Make sure that we get the focus
 window.content.focus();
}

Please open chrome.manifest empty file with a text editor and paste the lines below.


content social jar:chrome/social.jar!/content/
overlay chrome://browser/content/browser.xul chrome://social/content/social.xul
skin social classic/1.0 jar:chrome/social.jar!/skin/

Open install.rdf and paste the lines below.


<?xml version="1.0"?>
<RDF:RDF xmlns:em="http://www.mozilla.org/2004/em-rdf#"
 xmlns:NC="http://home.netscape.com/NC-rdf#"
 xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

 <Description about="urn:mozilla:install-manifest">
 <em:id>contact@yourwebsitename.com</em:id>
 <em:version>1.0</em:version>
 <em:type>2</em:type>

 <!-- Target Application this extension can install into,
 with minimum and maximum supported versions. -->
 <em:targetApplication>
 <Description>
 <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
 <em:minVersion>2.0</em:minVersion>
 <em:maxVersion>3.5.*</em:maxVersion>
 </Description>
 </em:targetApplication>

 <!-- Front End MetaData -->
 <em:name>www.Karthikeyanc.com</em:name>
 <em:description>Karthikeyan C's Developer Blog</em:description>
 <em:creator>Karthikeyan Chockalingam</em:creator>
 <em:homepageURL>http://www.karthikeyanc.com/</em:homepageURL>
 </Description>
</RDF>

Now navigate to chrome folder and execute the below command. (I am using Ubuntu. Same can be achieved in other OS like windows using a zip utility like 7-zip)


zip -r social.jar content/* skin/*

Now navigate one step up to ROOT folder and execute the below command to create your Firefox plugin social.xpi. You can install the plugin by choosing File –Open File in Firefox menu.


zip social.xpi install.rdf chrome.manifest chrome/social.jar
  • Share/Bookmark

How to improve performance of Seam based RichFaces JSF application

December 2nd, 2009 Karthikeyan C 3 comments

I was trying to improve the performance (decrease the time taken to render the pages)   of a Seam based JSF application (Infact this post is applicable to any web application using RichFaces).

Initially the response time for JSF pages was around 12 to 20 seconds (The internet connection speed was roughly 512kbps). I tried the optimisation techniques mentioned in the links in this post http://www.karthikeyanc.com/blog/index.php/2009/11/jsf-and-richfaces-optimization-links/

Even then the page rendering time did not decrease substantially. So started using Firefox with YSlow and HttpFox add-ons.

Found that the js, css and image files related to RichFaces were expiring immediately and hence each time a JSF page was rendered, the resources (js,css and image files) were not fetched from local cache and the request was sent to the server again (It does not matter even if it is 304 Unmodified as already the damage is done and increases the response time).

Hence wrote a Servlet Filter which will override the default time to expire (which is immediate) and add a configurable duration as the time to expire. Part of the Filter code is provided below. The Filter name is ResponseHeaderFilter.java


public void init(FilterConfig filterConfig) {
 this.filterConfig = filterConfig;
 }

@Override
 public void doFilter(ServletRequest request, ServletResponse response,
 FilterChain chain)
 throws IOException, ServletException {

 HttpServletResponse httpResp = (HttpServletResponse) response;
 Enumeration e = filterConfig.getInitParameterNames();
 while (e.hasMoreElements()) {
 String headerName = (String) e.nextElement();
 httpResp.addHeader(headerName, filterConfig.getInitParameter(headerName));
 }
 chain.doFilter(request, response);
 }

In web.xml I added the following configuration for the filter.


<filter>
 <filter-name>CacheForDuration</filter-name>
 <filter-class>com.packagename.ResponseHeaderFilter</filter-class>
 <init-param>
 <param-name>Cache-Control</param-name>
 <param-value>max-age=900000, public</param-value>
 </init-param>
 </filter>

<filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/img/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/js/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/css/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/scripts/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
 <filter-name>CacheForDuration</filter-name>
 <url-pattern>/a4j/*</url-pattern>
 </filter-mapping>

Once this configuration was in place the resources (js, css and scripts) were fetched from local cache of the browser and hence the response time was reduced to 2 to 3 seconds. Hope this is useful.

  • Share/Bookmark
Categories: Development Tags: ,

Redirecting permanently in HttpServlet from your old domain (website) to new domain

November 29th, 2009 Karthikeyan C No comments

Let us assume we face a scenario where we move from a old domain (website) to a new domain (website). You may do this when you expand your business offering and the old name may not be appropriate.

But your old domain has gained reputation (like a good page rank and a number of incoming links) which you wish to carry forward to your new domain. One way of doing this is respond with HTTP code 301 which is permanently moved.

You may also make use of use Google web master tools to inform the change in domain address (as Google is currently the #1 search engine ). This will prevent the decrease in hits to your website from search engines.

The below code shows how to implement it if you host a Java web application for your old domain.

In web.xml make the below entry.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
 <servlet-name>RedirectionServlet</servlet-name>
 <servlet-class>com.olddomain.web.servlet.RedirectionServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>RedirectionServlet</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>
 </web-app>

Next the code for RedirectionServlet.java


package com.olddomain.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RedirectionServlet extends HttpServlet {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {

 //Get the incoming URL request.
 response.setHeader("Location", "http://www.yournewdomain.com"+request.getRequestURI());
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 }

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 processRequest(request, response);
 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 processRequest(request, response);
 }
}

Note: Using response.sendRedirect will produce only HTTP status code 302 which is temporarily moved and not 301 which indicates it is permanently moved.

  • Share/Bookmark

MySQL database schema browser and SQL executor in Ubuntu 9.10

November 26th, 2009 Karthikeyan C 1 comment

To install MySQL database schema browser,  SQL executor and administrator, use the following command. It gives a cool GUI for  us to work with.


sudo apt-get install mysql-query-browser mysql-admin
  • Share/Bookmark

PrimeFaces File Upload component hack

November 25th, 2009 Karthikeyan C No comments

If PrimeFaces file upload does not work, mostly the reason will be that the prependId attribute was not set to false in the h:form. After the user browses the file nothing happens and we are left wondering as there is no JavaScript error.

I have configured the PrimeFaces file upload as below in one of my UI page.


<h:form id="myform" enctype="multipart/form-data" prependId="false">
 <p:fileUpload fileUploadListener="#{userprofile.handleFileUpload}"
 allowTypes="*.jpg;*.png;*.gif;" description="User Profile Image"/>

 </h:form>
  • Share/Bookmark
Categories: Development Tags: