June 10, 2007, 2:45 p.m.
posted by hashspark
Exercise 11.2: XML OverrideThis 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 JBossIf you already have JBoss running, there is no reason to restart it. Otherwise, start it up as instructed in Workbook 1. Initialize the DatabaseThe 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 ProgramsPerform the following steps:
As in the earlier exercise, titan.jar is rebuilt, copied to the JBoss deploy directory, and redeployed by the application server. Examine ProcessPaymentBeanIn 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 ClientThe 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 ApplicationRun 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
|
- Comment