熟悉Jboss Server的兄弟请指教,如何绑定一个自定义的JNDI到服务器上

YPTClAomAo 2005-07-07 09:14:07
熟悉jobss配置的兄弟请帮忙,大致情况如此:
...

看过jobss关于数据源的绑定,在deploy目录下的一个xml文件,会绑定到服务器上,比如:
<datasources>
<local-tx-datasource>
<jndi-name>OracleDS</jndi-name>
<connection-url>jdbc:oracle:thin:@youroraclehost:1521:yoursid</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>x</user-name>
<password>y</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>

</datasources>
上述内容可以通过查找名为:OracleDS的jndi获取并使用。

我需要在服务器上绑定一个自定义的jndi,类似与这样:
<jndi-name>MyJNDI</jndi-name>
<jndi-value>abcdefg</jndi-value>
我想自己能查找并使用MyJNDI,该如何做。请兄弟们多指教 ..谢谢..

高分相赠
...全文
472 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
108041217 2005-07-27
  • 打赏
  • 举报
回复
gz
YPTClAomAo 2005-07-13
  • 打赏
  • 举报
回复
谢谢楼上热心的兄弟,我的描述不是很清楚,重新叙述一下吧:
我想通过修改jboss server的配置文件在服务器上bind一个resource(string),以便能在代码中通过jndi查找并使用这个resource。要实现的效果,和下面的代码实现是一致的:

javax.naming.InitialContext().bind("myJndi", "a-string");

此时在其他地方我可以通过jndi获取到一个resource(下例中的string s):

Context iniCtx = new InitialContext();
Object o= iniCtx.lookup("myJndi");
String s=o.toString();

//-----------------------------------------------------------------------------------
问题尚未解决,请知情的兄弟多多帮忙。
再次谢谢楼上几位热心的朋友。
minsky 2005-07-12
  • 打赏
  • 举报
回复
楼主请看这一段说明:
Additional Naming MBeans
In addition to the NamingService MBean that configures an embedded JBossNS server within JBoss, there are three other MBean services related to naming that ship with JBoss:

ExternalContext, NamingAlias, and JNDIView.



The org.jboss.naming.ExternalContext MBean
The ExternalContext MBean allows you to federate external JNDI contexts into the JBoss server JNDI namespace. The term external refers to any naming service external to the JBossNS naming service running inside the JBoss server VM. You can incorporate LDAP servers, file systems, DNS servers, and so on, even if the JNDI provider root context is not serializable. The federation can be made available to remote clients if the naming service supports remote access.

To incorporate an external JNDI naming service, you have to add a configuration of the ExternalContext MBean service to the jboss-service.xml configuration file. The configurable attributes of the ExternalContext service are as follows:

JndiName The JNDI name under which the external context is to be bound.

RemoteAccess A Boolean flag that indicates whether the external InitialContext should be bound using a Serializable form that allows a remote client to create the external InitialContext. When a remote client looks up the external context via the JBoss JNDI InitialContext, the client effectively creates an instance of the external InitialContext, using the same env properties passed to the ExternalContext MBean. This works only if the client can do a new InitialContext (env) remotely. This requires that the Context.PROVIDER_URL value of env be resolvable in the remote VM that is accessing the context. This should work for the LDAP example. For the file system example, this most likely won't work unless the file system path refers to a common network path. If this property is not given, it defaults to false.

CacheContext The cacheContext flag. When it is set to true, the external context is created only when the MBean is started, and then it is stored as an in-memory object until the MBean is stopped. If cacheContext is set to false, the external Context is created on each lookup, using the MBean properties and InitialContext class. When a client looks up the uncached context, the client should invoke close() on the context to prevent resource leaks.

InitialContext The fully qualified classname of the InitialContext implementation to use. It must be one of these: javax.naming.InitialContext, javax.naming.directory.InitialDirContext, or javax.naming.ldap.InitialLdapContext. In the case of InitialLdapContext, a null Controls array is used. The default is javax.naming.InitialContext.

Properties The JNDI properties for the external InitialContext. The input should be the text equivalent of what would go into a jndi.properties file.

PropertiesURL The jndi.properties information for the external InitialContext from an external properties file. This is either a URL, a string, or a classpath resource name. The following are some examples:

file:///config/myldap.properties

http://config.mycompany.com/myldap.properties

/conf/myldap.properties

myldap.properties

The following MBean definition shows a binding to an external LDAP context into the JBoss JNDI namespace under the name external/ldap/jboss:

<!-- Bind a remote LDAP server -->
<mbean code="org.jboss.naming.ExternalContext"
name="jboss.jndi:service=ExternalContext,jndiName=external/ldap/jboss">
<attribute name="JndiName">external/ldap/jboss</attribute>
<attribute name="Properties">
java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
java.naming.provider.url=ldap://ldaphost.jboss.org:389/o=jboss.org
java.naming.security.principal=cn=Directory Manager
java.naming.security.authentication=simple
java.naming.security.credentials=secret
</attribute>
<attribute name="InitialContext"> javax.naming.ldap.InitialLdapContext
</attribute>
<attribute name="RemoteAccess">true</attribute>
</mbean>



With this configuration, you can access the external LDAP context located at ldap://ldaphost.jboss.org:389/o=jboss.org from within the JBoss VM by using the following code fragment:

InitialContext iniCtx = new InitialContext();
LdapContext ldapCtx = iniCtx.lookup("external/ldap/jboss");



Using the same code fragment outside the JBoss server VM would work in this case because the RemoteAccess property is set to true. If it were set to false, it would not work because the remote client would receive a Reference object with an ObjectFactory that would not be able to re-create the external InitialContext:

<!-- Bind the /usr/local file system directory -->
<mbean code="org.jboss.naming.ExternalContext"
name="jboss.jndi:service=ExternalContext,jndiName=external/fs/usr/local">
<attribute name="JndiName">external/fs/usr/local</attribute>
<attribute name="Properties">
java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
java.naming.provider.url=file:///usr/local
</attribute>
<attribute name="InitialContext">javax.naming.InitialContext</attribute>
</mbean>



This configuration describes binding a local file system directory /usr/local into the JBoss JNDI namespace under the name external/fs/usr/local.

With this configuration, you can access the external file system context located at the file ///usr/local from within the JBoss VM, using the following code fragment:

InitialContext iniCtx = new InitialContext();
Context ldapCtx = iniCtx.lookup("external/fs/usr/local");



Note that this code uses one of the Sun JNDI service providers, which must be downloaded from http://java.sun.com/products/jndi/serviceproviders.html. The provider JARs should be placed in the server configuration lib directory.

The org.jboss.naming.NamingAlias MBean
The NamingAlias MBean is a simple utility service that allows you to create an alias in the form of a JNDI javax.naming.LinkRef from one JNDI name to another. This is similar to a symbolic link in the Unix file system. To an alias you add a configuration of the NamingAlias MBean to the jboss-service.xml configuration file.

The configurable attributes of the NamingAlias service are as follows:

FromName The location where the LinkRef is bound under JNDI.

ToName The to name of the alias. This is the target name to which the LinkRef refers. The name is a URL, or a name to be resolved relative to the InitialContext; or if the first character of the name is a dot (.), the name is relative to the context in which the link is bound.

The following example provides a mapping of the JNDI name QueueConnectionFactory to the name ConnectionFactory:

<mbean code="org.jboss.naming.NamingAlias"
name="jboss.mq:service=NamingAlias,fromName=QueueConnectionFactory">
<attribute name="ToName">ConnectionFactory</attribute>
<attribute name="FromName">QueueConnectionFactory</attribute>
</mbean>

minsky 2005-07-08
  • 打赏
  • 举报
回复
楼上的两位,楼主的意思,似乎是要部署一个自己定义的配置文件,由配置文件提供环境变量信息,请看这一段:
我需要在服务器上绑定一个自定义的jndi,类似与这样:
<jndi-name>MyJNDI</jndi-name>
<jndi-value>abcdefg</jndi-value>
jboss支持外部扩展文件的自定义部署,但具体用什么类就不知道了.。楼主查文档也许能找到。祝好运!
楼下的继续
YPTClAomAo 2005-07-08
  • 打赏
  • 举报
回复
再次顶一下
tanxiuming 2005-07-08
  • 打赏
  • 举报
回复
deploy下所放的都是部署文件,楼主所说的是配置数据库连接,在docs\examples\jca下的都是相应的数据库连接设置文件,你COPY到deploy下,修改相应的JNDI名,则实现了数据库连接设置了
tanxiuming 2005-07-08
  • 打赏
  • 举报
回复
简单办法,COPY这个文件重命名,修改相应的JNDI名,则OK啦
minsky 2005-07-08
  • 打赏
  • 举报
回复
学习,帮你up
YPTClAomAo 2005-07-07
  • 打赏
  • 举报
回复
自己up,请高手帮忙

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧