package com.capco.util;

/**
 * Helper class providing access to the execution stack. Typically the
 * execution stack is useful for loading application resources from
 * Java extensions or framework classes because those may not have
 * the same <code>ClassLoader</code> as the calling application. By
 * getting the execution stack, one can get a handle to the calling
 * <code>Class</code> and then use its <code>ClassLoader</code> for
 * retrieving application resources.
 * <p>For example, the TMXResourceBundle uses this class for retrieving
 * the TMX file to parse as shown below:
 * <blockquote>
 * <pre>
 *		ClassLoader cl = ClassContextHelper.getCallerClassLoader();
 *		InputStream stream = cl.getResourceAsStream(tmx); 
 * </pre>
 * </blockquote>
 *
 * @author Koen Van der Auwera (KVWR - koen.van.der.auwera@capco.com)
 * @version 1.0
 */

public class ClassContextHelper extends SecurityManager
{
	static private ClassContextHelper helper = new ClassContextHelper();
	
	/**
	 * Return the execution stack as found within this operation.
	 * The most common usage of the stack is to retrieve the caller's
	 * <code>Class</code>, which is found at index 2 of the returned array.
	 * @return the execution stack as an array of <code>Class</code> instances
	 */
	static public Class[] getExecutionStack() 
	{ 
		return helper.getClassContext(); 
	}
	
	/**
	 * Return the caller's <code>Class</code>, that is to say, the <code>Class</code> instance of
	 * the object calling the caller of this operation.
	 * @return the caller's <code>Class</code>
	 */
	static public Class getCallerClass() {
		return (helper.getClassContext())[2];
	}
	
	/**
	 * Return the caller's <code>ClassLoader</code>, that is the
	 * <code>ClassLoader</code> instance of the object calling the caller
	 * of this operation.
	 * @return the caller's <code>ClassLoader</code>
	 */
	static public ClassLoader getCallerClassLoader() {
		return ((helper.getClassContext())[2]).getClassLoader();
	}
	
	
	
}
