/*
 * Copyright 2005-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.ldap;

import java.util.Properties;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.ContextNotEmptyException;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.directory.server.core.configuration.ShutdownConfiguration;
import org.apache.directory.server.jndi.ServerContextFactory;
import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;

/**
 * Utility class to initialize the apache directory server for use in the
 * integration tests.
 * 
 * @author Mattias Arthursson
 * 
 */
public class LdapServerManager implements DisposableBean {
    private static Log log = LogFactory.getLog(LdapServerManager.class);

    private ContextSource contextSource;

    public void setContextSource(ContextSource contextSource) {
        this.contextSource = contextSource;
    }

    public void destroy() throws Exception {
        Properties env = new Properties();
        env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                ServerContextFactory.class.getName());
        env.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
        env.setProperty(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
        env.setProperty(Context.SECURITY_CREDENTIALS, "secret");

        ShutdownConfiguration configuration = new ShutdownConfiguration();
        env.putAll(configuration.toJndiEnvironment());

        new InitialContext(env);
    }

    public void cleanAndSetup(String ldifFile) throws Exception {
        DirContext ctx = contextSource.getReadWriteContext();

        // First of all, make sure the database is empty.
        Name startingPoint = null;

        // Different test cases have different base paths. This means that the
        // starting point will be different.
        if (ctx.getEnvironment().get(
                DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY) != null) {
            startingPoint = DistinguishedName.EMPTY_PATH;
        } else {
            startingPoint = new DistinguishedName("dc=jayway,dc=se");
        }

        try {
            log.info("Cleaning all present data.");
            clearSubContexts(ctx, startingPoint);
            // Load the ldif to the recently started server
            log.info("Loading setup data");
            LdifFileLoader loader = new LdifFileLoader(ctx, ldifFile);
            loader.execute();
        } finally {
            ctx.close();
        }
    }

    private void clearSubContexts(DirContext ctx, Name name)
            throws NamingException {

        NamingEnumeration enumeration = null;
        try {
            enumeration = ctx.listBindings(name);
            while (enumeration.hasMore()) {
                Binding element = (Binding) enumeration.next();
                DistinguishedName childName = new DistinguishedName(element
                        .getName());
                childName.prepend((DistinguishedName) name);

                try {
                    ctx.destroySubcontext(childName);
                } catch (ContextNotEmptyException e) {
                    clearSubContexts(ctx, childName);
                    ctx.destroySubcontext(childName);
                }
            }
        } catch (NamingException e) {
            e.printStackTrace();
        } finally {
            try {
                enumeration.close();
            } catch (Exception e) {
                // Never mind this
            }
        }
    }
}

@@ -1509,39 +1509,33 @@
 support.
-DefaultDirObjectFactory
+BaseLdapPathAware
 ;%0D%0A%0D%0A/**
@@ -1736,16 +1736,35 @@
 ableBean
+, BaseLdapPathAware
  %7B%0D%0A    
@@ -1875,24 +1875,68 @@
 tSource;%0D%0A%0D%0A
+%09private DistinguishedName baseLdapPath;%0D%0A%0D%0A
     public v
@@ -2033,16 +2033,16 @@
 ource;%0D%0A
-
     %7D%0D%0A%0D
@@ -2034,32 +2034,139 @@
 urce;%0D%0A    %7D%0D%0A%0D%0A
+%09public void setBaseLdapPath(DistinguishedName baseLdapPath) %7B%0D%0A%09%09this.baseLdapPath = baseLdapPath;%0D%0A%09%7D%0D%0A%0D%0A
     public void 
@@ -3115,106 +3115,32 @@
 if (
-ctx.getEnvironment().get(%0D%0A                DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY
+baseLdapPath.size(
 ) != 
-null
+0
 ) %7B%0D
@@ -3271,25 +3271,20 @@
 ame(
-%22dc=jayway,dc=se%22
+baseLdapPath
 );%0D%0A

Change LdapServerManager to be BaseLdapPathAware.