User: osh     
  Date: 01/03/06 12:41:06

  Modified:    src/main/org/jboss/test/cts/ejb CtsBmpBean.java
  Log:
  CTS BMP test and test bean changes.
  
  Revision  Changes    Path
  1.5       +432 -502  jbosstest/src/main/org/jboss/test/cts/ejb/CtsBmpBean.java
  
  Index: CtsBmpBean.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jbosstest/src/main/org/jboss/test/cts/ejb/CtsBmpBean.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CtsBmpBean.java   2001/03/01 13:23:19     1.4
  +++ CtsBmpBean.java   2001/03/06 20:41:05     1.5
  @@ -1,23 +1,39 @@
   package org.jboss.test.cts.ejb;
   
  +import java.util.Collection;
   
  -import org.jboss.test.cts.jms.*;
  -import org.jboss.test.cts.keys.*;
   import java.rmi.RemoteException;
  -import java.util.Vector;
  -import java.util.Collection;
  -import java.sql.*;
  -import javax.naming.*;
  -import javax.ejb.*;
  +
   import javax.sql.DataSource;
  +import java.sql.Connection;
  +import java.sql.Statement;
  +import java.sql.PreparedStatement;
  +import java.sql.ResultSet;
  +import java.sql.ResultSetMetaData;
  +import java.sql.Types;
  +import java.sql.SQLException;
  +
  +import javax.naming.InitialContext;
  +import javax.naming.Context;
  +import javax.naming.NamingException;
  +
  +import javax.jms.JMSException;
   
  +import javax.ejb.*;
  +
  +import org.jboss.test.cts.keys.AccountPK;
  +
  +import org.jboss.test.cts.jms.ContainerMBox;
  +import org.jboss.test.cts.jms.MsgSender;
   
   /**
  - * Class CtsBmpBean
  + *  Class CtsBmpBean is a simple BMP entity bean for testing.
    *
  + *  If the table used for persistence here does not exist, ejbFindAll()
  + *  will create it.
    *
  - * @author
  - * @version %I%, %G%
  + *  @author $Author: osh $
  + *  @version $Revision: 1.5 $
    */
   
   public class CtsBmpBean
  @@ -26,6 +42,7 @@
      private static final String TABLE_NAME = "BMP_BEAN_TBL";
      EntityContext               ctx        = null;
      DataSource                  ds         = null;
  +
      private MsgSender ms = null;
   
      // bmp fields
  @@ -33,611 +50,524 @@
      String personsName;
   
      /**
  -    * Method ejbCreate
  -    *
  +    *  Create a new instance.
       *
  -    * @param pk
  -    * @param personsName
  +    *  @param pk The primary key of the new instance.
  +    *  @param personsName Person name of the new instance.
       *
  -    * @return
  -    *
  -    * @throws CreateException
  -    * @throws DuplicateKeyException
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  -    */
  +    *  @throws CreateException
  +    *          In case of database row creation failure.
  +    *  @throws DuplicateKeyException
  +    *          If another instance with this primary key already exist.
  +    */
  +   public AccountPK ejbCreate(AccountPK pk, String personsName)
  +      throws CreateException, DuplicateKeyException
  +   {
  +      System.out.println("entry ejbCreate(\"" + pk.getKey() + "\", " +
  +                                         "\"" + personsName + "\")");
  +
  +      sendMsg(ContainerMBox.EJB_CREATE_MSG);
  +
  +      try {
  +        Connection con = ds.getConnection();
  +        try {
  +           PreparedStatement ps;
  + 
  +           // Check for duplicates.
  +           ps = con.prepareStatement("SELECT accountNumber " +
  +                                     "FROM " + TABLE_NAME + " " +
  +                                     "WHERE accountNumber=?");
  +           try {
  +              ps.setString(1, pk.getKey());
  +
  +              ResultSet rs = ps.executeQuery();
  +
  +              if (rs.next())
  +                 throw new DuplicateKeyException("Bean with accountNumber=" +
  +                                                 pk.getKey() +
  +                                                 " already exists.");
  +           } finally {
  +              ps.close();
  +           }
  + 
  +           // Create in database.
  +           ps = con.prepareStatement("INSERT INTO " + TABLE_NAME +
  +                                     " VALUES (?,?)");
  +           try {
  +              ps.setString(1, pk.getKey());
  +              ps.setString(2, personsName);
  +
  +              ps.execute();
  +           } finally {
  +              ps.close();
  +           }
  +         } finally {
  +           con.close();
  +         }
  +      } catch (SQLException e) {
  +         e.printStackTrace();
   
  -   public AccountPK ejbCreate (AccountPK pk, String personsName)
  -      throws CreateException, DuplicateKeyException, EJBException,
  -             RemoteException
  -   {
  -      System.out.println("entry ejbCreate");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_CREATE_MSG);
  -      tearDown();
  -      setUp();
  +         throw new CreateException("Entity bean creation failure: " +
  +                                   e.getMessage());
  +      }
   
         this.accountNumber = pk.getKey();
         this.personsName   = personsName;
   
  -      boolean    duplicate = false;
  -      Connection con       = null;
  +      System.out.println("Created \"" + accountNumber + "\".");
   
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         Statement s  = con.createStatement();
  -         ResultSet rs = s.executeQuery("SELECT accountNumber FROM "
  -                                       + TABLE_NAME + " WHERE accountNumber="
  -                                       + accountNumber);
  -
  -         duplicate = rs.next();
  -
  -         rs.close();
  -         s.close();
  -
  -         if (!duplicate)
  -         {
  -            PreparedStatement ps = con.prepareStatement("INSERT INTO "
  -                                                        + TABLE_NAME
  -                                                        + " VALUES (?,?)");
  -
  -            ps.setString(1, accountNumber);
  -            ps.setString(2, personsName);
  -            ps.execute();
  -            ps.close();
  -         }
  -      }
  -      catch (Exception ex)
  -      {
  -         ex.printStackTrace();
  -
  -         throw new EJBException("Entity bean creation failure: "
  -                                + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  -         }
  -         catch (Exception _sqle){}
  -      }
  +      return pk;
  +   }
   
  -      if (duplicate)
  -         throw new DuplicateKeyException("Bean with accountNumber="
  -                                         + accountNumber
  -                                         + " already exists.");
  +   /**
  +    *  Method ejbPostCreate
  +    */
  +   public void ejbPostCreate(AccountPK pk, String personsName)
  +   {
  +      System.out.println("ejbPostCreate(AccountPK, String) called");
   
  -      return new AccountPK(accountNumber);
  +      sendMsg(ContainerMBox.EJB_POST_CREATE_MSG);
      }
   
      /**
  -    * Method ejbFindByPrimaryKey
  -    *
  +    *  Find a single instance by primary key.
       *
  -    * @param pk
  +    *  @param pk Primary key of the instance searched for.
       *
  -    * @return
  -    *
  -    * @throws EJBException
  -    * @throws FinderException
  -    * @throws RemoteException
  -    *
  +    *  @throws ObjectNotFoundException
  +    *          If no instance with this primary key exists.
  +    *  @throws FinderException
  +    *          If the lookup failed.
       */
  -
  -   public AccountPK ejbFindByPrimaryKey (AccountPK pk)
  -      throws FinderException, EJBException, RemoteException
  +   public AccountPK ejbFindByPrimaryKey(AccountPK pk)
  +      throws FinderException
      {
         System.out.println("entry ejbFindByPrimaryKey");
  -
  -      Connection con   = null;
  -      boolean    found = false;
  -
  -      try
  -      {
  -         con = ds.getConnection();
   
  -         PreparedStatement ps =
  -            con.prepareStatement("SELECT accountNumber FROM " + TABLE_NAME
  -                                 + " WHERE accountNumber=?");
  +      try {
  +        Connection con = ds.getConnection();
  +        try {
  +           PreparedStatement ps;
  + 
  +           ps = con.prepareStatement("SELECT accountNumber " +
  +                                     "FROM " + TABLE_NAME + " " +
  +                                     "WHERE accountNumber=?");
  +           try {
  +              ps.setString(1, pk.getKey());
   
  -         ps.setString(1, pk.getKey());
  +              ResultSet rs = ps.executeQuery();
   
  -         ResultSet rs = ps.executeQuery();
  +              if (!rs.next())
  +                 throw new ObjectNotFoundException("No bean with " +
  +                                                   "accountNumber=" +
  +                                                   pk.getKey() + " found.");
   
  -         found = rs.next();
  -
  -         rs.close();
  -         ps.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         throw new EJBException("couldnt find: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  +              return pk;
  +           } finally {
  +              ps.close();
  +           }
  +         } finally {
  +           con.close();
            }
  -         catch (Exception _sqle){}
  -      }
  -
  -      if (!found)
  -         throw new FinderException("No bean with accountNumber="
  -                                   + pk.getKey() + " found.");
  +      } catch (SQLException e) {
  +         e.printStackTrace();
   
  -      return pk;
  +         throw new FinderException("Could not find: " + e.getMessage());
  +      }
      }
   
      /**
  -    * Method ejbFindAll
  -    *
  -    *
  -    * @return
  +    *  Find all instances.
       *
  -    * @throws EJBException
  -    * @throws FinderException
  -    * @throws RemoteException
  -    *
  +    *  @throws FinderException
  +    *          If the lookup failed.
       */
  -
  -   public Collection ejbFindAll ()
  -      throws EJBException, FinderException, RemoteException
  +   public Collection ejbFindAll()
  +      throws FinderException
      {
         System.out.println("entry ejbFindAll");
   
  -      Connection con    = null;
  -      Vector     result = new Vector();
  +      ensureTableExists();
   
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         Statement s  = con.createStatement();
  -         ResultSet rs = s.executeQuery("SELECT accountNumber FROM "
  -                                       + TABLE_NAME + "");
  -
  -         while (rs.next())
  -         {
  -            // Return PK's of result set
  -            result.add( new AccountPK(rs.getString("accountNumber") ));
  -         }
  +      Collection result = new java.util.LinkedList();
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            PreparedStatement ps;
  + 
  +            ps = con.prepareStatement("SELECT accountNumber " +
  +                                      "FROM " + TABLE_NAME);
  +            try {
  +               ResultSet rs = ps.executeQuery();
   
  -         rs.close();
  -         s.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         throw new EJBException("couldnt seek: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  +               while (rs.next())
  +                  result.add(new AccountPK(rs.getString(1)));
  +
  +               return result;
  +            } finally {
  +               ps.close();
  +            }
  +         } finally {
  +            con.close();
            }
  -         catch (Exception _sqle){}
  -      }
  +      } catch (SQLException e) {
  +          e.printStackTrace();
   
  -      return result;
  +          throw new FinderException("Could not find: " + e.getMessage());
  +      }
      }
   
      /**
  -    * Method ejbFindByPersonsName
  -    *
  -    *
  -    * @return
  -    *
  -    * @throws EJBException
  -    * @throws FinderException
  -    * @throws RemoteException
  +    *  Find all instances where the personsName property is
  +    *  equal to the argument.
       *
  +    *  @throws FinderException
  +    *          If the lookup failed.
       */
  -   public Collection ejbFindByPersonsName (String guysName )
  -      throws EJBException, FinderException, RemoteException
  -    {
  -      System.out.println("entry ejbFindByPersonsName");
  -
  -      Connection con    = null;
  -      Vector     result = new Vector();
  -
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         Statement s  = con.createStatement();
  -         ResultSet rs = s.executeQuery("SELECT accountNumber FROM "
  -                                       + TABLE_NAME + " WHERE name='" + guysName + 
"'" );
  -
  -         while (rs.next())
  -         {
  -            // Return PK's of result set
  -         String accountNumber = rs.getString("accountNumber");
  -            result.addElement( new AccountPK( accountNumber ));
  -            System.out.println( "Adding: " + accountNumber + " to result set" );
  -         }
  +   public Collection ejbFindByPersonsName(String guysName)
  +      throws FinderException
  +   {
  +      System.out.println("entry ejbFindByPersonsName(\"" + guysName + "\").");
   
  -         rs.close();
  -         s.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         throw new EJBException("couldnt find: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  -         }
  -         catch (Exception _sqle){}
  -      }
  +      Collection result = new java.util.LinkedList();
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            PreparedStatement ps;
  + 
  +            ps = con.prepareStatement("SELECT accountNumber " +
  +                                      "FROM " + TABLE_NAME + " " +
  +                                      "WHERE name=?");
  +            try {
  +               ps.setString(1, guysName);
   
  -      return result;
  -    }
  +               ResultSet rs = ps.executeQuery();
   
  +               while (rs.next())
  +                  result.add(new AccountPK(rs.getString(1)));
   
  -   /**
  -    * Method ejbPostCreate
  -    *
  -    *
  -    * @param pk
  -    * @param personsName
  -    *
  -    * @throws CreateException
  -    * @throws DuplicateKeyException
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  -    */
  +               return result;
  +            } finally {
  +               ps.close();
  +            }
  +         } finally {
  +            con.close();
  +         }
  +      } catch (SQLException e) {
  +         e.printStackTrace();
   
  -   public void ejbPostCreate (AccountPK pk, String personsName)
  -      throws CreateException, DuplicateKeyException, EJBException,
  -             RemoteException
  -   {
  -      System.out.println("ejbPostCreate (AccountPK, String) called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_POST_CREATE_MSG);
  +         throw new FinderException("Could not find: " + e.getMessage());
  +      }
      }
   
      /**
  -    * Method ejbLoad
  -    *
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method ejbLoad
       */
  -
  -   public void ejbLoad ()
  -      throws EJBException, RemoteException
  +   public void ejbLoad()
      {
  -      System.out.println("ejbLoad () called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_LOAD_MSG);
  +      System.out.println("ejbLoad(\"" +
  +                         ((AccountPK)ctx.getPrimaryKey()).getKey() +
  +                         "\") called");
   
  -      Connection con = null;
  +      sendMsg(ContainerMBox.EJB_LOAD_MSG);
   
  -      try
  -      {
  -         con = ds.getConnection();
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            PreparedStatement ps;
  + 
  +            ps = con.prepareStatement("SELECT accountNumber,name " +
  +                                       "FROM " + TABLE_NAME + " " +
  +                                       "WHERE accountNumber=?");
  +            try {
  +               AccountPK pk = (AccountPK)ctx.getPrimaryKey();
   
  -         PreparedStatement ps =
  -            con.prepareStatement("SELECT accountNumber,name FROM "
  -                                 + TABLE_NAME + " WHERE accountNumber=?");
  +               ps.setString(1, pk.getKey());
  +               ResultSet rs = ps.executeQuery();
   
  -         ps.setInt(1, (( Integer ) ctx.getPrimaryKey()).intValue());
  +               if (rs.next() == false)
  +                  throw new NoSuchEntityException("Instance " + pk.getKey() +
  +                                                  " not found in database.");
   
  -         ResultSet rs = ps.executeQuery();
  -
  -         if (rs.next())
  -         {
  -            accountNumber = rs.getString("accountNumber");
  -            personsName   = rs.getString("name");
  +               accountNumber = rs.getString(1);
  +               personsName = rs.getString(2);
  +            } finally {
  +               ps.close();
  +            }
  +         } finally {
  +          con.close();
            }
  +      } catch (SQLException e) {
  +         e.printStackTrace();
   
  -         rs.close();
  -         ps.close();
  +         throw new EJBException(e);
         }
  -      catch (Exception ex)
  -      {
  -         throw new EJBException("couldnt load: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  -         }
  -         catch (Exception sqlex){}
  -      }
  +
  +      System.out.println("ejbLoad(\"" +
  +                         ((AccountPK)ctx.getPrimaryKey()).getKey() +
  +                         "\") returning");
  +
      }
   
      /**
  -    * Method ejbStore
  -    *
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method ejbStore
       */
  -
  -   public void ejbStore ()
  -      throws EJBException, RemoteException
  +   public void ejbStore()
      {
  -      System.out.println("ejbStore () called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_STORE_MSG);
  -
  -      Connection con = null;
  -
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         PreparedStatement ps =
  -            con.prepareStatement("UPDATE " + TABLE_NAME
  -                                 + " SET name=? WHERE accountNumber=?");
  -
  -         ps.setString(1, personsName);
  -         ps.setString(2, accountNumber);
  -         ps.execute();
  -         ps.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         throw new EJBException("couldnt store: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  +      System.out.println("ejbStore(\"" + accountNumber + "\") called");
  +//Thread.currentThread().dumpStack();
  +
  +      sendMsg(ContainerMBox.EJB_STORE_MSG);
  +
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            PreparedStatement ps;
  + 
  +            ps = con.prepareStatement("UPDATE " + TABLE_NAME + " " +
  +                                       "SET name=? " +
  +                                       "WHERE accountNumber=?");
  +            try {
  +               ps.setString(1, personsName);
  +               ps.setString(2, accountNumber);
  + 
  +               ps.executeUpdate();
  +            } finally {
  +               ps.close();
  +            }
  +         } finally {
  +            con.close();
            }
  -         catch (Exception _sqle){}
  +      } catch (SQLException e) {
  +         e.printStackTrace();
  +
  +         throw new EJBException(e);
         }
  +
  +      System.out.println("ejbStore(\"" + accountNumber + "\") returning");
      }
   
      /**
  -    * Method ejbRemove
  -    *
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method ejbRemove
       */
  -
  -   public void ejbRemove ()
  -      throws EJBException, RemoteException
  +   public void ejbRemove()
      {
  -      System.out.println("ejbRemove () called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_REMOVE_MSG);
  -
  -      Connection con = null;
  -
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         PreparedStatement ps =
  -            con.prepareStatement("DELETE FROM " + TABLE_NAME
  -                                 + " WHERE accountNumber=?");
  -
  -         ps.setString(1, accountNumber);
  -         ps.execute();
  -         ps.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         throw new EJBException("couldnt remove: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            tearDown();
  +      System.out.println("ejbRemove(\"" + accountNumber + "\") called");
  +
  +      sendMsg(ContainerMBox.EJB_REMOVE_MSG);
   
  -            if (con != null) con.close();
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            PreparedStatement ps;
  + 
  +            ps = con.prepareStatement("DELETE FROM " + TABLE_NAME + " " +
  +                                       "WHERE accountNumber=?");
  +            try {
  +               ps.setString(1, accountNumber);
  + 
  +               ps.executeUpdate();
  +            } finally {
  +               ps.close();
  +            }
  +         } finally {
  +            con.close();
            }
  -         catch (Exception _sqle){}
  +      } catch (SQLException e) {
  +         e.printStackTrace();
  +
  +         throw new EJBException(e);
         }
  +
  +      System.out.println("Removed \"" + accountNumber + "\".");
      }
   
      /**
  -    * Method ejbActivate
  -    *
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method ejbActivate
       */
  -
  -   public void ejbActivate ()
  -      throws EJBException, RemoteException
  +   public void ejbActivate()
      {
  -      System.out.println("ejbActivate () called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_ACTIVATE_MSG);
  +      System.out.println("ejbActivate() called");
  +
  +      sendMsg(ContainerMBox.EJB_ACTIVATE_MSG);
      }
   
      /**
  -    * Method ejbPassivate
  -    *
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method ejbPassivate
       */
  -
  -   public void ejbPassivate ()
  -      throws EJBException, RemoteException
  +   public void ejbPassivate()
      {
  -      System.out.println("ejbPassivate () called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.EJB_PASSIVATE_MSG);
  +      System.out.println("ejbPassivate() called");
  +
  +      sendMsg(ContainerMBox.EJB_PASSIVATE_MSG);
  +
  +      // drop message sender
  +      if (ms != null) {
  +         try {
  +            ms.close();
  +         } catch (JMSException e) {
  +            e.printStackTrace();
  +            // otherwise ignore
  +         }
  +         ms = null;
  +      }
      }
   
      /**
  -    * Method setEntityContext
  -    *
  -    *
  -    * @param ctx
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method setEntityContext
       */
  -
  -   public void setEntityContext (EntityContext ctx)
  -      throws EJBException, RemoteException
  +   public void setEntityContext(EntityContext ctx)
      {
  -      System.out.println("setEntityContext (\"" + ctx.getPrimaryKey()
  +      System.out.println("setEntityContext(\"" + ctx.getPrimaryKey()
                            + "\") called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.SET_ENTITY_CONTEXT_MSG);
  +
  +      sendMsg(ContainerMBox.SET_ENTITY_CONTEXT_MSG);
  +
         this.ctx = ctx;
   
         // lookup the datasource
  -      try
  -      {
  -         ds = ( DataSource ) new InitialContext().lookup(
  -            "java:comp/env/datasource");
  -      }
  -      catch (NamingException nex)
  -      {
  +      try {
  +         Context context = new InitialContext();
  +
  +         ds = (DataSource)context.lookup("java:comp/env/datasource");
  +      } catch (NamingException nex) {
  +         nex.printStackTrace();
  +
            throw new EJBException("Datasource not found: " + nex.getMessage());
         }
      }
   
      /**
  -    * Method unsetEntityContext
  -    *
  -    *
  -    * @throws EJBException
  -    * @throws RemoteException
  -    *
  +    *  Method unsetEntityContext
       */
  -
  -   public void unsetEntityContext ()
  -      throws EJBException, RemoteException
  +   public void unsetEntityContext()
      {
  -      System.out.println("unsetEntityContext () called");
  -      ms = new MsgSender( );
  -      ms.sendMsg(ContainerMBox.UNSET_ENTITY_CONTEXT_MSG);
  +      System.out.println("unsetEntityContext() called");
  +
  +      sendMsg(ContainerMBox.UNSET_ENTITY_CONTEXT_MSG);
  +
         ctx = null;
  +      ds = null;
      }
   
  +
  +   //
  +   //  Private methods
  +   //
  +
  +   /**
  +    *  Check if a good table exists, and create it if needed.
  +    */
  +   private void ensureTableExists()
  +   {
  +      boolean exists = true;
  + 
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            Statement s  = con.createStatement();
  +            try {
  +               ResultSet rs = s.executeQuery("SELECT * FROM " + TABLE_NAME);
  +               ResultSetMetaData md = rs.getMetaData();
  + 
  +               if (md.getColumnCount() != 2)
  +                 throw new SQLException("Not two columns");
  + 
  +               if (!"ACCOUNTNUMBER".equals(md.getColumnName(1).toUpperCase()))
  +                 throw new SQLException("First column name not \"accountNumber\"");
  +               if (!"NAME".equals(md.getColumnName(2).toUpperCase()))
  +                 throw new SQLException("Second column name not \"name\"");
  + 
  +               if (md.getColumnType(1) != Types.VARCHAR)
  +                 throw new SQLException("First column type not VARCHAR");
  +               if (md.getColumnType(2) != Types.VARCHAR)
  +                 throw new SQLException("Second column type not VARCHAR");
  +            } finally {
  +               s.close();
  +            }
  +         } finally {
  +            con.close();
  +         }
  +      } catch (SQLException e) {
  +        exists = false;
  +      }
  + 
  +      if (!exists)
  +         initializeDatabaseTable();
  +   }
  +
  +   /**
  +    *  Create the table, removing any old table first.
  +    */
  +   private void initializeDatabaseTable()
  +   {
  +      System.out.println("Initializing DATABASE tables for BMP test...");
  + 
  +      try {
  +         Connection con = ds.getConnection();
  +         try {
  +            Statement s;
  + 
  +            s = con.createStatement();
  +            try {
  +               s.executeUpdate("DROP TABLE " + TABLE_NAME);
  +               System.out.println("Dropped old table.");
  +            } catch (SQLException e) {
  +               // Ignore: Presume the table didn't exist.
  +            } finally {
  +               s.close();
  +            }
  + 
  +            s = con.createStatement();
  +            try {
  +               s.executeUpdate("CREATE TABLE " + TABLE_NAME + " " +
  +                               "(accountNumber VARCHAR(25)," +
  +                               " name VARCHAR(200))");
  +               System.out.println("Created new table.");
  +            } finally {
  +               s.close();
  +            }
  +         } finally {
  +            con.close();
  +         }
  +      } catch (SQLException e) {
  +         e.printStackTrace();
  + 
  +         throw new EJBException(e);
  +      }
  + 
  +      System.out.println("Initialized DATABASE tables for BMP test.");
  +   }
  +
  +   /**
  +    *  Send a JMS message.
  +    */
  +   private void sendMsg(String msg)
  +   {
  +      if (ms == null)
  +         ms = new MsgSender();
  +
  +      ms.sendMsg(msg);
  +   }
  +
  +   //
  +   //  Business methods
  +   //
  +
      /**
  -    * Method setPersonsName
  -    *
  -    *
  -    * @param personsName
  -    *
  -    * @throws RemoteException
  -    *
  +    *  Setter for property personsName.
       */
  -
  -   public void setPersonsName (String personsName)
  -      throws RemoteException
  +   public void setPersonsName(String personsName)
      {
         this.personsName = personsName;
      }
   
      /**
  -    * Method getPersonsName
  -    *
  -    *
  -    * @return
  -    *
  -    * @throws RemoteException
  -    *
  +    *  Getter for property personsName.
       */
  -
  -   public String getPersonsName ()
  -      throws RemoteException
  +   public String getPersonsName()
      {
         return this.personsName;
      }
  -
  -   private void setUp ()
  -      throws CreateException, EJBException
  -   {
  -      System.out.println("Setting up DATBASE tables for BMP test...");
  -
  -      try
  -      {
  -         ds = ( DataSource ) new InitialContext().lookup(
  -            "java:comp/env/datasource");
  -      }
  -      catch (NamingException nmEx)
  -      {
  -         throw new CreateException("Datasource not found: "
  -                                   + nmEx.getMessage());
  -      }
  -
  -      Connection con = null;
  -
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         Statement s = con.createStatement();
  -
  -         s.executeUpdate("CREATE TABLE " + TABLE_NAME
  -                         + " (accountNumber VARCHAR(25), name VARCHAR(200))");
  -         s.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         ex.printStackTrace();
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  -         }
  -         catch (Exception sqlEx){}
  -      }
  -   }
  -
  -   private void tearDown ()
  -   {
  -      System.out.println("Cleaning up DATABASE tables for BMP test...");
  -
  -      Connection con = null;
  -
  -      try
  -      {
  -         con = ds.getConnection();
  -
  -         Statement s = con.createStatement();
  -
  -         s.executeUpdate("DROP TABLE " + TABLE_NAME);
  -         s.close();
  -      }
  -      catch (Exception ex)
  -      {
  -         System.out.println("Error while dropping table: " + ex.getMessage());
  -      }
  -      finally
  -      {
  -         try
  -         {
  -            if (con != null) con.close();
  -         }
  -         catch (Exception sqlEx)
  -         {   // ignore..
  -         }
  -      }
  -   }
  -
   }
   
  -
  -/*------ Formatted by Jindent 3.23 Basic 1.0 --- http://www.jindent.de ------*/
  
  
  

Reply via email to