23.2. Example

This example was tested on JBoss 3.0.0-Tomcat 4.0.6

23.2.1. Example interceptor

 public class BasicTestInterceptor extends DbEventInterceptorSupport
 {
 
  static Category log = Category.getInstance(BasicTestInterceptor.class.getName());
 
  /**
   * Checks to see if a number is in range inclusively given three Strings.
   * @param check String representing a number.
   * @param low String representing the low number of the range.
   * @param high String representing the high number of the range
   * @param inclusive set to true if you want the low <= check and check <= high .
   * @param digits the number in digits to the left of the decimal to round.
         *  positive, non-zero int
   * @return value converted to a string.
   * @throws ValidationException when encountering other exceptions like
         *  NumberFormatException.
   *
   */
  public static boolean inRange(String check, String low, String high, boolean inclusive)
    throws ValidationException
  {
    try
    {
     NumericValidationLocal numericValidation = EjbUtil.getNumericValdiationBean();
     if(numericValidation==null)
     {
       System.out.println("Null numericValidation");
       return false;
     }
     return numericValidation.inRange(check,low,high,inclusive);     
      }
      catch(MathException me)
      {
      throw new ValidationException(me.getMessage());
    }
    catch(Exception e)
    {
      throw new ValidationException("");
    }
 
     }//inRange(...)
 
  ...
 }//BasicTestInterceptor
 
 

23.2.1.1. NumericValidationLocal

 package com.atser.sejb.math;
 

 public interface NumericValidationLocal 
  extends javax.ejb.EJBLocalObject
 {
 
  /**
   * Checks to see if a number is in range.
   * Range is inclusive given three Strings.
   * @param check String representing a number.
   * @param low String representing the low number of the range.
   * @param high String representing the high number of the range
   * @param inclusive set to true 
   *  if you want the low <= check and check <= high .
   * @param digits the number in digits to the 
   *  left of the decimal to round. positive, non-zero integer only. 
         *  For example: 1438 with 1 passed in will give us 1440.
   * @return value converted to a string.
   * @throws ValidationException for any number format problems.
   */
  public boolean inRange(
      String check, 
      String low, 
      String high, 
      boolean inclusive
      )
    throws MathException; 
 
 
 }//NumericValidationLocal
 

23.2.1.2. NumericValidationLocalHome

 package com.atser.sejb.math;
 
 
 public interface NumericValidationLocalHome
  extends javax.ejb.EJBLocalHome
 {
 
  NumericValidationLocal create() throws javax.ejb.CreateException;
 
 }//NumericValidationLocal
 

23.2.1.3. NumericValidationStatelessBean

 import javax.ejb.*;//SessionContext
 import org.apache.log4j.*;//Category
 
 
 public class NumericValidationStatelessBean  
  implements javax.ejb.SessionBean
 {
 
  private static Category log = 
      Category.getInstance("NumericValidationStatefulBean");
 
 
  private javax.ejb.SessionContext ctx;
 
  /**
   * Provides generic range checking. 
   * Checks to see if a number is in range.
   * Range is inclusive given three Strings.
   * and returns true if in range or false if out of
   * specified range.
   */
  public static boolean inRange(String check, 
      String low, String high, boolean inclusive)
    throws MathException
  {
    try
    {
      double c = Double.parseDouble(check);
      double l = Double.parseDouble(low);
      double h = Double.parseDouble(high);
      if(inclusive)
        return (l<=c && c<=h);
      else
        return (l<c && c<h);
    }
    catch(NumberFormatException nfe)
    {
      throw new MathException("Invalid arguements to method:
                            inRange(String check, String low, String high,
                            boolean inclusive):"+check+","+low+"
                            ,"+high+","+inclusive);
    }
 
 
    }//inRange(...)
 
 
  public void ejbCreate()
  {
    log.debug("ejbCreate()");
  }//ejbCreate()
 
 
  public void ejbRemove()
  {
    log.debug("ejbRemove()");
  }//ejbRemove()
 
 
  public void ejbActivate()
  {
    log.debug("ejbActivate()");
  }//ejbActivate()
 
 
  public void ejbPassivate()
  {
    log.debug("ejbPassivate()");
  }//ejbPassivate()
 
 
  public void setSessionContext(javax.ejb.SessionContext ctx)
  {
    this.ctx = ctx;
    log.debug("setSessionContext(..)");
    return;
  }//setSessionContext(..)
 
    
 }//NumericValidationStatefulBean
 

23.2.2. jboss.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss>
   <enterprise-beans>
     <session>
       <ejb-name>com/atser/sejb/math/NumericValidation</ejb-name>
       <jndi-name>com/atser/sejb/math/NumericValidation</jndi-name>
     </session>
   </enterprise-beans>
 </jboss>
 

23.2.3. ejb-jar.xml

 <!DOCTYPE ejb-jar
   PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
   'http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd'>
 <ejb-jar>
   <enterprise-beans>
     <session>
       <!-- The JNDI name of the bean.  -->
       <ejb-name>com/atser/sejb/math/NumericValidation</ejb-name>
 
       <!-- Class configuration for the bean -->
       <local-home>com.atser.sejb.math.NumericValidationLocalHome</local-home>
       <local>com.atser.sejb.math.NumericValidationLocal</local>
       <ejb-class>com.atser.sejb.math.NumericValidationStatelessBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
     </session>
   </enterprise-beans>
 </ejb-jar>
 

23.2.4. web.xml

 <ejb-local-ref>
         <ejb-ref-name>ejb/NumericValidation</ejb-ref-name>
         <ejb-ref-type>Session</ejb-ref-type>
         <local-home>com.atser.sejb.math.NumericValidationLocalHome</local-home>
         <local>com.atser.sejb.math.NumericValidationLocal</local>
         <ejb-link>com/atser/sejb/math/NumericValidation</ejb-link>
   </ejb-local-ref>