FileDocCategorySizeDatePackage
ApplicationContextFacade.javaAPI DocGlassfish v2 API18125Fri May 04 22:31:54 BST 2007org.apache.catalina.core

ApplicationContextFacade

public final class ApplicationContextFacade extends Object implements ServletContext
Facade object which masks the internal ApplicationContext object from the web application.
author
Remy Maucherat
author
Jean-Francois Arcand
version
$Revision: 1.7 $ $Date: 2007/05/05 05:31:53 $

Fields Summary
private static HashMap
classCache
Cache Class object used for reflection.
private HashMap
objectCache
Cache method object.
private static com.sun.org.apache.commons.logging.Log
sysLog
private ApplicationContext
context
Wrapped application context.
Constructors Summary
public ApplicationContextFacade(ApplicationContext context)
Construct a new instance of this class, associated with the specified Context instance.

param
context The associated Context instance


        
    // ----------------------------------------------------------- Constructors


                            
       
        super();
        this.context = context;
        
        objectCache = new HashMap();
    
Methods Summary
private java.lang.ObjectdoPrivileged(ApplicationContext appContext, java.lang.String methodName, java.lang.Object[] params)
Use reflection to invoke the requested method. Cache the method object to speed up the process

param
appContext The AppliationContext object on which the method will be invoked
param
methodName The method to call.
param
params The arguments passed to the called method.

        try{
            return invokeMethod(appContext, methodName, params );
        } catch (Throwable t){
            throw new RuntimeException(t.getMessage());
        } finally {
            params = null;
        }

    
private java.lang.ObjectdoPrivileged(java.lang.String methodName, java.lang.Object[] params)
Use reflection to invoke the requested method. Cache the method object to speed up the process will be invoked

param
methodName The method to call.
param
params The arguments passed to the called method.

        try{
            return invokeMethod(context, methodName, params);
        }catch(Throwable t){
            throw new RuntimeException(t.getMessage());
        } finally {
            params = null;
        }
    
private java.lang.ObjectdoPrivileged(java.lang.String methodName, java.lang.Class[] clazz, java.lang.Object[] params)
Use reflection to invoke the requested method. Cache the method object to speed up the process

param
appContext The AppliationContext object on which the method will be invoked
param
methodName The method to call.
param
params The arguments passed to the called method.


        try{
            Method method = context.getClass()
                    .getMethod(methodName, (Class[])clazz);
            return executeMethod(method,context,params);
        } catch (Exception ex){
            try{
                handleException(ex, methodName);
            }catch (Throwable t){
                throw new RuntimeException(t.getMessage());
            }
            return null;
        } finally {
            params = null;
        }
    
private java.lang.ObjectexecuteMethod(java.lang.reflect.Method method, ApplicationContext context, java.lang.Object[] params)
Executes the method of the specified ApplicationContext

param
method The method object to be invoked.
param
context The AppliationContext object on which the method will be invoked
param
params The arguments passed to the called method.

                                     
        if (Globals.IS_SECURITY_ENABLED){
           return AccessController.doPrivileged(new PrivilegedExceptionAction(){
                public Object run() throws IllegalAccessException, InvocationTargetException{
                    return method.invoke(context,  params);
                }
            });
        } else {
            return method.invoke(context, params);
        }        
    
public java.lang.ObjectgetAttribute(java.lang.String name)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return doPrivileged("getAttribute", new Object[]{name});
        } else {
            return context.getAttribute(name);
        }
     
public java.util.EnumerationgetAttributeNames()

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (Enumeration) doPrivileged("getAttributeNames", null);
        } else {
            return context.getAttributeNames();
        }
    
public javax.servlet.ServletContextgetContext(java.lang.String uripath)

        ServletContext theContext = null;
        if (SecurityUtil.isPackageProtectionEnabled()) {
            theContext = (ServletContext)
                doPrivileged("getContext", new Object[]{uripath});
        } else {
            theContext = context.getContext(uripath);
        }
        if ((theContext != null) &&
            (theContext instanceof ApplicationContext)){
            theContext = ((ApplicationContext)theContext).getFacade();
        }
        return (theContext);
    
public java.lang.StringgetContextPath()

    


    // ------------------------------------------------- ServletContext Methods

       
        return context.getContextPath();
    
public java.lang.StringgetInitParameter(java.lang.String name)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (String) doPrivileged("getInitParameter", 
                                         new Object[]{name});
        } else {
            return context.getInitParameter(name);
        }
    
public java.util.EnumerationgetInitParameterNames()

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (Enumeration) doPrivileged("getInitParameterNames", null);
        } else {
            return context.getInitParameterNames();
        }
    
public intgetMajorVersion()

        return context.getMajorVersion();
    
public java.lang.StringgetMimeType(java.lang.String file)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (String)doPrivileged("getMimeType", new Object[]{file});
        } else {
            return context.getMimeType(file);
        }
    
public intgetMinorVersion()

        return context.getMinorVersion();
    
public javax.servlet.RequestDispatchergetNamedDispatcher(java.lang.String name)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (RequestDispatcher) doPrivileged("getNamedDispatcher", 
                                                    new Object[]{name});
        } else {
            return context.getNamedDispatcher(name);
        }
    
public java.lang.StringgetRealPath(java.lang.String path)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (String) doPrivileged("getRealPath", new Object[]{path});
        } else {
            return context.getRealPath(path);
        }
    
public javax.servlet.RequestDispatchergetRequestDispatcher(java.lang.String path)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (RequestDispatcher) doPrivileged("getRequestDispatcher", 
                                                    new Object[]{path});
        } else {
            return context.getRequestDispatcher(path);
        }
    
public java.net.URLgetResource(java.lang.String path)

        if (Globals.IS_SECURITY_ENABLED) {
            try {
                return (URL) invokeMethod(context, "getResource", 
                                          new Object[]{path});
            } catch(Throwable t) {
                if (t instanceof MalformedURLException){
                    throw (MalformedURLException)t;
                }
                return null;
            }
        } else {
            return context.getResource(path);
        }
    
public java.io.InputStreamgetResourceAsStream(java.lang.String path)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (InputStream) doPrivileged("getResourceAsStream", 
                                              new Object[]{path});
        } else {
            return context.getResourceAsStream(path);
        }
    
public java.util.SetgetResourcePaths(java.lang.String path)

        if (SecurityUtil.isPackageProtectionEnabled()){
            return (Set)doPrivileged("getResourcePaths", new Object[]{path});
        } else {
            return context.getResourcePaths(path);
        }
    
public java.lang.StringgetServerInfo()

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (String) doPrivileged("getServerInfo", null);
        } else {
            return context.getServerInfo();
        }
    
public javax.servlet.ServletgetServlet(java.lang.String name)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            try {
                return (Servlet) invokeMethod(context, "getServlet", 
                                              new Object[]{name});
            } catch (Throwable t) {
                if (t instanceof ServletException) {
                    throw (ServletException) t;
                }
                return null;
            }
        } else {
            return context.getServlet(name);
        }
    
public java.lang.StringgetServletContextName()

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (String) doPrivileged("getServletContextName", null);
        } else {
            return context.getServletContextName();
        }
    
public java.util.EnumerationgetServletNames()

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (Enumeration) doPrivileged("getServletNames", null);
        } else {
            return context.getServletNames();
        }
   
public java.util.EnumerationgetServlets()

        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (Enumeration) doPrivileged("getServlets", null);
        } else {
            return context.getServlets();
        }
    
public StandardContextgetUnwrappedContext()
Gets the underlying StandardContext to which this ApplicationContextFacade is ultimately delegating.

return
The underlying StandardContext

        return context.getUnwrappedContext();
    
private voidhandleException(java.lang.Exception ex, java.lang.String methodName)
Throw the real exception.

param
ex The current exception


        Throwable realException;

        if (sysLog.isDebugEnabled()) {   
            sysLog.debug("ApplicationContextFacade." + methodName, ex);
        }

	if (ex instanceof PrivilegedActionException) {
            ex = ((PrivilegedActionException) ex).getException();
	}

        if (ex instanceof InvocationTargetException) {
            realException =
		((InvocationTargetException) ex).getTargetException();
        } else {
            realException = ex;
        }   

        throw realException;
    
private java.lang.ObjectinvokeMethod(ApplicationContext appContext, java.lang.String methodName, java.lang.Object[] params)
Use reflection to invoke the requested method. Cache the method object to speed up the process

param
appContext The AppliationContext object on which the method will be invoked
param
methodName The method to call.
param
params The arguments passed to the called method.


        try{
            Method method = (Method)objectCache.get(methodName);
            if (method == null){
                method = appContext.getClass()
                    .getMethod(methodName, (Class[])classCache.get(methodName));
                objectCache.put(methodName, method);
            }
            
            return executeMethod(method,appContext,params);
        } catch (Exception ex){
            handleException(ex, methodName);
            return null;
        } finally {
            params = null;
        }
    
public voidlog(java.lang.String msg)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("log", new Object[]{msg} );
        } else {
            context.log(msg);
        }
    
public voidlog(java.lang.Exception exception, java.lang.String msg)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("log", new Class[]{Exception.class, String.class}, 
                         new Object[]{exception,msg});
        } else {
            context.log(exception, msg);
        }
    
public voidlog(java.lang.String message, java.lang.Throwable throwable)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("log", new Class[]{String.class, Throwable.class}, 
                         new Object[]{message, throwable});
        } else {
            context.log(message, throwable);
        }
    
public voidremoveAttribute(java.lang.String name)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("removeAttribute", new Object[]{name});
        } else {
            context.removeAttribute(name);
        }
    
public voidsetAttribute(java.lang.String name, java.lang.Object object)

        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("setAttribute", new Object[]{name,object});
        } else {
            context.setAttribute(name, object);
        }