You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2006/02/03 13:06:08 UTC

svn commit: r374647 - in /directory/sandbox/akarasulu/rc1refactor/apacheds: core/src/main/java/org/apache/ldap/server/authn/ core/src/main/java/org/apache/ldap/server/jndi/ core/src/test/java/org/apache/ldap/server/jndi/ protocols/ldap/src/main/java/or...

Author: akarasulu
Date: Fri Feb  3 04:05:57 2006
New Revision: 374647

URL: http://svn.apache.org/viewcvs?rev=374647&view=rev
Log:
fixed runtime errors due to failing tests

Modified:
    directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/authn/AuthenticationService.java
    directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/LdapJndiProperties.java
    directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java
    directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerLdapContext.java
    directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/test/java/org/apache/ldap/server/jndi/LdapJndiPropertiesTest.java
    directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java
    directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/UnbindHandler.java

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/authn/AuthenticationService.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/authn/AuthenticationService.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/authn/AuthenticationService.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/authn/AuthenticationService.java Fri Feb  3 04:05:57 2006
@@ -42,6 +42,7 @@
 import org.apache.ldap.server.interceptor.Interceptor;
 import org.apache.ldap.server.interceptor.NextInterceptor;
 import org.apache.ldap.server.invocation.InvocationStack;
+import org.apache.ldap.server.jndi.LdapJndiProperties;
 import org.apache.ldap.server.jndi.ServerContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -433,7 +434,7 @@
             log.debug( "Nexus succeeded on bind operation." );
             // bind succeeded if we got this far 
             ctx.setPrincipal( new TrustedPrincipalWrapper( 
-                new LdapPrincipal( bindDn, ctx.getLdapJndiProperties().getAuthenticationLevel() ) ) );
+                new LdapPrincipal( bindDn, LdapJndiProperties.getAuthenticationLevel( ctx.getEnvironment() ) ) ) );
             // remove creds so there is no security risk
             ctx.removeFromEnvironment( Context.SECURITY_CREDENTIALS );
             return;

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/LdapJndiProperties.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/LdapJndiProperties.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/LdapJndiProperties.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/LdapJndiProperties.java Fri Feb  3 04:05:57 2006
@@ -29,22 +29,63 @@
     private AuthenticationLevel level;
     private List mechanisms = new ArrayList();
     private byte[] credentials;
-    private final Hashtable env;
     
-    
-    public LdapJndiProperties( Hashtable env ) throws NamingException
+
+    public static AuthenticationLevel getAuthenticationLevel( Hashtable env ) throws NamingException 
     {
-        if ( env == null )
+        AuthenticationLevel level;
+        Object credobj = env.get( Context.SECURITY_CREDENTIALS );
+        Object authentication = env.get( Context.SECURITY_AUTHENTICATION );
+
+        // -------------------------------------------------------------------
+        // Figure out and set the authentication level and mechanisms
+        // -------------------------------------------------------------------
+
+        if ( authentication == null )
+        {
+            // if the property is not set but Context.SECURITY_CREDENTIALS is then SIMPLE
+            if ( credobj == null )
+            {
+                level = AuthenticationLevel.NONE;
+            }
+            else
+            {
+                level = AuthenticationLevel.SIMPLE;
+            }
+        }
+        else if ( ! ( authentication instanceof String ) ) 
+        {
+            throw new LdapConfigurationException( "Don't know how to interpret " + authentication.getClass()
+                + " objects for environment property " + Context.SECURITY_AUTHENTICATION );
+        }
+        else 
         {
-            throw new LdapConfigurationException( "Non-null environment expected." );
+            if ( "none".equals( authentication ) )
+            {
+                level = AuthenticationLevel.NONE;
+            }
+            else if ( "simple".equals( authentication ) )
+            {
+                level = AuthenticationLevel.SIMPLE;
+            }
+            else
+            {
+                level = AuthenticationLevel.STRONG;
+            }
         }
-        this.env = env;
-        init();
+        
+        return level;
     }
-
-
-    private void init() throws NamingException
+    
+    
+    public static LdapJndiProperties getLdapJndiProperties( Hashtable env ) throws NamingException
     {
+        if ( env == null )
+        {
+            throw new LdapConfigurationException( "environment cannot be null" );
+        }
+        
+        LdapJndiProperties props = new LdapJndiProperties();
         Object principal = env.get( Context.SECURITY_PRINCIPAL );
         Object credobj = env.get( Context.SECURITY_CREDENTIALS );
         Object authentication = env.get( Context.SECURITY_AUTHENTICATION );
@@ -70,11 +111,11 @@
 
         if ( url.trim().equals( "" ) )
         {
-            providerDn = LdapName.EMPTY_LDAP_NAME;
+            props.providerDn = LdapName.EMPTY_LDAP_NAME;
         }
         else
         {
-            providerDn = new LdapName( url );
+            props.providerDn = new LdapName( url );
         }
 
         // -------------------------------------------------------------------
@@ -86,13 +127,13 @@
             // if the property is not set but Context.SECURITY_CREDENTIALS is then SIMPLE
             if ( credobj == null )
             {
-                level = AuthenticationLevel.NONE;
-                mechanisms.add( "none" );
+                props.level = AuthenticationLevel.NONE;
+                props.mechanisms.add( "none" );
             }
             else
             {
-                level = AuthenticationLevel.SIMPLE;
-                mechanisms.add( "simple" );
+                props.level = AuthenticationLevel.SIMPLE;
+                props.mechanisms.add( "simple" );
             }
         }
         else if ( ! ( authentication instanceof String ) ) 
@@ -104,23 +145,23 @@
         {
             if ( "none".equals( authentication ) )
             {
-                level = AuthenticationLevel.NONE;
-                mechanisms.add( "none" );
+                props.level = AuthenticationLevel.NONE;
+                props.mechanisms.add( "none" );
             }
             else if ( "simple".equals( authentication ) )
             {
-                level = AuthenticationLevel.SIMPLE;
-                mechanisms.add( "simple" );
+                props.level = AuthenticationLevel.SIMPLE;
+                props.mechanisms.add( "simple" );
             }
             else
             {
-                level = AuthenticationLevel.STRONG;
+                props.level = AuthenticationLevel.STRONG;
                 String[] mechList = ( ( String ) authentication ).trim().split( " " );
                 for ( int ii = 0; ii < mechList.length; ii++ )
                 {
                     if ( ! mechList[ii].trim().equals( "" ) )
                     {
-                        mechanisms.add( mechList[ii] );
+                        props.mechanisms.add( mechList[ii] );
                     }
                 }
             }
@@ -144,33 +185,33 @@
         
         if ( ( ( String ) principal ).trim().equals( "" ) )
         {
-            bindDn = LdapName.EMPTY_LDAP_NAME;
+            props.bindDn = LdapName.EMPTY_LDAP_NAME;
         }
         else
         {
-            bindDn = new LdapName( ( String ) principal );
+            props.bindDn = new LdapName( ( String ) principal );
         }
         
-        if ( env.get( SASL_AUTHID ) != null && level == AuthenticationLevel.STRONG )
+        if ( env.get( SASL_AUTHID ) != null && props.level == AuthenticationLevel.STRONG )
         {
             Object obj = env.get( SASL_AUTHID );
             if ( obj instanceof String )
             {
-                saslAuthId = ( String ) obj;
+                props.saslAuthId = ( String ) obj;
             }
             else 
             {
                 throw new LdapConfigurationException( "Don't know how to interpret " + obj.getClass()
                     + " objects for environment property " + SASL_AUTHID );
             }
-            saslAuthId = ( String ) principal;
+            props.saslAuthId = ( String ) principal;
         }
         
         // -------------------------------------------------------------------
         // Figure out the credentials
         // -------------------------------------------------------------------
 
-        if ( level == AuthenticationLevel.SIMPLE && credobj == null )
+        if ( props.level == AuthenticationLevel.SIMPLE && credobj == null )
         {
             throw new LdapConfigurationException( "cannot specify simple authentication with supplying credentials" );
         }
@@ -178,11 +219,11 @@
         {
             if ( credobj instanceof String )
             {
-                credentials = StringTools.getBytesUtf8( ( String ) credobj );
+                props.credentials = StringTools.getBytesUtf8( ( String ) credobj );
             }
             else if ( credobj instanceof byte[] )
             {
-                credentials = ( byte[] ) credobj;
+                props.credentials = ( byte[] ) credobj;
             }
             else
             {
@@ -190,21 +231,11 @@
                     + " objects for environment property " + Context.SECURITY_CREDENTIALS );
             }
         }
+        
+        return props;
     }
 
     
-    public Object get( Object key )
-    {
-        return env.get( key );
-    }
-    
-    
-    public Object put( Object key, Object val )
-    {
-        return env.put( key, val );
-    }
-    
-    
     public LdapName getBindDn()
     {
         return bindDn;
@@ -238,23 +269,5 @@
     public byte[] getCredentials()
     {
         return credentials;
-    }
-    
-    
-    public Hashtable getEnvironment()
-    {
-        return this.env;
-    }
-
-
-    public Object remove( String propName )
-    {
-        return env.remove( propName );
-    }
-
-
-    public void putAll( Hashtable env )
-    {
-        this.env.putAll( env );
     }
 }

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerContext.java Fri Feb  3 04:05:57 2006
@@ -75,7 +75,7 @@
     private final DirectoryPartitionNexus nexusProxy;
 
     /** The cloned environment used by this Context */
-    private final LdapJndiProperties ldapEnv;
+    private final Hashtable env;
 
     /** The distinguished name of this Context */
     private final LdapName dn;
@@ -113,14 +113,14 @@
         this.nexusProxy = new DirectoryPartitionNexusProxy( this, service );
         
         DirectoryServiceConfiguration cfg = service.getConfiguration();
-        
-        this.ldapEnv = new LdapJndiProperties( ( Hashtable ) cfg.getEnvironment().clone() );
-        this.ldapEnv.putAll( env );
-        dn = ldapEnv.getProviderDn();
+        this.env = ( Hashtable ) cfg.getEnvironment().clone();
+        this.env.putAll( env );
+        LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( this.env );
+        dn = props.getProviderDn();
 
         // need to issue a bind operation here
-        this.nexusProxy.bind( ldapEnv.getBindDn(), ldapEnv.getCredentials(), 
-            ldapEnv.getAuthenticationMechanisms(), ldapEnv.getSaslAuthId() );
+        this.nexusProxy.bind( props.getBindDn(), props.getCredentials(), 
+            props.getAuthenticationMechanisms(), props.getSaslAuthId() );
 
         if ( dn.size() == 0 ) return;
         if ( ! nexusProxy.hasEntry( dn ) )
@@ -145,9 +145,8 @@
         this.service = service;
         this.dn = ( LdapName ) dn.clone();
 
-        Hashtable tmp = ( Hashtable ) service.getConfiguration().getEnvironment().clone();
-        tmp.put( PROVIDER_URL, dn.toString() );
-        this.ldapEnv = new LdapJndiProperties( tmp );
+        this.env = ( Hashtable ) service.getConfiguration().getEnvironment().clone();
+        this.env.put( PROVIDER_URL, dn.toString() );
         this.nexusProxy = new DirectoryPartitionNexusProxy( this, service );;
         this.principal = principal;
     }
@@ -157,16 +156,6 @@
     // New Impl Specific Public Methods
     // ------------------------------------------------------------------------
 
-    
-    /**
-     * The JNDI properties wrapped with convenience methods.
-     */
-    public LdapJndiProperties getLdapJndiProperties()
-    {
-        return ldapEnv;
-    }
-    
-    
     /**
      * Returns the {@link DirectoryService} which manages this context.
      */
@@ -257,7 +246,7 @@
      */
     public Hashtable getEnvironment()
     {
-        return ldapEnv.getEnvironment();
+        return env;
     }
 
 
@@ -267,7 +256,7 @@
      */
     public Object addToEnvironment( String propName, Object propVal ) throws NamingException
     {
-        return ldapEnv.put( propName, propVal );
+        return env.put( propName, propVal );
     }
 
 
@@ -276,7 +265,7 @@
      */
     public Object removeFromEnvironment( String propName ) throws NamingException
     {
-        return ldapEnv.remove( propName );
+        return env.remove( propName );
     }
 
 
@@ -356,7 +345,7 @@
     public void bind( Name name, Object obj ) throws NamingException
     {
         // First, use state factories to do a transformation
-        DirStateFactory.Result res = DirectoryManager.getStateToBind( obj, name, this, ldapEnv.getEnvironment(), null );
+        DirStateFactory.Result res = DirectoryManager.getStateToBind( obj, name, this, env, null );
         Attributes outAttrs = res.getAttributes();
 
         if ( outAttrs != null )
@@ -451,9 +440,9 @@
          * Attempt to use the java.naming.ldap.deleteRDN environment property
          * to get an override for the deleteOldRdn option to modifyRdn.  
          */
-        if ( null != ldapEnv.get( DELETE_OLD_RDN_PROP ) )
+        if ( null != env.get( DELETE_OLD_RDN_PROP ) )
         {
-            String delOldRdnStr = ( String ) ldapEnv.get( DELETE_OLD_RDN_PROP );
+            String delOldRdnStr = ( String ) env.get( DELETE_OLD_RDN_PROP );
             delOldRdn = ! delOldRdnStr.equals( "false" );
             delOldRdn = delOldRdn || delOldRdnStr.equals( "no" );
             delOldRdn = delOldRdn || delOldRdnStr.equals( "0" );
@@ -554,7 +543,7 @@
 
         try
         {
-            obj = DirectoryManager.getObjectInstance( null, name, this, ldapEnv.getEnvironment(), attributes );
+            obj = DirectoryManager.getObjectInstance( null, name, this, env, attributes );
         }
         catch ( Exception e )
         {

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerLdapContext.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerLdapContext.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerLdapContext.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/main/java/org/apache/ldap/server/jndi/ServerLdapContext.java Fri Feb  3 04:05:57 2006
@@ -19,6 +19,7 @@
 
 import java.util.Hashtable;
 
+import javax.naming.Context;
 import javax.naming.Name;
 import javax.naming.NamingException;
 import javax.naming.ldap.Control;
@@ -31,6 +32,8 @@
 import org.apache.ldap.server.authn.LdapPrincipal;
 import org.apache.ldap.server.referral.ReferralService;
 
+import com.sun.jndi.ldap.LdapName;
+
 
 /**
  * An implementation of a JNDI LdapContext.
@@ -177,7 +180,8 @@
      */
     public void ldapUnbind() throws NamingException
     {
-        super.getNexusProxy().unbind( getLdapJndiProperties().getBindDn() );
+        String bindDn = ( String ) getEnvironment().get( Context.SECURITY_PRINCIPAL );
+        super.getNexusProxy().unbind( new LdapName( bindDn ) );
     }
     
     

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/test/java/org/apache/ldap/server/jndi/LdapJndiPropertiesTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/test/java/org/apache/ldap/server/jndi/LdapJndiPropertiesTest.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/test/java/org/apache/ldap/server/jndi/LdapJndiPropertiesTest.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/core/src/test/java/org/apache/ldap/server/jndi/LdapJndiPropertiesTest.java Fri Feb  3 04:05:57 2006
@@ -1,4 +1,5 @@
 package org.apache.ldap.server.jndi;
+ 
 
 import java.util.Hashtable;
 
@@ -18,7 +19,7 @@
     {
         try
         {
-            new LdapJndiProperties( new Hashtable() );
+            LdapJndiProperties.getLdapJndiProperties( new Hashtable() );
             fail( "should never get here" );
         }
         catch ( LdapConfigurationException e )
@@ -31,7 +32,7 @@
     {
         try
         {
-            new LdapJndiProperties( null );
+            LdapJndiProperties.getLdapJndiProperties( null );
             fail( "should never get here" );
         }
         catch ( LdapConfigurationException e )
@@ -46,7 +47,7 @@
         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
         env.put( Context.SECURITY_CREDENTIALS, "asdf" );
         env.put( Context.PROVIDER_URL, "" );
-        LdapJndiProperties props = new LdapJndiProperties( env );
+        LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
         assertEquals( AuthenticationLevel.SIMPLE, props.getAuthenticationLevel() );
         assertEquals( 1, props.getAuthenticationMechanisms().size() );
         assertEquals( "simple", props.getAuthenticationMechanisms().get( 0 ) );
@@ -59,7 +60,7 @@
         Hashtable env = new Hashtable();
         env.put( Context.SECURITY_PRINCIPAL, "" );
         env.put( Context.PROVIDER_URL, "" );
-        LdapJndiProperties props = new LdapJndiProperties( env );
+        LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
         assertEquals( AuthenticationLevel.NONE, props.getAuthenticationLevel() );
         assertEquals( 1, props.getAuthenticationMechanisms().size() );
         assertEquals( "none", props.getAuthenticationMechanisms().get( 0 ) );
@@ -74,7 +75,7 @@
         env.put( Context.SECURITY_AUTHENTICATION, "simple" );
         try
         {
-            new LdapJndiProperties( env );
+            LdapJndiProperties.getLdapJndiProperties( env );
             fail( "should never get here" );
         }
         catch ( LdapConfigurationException e )
@@ -89,7 +90,7 @@
         env.put( Context.SECURITY_PRINCIPAL, "" );
         env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5 CRAM-MD5" );
         env.put( Context.PROVIDER_URL, "" );
-        LdapJndiProperties props = new LdapJndiProperties( env );
+        LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
         assertEquals( AuthenticationLevel.STRONG, props.getAuthenticationLevel() );
         assertEquals( 2, props.getAuthenticationMechanisms().size() );
         assertEquals( "DIGEST-MD5", props.getAuthenticationMechanisms().get( 0 ) );
@@ -105,7 +106,7 @@
         env.put( Context.SECURITY_CREDENTIALS, "asdf" );
         env.put( Context.SECURITY_AUTHENTICATION, "DIGEST-MD5 CRAM-MD5" );
         env.put( Context.PROVIDER_URL, "" );
-        LdapJndiProperties props = new LdapJndiProperties( env );
+        LdapJndiProperties props = LdapJndiProperties.getLdapJndiProperties( env );
         assertEquals( AuthenticationLevel.STRONG, props.getAuthenticationLevel() );
         assertEquals( 2, props.getAuthenticationMechanisms().size() );
         assertEquals( "DIGEST-MD5", props.getAuthenticationMechanisms().get( 0 ) );

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/ExtendedHandler.java Fri Feb  3 04:05:57 2006
@@ -15,7 +15,7 @@
  *
  */
 package org.apache.ldap.server.protocol.support;
-
+ 
 
 import java.util.Collections;
 import java.util.HashMap;

Modified: directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/UnbindHandler.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/UnbindHandler.java?rev=374647&r1=374646&r2=374647&view=diff
==============================================================================
--- directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/UnbindHandler.java (original)
+++ directory/sandbox/akarasulu/rc1refactor/apacheds/protocols/ldap/src/main/java/org/apache/ldap/server/protocol/support/UnbindHandler.java Fri Feb  3 04:05:57 2006
@@ -47,20 +47,14 @@
 
         try
         {
-            ServerLdapContext ctx = null;
-            LdapContext ldapCtx = ( ServerLdapContext ) SessionRegistry.getSingleton().getLdapContext( session, null, false );
-            if ( ! ( ldapCtx instanceof ServerLdapContext ) )
-            {
-                ctx = ( ServerLdapContext ) ldapCtx.lookup( "" );
-            }
-            else
-            {
-                ctx = ( ServerLdapContext ) ldapCtx;
-            }
+            LdapContext ctx = ( LdapContext ) SessionRegistry.getSingleton().getLdapContext( session, null, false );
             
             if ( ctx != null )
             {
-                ctx.ldapUnbind();
+                if ( ctx instanceof ServerLdapContext && ( ( ServerLdapContext ) ctx ).getService().isStarted() )
+                {
+                    ( ( ServerLdapContext ) ctx ).ldapUnbind();
+                }
                 ctx.close();
             }
             registry.terminateSession( session );