/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.apache.directory.server.core.configuration;


import java.io.Serializable;
import java.util.Hashtable;

import org.apache.directory.server.core.DirectoryService;


/**
 * A configuration that provides required, optional, or default properties
 * to configure {@link DirectoryService}.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public abstract class Configuration implements Cloneable, Serializable
{
    /**
     * A JNDI environment key that configuration instance is put on. 
     */
    public static final String JNDI_KEY = Configuration.class.getName();

    /**
     * The default ID of {@link DirectoryService} that is used
     * when no instance ID is specified. 
     */
    public static final String DEFAULT_INSTANCE_ID = "default";


    /**
     * Gets {@link Configuration} instance from the specified JNDI environment
     * {@link Hashtable}.
     * 
     * @throws ConfigurationException if the specified environment doesn't
     *                                contain the configuration instance.
     */
    public static Configuration toConfiguration( Hashtable jndiEnvironment )
    {
        Object value = jndiEnvironment.get( JNDI_KEY );
        if ( value == null || !( value instanceof Configuration ) )
        {
            throw new ConfigurationException( "Not an ApacheDS configuration: " + value );
        }

        return ( Configuration ) value;
    }

    private String instanceId = DEFAULT_INSTANCE_ID;


    /**
     * Creates a new instance.
     */
    protected Configuration()
    {
    }


    /**
     * Returns the ID of {@link DirectoryService} instance to configure.
     */
    public String getInstanceId()
    {
        return instanceId;
    }


    /**
     * Sets the ID of {@link DirectoryService} instance to configure.
     */
    protected void setInstanceId( String instanceId )
    {
        instanceId = instanceId.trim();
        this.instanceId = instanceId;
    }


    /**
     * Validates this configuration.
     * @throws ConfigurationException if this configuration is not valid
     */
    public void validate()
    {
    }


    /**
     * Converts this configuration to JNDI environment {@link Hashtable}.
     * This method simple returns a {@link Hashtable} that contains an entry
     * whose key is {@link #JNDI_KEY} and whose value is <tt>this</tt>.
     */
    public Hashtable toJndiEnvironment()
    {
        Hashtable env = new Hashtable();
        env.put( JNDI_KEY, this );
        return env;
    }


    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch ( CloneNotSupportedException e )
        {
            throw new InternalError();
        }
    }
}

@@ -1765,25 +1765,156 @@
  Hashtable%7D.
+  If a configuration instance is not present the default%0A     * StartupConfiguration is returned and injected into the environment.
 %0A
-
      * %0A    
@@ -2034,16 +2034,23 @@
 ain the 
+proper 
 configur
@@ -2208,24 +2208,33 @@
 JNDI_KEY );%0A
+        %0A
         if (
@@ -2252,10 +2252,206 @@
 ull 
-%7C%7C
+)%0A        %7B%0A            MutableStartupConfiguration msc = new MutableStartupConfiguration();%0A            jndiEnvironment.put( JNDI_KEY, msc );%0A            return msc;%0A        %7D%0A        %0A        if (
  !( 

Fix for DIRSERVER-291: If configuration object is missing in env to startup server a default StartupConfiguration can be presumed.