/*
 * BasicApplicationLogger.java  $Revision: 1.6 $  $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.application;

import java.util.Map;

import com.meldcraft.util.PropertiesHelper;
import com.meldcraft.util.ReflectUtil;

/**
 * @version $Revision: 1.6 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class BasicApplicationLogger implements ApplicationContextListener
{
    private Map                 mConfig;
    private String              mElementId;

    public BasicApplicationLogger(Map config, String elementId)
    {
        mConfig = config;
        mElementId = elementId;
    }

    public void applicationContextChanged(ApplicationContextEvent e)
    {
        String log = PropertiesHelper.getString(mConfig, "logLevel", "info").toLowerCase();
        if (log.equals("warning"))
        {
            e.getApplicationContext().getLogger().warning(asString(e));
        }
        else if (log.equals("error"))
        {
            e.getApplicationContext().getLogger().severe(asString(e));
        }
        else if (log.equals("debug"))
        {
            e.getApplicationContext().getLogger().fine(asString(e));
        }
        else
        {
            e.getApplicationContext().getLogger().info(asString(e));
        }
    }

    public static String basicLogString(ApplicationContextEvent e)
    {
        StringBuffer buf = new StringBuffer(256);
        buf.append("key=")
           .append(e.getKey())
           .append(" value=")
           .append(e.getValue());
        return basicLogString(e.getApplicationContext(), buf.toString());
    }

    public static String basicLogString(Object caller, ApplicationContext context, String message)
    {
        String ret = basicLogString(context, message);
        if (caller != null)
        {
            Class cls = (caller instanceof Class) ? (Class)caller : caller.getClass();
            ret = ReflectUtil.getSimpleClassName(cls) + " " + ret;
        }
        return ret;
    }

    public static String basicLogString(ApplicationContext context, String message)
    {
        StringBuffer buf = new StringBuffer(256);
        String name = PropertiesHelper.getString(context.getConfiguration(), "Application.name", null);
        if (name == null)
        {
            name = ReflectUtil.getSimpleClassName(context.getApplication().getClass());
        }

        buf.append("[")
           .append(name)
           .append("] contextChanged[")
           .append(Thread.currentThread().getName())
           .append("]: ")
           .append(message);

        if (buf.length() > 2003)
        {
            buf.delete(2000, buf.length()).append("...");
        }

        String ret = buf.toString();
        return ret;
    }

    protected String asString(ApplicationContextEvent e)
    {
        return basicLogString(e);
    }
}



