/*
 * ConfigurationManager.java  $Revision: 1.10 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.util;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @version $Revision: 1.10 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class ConfigurationManager
{
    // Static variables
    public static final String  CONFIGURATION_BASE_NAME = "configurationBaseName";
    protected static final Map  sConfigMap = new HashMap();

// Member variables
// Constructors and initialization
    // prevent instantiation
    private ConfigurationManager()
    {
    }

// Interface implemenations
// Public methods
    public static Map getConfiguration(Object context)
    {
        Map ret = getContextConfiguration(context);
        if (ret == null)
        {
            ret = Collections.unmodifiableMap(new HashMap());
        }
        return ret;
    }

// Protected methods
    protected static Map getContextConfiguration(Object context)
    {
        synchronized (sConfigMap)
        {
            String className;
            if (context instanceof String)
            {
                className = (String)context;
                context = ConfigurationManager.class;
            }
            else
            {
                Class clazz = context.getClass();
                if (context instanceof Class)
                {
                    clazz = (Class)context;
                }
                className = clazz.getName();
            }

            Map map = (Map)sConfigMap.get(className);
            if (map == null)
            {
                try
                {
                    //map = LayeredProperties.getProperties(context, ConfigurationExpander.getResourceLayers(context, className));
                    map = LayeredProperties.getProperties(context, new String[]{className.replace('.', '/')});

                    if (map != null)
                    {
                        map.put(CONFIGURATION_BASE_NAME, className);
                        map = ConfigurationExpander.getExpandedConfig(map);
                        map = Collections.unmodifiableMap(map);
                        sConfigMap.put(className, map);
                    }
                }
                catch (IOException e)
                {
                    throw new RuntimeException("Error reading layered properties.", e);
                }
            }

            if (map != null)
            {
                Object key = map.get("dumpConfiguration");
                if (key != null)
                {
                    Object[] keys = map.keySet().toArray();
                    if (keys != null)
                    {
                        java.util.Arrays.sort(keys);
                        System.out.println("=== Configuration for " + className + " begin");
                        for (int i=0 ; i < keys.length ; i++)
                        {
                            key = keys[i];
                            System.out.println(key + " = " + map.get(key));
                        }
                        System.out.println("=== Configuration for " + className + " end");
                    }
                }
            }

            return map;
        }
    }

// Private methods
// Inner classes
}
