driver-legacy/src/main/com/mongodb/client/jndi/MongoClientFactory.java
@Override
    public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment)
            throws Exception {

        // Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory
        String connectionString = null;

        if (environment.get(CONNECTION_STRING) instanceof String) {
            connectionString = (String) environment.get(CONNECTION_STRING);
        }

        if (connectionString == null || connectionString.isEmpty()) {
            LOGGER.debug(format("No '%s' property in environment.  Casting 'obj' to java.naming.Reference to look for a "
                                        + "javax.naming.RefAddr with type equal to '%s'", CONNECTION_STRING, CONNECTION_STRING));

            // Some app servers, e.g. Tomcat, pass obj as an instance of javax.naming.Reference and pass location information in a
            // javax.naming.RefAddr
            if (obj instanceof Reference) {
                Enumeration<RefAddr> props = ((Reference) obj).getAll();

                while (props.hasMoreElements()) {
                    RefAddr addr = props.nextElement();
                    if (addr != null) {
                        if (CONNECTION_STRING.equals(addr.getType())) {
                            if (addr.getContent() instanceof String) {
                                connectionString = (String) addr.getContent();
                                break;
                            }
                        }
                    }
                }
            }
        }

        if (connectionString == null || connectionString.isEmpty()) {
            throw new MongoException(format("Could not locate '%s' in either environment or obj", CONNECTION_STRING));
        }

        MongoClientURI uri = new MongoClientURI(connectionString);

        return new MongoClient(uri);
    }
@Override
    public Object getObjectInstance(final Object obj, final Name name, final Context nameCtx, final Hashtable<?, ?> environment)
            throws Exception {

        // Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory
        String connectionString = null;

        if (environment.get(CONNECTION_STRING) instanceof String) {
            connectionString = (String) environment.get(CONNECTION_STRING);
        }

        if (connectionString == null || connectionString.isEmpty()) {
            LOGGER.debug(format("No '%s' property in environment.  Casting 'obj' to java.naming.Reference to look for a "
                                        + "javax.naming.RefAddr with type equal to '%s'", CONNECTION_STRING, CONNECTION_STRING));

            // Some app servers, e.g. Tomcat, pass obj as an instance of javax.naming.Reference and pass location information in a
            // javax.naming.RefAddr
            if (obj instanceof Reference) {
                Enumeration<RefAddr> props = ((Reference) obj).getAll();

                while (props.hasMoreElements()) {
                    RefAddr addr = props.nextElement();
                    if (addr != null) {
                        if (CONNECTION_STRING.equals(addr.getType())) {
                            if (addr.getContent() instanceof String) {
                                connectionString = (String) addr.getContent();
                                break;
                            }
                        }
                    }
                }
            }
        }

        if (connectionString == null || connectionString.isEmpty()) {
            throw new MongoException(format("Could not locate '%s' in either environment or obj", CONNECTION_STRING));
        }

        MongoClientURI uri = new MongoClientURI(connectionString);

        return new MongoClient(uri);
    }
@ Override public Object getObjectInstance ( final Object obj , final Name name , final Context nameCtx , final Hashtable < ? , ? > environment ) throws Exception { // Some app servers, e.g. Wildfly, use the environment to pass location information to an ObjectFactory String connectionString = null ; if ( environment . get ( CONNECTION_STRING ) instanceof String ) { connectionString = ( String ) environment . get ( CONNECTION_STRING ) ; } if ( connectionString == null || connectionString . isEmpty ( ) ) { LOGGER . debug ( format ( "No '%s' property in environment.  Casting 'obj' to java.naming.Reference to look for a " + "javax.naming.RefAddr with type equal to '%s'" , CONNECTION_STRING , CONNECTION_STRING ) ) ; // Some app servers, e.g. Tomcat, pass obj as an instance of javax.naming.Reference and pass location information in a // javax.naming.RefAddr if ( obj instanceof Reference ) { Enumeration < RefAddr > props = ( ( Reference ) obj ) . getAll ( ) ; while ( props . hasMoreElements ( ) ) { RefAddr addr = props . nextElement ( ) ; if ( addr != null ) { if ( CONNECTION_STRING . equals ( addr . getType ( ) ) ) { if ( addr . getContent ( ) instanceof String ) { connectionString = ( String ) addr . getContent ( ) ; break ; } } } } } } if ( connectionString == null || connectionString . isEmpty ( ) ) { throw new MongoException ( format ( "Could not locate '%s' in either environment or obj" , CONNECTION_STRING ) ) ; } MongoClientURI uri = new MongoClientURI ( connectionString ) ; return new MongoClient ( uri ) ; }
This implementation will create instances of {@link MongoClient} based on a connection string conforming to the format specified in
{@link MongoClientURI}.
<p>The connection string is specified in one of two ways:</p>
<ul>
<li>As the {@code String} value of a property in the {@code environment} parameter with a key of {@code "connectionString"}</li>
<li>As the {@code String} value of a {@link RefAddr} with type {@code "connectionString"} in an {@code obj} parameter
of type {@link Reference}</li>
</ul>

Specification of the connection string in the {@code environment} parameter takes precedence over specification in the {@code obj}
parameter.  The {@code name} and {@code nameCtx} parameters are ignored.

If a non-empty connection string is not specified in either of these two ways, a {@link MongoException} is thrown.
@return an instance of {@link MongoClient} based on the specified connection string
@throws MongoException

Note: Not all options that can be specified via {@link com.mongodb.MongoClientOptions} can be specified via the connection string.
This implementation will create instances of { @link MongoClient } based on a connection string conforming to the format specified in { @link MongoClientURI } . <p > The connection string is specified in one of two ways : < / p > <ul > <li > As the { @code String } value of a property in the { @code environment } parameter with a key of { @code connectionString } < / li > <li > As the { @code String } value of a { @link RefAddr } with type { @code connectionString } in an { @code obj } parameter of type { @link Reference } < / li > < / ul >