JNDI全攻略(一)
摘要:
本文详细介绍了JNDI的架构与实现,JNDI的工作原理,并给出了具体代码,帮助读者更理解J2EE主要常用技术---JNDI.本文为系列文章的第一篇,其它相关文章会在近期推出。
JNDI的架构与JDBC的架构非常类似.JNDI架构提供了一组标准命名系统的API,这些API在JDK1.3之前是作为一个单独的扩展包jndi.jar(通过这个地址下载),这个基础API构建在与SPI之上。这个API提供如下五个包
- javax.naming
- javax.naming.directory
- javax.naming.event
- javax.naming.ldap
- javax.naming.spi
图中所列的一些SPI可从http://java.sun.com/products/jndi/downloads/index.html下载.
它包括了几个增强和下面的命名/目录服务提供者:
- LDAP(Lightweight Directory Access Protocol)服务提供者
- CORBA COS(Common Object Request Broker Architecture Common Object Services)命名服务提供者
- RMI(Java Remote Method Invocation)注册服务提供者
- DNS(Domain Name System)服务提供者.
- FSSP(File System Service Provider)文件系统服务提供者
- 其它服务提供者
下面通过一个示例程序来说明JNDI工作原理(代码为自解释).
/* * Created on 2005-3-4 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package com.sily.jndi; import java.io.FileInputStream; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext;/*** @author shizy** To change the template for this generated type comment go to* Window>Preferences>Java>Code Generation>Code and Comments*/public class TestJbossJNDI {/****/public TestJbossJNDI() { super();// TODO Auto-generated constructor stub} public static void main(String[] args) { try { Properties env = new Properties();//载入jboss的SPI相关参数,包括初始上下文工厂,服务URL,等等 env.load(new FileInputStream("jbossJndi.properties")); env.list(System.out);//通过JNDI api 初始化上下文 InitialContext ctx = new javax.naming.InitialContext(env); System.out.println("Got context"); //create a subContext ctx.createSubcontext("/sylilzy"); ctx.createSubcontext("sylilzy/sily"); //rebind a object ctx.rebind("sylilzy/sily/a", "I am sily a!"); ctx.rebind("sylilzy/sily/b", "I am sily b!"); //lookup context Context ctx1=(Context)ctx.lookup("sylilzy"); Context ctx2=(Context)ctx1.lookup("/sylilzy/sily"); ctx2.bind("/sylilzy/g", "this is g"); //lookup binded object Object o; o=ctx1.lookup("sily/a"); System.out.println("get object from jndi:"+o);//rename the object ctx2.rename("/sylilzy/g", "g1"); o=ctx2.lookup("g1"); System.out.println("get object from jndi:"+o); } catch (Exception e) { e.printStackTrace();}}}
结果输出如下:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Got context
get object from jndi:I am sily a!
get object from jndi:this is g
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
/sylilzy/sily
-----------------------------
/sylilzy/sily/b:I am sily b!
/sylilzy/sily/a:I am sily a!
/sylilzy/sily/g1:this is g
-----------------------------
-----------------------------
/* * Created on 2005-3-1 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package com.sily.jndi; import java.io.FileInputStream; import java.util.Properties; import javax.naming.*; import javax.naming.Context; import javax.naming.InitialContext;/*** @author shizy** To change the template for this generated type comment go to* Window>Preferences>Java>Code Generation>Code and Comments*/public class JndiTest1 {/****/public JndiTest1() { super(); // TODO Auto-generated constructor stub } public static void main(String[] args) { try { Properties env = new Properties(); env.load(new FileInputStream("fileSystemService.properties")); env.put(Context.PROVIDER_URL, "file:///c:/"); Context ctx = new InitialContext(env); ctx.createSubcontext("sylilzy");
NamingEnumeration list = ctx.list("/"); while (list.hasMore()) { NameClassPair nc = (NameClassPair) list.next(); System.out.println(nc); }
} catch (Exception e) { e.printStackTrace(); } }}
总结:
/* * Created on 2005-3-4 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package com.sily.jndi; import java.io.FileInputStream; import java.util.Properties; import javax.naming.*; import javax.naming.Context; import javax.naming.InitialContext;/*** @author shizy** To change the template for this generated type comment go to* Window>Preferences>Java>Code Generation>Code and Comments*/public class ListJbossJndi {/****/public ListJbossJndi() { super(); // TODO Auto-generated constructor stub } public static void main(String[] args) { try { Properties env = new Properties(); env.load(new FileInputStream("jbossJndi.properties")); //env.list(System.out); Context ctx = new InitialContext(env); listCtx(ctx.lookup("sylilzy")); } catch (Exception e) { e.printStackTrace(); } } static void listCtx(Object o){ if(!(o instanceof Context))log(":"+o); else { log("\n-----------------------------"); try { Context ctx=(Context)o; //log(ctx.getNameInNamespace()+"/:"); NamingEnumeration list=ctx.listBindings(""); while(list.hasMore()){ Binding bind=(Binding)list.next(); log("\n/"+ctx.getNameInNamespace()+"/"+bind.getName()); listCtx(bind.getObject()); } log("\n-----------------------------"); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static void log(Object o){ System.out.print(o); }}
JNDI全攻略(一)相关推荐
- 组策略分发软件全攻略
组策略分发软件全攻略 在规模比较大的网络环境里面,为了对服务器和客户机上的软件.系统补丁进行集中统一的管理,我们可能会用到SUS.WSUS.SMS等.SUS.WSUS管理系统更新,不在本文讨论,请参考 ...
- 根据16S预测微生物群落功能最全攻略
文章原文转载自"宏基因组"公众号,由于微信图片不支持外部网站访问.请点击原文链接跳转公众号阅读. 根据16S预测微生物群落功能最全攻略
- chrome 插件开发各种功能demo_Chrome 插件开发全攻略
Chrome 浏览器相信大家都用得比较多,有很多的优点,比如简洁.强大的开发者工具等,但是更让大家映像深刻的是有各种各样有趣.有用的插件,今天要给大家推荐的开源项目是 Chrome 插件开发全攻略,你 ...
- MySQL与优化有关的命令_MySQL优化全攻略-相关数据库命令
MySQL优化全攻略-相关数据库命令 更新时间:2006年11月25日 00:00:00 作者: 接下来我们要讨论的是数据库性能优化的另一方面,即运用数据库服务器内建的工具辅助性能分析和优化. ▲ ...
- Retrofit全攻略——进阶篇
最近事比较多,距离上次写文章已经过去了一个月了.上一篇文章Retrofit全攻略--基础篇 介绍了Retrofit的基础用法,这篇文章介绍点进阶的用法. 打印网络日志 在开发阶段,为了方便调试,我们需 ...
- nagios全攻略(三)----使用插件监控更多信息
三. 使用命令和插件监控更多信息<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office&quo ...
- [RHEL5企业级Linux服务攻略]--第3季 DHCP服务全攻略
1 DHCP原理 1.1 DHCP概述 DHCP(Dynamic Host Configuration Protocal)就是动态主机配置协议哈,可以自动配置主机的IP地址.子网掩码.网关及DNS等 ...
- EqualLogic全攻略视频[(一)介绍]
制片人: 戴尔中国大客户部高级市场经理 Andy Peng 彭宇恒 演讲者: 戴尔亚太区存储技术总监 Alvin Kho 许良谋 戴尔中国高级系统工程师 English Li 李英文 EqualLog ...
- c盘怎么扩容_给电脑减压,C盘清理全攻略!
本文共1839字,预计阅读时间6分钟相信很多小伙伴发现,电脑没用一段时间就会速度变得很慢,轻则影响学习工作效率,重则电脑卡死,做到一半的工作前功尽弃,让人分分钟想要砸电脑,那这个时候呢,你就要检查一下 ...
- iSCSI存储技术全攻略
什么是iSCSI iSCSI(iSCSI = internet Small Computer System Interface )是由IEETF开发的网络存储标准,目的是为了用IP协议将存储设备连接在 ...
最新文章
- 几种filter的比较
- 《研磨设计模式》读后感一
- MVC与三层架构的区别
- css a标签去掉下划线_CSS入门知识汇总
- 梁兴珍 java_数据结构与算法_Java语言
- 2017-9-26 NOIP模拟赛
- 特斯拉:未授权第三方对外开展特斯拉充电站有关投资或集资项目
- java匹配uri_使用查询字符串中的:在Java中创建URI
- pyside6(1):Qt 资源系统和qrc文件使用
- js vue echarts 前端绘制 cie1931 马蹄图 色度图
- 软件工程-软件测试和系统运维
- Swift语言实战晋级-第9章 游戏实战-跑酷熊猫-3 显示一个动态的熊猫
- GHO文件安装全教程
- 最小圆覆盖问题 算法步骤与证明+代码模板
- flask Not Found The requested URL 20211207
- 天猫菜鸟强势宣布!春节不休,年货照常买照常送!
- kotlin data class 遇到的坑
- 11. Python3 测试代码
- java 杨辉三角(贾宪三角,帕斯卡三角)
- 学校的论文答辩---问题准备
热门文章
- Arduino 多LED灯控制
- BUU XXE COURSE
- 深入浅出matplotlib(9):知道两点坐标画直线
- UVA1449 Dominating Patterns
- 面向对象:上得厅堂,下得厨房,站起来杀猪,坐下来写书
- python实现multi函数参数个数不限、返回所有参数乘积_实现multi( )函数,参数个数不限,返回所有参数的乘积。_学小易找答案...
- jq中的addClass()方法与样式冲突
- 说说在JAVA中使用Redis,以及Redis分布锁
- Xilinx UCF约束语法一
- macos双系统 wintogo_aigo固态硬盘,轻松实现macOS运行Windows双系统