package jndi;
import javax.naming.*;
import java.util.Hashtable;
class JNDI {
static Context ctx = null;
public JNDI()
{ }
//将对象object绑定到WebLogic Server的名字服务中
public static void bind(String name, String object) {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
ctx = new InitialContext(ht);
ctx.rebind(name, object);
}
catch (NamingException e) {
System.out.println(e);
}
finally {
try {
ctx.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
//通过JNDI查询指定的对象
public static Object lookUp(String name) {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
ctx = new InitialContext(ht);
Object object = ctx.lookup(name);
return object;
}
catch (NamingException e) {
System.out.println(e);
}
finally {
try {
ctx.close();
}
catch (Exception e) {
System.out.println(e);
}
}
return null;
}
public static void main(String args[]) {
String arg = args[0];
if(arg.equals("bind")) {
System.out.println("bind begin...");
String bookName = "WebLogic introduction";
bind("bookname", bookName);
System.out.println("bind end");
}
if(arg.equals("lookup")) {
System.out.println("lookup begin...");
String bookName;
bookName = (String)lookUp("bookname");
System.out.println("bookname is: "+bookName);
System.out.println("lookup end");
}
}
}
阅读(1757) | 评论(0) | 转发(0) |