Java Naming and Directory Interface (JNDI)Agenda :Introduction :![]()
Procedure to configure Weblogic 10.3 version (pure java s/w) :
The above steps will create a folder with name myDomain in user_projects. Once if a configure the WebLogic server to start the Weblogic server we need we need to double click on startweblogic.cmd or startweblogic.sh To access the Weblogic server perform the following steps :
Develop JNDI application which can get the InitialContext object :
package com.jndi;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JndiDemo {
public static void main(String args[]) throws NamingException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
InitialContext ic=new InitialContext(h);
System.out.println("Object is : "+ ic);
}
}
Note : To run the above java program we have to set wlclient.war in the class path.
Procedure to develop java application which interacts with the directory server :
Admin server
We have to use the following key in the Hashtable :javax.naming packagecontext.INITIAL_CONTEXT_FACTORY context.PROVIDER_URL context.SECURITY_PRINCIPAL context.SECURITY_CREDENTIALS Develop a JNDI application which add a name object to the directory server :
package com.jndi;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JndiCreateObject {
public static void main(String args[]) throws NamingException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
InitialContext ic=new InitialContext(h);
String name="Ashok JavaProgrammer";
ic.bind("same", name);
System.out.println("object is added to directory server");
}
}
Develop a JNDI application which adds an ArrayList object to Directory server :To store the ArrayList object use the key name as emplist
package com.jndi;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JndiAddEmpObject {
public static void main(String args[]) throws NamingException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
InitialContext ic=new InitialContext(h);
ArrayList a=new ArrayList();
a.add("EmpOne");
a.add("EmpTwo");
ic.bind("emplist", a);
System.out.println("object is added to directory server");
}
}
String name="Sai Java"; Object o=name;Ex 2 : ArrayList a = new ArrayList(); Object o=a;// ArrayList is type casted to Object class Develop a JNDI application which delete an object stored in Directory server :Delete object which is associated to sname key
package com.jndi;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JndiDeleteObject {
public static void main(String args[]) throws NamingException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
InitialContext ic=new InitialContext(h);
ic.unbind("sname");
System.out.println("object is removed from directory server");
}
}
If we try to add different objects with the same key the directory server will not allow.It will give the Exception i.e., javax.naming.NameAlreadyBoundException
Develop a JNDI application, update an object stored in Directory server :Update the object which is associated to 'sname' key
package com.jndi;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JndiUpdateObject {
public static void main(String args[]) throws NamingException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
Context ic=new InitialContext(h);
ic.rebind("sname", "Student name modified");
System.out.println("object is updated in directory server");
}
}
Develop a JNDI application which retrieves an object stored in Directory server :The key name of the object is 'sname'
package com.jndi;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JndiRetrieveObject {
public static void main(String args[]) throws NamingException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
Context ic=new InitialContext(h);
Object o=ic.lookup("sname");
String s=(String)o;
System.out.println("object is retrieved : " + s );
}
}
ic.createSubcontext("Student");
ic.createSubcontext("Student.MCA");
ic.destroySubcontext("Student");
The above code create a subcontext under the InitialContext
Note : We can store the objects inside the subcontext. Connection Pool Technique :
Procedure to configure the connections through in the Weblogic server :
Start --> Oracle 10g --> goto DB Home page --> Administration --> Monitor --> sessions Procedure to get a connection from Connection Pool :
DataSource interface is part of javax.sql package.
package com.jndi;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConnect {
public static void main(String args[])
throws NamingException, SQLException{
Hashtable h=new Hashtable();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_CREDENTIALS, "weblogic");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
Context ic=new InitialContext(h);
Object o=ic.lookup("myPool");
DataSource ds=(DataSource)o;
Connection con=ds.getConnection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from product");
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
}
|