Exercise 11.2: XML Override




Exercise 11.2: XML Override

This exercise is a duplicate of Exercise 11.1, except that the minimum check number required to make a check payment has been overridden in the ejb-jar.xml deployment descriptor.

Start Up JBoss

If you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in Workbook 1.

Initialize the Database

The database tables will be created when Exercise 11.2 is deployed to JBoss. If you have problems running this example, shut down JBoss and run the clean.db Ant task.

Build and Deploy the Example Programs

Perform the following steps:

  1. Open a command prompt or shell terminal and change to the ex11_2 directory created by the extraction process.

  2. Set the JAVA_HOME and JBOSS_HOME environment variables to point to where your JDK and JBoss 4.0 are installed. Examples:


    Windows:

    C:\workbook\ex11_2> set JAVA_HOME=C:\jdk1.5.0
    C:\workbook\ex11_2> set JBOSS_HOME=C:\jboss-4.0.x
    


    Unix:

    $ export JAVA_HOME=/usr/local/jdk1.5.0
    $ export JBOSS_HOME=/usr/local/jboss-4.0
    

  3. Add ant to your execution path. Ant is the build utility.


    Windows:

    C:\workbook\ex11_2> set PATH=..\ant\bin;%PATH%
    


    Unix:

    $ export PATH=../ant/bin:$PATH
    

  4. Perform the build by typing ant.

As in the earlier exercise, titan.jar is rebuilt, copied to the JBoss deploy directory, and redeployed by the application server.

Examine ProcessPaymentBean

In the ProcessPaymentBean implementation, we defined a minCheckNumber constant that was used to verify that a customer's check number was above a certain number. Otherwise, the payment was not allowed to be processed:

package com.titan.processpayment;

import com.titan.domain.*;

import java.sql.*;

import javax.ejb.*;
import javax.annotation.Resource;
import javax.sql.DataSource;
import javax.ejb.EJBException;

@Stateless
public class ProcessPaymentBean implements ProcessPaymentRemote,
                                           ProcessPaymentLocal
{

   final public static String CASH = "CASH";
   final public static String CREDIT = "CREDIT";
   final public static String CHECK = "CHECK";

   @Resource 
(mappedName="java:/DefaultDS") DataSource dataSource;

@Resource(name="min") int minCheckNumber = 100;

In Exercise 11.1, the minCheckNumber field's @Resource annotation was really more for documentation purposes than to provide any functionality for the application. In Exercise 11.2, however, we override this constant as an <env-entry> with ejb-jar.xml .

ejb-jar.xml
<ejb-jar
       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/ejb-jar_3_0.xsd"
       version="3.0">
   <enterprise-beans>
      <session>
         <ejb-name>ProcessPaymentBean</ejb-name>
<env-entry>
<env-entry-name>min</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>10</env-entry-value>
</env-entry>
      </session>
   </enterprise-beans>
</ejb-jar>

<env-entry-name> matches @Resource 's name( ) attribute and minCheckNumber is overridden with the value of 10.

Examine the Client

The main client is the com.titan.clients.MakePayment class:

package com.titan.clients;

import com.titan.processpayment.*;
import com.titan.domain.Customer;
import com.titan.access.DataAccess;

import java.util.Calendar;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

/**
 * Example demonstrating use of ProcessPayment EJB directly
 *
 */

public class MakePayment
{

   public static void main(String [] args)
   {
...

         System.out.println("Making a payment using byCheck( ) with a low
check number..");
         CheckDO check2 = new CheckDO("111000100111010110101", 50);
         try
         {
            procpay.byCheck(cust,check2,9000.0);
            System.out.println("The PaymentException has not been raised
because the min check number has been overridden in ejb-jar.xml");
         }
         catch(PaymentException pe)
         {
            System.out.println("Caught PaymentException:
 
 "+pe.getMessage( ));
         }

In Exercise 11.1, this bit of code aborted with a PaymentException because the check number was too low. Because the minCheckNumber field in ProcessPaymentBean has been overridden with XML, this check payment is now successful.

Run the Client Application

Run the MakePayment application by invoking ant run.payment at the command prompt. Remember to set your JBOSS_HOME and PATH environment variables. This is the output:

run.payment:
     [java] Making a payment using byCash( )..
     [java] Making a payment using byCheck( )..
     [java] Making a payment using byCredit( )..
     [java] Making a payment using byCheck( ) with a low check number..
     [java] The PaymentException has not been raised because the min check number
            has been overridden in ejb-jar.xml