Tuesday 26 November 2013

JNDI example of org.apache.naming.factory.BeanFactory

Hello friends . i am posting Feature Related to JNDI(Java Naming & Directory Service).i hope you are aware of Bean Factory Design Pattern where as custom object is pooled up & given back to consumer.
you can  do same functionality using Jndi.

before going through the code you should be familiar with org.apache.naming.factory.BeanFactory & basics of server.xml which should be placed in META-INF.


what is required is a single html form inoder to make
I am posting code as given below :

server.xml code

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  ...
  <Resource name="bean/MyBeanFactory" auth="Container"
            type="org.model.MyBean"
            factory="org.apache.naming.factory.BeanFactory"
            bar="23"/>
 
</Context>

here is Traditional example of pojo class :
package org.model;

public class MyBean
{
   
    private String foo = "Default Foo";

      public String getFoo() {
        return (this.foo);
      }

      public void setFoo(String foo) {
        this.foo = foo;
      }

      private int bar = 0;

      public int getBar() {
        return (this.bar);
      }

      public void setBar(int bar) {
        this.bar = bar;
      }


}
By Defining Resource into server.xml you are declaring  JNDI object on server side .inorder to actually get that resource into web application you need to have resource.you can get this resource by writing following code into web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 
  <display-name>ServletJpa</display-name>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
 
  
 
  <servlet>
    <servlet-name>ResourceFactory</servlet-name>
    <servlet-class>org.model.ResourceFactory</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ResourceFactory</servlet-name>
    <url-pattern>/resource/</url-pattern>
  </servlet-mapping>
 
  <resource-env-ref>
    <description>Object factory for MyBean instances.</description>
   
    <resource-env-ref-name>bean/MyBeanFactory</resource-env-ref-name>
   
    <resource-env-ref-type>
    org.model.MyBean
  </resource-env-ref-type>
  </resource-env-ref>
 
</web-app>

now you can have bean/MyBeanFactory resource.inorder to actually call that resource you should make a referance to jndi object as follows:

package org.model;

import java.io.IOException;
import java.io.PrintWriter;



import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Application Lifecycle Listener implementation class ResourceFactory
 *
 */
@WebListener
public class ResourceFactory extends HttpServlet
{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public ResourceFactory() {
        // TODO Auto-generated constructor stub
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
       
        PrintWriter out=response.getWriter();

       
        try
        {
            Context initCtx;
            initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");
           
            out.println("foo = " + bean.getFoo() + ", bar = " +
                    bean.getBar());
        } catch (NamingException e)
        {
            // TODO Auto-generated catch block
               e.printStackTrace();
        }
       

       

   
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

   
   
}





Hope you  can understand my post.
thanks
for any query ping me on nppathak90@gmail.com


No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...