July 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Developing a BMP Bean

  • July 31, 2003
  • By Saleem Siddiqui, Michael Landy, & Jeff Swisher
  • Send Email »
  • More Articles »

Deploying Your Entity Bean

Deploying your bean is no different from deploying a container managed persistent bean. Just like a CMP bean, the home interface is used to create, find, and remove instances of the entity.

Using Your Entity Bean

The beauty of using bean managed persistence or container managed persistence is that it gives the bean developer the flexibility to choose the implementation that best meets the persistence requirements. In fact, the client of the bean does not need to be concerned with the method of persistence, only that the persistence is accomplished. For example, using the same client used to test our container managed persistent bean, we can now test our bean managed persistent bean. The only change we need to make to the client is to change from using the Employee entity bean to the EmployeeBMP (see Listing 5).

Listing 5 Test Client to Exercise Our New Entity Bean (EmployeeTestClient.java)

package entitybeansample;

import java.math.*;
import java.sql.*;

import javax.naming.*;
import javax.rmi.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class EmployeeTestClient {
 static final private String ERROR_NULL_REMOTE = 
"Remote interface reference is null. It must be created by 
calling one of the Home interface methods first.";
 static final private int MAX_OUTPUT_LINE_LENGTH = 100;
 private boolean logging = true;
 private EmployeeBMPRemoteHome employeeBMPRemoteHome = null;
 private EmployeeBMPRemote employeeBMPRemote = null;

 //Construct the EJB test client
 public EmployeeTestClient() {
  long startTime = 0;
  if (logging) {
   log("Initializing bean access.");
   startTime = System.currentTimeMillis();
  }

  try {
   //get naming context
   Context ctx = new InitialContext();

   //look up jndi name
   Object ref = ctx.lookup("EmployeeBMPRemote");

   //cast to Home interface
   employeeBMPRemoteHome = (EmployeeBMPRemoteHome) 
PortableRemoteObject.narrow(ref, EmployeeBMPRemoteHome.class);
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded initializing bean access.");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }

   /* Test your component Interface */
   this.findByPrimaryKey(new java.lang.Short("5"));
   this.getEmpNo();

  }
  catch(Exception e) {
   if (logging) {
    log("Failed initializing bean access.");
   }
   e.printStackTrace();
  }
 }

 //-----------------------------------------------------------------------
 // Methods that use Home interface methods to generate a Remote 
 // interface reference
 //-----------------------------------------------------------------------

 public EmployeeBMPRemote create(Short empNo) {
  long startTime = 0;
  if (logging) {
   log("Calling create(" + empNo + ")");
   startTime = System.currentTimeMillis();
  }
  try {
   employeeBMPRemote = employeeBMPRemoteHome.create(empNo);
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded: create(" + empNo + ")");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }
  }
  catch(Exception e) {
   if (logging) {
    log("Failed: create(" + empNo + ")");
   }
   e.printStackTrace();
  }

  if (logging) {
   log("Return value from create(" + empNo + "): " + 
employeeBMPRemote + ".");
  }
  return employeeBMPRemote;
 }

 public EmployeeBMPRemote findByPrimaryKey(Short empNo) {
  long startTime = 0;
  if (logging) {
   log("Calling findByPrimaryKey(" + empNo + ")");
   startTime = System.currentTimeMillis();
  }
  try {
   employeeBMPRemote = employeeBMPRemoteHome.findByPrimaryKey(empNo);
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded: findByPrimaryKey(" + empNo + ")");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }
  }
  catch(Exception e) {
   if (logging) {
    log("Failed: findByPrimaryKey(" + empNo + ")");
   }
   e.printStackTrace();
  }

  if (logging) {
   log("Return value from findByPrimaryKey(" + empNo + "): " + 
employeeBMPRemote + ".");
  }
  return employeeBMPRemote;
 }

 //-----------------------------------------------------------------------
 // Methods that use Remote interface methods to access data through the bean
 //-----------------------------------------------------------------------

 public Short getEmpNo() {
  Short returnValue = null;
  if (employeeBMPRemote == null) {
   System.out.println("Error in getEmpNo(): " + ERROR_NULL_REMOTE);
   return returnValue;
  }
  long startTime = 0;
  if (logging) {
   log("Calling getEmpNo()");
   startTime = System.currentTimeMillis();
  }

  try {
   returnValue = employeeBMPRemote.getEmpNo();
   if (logging) {
    long endTime = System.currentTimeMillis();
    log("Succeeded: getEmpNo()");
    log("Execution time: " + (endTime - startTime) + " ms.");
   }
  }
  catch(Exception e) {
   if (logging) {
    log("Failed: getEmpNo()");
   }
   e.printStackTrace();
  }

  if (logging) {
   log("Return value from getEmpNo(): " + returnValue + ".");
  }
  return returnValue;
 }

 //-----------------------------------------------------------------------
 // Utility Methods
 //-----------------------------------------------------------------------

 private void log(String message) {
  if (message == null) {
   System.out.println("-- null");
   return ;
  }
  if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
   System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + 
" ...");
  }
  else {
   System.out.println("-- " + message);
  }
 }
 //Main method

 public static void main(String[] args) {
  EmployeeTestClient client = new EmployeeTestClient();
  // Use the client object to call one of the Home interface wrappers
  // above, to create a Remote interface reference to the bean.
  // If the return value is of the Remote interface type, you can use it
  // to access the remote interface methods. You can also just use the
  // client object to call the Remote interface wrappers.
 }
} 

The test client that is generated can then be tweaked or modified to test the implementation. Test clients are not normally designed to fully test a client or live beyond the life of an appropriate client. Typically, when you want long-term test fixtures, you will use JBuilder's Test Case Wizards. See Chapter 5, "Unit Testing," for more information.

About the Authors

Saleem Siddiqui is a technical architect and trainer with Dunn Solutions Group. He is also a Sun Certified Developer and a Borland Certified JBuilder Instructor. At Dunn, he provides consulting services and business application development.

Michael Landy is Director of Business Applications for Dunn Solutions Group directing the strategic solutions for clients.

Jeff Swisher is a technical manager for the Dunn Solutions Group business applications. He works as a programmer, trainer, architect, and instructional designer. He is a Sun Certified Java Developer and a Borland Certified Instructor in JBuilder.

Source of this material

This material is from Chapter 23: Developing Entity Beans from the book JBuilder Developer's Guide (ISBN: 0-672-32427-X) written by Saleem Siddiqui, Michael Landy, and Jeff Swisher, published by Sams Publishing.

To access the full Table of Contents for the book.


Other Chapters from Sams Publishing:

Web Services and Flows (WSFL)
Overview of JXTA
Introduction to EJBs
Processing Speech with Java
The Java Database Control in BEA Weblogic
Databases and Tomcat
Working with JAX-RPC
Understanding Servlets






Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel