Java Tutorial - Java Scipt : Running the TestUserEJB Application

Java Tutorial - Java Scipt :

Running the TestUserEJB Application


The application can be deployed into JBoss using the deploy task in the build.xml file. The application can then be executed with the following URL: http://localhost:8080/ch14/servlet/TestUserEJB The response should consist of the following lines:

Hello:Hello World!
 Name:John Doe 0000000-000000-00000000-0-00000000-1

Java Tutorial - Java Scipt : Compiling the TestUserEJB Application

Java Tutorial - Java Scipt :

Compiling the TestUserEJB Application


The following build.xml file can be used with Ant to compile the application and create a .war file for deployment into JBoss.

<?xml version=”1.0” encoding=”UTF-8”?>
<project basedir=”.” default=”compile” name=”ch14”>
<property name=”app.name” value=”ch14”/>
<property name=”jboss.home”
value=”C:/openjava/jboss-3.0.6_tomcat-4.1.18”/>
<property name=”jboss.deploy”
value=”${jboss.home}/server/default/deploy”/>
<property name=”lib.dir” value=”${jboss.home}/client”/>
<path id=”build.classpath”>
<fileset dir=”${lib.dir}”>
<include name=”*.jar”/>
</fileset>
</path>
<target name=”init”>
</target>
<target depends=”init” name=”compile”>
<javac
classpathref=”build.classpath”
debug=”true”
deprecation=”true”
destdir=”./WEB-INF/classes”
srcdir=”./WEB-INF/classes”>
</javac>
</target>
<target depends=”compile” name=”war”>
<war destfile=”${app.name}.war”
update=”no”
excludes=”build.xml”
webxml=”./WEB-INF/web.xml”>
<fileset dir=”.”>
<include name=”**/*.jar”/>
<include name=”**/*.class”/>
<include name=”**/*.java”/>
<include name=”**/*.xml”/>
<exclude name=”**/web.xml”/>
</fileset>
</war>
</target>
<target depends=”war” name=”all”/>
<target depends=”war” name=”deploy”>
<copy file=”${app.name}.war” todir=”${jboss.deploy}”/>
</target>
<target depends=”war” name=”undeploy”>
<delete file=”${jboss.deploy}/${app.name}.war”/>
</target>
</project>

The .war target compiles any code that still needs to be compiled and builds the .war file. Make certain you edit the jboss.home and app.name properties near the top of the file to reflect the values for your system. You can include the Java source code in your .war files so that they can be edited in place. If you do not want to include the source code, then remove the line in the .war task that reads:
<include name=”**/*.java”/>

Java Tutorial - Java Scipt : The TestUserEJB Servlet

Java Tutorial - Java Scipt :

The TestUserEJB Servlet


The next step is to create a servlet that uses the UserDelegate class. The source code for the servlet follows:

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oldfriends.user.*;
import javax.ejb.*;
public class TestUserEJB extends HttpServlet
{
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
UserDelegate ud;
try
{
ud = new UserDelegate();
UserDTO udata =
ud.getUserByUserid(“0000000-000000-00000000-0-00000000-1”);
out.println(“Hello:”+ud.getHello());
out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
}
catch( Exception e )
{
e.printStackTrace(out);
}
}
}

Note that the servlet has no knowledge that the actual implementation of UserDelegate occurs via an EJB.


Java Tutorial - Java Scipt : Rebuilding the .jar File

Java Tutorial - Java Scipt :

Rebuilding the .jar File


We do not need to make any changes to the existing Ant script to rebuild the .jar file. It will happily build the new EJB and install the new .jar file by executing the following command: ant deploy If nothing goes wrong, the output will be similar to what we saw earlier when we compiled and deployed the bean under the Compiling the Bean
section.

Java Tutorial - Java Scipt : Building the Test Client

Java Tutorial - Java Scipt :

Building the Test Client


We will add a new method to our test client to test the new session bean, as shown in the following code:
public void userSessionTest(
{
UserSessionRemote remote;
UserSessionHome home;
System.out.println(“userSessionTest:”);
try
{
System.out.println( “Getting Home Interface”);
home = (UserSessionHome)getHomeInterface(
“ejb/UserSession”,
com.oldfriends.session.UserSessionHome.class);
System.out.println( “Getting Remote Interface”);
remote = home.create();
System.out.println(remote.toString());
System.out.println( “Calling getHello”);
System.out.println(“Hello:”+remote.getHello());
System.out.println( “Retrieving Records”);
System.out.println( “getUserByUserid”);
UserDTO udata =
remote.getUserByUserid(“0000000-000000-00000000-0-00000000-1”);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
System.out.println( “getUserByLogin”);
udata = remote.getUserByLogin(“jsmith”);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
System.out.println( “Update User”);
udata.setFirstName(“NotJack”);
remote.updateUser(udata);
udata = remote.getUserByLogin(“jsmith”);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
System.out.println( “Add User”);
udata.setFirstName(“One”);
udata.setLastName(“LastName”);
udata.setMi(“Z”);
udata.setMaidenName(“”);
udata.setLoginId(“lgid”);
udata.setPassword(“somepwd”);
udata.setEmail(“email@foo.bar”);
udata = remote.addUser(udata);
System.out.println( “Name:”+
udata.getFirstName() + “ “ +
udata.getLastName()+” “+udata.getUserid());
remote.remove();
}
catch( Exception ex )
{
System.err.println(
“userSessionTest: Caught an unexpected exception!”);
ex.printStackTrace();
}
finally
{
remote = null;
home = null;
System.out.println( “userSessionTest: Finally”);
}
}
The new userSessionTest method is similar to the userEntityTest method covered earlier, except that it uses the UserDTO object to move user data between the client the EJB container. We demonstrate the session bean methods for retrieving users, updating users, and adding a user to the system. We also need to make a change to the main method so that our new method is executed. The new main method follows:
public static void main(String[] args)
{
UserClient uc = new UserClient();
uc.userEntityTest();
uc.userSessionTest();
}
We can execute this from Ant without any further changes. Use the following command to run this by using Ant:
ant run-test The output will be similar to the output previously observed for the entity  bean under the Testing the Bean section.
.

Java Tutorial - Java Scipt : Changing the jboss.xml File

Java Tutorial - Java Scipt :

Changing the jboss.xml File


Because this is not an entity bean, we do not need to modify the jbosscmpjdbc. xml file. The final changes we need to make for deploying our session bean are made to the jboss.xml file. The change is the four following lines:

<session>
<ejb-name>UserSession</ejb-name>
<jndi-name>ejb/UserSession</jndi-name>
</session>

These lines should be added immediately after the </entity> tag that we put into the file earlier.

Java Tutorial - Java Scipt : Creating a Deployment Descriptor

Java Tutorial - Java Scipt :

Creating a Deployment Descriptor

Now, we need to modify the EJB deployment descriptor ejb-jar.xml to add our new session bean. We will start with the file we created earlier, and this time just focus on the additions needed to add our session bean. The first part of this addition is shown below. It should be added immediately after the closing

</entity> tag:
<session>
<display-name>UserSession</display-name>
<ejb-name>UserSession</ejb-name>
<home>com.oldfriends.session.UserSessionHome</home>
<remote>com.oldfriends.session.UserSessionRemote</remote>
<ejb-class>com.oldfriends.session.UserSessionEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

The values here are almost identical to those we have already discussed for the entity bean. There are a couple of differences here. The <session-type> can be either Stateless or Stateful. Ours is a stateless bean. The transaction
type is set for the container to manage transactions. The next section, which follows, is the <ejb-local-ref> element, which is needed so that we can find the entity bean:

<ejb-local-ref>
<ejb-ref-name>ejb/LocalUserEJB</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>com.oldfriends.entity.UserLocalHome</local-home>
<local>com.oldfriends.entity.UserLocal</local>
<ejb-link>UserEJB</ejb-link>
</ejb-local-ref>

The <ejb- ref-name> value should be set to the JNDI name that is used in our code to retrieve the LocalHome interface of the entity bean. The <ejb-link> provides the connection between this JNDI name and the entry for the bean. This will be used in the jboss.xml file to complete the JNDI mapping. The <ejb-ref-type> tells the container whether we are referencing an entity bean or a session bean. The <local-home> and <local> elements provide the fully qualified class names for the LocalHome and local interfaces for the bean we are referencing. The next section is also a reference, but it is a resource reference that will be used to get the DataSource from the container, as you can see in the following code:

<resource-ref>
<description>DataSource for oldfriends database</description>
<res-ref-name>java:/jdbc/MySqlDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Sharable</res-sharing-scope>
</resource-ref>
</session>

The <res-ref-name> is the JNDI name that we use in our code to get the DataSource. The <res-type> element provides the fully qualified class name of the class that is returned by the lookup. The <res-auth> element says that the container has the responsibility for providing username and password needed to generate connections from the DataSource. Finally, <res-sharing-scope> says that this resource is sharable with other beans. The </session> tag ends the changes to the ejb-jar.xml file.