Results 1 to 3 of 3

Thread: JndiObjectFactoryBean

  1. #1
    Join Date
    Nov 2006
    Posts
    18

    Default JndiObjectFactoryBean

    I am new to Spring and have some code below that I want to change so it uses JndiObjectFactoryBean.
    I have read Chapter 9 of Professional Java Development With The Spring Framework and how it allows a client to be provided with the home object using IoC but having read the sample code at the start of that chapter I am not sure about one thing:

    Can the try-catch block be removed from the lookupConverterBean method when change this code to use JndiObjectFactoryBean?

    Code:
    /*
     * ConverterServlet.java
     *
     * Created on 05 February 2007, 13:11
     */
    
    package converter;
    
    import java.io.*;
    import java.net.*;
    import javax.naming.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /**
     *
     * @author Owner
     * @version
     */
    public class ConverterServlet extends HttpServlet {
        
        /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
         * @param request servlet request
         * @param response servlet response
         */
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println("<h1><b><center>Converter</center></b></h1>");
    out.println("<hr>");
    out.println("<p>Enter an amount to convert:</p>");
    out.println("<form method=\"get\">");
    out.println("<input type=\"text\"name=\"amount\" size=\"25\">");
    out.println("<br>");
    out.println("<p>");
    out.println("<input type=\"submit\" value=\"Submit\">");
    out.println("<input type=\"reset\" value=\"Reset\">");
    out.println("</form>");
    String amount = request.getParameter("amount");
    if ( amount != null && amount.length() > 0 ) {
    try {
    converter.ConverterRemote converter;
    converter = lookupConverterBean();
    java.math.BigDecimal d =
    new java.math.BigDecimal(amount);
    out.println("<p>");
    out.println("<p>");
    out.println(amount + " Dollars are "
    + converter.dollarToYen(d) + " Yen.");
    out.println("<p>");
    out.println(amount + " Yen are "
    + converter.yenToEuro(d) + " Euro.");
    converter.remove();
    } catch (Exception e){
    out.println("Cannot lookup or execute EJB!");
    }
    }
    
            out.close();
        }
        
        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /** Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
        
        /** Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
        
        /** Returns a short description of the servlet.
         */
        public String getServletInfo() {
            return "Short description";
        }
        // </editor-fold>
    
       private converter.ConverterRemote lookupConverterBean() {
            try {
               //javax.naming.Context c = new javax.naming.InitialContext();
                InitialContext ctx = new InitialContext();
                Object remote = ctx.lookup("java:comp/env/ejb/ConverterBean");
                converter.ConverterRemoteHome rv = (converter.ConverterRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, converter.ConverterRemoteHome.class);
                return rv.create();
            }
            catch(javax.naming.NamingException ne) {
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
                throw new RuntimeException(ne);
            }
            catch(javax.ejb.CreateException ce) {
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
                throw new RuntimeException(ce);
            }
            catch(java.rmi.RemoteException re) {
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,re);
                throw new RuntimeException(re);
            }
        }
       
       
       
    }

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Don't use JndiObjectFactoryBean for your EJB client: use one of the EJB client-side proxies such as LocalStatelessSessionProxyFactoryBean.

    Using Spring you inject the business interface of the target EJB. You never need to write JNDI lookup code. Steps:

    1. Define EJB client-side proxy as a bean definition
    2. Inject that into the class that wants to use it.

    See EJB chapter in the reference manual for guidance.

    PS You shouldn't really be generating HTML in Java code. Why not use Spring MVC with a JSP or other view?
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Nov 2006
    Posts
    18

    Default

    So far I have configured an xml file called "Spring.xml" like this:
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    
        "http://www.springframework.org/dtd/spring-beans.dtd"> 
    
    <beans>
    
    <bean id="myComponent" class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
      <property name="jndiName" value="ConverterBean"/>
      <property name="businessInterface" value="ConverterBean"/>
    </bean>
    
    <bean id="myController" class="java:comp/env/ejb/ConverterBean">
      <property name="myComponent" ref="myComponent"/>
    </bean>
    
    </beans>

    and the lookupConverterBean1 method in my Servlet class now looks like this:

    Code:
        private converter.ConverterRemote lookupConverterBean1() {
            try {
                
                ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("C:Documents and Settings/Owner/Converter/Converter-ejb/src/conf/Spring.xml");           
                converter.ConverterRemoteHome obj = (converter.ConverterRemoteHome) ctx.getBean("ConverterBean");
                return obj.create();
            }
            
            catch(javax.ejb.CreateException ce) {
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
                throw new RuntimeException(ce);
            }
            catch(java.rmi.RemoteException re) {
                java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,re);
                throw new RuntimeException(re);
            }
        }
    }
    I am using ClassPathXmlApplicationContext to find the xml file.
    It is not generating any errors but when it is deployed to the server I get
    The requested resource () is not available.

    (I take your point about the html, I will get to that later).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •