Section 6.8. EJB-Based JNDI References in Web-Based Deployment Descriptors


6.8. EJB-Based JNDI References in Web-Based Deployment Descriptors

In previous chapters we've used the web.xml file to describe and deploy Servlets and JNDI resources. Example 6-6 shows the new EJB-based JNDI references in web.xml so we can use the InventoryFacade EJB from the web tier.

Example 6-6. web.xml
 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">   ...   <ejb-local-ref>     <ejb-ref-name>ejb/InventoryFacadeLocal</ejb-ref-name>     <ejb-ref-type>Session</ejb-ref-type>     <local-home>InventoryFacadeLocalHome</local-home>     <local>InventoryFacadeLocal</local>   </ejb-local-ref>   ... </web-app> 

The <ejb-local-ref> element enables the web tier to access the InventoryFacade EJB through its Local Interface. The <ejb-ref-name> is the JNDI name for the EJB java:comp/env/ejb/InventoryFacadeLocal. Notice that you don't have to specify java:comp/env/because it is the assumed prefix. The <ejb-type> tells JBoss that this is a Session Bean. The <local-home> and <local> elements respectively specify the class name of the InventoryFacade EJB's Local Home and Local Component Interfaces.

A JNDI resource is linked into an application only if we ask for it. JBoss binds resources under its in-JVM context, java:/. The jboss-web.xml file provides a mapping between the J2EE-style ENC names and the local JBoss-specific JNDI names that JBoss uses to deploy JNDI-based resources. Example 6-7 shows the EJB-related JNDI references in jboss-web.xml.

Example 6-7. jboss-web.xml
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.4//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd"> <jboss-web>   ...   <ejb-local-ref>     <ejb-ref-name>ejb/InventoryFacadeLocal</ejb-ref-name>     <local-jndi-name>InventoryFacadeLocal</local-jndi-name>   </ejb-local-ref>   ... </jboss-web> 

The jboss-web.xml descriptor maps the J2EE-style JNDI names to JBoss-specific JNDI names. The <ejb-local-ref> element defines a local reference to the InventoryFacade Bean. The textual value of each <ejb-ref-name> element in jboss-web.xml MUST match the value of an <ejb-ref-name> in web.xml. The JNDI name in <ejb-ref-name> is relative to java:comp/env, so the full JNDI name is what we want: java:comp/env/ejb/InventoryFacadeLocal. The JNDI name in <local-jndi-name> is the name JBoss uses internally to reference the EJB for local access.

6.8.1. Automating EJB-Related JNDI Settings in Web-Based Deployment Descriptors

As in previous chapters, we don't want to hardcode our deployment descriptors. Since the JAW Motors application uses EJBs from the web tier, we need to add XDoclet tags to the Controller Servlet so the Ant build process generates the J2EE standard (web.xml) and JBoss-specific (jboss-web.xml) web deployment descriptors. Example 6-8 shows the new XDoclet tags in the Controller Servlet.

Example 6-8. ControllerServlet.java
 /**  * ...  *  * @web.ejb-local-ref  *  name="ejb/InventoryFacadeLocal"  *  type="Session"  *  home="InventoryFacadeLocalHome"  *  local="InventoryFacadeLocal"  *  * @jboss.ejb-local-ref  *  ref-name="InventoryFacadeLocal"  *  jndi-name="InventoryFacadeLocal"  *  */ public class ControllerServlet extends HttpServlet {     ... } 

The @web.ejb-local-ref XDoclet tag generates the <ejb-local-ref> element for the InventoryFacade EJB in web.xml, and the @jboss.ejb-local-ref XDoclet tag generates the corresponding <ejb-local-ref> element in jboss-web.xml.

Now that we've created the infrastructure to call the InventoryFacade Bean from the web application, we need to choose which type of Session Bean to use.



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

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net