Archive

Posts Tagged ‘JSF’

Chrome error – NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7

June 6th, 2010 Karthikeyan C No comments

I came across the below error in Chrome while rendering a page ( the same page was working fine in Firefox, Opera).

NO_MODIFICATION_ALLOWED_ERR: DOM Exception 7

I was using JSF 1.2 with Facelets. The solution is to mention the contentType attribute value as text/html in the f:view tag.


<f:view xmlns:f="http://java.sun.com/jsf/core" contentType="text/html">

Note: If the content is not mentioned as text/html, strict validation will occur where few DOM modifications like assigning to innerHTML is prohibited and hence the error.

  • Share/Bookmark
Categories: Development Tags:

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

December 7th, 2009 Karthikeyan C 4 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

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: ,