Hashtable env = SessionRegistry.getSingleton().getEnvironmentByCopy();
    public void messageReceived( IoSession session, Object request ) throws Exception    {        LdapContext ctx;        BindRequest req = ( BindRequest ) request;        LdapResult result = req.getResultResponse().getLdapResult();        Hashtable env = SessionRegistry.getSingleton().getEnvironmentByCopy();        // if the bind request is not simple then we freak: no strong auth yet        if ( !req.isSimple() )        {            result.setResultCode( ResultCodeEnum.AUTHMETHODNOTSUPPORTED );            result.setErrorMessage( "Only simple binds currently supported" );            session.write( req.getResultResponse() );            return;        }        // clone the environment first then add the required security settings        byte[] creds = req.getCredentials();        env.put( Context.SECURITY_PRINCIPAL, req.getName() );        env.put( Context.SECURITY_CREDENTIALS, creds );        env.put( Context.SECURITY_AUTHENTICATION, "simple" );        env.put( PropertyKeys.PARSED_BIND_DN, req.getName() );        if ( req.getControls().containsKey( ManageDsaITControl.CONTROL_OID ) )        {            env.put( Context.REFERRAL, "ignore" );        }        else        {            env.put( Context.REFERRAL, "throw" );        }        try        {            if ( env.containsKey( "server.use.factory.instance" ) )            {                InitialContextFactory factory = ( InitialContextFactory ) env.get( "server.use.factory.instance" );                if ( factory == null )                {                    throw new NullPointerException( "server.use.factory.instance was set in env but was null" );                }                // Bind is a special case where we have to use the referral property to deal                ctx = ( LdapContext ) factory.getInitialContext( env );            }            else            {                Control[] connCtls = ( Control[] ) req.getControls().values().toArray( EMPTY );                ctx = new InitialLdapContext( env, connCtls );            }        }        catch ( NamingException e )        {            ResultCodeEnum code;            if ( e instanceof LdapException )            {                code = ( ( LdapException ) e ).getResultCode();                result.setResultCode( code );            }            else            {                code = ResultCodeEnum.getBestEstimate( e, req.getType() );                result.setResultCode( code );            }            String msg = "Bind failed";                        if ( IS_DEBUG )            {                msg += ":\n" + ExceptionUtils.getStackTrace( e );                msg += "\n\nBindRequest = \n" + req.toString();            }            if ( ( e.getResolvedName() != null )                && ( ( code == ResultCodeEnum.NOSUCHOBJECT ) || ( code == ResultCodeEnum.ALIASPROBLEM )                    || ( code == ResultCodeEnum.INVALIDDNSYNTAX ) || ( code == ResultCodeEnum.ALIASDEREFERENCINGPROBLEM ) ) )            {                result.setMatchedDn( (LdapDN)e.getResolvedName() );            }            result.setErrorMessage( msg );            session.write( req.getResultResponse() );            return;        }        SessionRegistry.getSingleton().setLdapContext( session, ctx );        result.setResultCode( ResultCodeEnum.SUCCESS );        session.write( req.getResultResponse() );    }