Show Table of Contents
9.5.3. org.jboss.naming.ExternalContext MBean
The
ExternalContext MBean allows you to federate external JNDI contexts into the server JNDI namespace. The term external refers to any naming service external to the JBossNS naming service running inside of the 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 indicating if the external
InitialContextshould be bound using aSerializableform that allows a remote client to create the externalInitialContext. When a remote client looks up the external context via the JBoss JNDIInitialContext, they effectively create an instance of the externalInitialContextusing the same env properties passed to theExternalContextMBean. This will only work if the client can do anew InitialContext(env)remotely. This requires that theContext.PROVIDER_URLvalue of env is 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 will not work unless the file system path refers to a common network path. If this property is not given it defaults to false. - CacheContext: The
cacheContextflag. When set to true, the externalContextis only created when the MBean is started and then stored as an in memory object until the MBean is stopped. If cacheContext is set to false, the externalContextis created on each lookup using the MBean properties and InitialContext class. When the uncachedContextis looked up by a client, the client should invokeclose()on the Context to prevent resource leaks. - InitialContext: The fully qualified class name of the
InitialContextimplementation to use. Must be one of:javax.naming.InitialContext,javax.naming.directory.InitialDirContextorjavax.naming.ldap.InitialLdapContext. In the case of theInitialLdapContexta nullControlsarray is used. The default isjavax.naming.InitialContex. - Properties: The
Propertiesattribute contains the JNDI properties for the externalInitialContext. The input should be the text equivalent to what would go into ajndi.propertiesfile. - PropertiesURL: This set the
jndi.propertiesinformation for the externalInitialContextfrom an external properties file. This is either a URL, string or a classpath resource name. Examples are as follows:- file:///config/myldap.properties
- http://config.mycompany.com/myldap.properties
- /conf/myldap.properties
- myldap.properties
The MBean definition below 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 using the following code fragment:
InitialContext iniCtx = new InitialContext();
LdapContext ldapCtx = iniCtx.lookup("external/ldap/jboss");
Using the same code fragment outside of the server VM will work in this case because the
RemoteAccess property was 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 recreate 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.IntialContext</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
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");

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.