Seam 2.2.2.Final with Apache Tomcat 7.0

From last three days, I was messing up with Seam 2.2, trying to get it work with Tomcat 7.

Seam can work with JBoss AS, GlassFish & other AS. but Tomcat being just a Servlet Container has many drawbacks.

My Setup:

  • JDK 1.7.0
  • Apache Tomcat 7.0
  • eclipse indigo (JBossTools  for indigo installed)
  • Seam 2.2.2.Final
  • JPA with Transaction type: RESOURCE_LOCAL (an alternative to default JTA)
  • Database : MySql 5.1 with MySql J/Connection 5.1

From Seam 2.2.2Final distribution, locate LIB folder, copy following JARs to tomcat 7/LIB folder:

Hibernate dependencies:

  1. annotations-api.jar
  2. cglib-nodep.jar
  3. commons-collections.jar
  4. commons-logging.jar
  5. dom4j.jar
  6. javassist.jar

Hibernate:

  1. hibernate-core.jar
  2. hibernate-annotations.jar
  3. hibernate-commons-annotations.jar
  4. hibernate-entitymanager.jar
  5. hibernate-search.jar
  6. lucene.jar
  7. hibernate-validator.jar

JSF 1.2:

  1. jsf-api.jar
  2. jsf-impl.jar
  3. jsf-facelets.jar

JPA:

  1. jta.jar
  2. persistence-api.jar
  3. ejb3-persistence.jar (used by hibernate+JPA)

Logging for Hibernate and Seam:

  1. log4j.jar
  2. slf4j-api.jar
  3. slf4j-log4j12.jar
  4. log4j.properties (for debug messages)

Drools:

  • drools-api.jar
  • drools-compiler.jar
  • drools-core.jar
  • drools-decisiontables.jar
  • drools-templates.jar

Now,after adding all these JAR’s your tomcat, your tomcat is ready to host seam 2.2 application.

  1. Create new Seam Web Project in eclipse
    Step 1
  2. Use Apache Tomcat 7.0 as Application Server (add if not done already)
  3. use selected configuration (which includes Seam 2.2)
  4. JSF Configuration: Disable Library configuration (We have added all JSF 1.2 Jars to our Tomcat)
  5. Use Seam Runtime : <<select folder where  you have extracted Seam 2.2.2>>
  6. Use Database connection New MSQL (Click on “New” button to create new connection, make sure that you have already created one EMPTY database in MySQL with name TestDB2)
  7. Click Finish to end project creation.
  8. Now, open “Package Explorer” 
  9. Inside package explorer … now you will get “WebContent” Folder which contains another one “META-INF” folder. I will now create context.xml inside this folder. This context will be deployed at the time of application deployment, which will result in deploying a (local) javax.sql.DataSource to Tomcat for current web application.
    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/TestApp15" docBase="TestApp15" crossContext="true">
        <Resource name="MyData" auth="Container" 
            type="javax.sql.DataSource"
            driverClassName="com.mysql.jdbc.Driver"
            username="root" password="mahendra" 
            url="jdbc:mysql://localhost:3306/TestDB2"/>
    </Context>
  10. Now, there is another META-INF inside src/main with one “persistence.xml”. make few changes in it:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Persistence deployment descriptor for dev profile -->
    <persistence 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" 
                 version="1.0">
    
       <persistence-unit name="TestApp15PU" transaction-type="RESOURCE_LOCAL">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:comp/env/MyData</non-jta-data-source>
          <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>
             <property name="hibernate.default_catalog" value="TestDB2"/>
    
          </properties>
       </persistence-unit>
    </persistence>
  11. We are using NON JTA data source instead of default JTA, we are not using any Caching provider from Hibernate so remove Caching provider if any.
  12. From WebContents/WEB-INF folder, open “components.xml”, make following changes:
    <?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:tx="http://jboss.com/products/seam/transaction"
                xmlns:drools="http://jboss.com/products/seam/drools"
                xmlns:bpm="http://jboss.com/products/seam/bpm"
                xmlns:security="http://jboss.com/products/seam/security"
                xmlns:mail="http://jboss.com/products/seam/mail"
                xmlns:web="http://jboss.com/products/seam/web"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation=
                    "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                     http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
         http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd 
                     http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                     http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                     http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                     http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                     http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                     http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
            <core:init />
       <core:manager concurrent-request-timeout="500"
                     conversation-timeout="120000"
                     conversation-id-parameter="cid"
                     parent-conversation-id-parameter="pid"/>
       <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
       <web:hot-deploy-filter url-pattern="*.seam"/>
       <!-- If you are still using JBoss 4, uncomment this to have your PU started -->
    <!--   <persistence:entity-manager-factory name="entityManagerFactory"-->
    <!--                      persistence-unit-name="TestApp15"/>-->
       <!-- If you are still using JBoss 4, uncomment this to have the entityManagerFactory ysed -->
    <!--   <persistence:managed-persistence-context name="entityManager" auto-create="true"-->
    <!--                          entity-manager-factory="#{entityManagerFactory}"/>-->
            <tx:entity-transaction entity-manager="#{entityManager}" auto-create="true"/>
      <persistence:managed-persistence-context 
                   name="entityManager" 
                   auto-create="true"
                   entity-manager-factory="#{entityManagerFactory}"/>
      <persistence:entity-manager-factory 
                   name="entityManagerFactory" 
                   auto-create="true" 
                   persistence-unit-name="TestApp15PU"/>
       <drools:rule-base name="securityRules">
          <drools:rule-files><value>/security.drl</value></drools:rule-files>
       </drools:rule-base>
       <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
       <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
       <event type="org.jboss.seam.security.notLoggedIn">
          <action execute="#{redirect.captureCurrentView}"/>
       </event>
       <event type="org.jboss.seam.security.loginSuccessful">
          <action execute="#{redirect.returnToCapturedView}"/>
       </event>
       <mail:mail-session host="localhost" port="25"/>
       <!-- For use with jBPM pageflow or process management -->
       <!--
       <bpm:jbpm>
          <bpm:process-definitions></bpm:process-definitions>
          <bpm:pageflow-definitions></bpm:pageflow-definitions>
       </bpm:jbpm>
       -->
    </components>
  13. Run application in Tomcat.

NOTE: You may download Video screen cast for the above demo at my box account