package CoreFS.SD03;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;

import CoreFS.SA01.CoreException;

public class CoreLogFactory {

	public CoreLogFactory() {
	}

	private static Class loadClass(String s) throws ClassNotFoundException {
		Class class1 = null;
		try {
			ClassLoader classloader = Thread.currentThread()
					.getContextClassLoader();
			class1 = classloader.loadClass(s);
		} catch (ClassNotFoundException classnotfoundexception) {
			class1 = CoreLogFactory.class.getClassLoader().loadClass(s);
		}
		return class1;
	}

	public static CoreFileLog getLogger(String s) {
		if (isLog4JAvailable)
			return getLogger("CoreLog4JLogger", s);
		if (isJdk14Available)
			return getLogger("CoreJdk14Logger", s);
		else
			return getLogger("CoreSimpleLogger", s);
	}

	public static CoreFileLog getLogger(Class class1) {
		return getLogger(class1.getName());
	}

	public static CoreFileLog getLogger(String s, String s1) {
		if (s == null)
			throw new CoreException("Logger class can not be NULL");
		if (s1 == null)
			throw new CoreException("Logger instance name can not be NULL");
		CoreFileLog CoreILog = (CoreFileLog) instances.get(s1);
		if (CoreILog == null) {
			CoreILog = newInstance(s, s1);
			instances.put(s1, CoreILog);
		}
		return CoreILog;
	}

	public static CoreFileLog getLogger(String s, Class class1) {
		return getLogger(s, class1.getName());
	}

	private static CoreFileLog newInstance(String s, String s1) {
		try {
			Class class1 = null;
			ClassLoader classloader = Thread.currentThread()
					.getContextClassLoader();
			try {
				class1 = classloader.loadClass(s);
			} catch (ClassNotFoundException classnotfoundexception1) {
				class1 = classloader.loadClass(CoreFileLog.class.getPackage()
						.getName()
						+ "." + s);
			}
			Constructor constructor = class1
					.getConstructor(new Class[] { java.lang.String.class });
			return (CoreFileLog) constructor.newInstance(new Object[] { s1 });
		} catch (ClassNotFoundException classnotfoundexception) {
			throw new CoreException("Unable to load class for logger '" + s
					+ "'");
		} catch (NoSuchMethodException nosuchmethodexception) {
			throw new CoreException(
					"Logger class does not have a single-arg constructor that takes an instance name");
		} catch (InstantiationException instantiationexception) {
			throw new CoreException("Unable to instantiate logger class '" + s
					+ "', exception in constructor?");
		} catch (InvocationTargetException invocationtargetexception) {
			throw new CoreException("Unable to instantiate logger class '" + s
					+ "', exception in constructor?");
		} catch (IllegalAccessException illegalaccessexception) {
			throw new CoreException("Unable to instantiate logger class '" + s
					+ "', constructor not public");
		} catch (ClassCastException classcastexception) {
			throw new CoreException("Logger class '" + s
					+ "' does not implement the '" + CoreFileLog.class.getName());
		}
	}

	protected static boolean isLog4JAvailable;
	protected static boolean isJdk14Available;
	protected static Hashtable instances = new Hashtable();

	static {
		try {
			loadClass("org.apache.log4j.Logger");
			loadClass("org.apache.commons.logging.impl.Log4JLogger");
			isLog4JAvailable = true;
		} catch (Throwable throwable) {
			isLog4JAvailable = false;
		}
		try {
			loadClass("java.util.logging.Logger");
			loadClass("org.apache.commons.logging.impl.Jdk14Logger");
			isJdk14Available = true;
		} catch (Exception exception) {
			isJdk14Available = false;
		}
	}
}
