Usage Examples
Example 1
To create an initial context that accesses a registry, set the
properties java.naming.factory.initial and
java.naming.provider.url as described in
Environment Properties above. Then the names stored
in the registry, for example, may be listed as follows:
Context ictx = new InitialContext(env);
NamingEnumeration enum = ictx.list("");
Example 2
Instead of using the properties as above, you may pass an RMI URL
as a name to resolve in the default initial context:
Context registryCtx = (Context)ictx.lookup("rmi://host");
Example 3
A registry context may be bound into another JNDI-accessible
namespace. To bind registryCtx (from the preceding example) into an
LDAP directory, for example:
Context ldapCtx = (Context)ictx.lookup("ldap://server/o=sun,c=us");
ldapCtx.bind("cn=rmi", registryCtx);
If the names R1 and R2 are bound in this registry, then a JNDI
client browsing the LDAP namespace will see R1 and R2 beneath the
cn=rmi entry.
Example 4
A remote object that is registered with an RMI registry may be
bound into another JNDI-accessible namespace by constructing a
reference for that object. If the variable obj holds the object
named R1 from the preceding examples, it may be bound into an LDAP
directory as follows:
RefAddr addr = new StringRefAddr("URL", "rmi://host/R1");
Reference ref = new Reference(obj.getClass().getName(), addr);
ldapCtx.bind("cn=R1", ref);
Security Considerations
Security Manager
The usual security considerations of RMI apply. For RMI to
dynamically load classes from a remote server, a security manager must
first be installed. This can be done in the same way as it would for
any other RMI application. See the
JavaTM Remote Method Invocation Specification. Or, if the
environment property java.naming.rmi.security.manager is passed
to the provider, then the provider will attempt to install the
RMISecurityManager itself.
Permissions
The application using JNDI and the RMI registry provider must be
granted the following permissions:
permission java.net.SocketPermission "host[:port]", "connect";
For each host/port identified in the java.naming.provider.url
property and in URL string names supplied to context methods.
permission java.net.SocketPermission "host[:port]", "connect,accept";
For each host/port identified in the URL strings in
javax.naming.References.
permission java.lang.RuntimePermission "setSecurityManager";
If using the java.naming.rmi.security.manager environment
property, which asks the RMI registry provider to install the
RMISecurityManager.
Copyright © 1999-2001 Sun Microsystems, Inc., All Rights Reserved.