Section 6.7. Calling the Session Bean from the Controller Servlet

6.7. Calling the Session Bean from the Controller Servlet

We're now going to introduce the InventoryFacadeBean to the JAW Motors application by calling it from the Controller Servlet. We'll modify the Controller Servlet so it uses the InventoryFacadeBean rather than the DAO to list the cars on the web page. For now, all other actions in the Controller Servlet still use the DAO. Example 6-4 shows the changes.

Example 6-4.
 package com.jbossatwork; ... import com.jbossatwork.ejb.*; ... import javax.ejb.*; ... public class ControllerServlet extends HttpServlet {     ...     protected void processRequest(HttpServletRequest request,     HttpServletResponse response) throws ServletException, IOException     {         ...         InventoryFacadeLocalHome inventoryHome;         inventoryHome = (InventoryFacadeLocalHome)         ServiceLocator.getEjbLocalHome(InventoryFacadeLocalHome.COMP_NAME);         InventoryFacadeLocal inventory = null;         try {             inventory = inventoryHome.create(  );         } catch (CreateException ce) {             throw new RuntimeException(ce.getMessage(  ));         }         // perform action         if (VIEW_CAR_LIST_ACTION.equals(actionName))         {             request.setAttribute("carList", inventory.listAvailableCars(  ));             destinationPage = "/carList.jsp";         }         ...     } } 

The InventoryFacadeBean is a JNDI-based resource, so we've encapsulated the JNDI lookup with the ServiceLocatorwe'll show the look up code in detail in the next section. The ServiceLocator.getEjbLocalHome( ) call does a JNDI lookup on the EJB's Local Home, and creates a Local Home Interface. The viewCarList action now calls the InventoryFacade's listAllAvailableCars( ) method to find all available (unsold) cars in the inventory.

Now that we've shown how to invoke the InventoryFacadeBean, let's take a closer look at the ServiceLocator that wraps the JNDI lookup.

6.7.1. Factoring Out the JNDI Calls

We've used the ServiceLocator tHRoughout this book to wrap JNDI lookup calls Example 6-5 is the new EJB-related method.

Example 6-5.
 package com.jbossatwork.util; ... import javax.ejb.*; ... import javax.naming.*; ... public class ServiceLocator {     ...     public static EJBLocalHome getEjbLocalHome(String localHomeJndiName)     throws ServiceLocatorException {         EJBLocalHome localHome = null;         try {             Context ctx = new InitialContext(  );             localHome = (EJBLocalHome) ctx.lookup(localHomeJndiName);         } catch (ClassCastException cce) {             throw new ServiceLocatorException(cce);         } catch (NamingException ne) {             throw new ServiceLocatorException(ne);         }         return localHome;     }     ... } 

The getEjbLocalHome( ) method encapsulates a JNDI look up for an EJB Local Home object. This method takes the following steps:

  1. Create the InitialContext to access the JNDI tree.

  2. Perform a JNDI lookup.

  3. Cast the object returned from JNDI to the correct typejavax.ejb.EJBLocalHome.

  4. Throw a ServiceLocatorException that chains a low-level JNDI-related exception and contains a corresponding error message.

We've written all the necessary code to call an EJB, and now we need to add EJB-based JNDI references to our web deployment descriptors.

JBoss at Work. A Practical Guide
JBoss at Work: A Practical Guide
ISBN: 0596007345
EAN: 2147483647
Year: 2004
Pages: 197

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: