Caught up with the recent changes in core

git-svn-id: https://svn.apache.org/repos/asf/directory/apacheds/branches/direve-158@190361 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/main/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java b/main/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java
index f7bd4da..7d560ef 100644
--- a/main/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java
+++ b/main/src/main/java/org/apache/ldap/server/jndi/ServerContextFactory.java
@@ -65,106 +65,48 @@
     // Members
     // ------------------------------------------------------------------------
 
-
-    /**
-     * Checks first for a shutdown operation and if so stops the server.  Otherwise
-     * it initializes the networking subsystem starting up the mina registery and
-     * other protocol provider services if enabled including the LDAP provider.
-     *
-     * @param env the JNDI environment parameters
-     * @return the new LDAP context to be wrapped by InitialContext
-     * @throws NamingException if there are problems starting or stopping the server
-     */
-    public Context getInitialContext( Hashtable env ) throws NamingException
+    protected void afterShutdown( ContextFactoryContext ctx )
     {
-        Context ctx = null;
-        Configuration cfg0 = Configuration.toConfiguration( env );
-        if ( cfg0 instanceof ShutdownConfiguration )
+        if ( minaRegistry != null )
         {
-            if ( this.provider == null )
+            if ( ldapService != null )
             {
-                return new DeadContext();
+                minaRegistry.unbind( ldapService );
+                ldapService = null;
             }
 
-            try
+            if ( kerberosService != null )
             {
-                this.provider.shutdown();
-
-                if ( minaRegistry != null )
-                {
-                    if ( ldapService != null )
-                    {
-                        minaRegistry.unbind( ldapService );
-                        ldapService = null;
-                    }
-
-                    if ( kerberosService != null )
-                    {
-                        minaRegistry.unbind( kerberosService );
-                        kerberosService = null;
-                    }
-                }
-            }
-            catch( NamingException ne )
-            {
-                throw ne;
-            }
-            catch( Throwable t )
-            {
-                NamingException ne = new NamingException( "Failed to shutdown." );
-                ne.setRootCause( t );
-                throw ne;
-            }
-            finally
-            {
-                ctx = new DeadContext();
-                provider = null;
-                initialEnv = null;
-                configuration = null;
-            }
-
-            return ctx;
-        }
-
-        boolean firstRun = provider == null;
-        ctx = super.getInitialContext( env );
-
-        // fire up the front end if we have not explicitly disabled it
-        if( ctx == null || ctx instanceof DeadContext ||
-                !( cfg0 instanceof ServerStartupConfiguration ) )
-        {
-            return ctx;
-        }
-        
-        if( firstRun )
-        {
-            ServerStartupConfiguration cfg =
-                ( ServerStartupConfiguration ) cfg0;
-            if ( cfg.isEnableNetworking() )
-            {
-                setupRegistry();
-                startLdapProtocol();
-
-                if ( cfg.isEnableKerberos() )
-                {
-                    startKerberosProtocol();
-                }
+                minaRegistry.unbind( kerberosService );
+                kerberosService = null;
             }
         }
-
-        return ctx;
     }
+    
+    protected void afterStartup( ContextFactoryContext ctx ) throws NamingException
+    {
+        ServerStartupConfiguration cfg =
+            ( ServerStartupConfiguration ) ctx.getConfiguration();
+        Hashtable env = ctx.getEnvironment();
 
+        if ( cfg.isEnableNetworking() )
+        {
+            setupRegistry( cfg );
+            startLdapProtocol( cfg, env );
+
+            if ( cfg.isEnableKerberos() )
+            {
+                startKerberosProtocol( env );
+            }
+        }
+    }
 
     /**
      * Starts up the MINA registry so various protocol providers can be started.
      */
-    private void setupRegistry()
+    private void setupRegistry( ServerStartupConfiguration cfg )
     {
-        ServerStartupConfiguration configuration =
-            ( ServerStartupConfiguration ) this.configuration;
-
-        minaRegistry = configuration.getMinaServiceRegistry();
+        minaRegistry = cfg.getMinaServiceRegistry();
     }
 
 
@@ -173,7 +115,7 @@
      *
      * @throws NamingException if there are problems starting up the Kerberos provider
      */
-    private void startKerberosProtocol() throws NamingException
+    private void startKerberosProtocol( Hashtable env ) throws NamingException
     {
         /*
          * Looks like KdcConfiguration takes properties and we use Hashtable for JNDI
@@ -181,26 +123,24 @@
          */
 
         Properties props = new Properties();
-
-        Iterator list = initialEnv.keySet().iterator();
-
+        Iterator list = env.keySet().iterator();
         while ( list.hasNext() )
         {
             String key = ( String ) list.next();
 
-            if ( initialEnv.get( key ) instanceof String )
+            if ( env.get( key ) instanceof String )
             {
-                props.setProperty( key, ( String ) initialEnv.get( key ) );
+                props.setProperty( key, ( String ) env.get( key ) );
             }
         }
 
         KdcConfiguration config = new KdcConfiguration( props );
 
-        int port = PropertiesUtils.get( initialEnv, KdcConfiguration.KERBEROS_PORT_KEY, KdcConfiguration.DEFAULT_KERBEROS_PORT );
+        int port = PropertiesUtils.get( env, KdcConfiguration.KERBEROS_PORT_KEY, KdcConfiguration.DEFAULT_KERBEROS_PORT );
 
         Service service= new Service( "kerberos", TransportType.DATAGRAM, new InetSocketAddress( port ) );
 
-        InitialLdapContext ctx = new InitialLdapContext( initialEnv, new Control[]{} );
+        InitialLdapContext ctx = new InitialLdapContext( env, new Control[]{} );
 
         PrincipalStore store = new JndiPrincipalStoreImpl( ctx, new LdapName( "ou=Users" ) );
 
@@ -222,17 +162,15 @@
      *
      * @throws NamingException if there are problems starting the LDAP provider
      */
-    private void startLdapProtocol() throws NamingException
+    private void startLdapProtocol( ServerStartupConfiguration cfg, Hashtable env ) throws NamingException
     {
-        ServerStartupConfiguration configuration =
-            ( ServerStartupConfiguration ) this.configuration;
-        int port = configuration.getLdapPort();
+        int port = cfg.getLdapPort();
 
         Service service = new Service( "ldap", TransportType.SOCKET, new InetSocketAddress( port ) );
 
         try
         {
-            minaRegistry.bind( service, new LdapProtocolProvider( ( Hashtable ) initialEnv.clone() ) );
+            minaRegistry.bind( service, new LdapProtocolProvider( ( Hashtable ) env.clone() ) );
 
             ldapService = service;
         }
diff --git a/main/src/test/org/apache/ldap/server/AbstractServerTest.java b/main/src/test/org/apache/ldap/server/AbstractServerTest.java
index ec9f157..186fae3 100644
--- a/main/src/test/org/apache/ldap/server/AbstractServerTest.java
+++ b/main/src/test/org/apache/ldap/server/AbstractServerTest.java
@@ -41,6 +41,7 @@
 import org.apache.ldap.common.name.LdapName;
 import org.apache.ldap.server.configuration.MutableServerStartupConfiguration;
 import org.apache.ldap.server.configuration.ShutdownConfiguration;
+import org.apache.ldap.server.jndi.ServerContextFactory;
 import org.apache.mina.util.AvailablePortFinder;
 
 
@@ -114,8 +115,8 @@
         Hashtable env = new Hashtable( configuration.toJndiEnvironment() );
 
         env.put( Context.SECURITY_PRINCIPAL, user );
-
         env.put( Context.SECURITY_CREDENTIALS, passwd );
+        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
 
         return setSysRoot( env );
     }
@@ -134,7 +135,7 @@
     {
         Hashtable envFinal = new Hashtable( env );
         envFinal.put( Context.PROVIDER_URL, "ou=system" );
-        envFinal.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
+        envFinal.put( Context.INITIAL_CONTEXT_FACTORY, ServerContextFactory.class.getName() );
 
         return sysRoot = new InitialLdapContext( envFinal, null );
     }
diff --git a/main/src/test/org/apache/ldap/server/DisableAnonBindTest.java b/main/src/test/org/apache/ldap/server/DisableAnonBindTest.java
index 545686b..4ea07f2 100644
--- a/main/src/test/org/apache/ldap/server/DisableAnonBindTest.java
+++ b/main/src/test/org/apache/ldap/server/DisableAnonBindTest.java
@@ -48,10 +48,7 @@
      */
     public void setUp() throws Exception
     {
-        if ( getName().equals( "testDisableAnonymousBinds" ) )
-        {
-            configuration.setAllowAnonymousAccess( false );
-        }
+        configuration.setAllowAnonymousAccess( false );
         super.setUp();
     }
 
@@ -69,9 +66,7 @@
         final Hashtable env = new Hashtable();
 
         env.put( Context.PROVIDER_URL, "ldap://localhost:" + port + "/ou=system" );
-
         env.put( Context.SECURITY_AUTHENTICATION, "none" );
-
         env.put( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
 
         try
diff --git a/main/src/test/org/apache/ldap/server/jndi/ServerContextFactoryTest.java b/main/src/test/org/apache/ldap/server/jndi/ServerContextFactoryTest.java
index 62b4615..ccf61b8 100644
--- a/main/src/test/org/apache/ldap/server/jndi/ServerContextFactoryTest.java
+++ b/main/src/test/org/apache/ldap/server/jndi/ServerContextFactoryTest.java
@@ -30,7 +30,7 @@
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.DirContext;
 
-import org.apache.ldap.server.AbstractCoreTest;
+import org.apache.ldap.server.AbstractAdminTestCase;
 import org.apache.ldap.server.configuration.MutableContextPartitionConfiguration;
 
 
@@ -40,7 +40,7 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
-public class ServerContextFactoryTest extends AbstractCoreTest
+public class ServerContextFactoryTest extends AbstractAdminTestCase
 {
     public ServerContextFactoryTest()
     {
@@ -184,11 +184,9 @@
         Hashtable env = new Hashtable( configuration.toJndiEnvironment() );
 
         env.put( Context.PROVIDER_URL, "dc=example" );
-
         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
-
         env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
+        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
 
         InitialContext initialContext = new InitialContext( env );
@@ -218,11 +216,9 @@
         Hashtable env = new Hashtable( configuration.toJndiEnvironment() );
 
         env.put( Context.PROVIDER_URL, "ou=testing" );
-
         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
-
         env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
+        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
 
         InitialContext initialContext = new InitialContext( env );
@@ -252,11 +248,9 @@
         Hashtable env = new Hashtable( configuration.toJndiEnvironment() );
 
         env.put( Context.PROVIDER_URL, "dc=MixedCase" );
-
         env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
-
         env.put( Context.SECURITY_CREDENTIALS, "secret" );
-
+        env.put( Context.SECURITY_AUTHENTICATION, "simple" );
         env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" );
 
         InitialContext initialContext = new InitialContext( env );