JAX-WS (Sun Ms)Creation

Jax-WS, EJB, EntityManger & Hibernate

1.1   Initial Set up

1.1.1   Required jar files

  No
Jar file
Component
1
antlr-2.7.6.jar
JAX-WS
2
commons-collections-3.1.jar
3
dom4j-1.6.1.jar
4
jaxb-impl.jar
5
jaxws-api.jar
6
jaxws-rt.jar
7
jsr311-api-1.1.jar
8
slf4j-api-1.5.8.jar
9
slf4j-log4j12-1.5.10.jar
10
stax-ex.jar
11
streambuffer.jar
12
hibernate-core-3.6.7.Final.jar
hibernate
13
hibernate-entitymanager-3.4.0.GA.jar
14
hibernate-jpa-2.0-api-1.0.0.Final.jar
15
postgresql-9.1-901.jdbc4.jar
postgres jdbc jar



 Note- Jax-WS jars to be cross checked.
Copy the postgresql-9.1-901.jdbc4.jar to <Jboss server root folder>\server\default\lib  and others should be placed in <web-module>/WEB-INF/lib

1.1.2   Configure Data Source     

a)    Start the server Jboss Server and login to admin console with default credentials(admin/admin)
b)    Configure the data source (Resources -> Local Tx Datasources)                                                  
                   JNDI Name = postgresDS
                                Username = postgres
                                Password   =  <password of db>
                                JDBC Driver Class = org.postgresql.Driver
                                Connection URL = jdbc:postgresql://localhost:5432/postgres           
1.2  Entity Manager
1.   Create the Persistence entity classes and corresponding mapping file (hbm.xml). Put these files under the same folder.
package employee;
import java.io.Serializable;
import org.hibernate.annotations.Entity;
@Entity
public class EmployeeTO  implements Serializable {
            private Long id;
            private String name;
            private Float Salary;
            private String department;
            public EmployeeTO() {
                        super();
            }
            public Long getId() {
                        return id;}

            public void setId(Long id) {
                        this.id = id;
            }
            public String getName() {
                        return name;
            }
            public void setName(String name) {
                        this.name = name;
            }
            public Float getSalary() {
                        return Salary;
            }
            public void setSalary(Float salary) {
                        Salary = salary;
            }
            public String getDepartment() {
                        return department;
            }
            public void setDepartment(String department) {
                        this.department = department;
            }
                    --------------------------hbm.xml------------------------
<?xml version="1.0"?>  <!DOCTYPE hibernate-mapping PUBLIC
                    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
            <class name="employee.EmployeeTO" table="Employee">
                        <id name="id" column="id">
                                    <generator class="sequence" />
                        </id>
                        <property name="name" />
                        <property name="Salary" />
                        <property name="department" />
            </class>
</hibernate-mapping> 

2.       Create persistence.xml file, place under \ejbModule\META-INF\.  Add the below entries in persistence.xml
a.       Create persistsnce-unit
b.      Set the created data source
c.        Add all the persistence entries classes to it
d.      Set hibernate dialect specific to Postgres database


<?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="employeeservice"  >
                        <provider>org.hibernate.ejb.HibernatePersistence</provider>
                        <jta-data-source>java:/postgresDS</jta-data-source>
                        <class>employee.EmployeeTO</class>
                        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                                    <property name="hibernate.hbm2ddl.auto" value="update" />
                        </properties>
            </persistence-unit>
</persistence>


           NoteFirst time to create the table definitions from hibernate configuration set hibernate.hbm2ddl.auto to “create-drop” then stop the server reset to “update” (otherwise every time server started it drops the old tables & create new tables).

1.3  EJB

1.       Create stateless session bean with remote interface as shown below.
2.       Inject the EntityManager in EJB and pass it to DAO to process the requested operation.
package employee.ejb;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import dao.EmployeeDAO;
import employee.EmployeeTO;
/**
 * Session Bean implementation class EmployeeEJB
 */
@Stateless(mappedName = "EmployeeEJB")
@LocalBean
public class EmployeeEJB implements EmployeeEJBRemote, EmployeeEJBLocal {
            public EmployeeEJB() {
                        // TODO Auto-generated constructor stub
            }
            @PersistenceContext(unitName = "employeeservice")
            EntityManager  entityManager;

            @Override
            public EmployeeTO getEmployeeDetails(Long empId) {
                        return new EmployeeDAO(entityManager).findEmployeeTO(empId);
            }

            public String getName() {
                        return " Employee EJB ";
            }
}
package employee.ejb;
import javax.ejb.Remote;
import employee.EmployeeTO;

@Remote
public interface EmployeeEJBRemote {
            public EmployeeTO getEmployeeDetails(Long empID);
            public String getName();
}

1.4    JAX-WS Creation

1.       Create the web service EmployeeWebService with annotations as shown below.
2.       Web Service call lookup EJB and transfer the call on EJB.
package com.sample.webservice;
import java.util.Properties;
import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import employee.EmployeeTO;
import employee.ejb.EmployeeEJBRemote;

@WebService
public class EmployeeWebService {
            @WebMethod
            public EmployeeTO getEmployeeDetails(@WebParam(name = "EmpId") Long empId) throws Exception {
           
                        if(empId == null)
                        {          
                                    EmployeeTO employeeTO =new EmployeeTO();
                                    employeeTO.setErrorCode("100");
                                    employeeTO.setErrorDiscription("Please enter valid employee Id");
                                    return employeeTO;
                        }
                        return lookUpEJB().getEmployeeDetails(empId);
            }
           
            public EmployeeWebService()
            { }
            //  EJB Injection is not working, using standard lookup
            public EmployeeEJBRemote lookUpEJB() {
                        EmployeeEJBRemote employeeEJB  = null;
        try {
            java.util.Properties props = new Properties();
            props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
            props.setProperty("java.naming.provider.url", "127.0.0.1:1099");        
            Context ctx = new InitialContext(props);           
            employeeEJB = (EmployeeEJBRemote) ctx.lookup("EmployeeEJB");
                     
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return employeeEJB;
    }
}

3.       Create file sun-jaxws.xml under WEB-INF folder and add end point entry for created web service as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint implementation="com.sample.webservice.EmployeeWebService" name="EmployeeWebService"  url-pattern="/employee"/>
</endpoints>

4.       Add required configuration’s in web.xml
a)    Add the listener class tag  for WSServletContextListener
b)    Add the WSServlet entry & map all url-pattern mentioned in sun-jaxws.xml to it.
<?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">

            <listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
            </listener>
            <servlet>
                        <servlet-name>WebService</servlet-name>
                        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
                        <load-on-startup>1</load-on-startup>
            </servlet>
           

            <servlet-mapping>
                        <servlet-name>WebService</servlet-name>
                        <url-pattern>/employee</url-pattern>
            </servlet-mapping>
           
            <welcome-file-list>
                        <welcome-file>index.html</welcome-file>
            </welcome-file-list>
</web-app>


5.       Access the deployed service on http://localhost:8080/<projectname>/<serverlet name>
            Ex:  http://localhost:8080/SampleWebservice/employee