package de.spieleck.util;

import java.util.HashMap;

/**
 * This is a replacement for Class.forName(...).newInstance().
 * <p>
 * <b>It is fast.</b>
 * In my bench it is much faster than the latter construct.
 * Efficient caching and loading mechanism to obtain classes
 * with a default constructor in the current ContextClassLoader.
 * </p>
 * <p>
 * <b>It treats some classloader intricaties.</b>
 * </p>
 *
 * @author fsn
 * @version 0
 *
 */
public class FastClassForName
{
    /** You do not want to construct this. */
    private FastClassForName() { }

    /** Keep track of all classes aquired for
        faster second access. */
    protected static HashMap classCache = new HashMap(15);

    public static Object newInstance(String className, Class require)
    {
        return newInstance(className, null, require);
    }

    public static Object newInstance(String className, String defaultPackage, 
                                     Class require)
    {
        return newInstance(className, defaultPackage, require, require);
    }

    public static Object newInstance(String className, String defaultPackage, 
                                     Class require, Class context)
    {
        if (className == null)
            return null;
        try
        {
            Class clazz = null;
            String classIndex;
            if (defaultPackage != null)
                classIndex = defaultPackage + className;
            else
                classIndex = className;

            //
            // Probe the class cache (this is supposed to speed things up
            // Note: This might find a wrong version of a class on a 
            // multi classloader context. This is a XXX.
            clazz = (Class)classCache.get(classIndex);

            //
            // If we failed...
            if (clazz == null)
            {
                try
                {
                    clazz = contextClassForName(className, context);
                }
                catch (Exception oneMoreTrial)
                {
                    // Second trial with extended path
                    if (defaultPackage != null)
                        clazz = contextClassForName(classIndex, context);
                }
                if (clazz != null)
                    classCache.put(classIndex, clazz);
            }
            if ( clazz == null )
            {
                System.err.println("?no Class '"+className
                                              +"@"+defaultPackage+"' found.");
            }
            else if ( require.isAssignableFrom(clazz))
                return clazz.newInstance();
            else
            {
                System.err.println("?'"+clazz+"' not assignable to '"
                        +require+"'!");
            }
        }
        catch (Exception help)
        {
            System.err.println("?no Class '" + className + "' found." + help);
            help.printStackTrace();
        }
        return null;
    }

    /**
     * Try to load a class from the right contextual classloader.
     * The right classloader can be somewhat strange in an Ant or
     * Servlet context.
     */
    public static Class contextClassForName(String className, Class context)
       throws ClassNotFoundException
    {
        try
        {
            ClassLoader loader = context.getClassLoader();
            return Class.forName(className, false, loader);
        }
        catch (NoSuchMethodError ignore)
        {
            // JDK1.1 fallback
            return Class.forName(className);
        }
        catch ( ClassNotFoundException e )
        {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            return Class.forName(className, false, loader);
        }
    }
}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library 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 2.1 of the License, or (at your option) any later version.
//
//    This library 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 this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//