JNDI

 1 import java.util.Hashtable;
 2 
 3 import javax.naming.Context;
 4 import javax.naming.InitialContext;
 5 
 6 public class TestBinding {
 7 
 8     private String bindName = "ssss";
 9 
10     public TestBinding() {
11         Hashtable env = new Hashtable();
12         env.put(Context.INITIAL_CONTEXT_FACTORY,
13                 "com.sun.jndi.fscontext.RefFSContextFactory");
14         env.put(javax.naming.Context.PROVIDER_URL, "file:d:\\");
15         try {
16             Context ctx = new InitialContext(env);
17             ctx.bind(bindName, "aa.txt");
18             String ds2 = (String) ctx.lookup("ssss");
19             System.out.println("---------" + ds2);
20             ctx.close();
21         } catch (Exception e) {
22             System.err.println(e.getMessage());
23         }
24     }
25 
26     public static void main(String[] args) {
27         TestBinding test = new TestBinding();
28     }
29 
30 }

是这样的,
1.命名服务是名字的映射吗,,例如一个名字对应一个相应的入口,而目录不也是这样的映射吗》我不懂有什么区别,每次看JNDI书,就把这两个名词相混淆,
2.文件系统和DNS还有LDAP服务器,区别是什么,我为什么只能在文件系统中进行lookup查询命名就好用,一用bind就报错啊,(得导入文件的两个jar包---前提是我lookup好用啊)
例子如上

-----------------------------------------

请教使用jndi的文件spi进行文件系统操作的问题。
程序是得到指定目录下一个文件的句柄,重命名文件后重绑定该文件。

fscontext.jar,providerutil.jar和jndi.jar已经正确设置,java.naming.factory.initial设置为com.sun.jndi.fscontext.RefFSContextFactory,java.naming.provider.url设置为本机的file:/C:/J2EEcode目录。程序里的Context初始化,lookup,rename都已经可以正常操作,可是在rebind函数出现了:
javax.naming.OperationNotSupportedException:   Can   only   bind   References   or   Referenceable   objects   错误

源程序主要部分如下:

//java.naming.factory.initial和java.naming.provider.url由
//propertiesFileName指定的文件提供
Properties   props   =   new   Properties();
props.setProperty(   "java.naming.factory.initial ",     "com.sun.jndi.fscontext.RefFSContextFactory "   );
props.setProperty(   "java.naming.provider.url ",   "file:/C:/J2EEcode "   );

Context   context   =   new   InitialContext(   props   );
NamingEnumeration   namesList   =   context.list(   " "   );
if   (   namesList   ==   null   ){
System.out.println(   target   +   "   contains   no   names   "   );
}
else{
while(   namesList.hasMore()   ){
System.out.println(   namesList.next()   );
}
}

java.lang.Object   obj   =   ontext.lookup(   "global.properties "   );
context.rename(   "global.properties ",   "newObj "   );

//这儿会出错,javax.naming.OperationNotSupportedException:   Can
//   only   bind   References   or   Referenceable   objects context.rebind(   "global.properties ",   obj   );

有点想不通,既然obj文件是从context中取得的。那么肯定是应该实现了Referenceable接口的,那么重新rebind回去为什么会出错呢?

原文地址:https://www.cnblogs.com/kelin1314/p/1966315.html