String msg = "failed to compare entry " + req.getName();
String msg = "failed to compare entry " + req.getName() + ": " + e.getMessage();
    public void messageReceived( IoSession session, Object request ) throws Exception    {        CompareRequest req = ( CompareRequest ) request;        LdapResult result = req.getResultResponse().getLdapResult();        try        {            LdapContext ctx = SessionRegistry.getSingleton().getLdapContext( session, null, true );            ServerLdapContext newCtx = ( ServerLdapContext ) ctx.lookup( "" );                        if ( req.getControls().containsKey( ManageDsaITControl.CONTROL_OID ) )            {                newCtx.addToEnvironment( Context.REFERRAL, "ignore" );            }            else            {                newCtx.addToEnvironment( Context.REFERRAL, "throw" );            }            newCtx.setRequestControls( ( Control[] ) req.getControls().values().toArray( EMPTY_CONTROLS ) );            if ( newCtx.compare( req.getName(), req.getAttributeId(), req.getAssertionValue() ) )            {                result.setResultCode( ResultCodeEnum.COMPARETRUE );            }            else            {                result.setResultCode( ResultCodeEnum.COMPAREFALSE );            }        }        catch ( ReferralException e )        {            ReferralImpl refs = new ReferralImpl();            result.setReferral( refs );            result.setResultCode( ResultCodeEnum.REFERRAL );            result.setErrorMessage( "Encountered referral attempting to handle compare request." );            result.setMatchedDn( (LdapDN)e.getResolvedName() );                        do            {                refs.addLdapUrl( ( String ) e.getReferralInfo() );            }            while ( e.skipReferral() );                        session.write( req.getResultResponse() );            return;        }        catch ( Exception e )        {            String msg = "failed to compare entry " + req.getName();            if ( IS_DEBUG )            {                msg += ":\n" + ExceptionUtils.getStackTrace( e );            }            ResultCodeEnum code;            if ( e instanceof LdapException )            {                code = ( ( LdapException ) e ).getResultCode();            }            else            {                code = ResultCodeEnum.getBestEstimate( e, req.getType() );            }            result.setResultCode( code );            result.setErrorMessage( msg );            if ( e instanceof NamingException )            {                NamingException ne = ( NamingException ) e;                if ( ( ne.getResolvedName() != null )                    && ( ( code == ResultCodeEnum.NOSUCHOBJECT ) || ( code == ResultCodeEnum.ALIASPROBLEM )                        || ( code == ResultCodeEnum.INVALIDDNSYNTAX ) || ( code == ResultCodeEnum.ALIASDEREFERENCINGPROBLEM ) ) )                {                    result.setMatchedDn( (LdapDN)ne.getResolvedName() );                }            }            session.write( req.getResultResponse() );            return;        }        result.setMatchedDn( req.getName() );        session.write( req.getResultResponse() );    }