Creating Seam2 ejb application on eclipse using Seam Tools and JBoss AS

Creating new Seam 2 EJB application on JBoss AS 5.1 using eclipse indigo.

My Configuration:
eclipse (indigo) with JBoss Tools installed at d:\seam-dev\eclipse
<JBOSS-AS>         JBoss AS 5.1.0 installed at D:\seam-dev\jboss-5.1.0.GA
<SEAM-HOME>      D:\jboss-seam-2.2.2.Final

Preparations:
create new workspace for eclipse
add Jboss AS 5.1 from above location to workspace (use “default” server profile)
copy hsqldb.jar from <SEAM-HOME>\lib to <JBOSS-AS>\lib folder.

Steps to create EMPTY EJB application :
1    Create new “Seam Web Project” with name Test-ent1 –> use JBoss AS 5.1 as server with default settings –>next


2    Use libaries provided by Runtime for JSF –>next


3    Application type: EAR with two modules : test-ent1-ejb and test-ent1-ear


4    Database connection : create new HSQL DB connection to database (empty database)
5    finish to create application.


6    Click on run to Run created application on JBoss AS 5.1 (data source first!  + test-ent1.ear)


NOTE: “war” module is part of “ear” so no need to run “Test-ent1” (war module) directly on server!
7    Incase if you got 404 error, just wait for server to finish loading (see server log in output panel) and then refresh page.

Steps to add “Entities”:
1    New –> New Seam
Entity : entity name: person


2    Open “Person.java” , add following two fields

 @Length(max=300)
 private String address;
 @Length(max=12,min=8)
 private String contact;

3    Generate getters and setters for them.
4    Open person.xhtml (from test-ent1 project), add follwoing lines just after “</s:decorate>”

 <s:decorate id="addressField" template="layout/edit.xhtml">
 <ui:define name="label">Address</ui:define>
 <h:inputText id="address" required="true"
 value="#{personHome.instance.address}"/>
 </s:decorate>
 <s:decorate id="contactField" template="layout/edit.xhtml">
 <ui:define name="label">Contact Number</ui:define>
 <h:inputText id="contact" required="true"
 value="#{personHome.instance.contact}"/>
 </s:decorate>

5    Open personList.xhtml page, change columns (inside <h:dataTable> )to following:

 <h:column>
 <f:facet name="header">Id</f:facet>
 #{person.id}
 </h:column>
 <h:column>
 <f:facet name="header">Name</f:facet>
 #{person.name}
 </h:column>
 <h:column>
 <f:facet name="header">Address</f:facet>
 #{person.address}
 </h:column>
 <h:column>
 <f:facet name="header">Phone number</f:facet>
 <s:link id="person"
 value="#{person.contact}"
 propagation="none"
 view="/person.xhtml">
 <f:param name="personId"
 value="#{person.id}"/>
 </s:link>
 </h:column>

6    Remove application from Server.


7    add following line inside “layout\menu.xhtml” to create menu item (only if not generated by eclipse)

 <s:link id="menuPersonListId" view="/personList.xhtml" value="Person" propagation="none"/>

8    Run application (causing un-deploy and then deploy)

Convert Seam2 example to eclipse project

Convert Seam example applications to eclipse Project

Project folder structure in seam 2. x examples is different than folder structure used by seam tools project.(Project created in eclipse using JBoss Tools).

Seam (JBoss) Tools  project:

  1. src/
    1. hot/
      1. <ALL-Action-classes>
      2. <other-classes-for-HOT-deployment>
      3. seam.properties <empty file>
    2. main/
      1. <ALL-entity/model-classes>
      2. components.propeties
      3. import.sql (Database DDL and DML statements)
      4. messages_en.properties
      5. security.drl
      6. seam.properties <empty file>
      7. META-INF/
        • persistence.xml
  2. WebContent/
    1. css/
    2. img/
    3. META-INF/
      1. context.xml
    4. <All-xhtml-pages>
    5. <All-xhtml.xml-pages>
    6. WEB-INF/
      1. classes/
      2. web.xml
      3. component.xml
      4. faces-config.xml
      5. jboss-web.xml (only for JBoss AS)
      6. pages.xml
  3. hibernate-console.properties
  4. <project_name>.launch

Convert “jboss-seam-jpa” example Application to eclipse (Seam) project.

1         Create new “Seam Web Project” à enter name of project “jboss-seam-jpa”

2         accept all details in above screen and press “Next” àNextàNext

3         Disable Library configuration for JSF capabilities (You need to copy all JAR’s manually to Tomcat’s LIB folder) àNext

4         Select (Or add new) JBoss Seam Runtime

  1. Deploy As :                          WAR
  2. Libraries:                             Copy library from Seam Runtime
  3. Database Type:                 Select HSQL
  4. Connection Profile:         create new Connection profile for HSQL DB
  5. DB Tables already exists in Database: FALSE
  6. Recreate Database tables and data on deploy : TRUE
  7. Package name(session):             org.jboss.seam.example.jpa
  8. Package name(Entity):                 org.jboss.seam.example.jpa
  9. Create Test Project:                      NO

5         Now click finish to generate Project

6         Now create new Package “org.jboss.seam.example.jpa” under source folder “src/main”

7         Delete auto-created “Authenticator.java” from src/hot

8         Open <SEAM-HOME> /LIB folder, copy following files:

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 (ONLY FOR SEAM VER = 2.2.2.FINAL)
  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)

Database Driver

1    hsqldb.jar

Drools:

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

9         paste them all inside <TOMCAT-HOME>/LIB

10     Open <SEAM-HOME>/examples/jpa folder

11     open “src/org/jboss/seam/example/jpa” folder

12     Copy all java classes which ends with “Action”

13     inside eclipse goto src/hot/org/jboss/seam/example/jpa folder à delete existsing classes

14     inside eclipse, goto src/hot/org/jboss/seam/example/jpa folder à paste

15     copy remaining files from “src/org/jboss/seam/example/jpa” folder to src/main/org/jboss/seam/example/jpa folder

16     inside eclipse, delete all .xhtml and .xml files from WebContent folder, delete “css” , “img” & “layout” folders

17     Open “VIEW” folder of examples /jpa àcopy all files

18     inside eclipse, paste them all in “WebContent” folder (Overwrite duplicates)

19     Open “Resource-tomcat” folder from your examples/jpa

20     copy import.sql file

21     inside eclipseà paste it in src/main

22     Copy “persistence.xml” from resource-tomcat/META-INF to eclipse project’s src/main/META-INF

23     Copy “context.xml” from resource-tomcat/META-INF to eclipse project’s WebContent/META-INF

24     copy “WEB-INF” folder from resources-tomcat to eclipse project’s WebContent folder (Overwrite existsing WEB-INF)

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