Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.catalina.core;
 
 
 import java.net.URL;
 import java.util.Map;
 import java.util.Set;
 
 
Facade object which masks the internal ApplicationContext object from the web application.

Author(s):
Remy Maucherat
Jean-Francois Arcand
Version:
$Revision: 1236 $ $Date: 2009-11-02 23:17:08 +0100 (Mon, 02 Nov 2009) $
 
 
 public final class ApplicationContextFacade
     implements ServletContext {
         
     // ---------------------------------------------------------- Attributes
     
Cache Class object used for reflection.
 
     private HashMap classCache;
    
    
    
Cache method object.
 
     private HashMap objectCache;
     
     
     // ----------------------------------------------------------- Constructors
 

    
Construct a new instance of this class, associated with the specified Context instance.

Parameters:
context The associated Context instance
 
     public ApplicationContextFacade(ApplicationContext context) {
         super();
         this. = context;
         
          = new HashMap();
          = new HashMap();
         initClassCache();
     }
     
     
     private void initClassCache(){
         // FIXME: redo method list
         Class[] clazz = new Class[]{String.class};
         .put("getContext"clazz);
        .put("getMimeType"clazz);
        .put("getResourcePaths"clazz);
        .put("getResource"clazz);
        .put("getResourceAsStream"clazz);
        .put("getRequestDispatcher"clazz);
        .put("getNamedDispatcher"clazz);
        .put("getServlet"clazz);
        .put("getInitParameter"clazz);
        .put("setAttribute"new Class[]{String.classObject.class});
        .put("removeAttribute"clazz);
        .put("getRealPath"clazz);
        .put("getAttribute"clazz);
        .put("log"clazz);
        .put("setSessionTrackingModes"new Class[]{EnumSet.class} );
        .put("setSessionCookieConfig",
                new Class[]{SessionCookieConfig.class});
    }
    // ----------------------------------------------------- Instance Variables


    
Wrapped application context.
    private ApplicationContext context = null;
    
    // ------------------------------------------------- ServletContext Methods
    public ServletContext getContext(String uripath) {
        ServletContext theContext = null;
        if (SecurityUtil.isPackageProtectionEnabled()) {
            theContext = (ServletContext)
                doPrivileged("getContext"new Object[]{uripath});
        } else {
            theContext = .getContext(uripath);
        }
        if ((theContext != null) &&
            (theContext instanceof ApplicationContext)){
            theContext = ((ApplicationContext)theContext).getFacade();
        }
        return (theContext);
    }
    public int getMajorVersion() {
        return .getMajorVersion();
    }
    public int getMinorVersion() {
        return .getMinorVersion();
    }
    public String getMimeType(String file) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (String)doPrivileged("getMimeType"new Object[]{file});
        } else {
            return .getMimeType(file);
        }
    }
    public Set getResourcePaths(String path) {
        if (SecurityUtil.isPackageProtectionEnabled()){
            return (Set)doPrivileged("getResourcePaths"new Object[]{path});
        } else {
            return .getResourcePaths(path);
        }
    }
    public URL getResource(String path)
        throws MalformedURLException {
        if (.) {
            try {
                return (URLinvokeMethod("getResource"
                                          new Object[]{path});
            } catch(Throwable t) {
                if (t instanceof MalformedURLException){
                    throw (MalformedURLException)t;
                }
                return null;
            }
        } else {
            return .getResource(path);
        }
    }
    public InputStream getResourceAsStream(String path) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (InputStreamdoPrivileged("getResourceAsStream"
                                              new Object[]{path});
        } else {
            return .getResourceAsStream(path);
        }
    }
    public RequestDispatcher getRequestDispatcher(final String path) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (RequestDispatcherdoPrivileged("getRequestDispatcher"
                                                    new Object[]{path});
        } else {
            return .getRequestDispatcher(path);
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (RequestDispatcherdoPrivileged("getNamedDispatcher"
                                                    new Object[]{name});
        } else {
            return .getNamedDispatcher(name);
        }
    }
    public Servlet getServlet(String name)
        throws ServletException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            try {
                return (ServletinvokeMethod("getServlet"
                                              new Object[]{name});
            } catch (Throwable t) {
                if (t instanceof ServletException) {
                    throw (ServletExceptiont;
                }
                return null;
            }
        } else {
            return .getServlet(name);
        }
    }
    public Enumeration getServlets() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (EnumerationdoPrivileged("getServlets"null);
        } else {
            return .getServlets();
        }
    }
    public Enumeration getServletNames() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (EnumerationdoPrivileged("getServletNames"null);
        } else {
            return .getServletNames();
        }
   }
    public void log(String msg) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("log"new Object[]{msg} );
        } else {
            .log(msg);
        }
    }
    public void log(Exception exceptionString msg) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("log"new Class[]{Exception.classString.class}, 
                         new Object[]{exception,msg});
        } else {
            .log(exceptionmsg);
        }
    }
    public void log(String messageThrowable throwable) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("log"new Class[]{String.classThrowable.class}, 
                         new Object[]{messagethrowable});
        } else {
            .log(messagethrowable);
        }
    }
    public String getRealPath(String path) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (StringdoPrivileged("getRealPath"new Object[]{path});
        } else {
            return .getRealPath(path);
        }
    }
    public String getServerInfo() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (StringdoPrivileged("getServerInfo"null);
        } else {
            return .getServerInfo();
        }
    }
    public String getInitParameter(String name) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (StringdoPrivileged("getInitParameter"
                                         new Object[]{name});
        } else {
            return .getInitParameter(name);
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (EnumerationdoPrivileged("getInitParameterNames"null);
        } else {
            return .getInitParameterNames();
        }
    }
    public Object getAttribute(String name) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return doPrivileged("getAttribute"new Object[]{name});
        } else {
            return .getAttribute(name);
        }
     }
    public Enumeration getAttributeNames() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (EnumerationdoPrivileged("getAttributeNames"null);
        } else {
            return .getAttributeNames();
        }
    }
    public void setAttribute(String nameObject object) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("setAttribute"new Object[]{name,object});
        } else {
            .setAttribute(nameobject);
        }
    }
    public void removeAttribute(String name) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("removeAttribute"new Object[]{name});
        } else {
            .removeAttribute(name);
        }
    }
    public String getServletContextName() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (StringdoPrivileged("getServletContextName"null);
        } else {
            return .getServletContextName();
        }
    }
       
    public String getContextPath() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (StringdoPrivileged("getContextPath"null);
        } else {
            return .getContextPath();
        }
    }
       
    public FilterRegistration.Dynamic addFilter(String filterNameString className)
            throws IllegalArgumentExceptionIllegalStateException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (FilterRegistration.DynamicdoPrivileged("addFilter",
                    new Object[]{filterNameclassName});
        } else {
            return .addFilter(filterNameclassName);
        }
    }
    public FilterRegistration.Dynamic addFilter(String filterNameFilter filter) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (FilterRegistration.DynamicdoPrivileged("addFilter",
                    new Object[]{filterNamefilter});
        } else {
            return .addFilter(filterNamefilter);
        }
    }
    public FilterRegistration.Dynamic addFilter(String filterName,
            Class<? extends FilterfilterClass) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (FilterRegistration.DynamicdoPrivileged("addFilter",
                    new Object[]{filterNamefilterClass});
        } else {
            return .addFilter(filterNamefilterClass);
        }
    }
    public ServletRegistration.Dynamic addServlet(String servletNameString className)
            throws IllegalArgumentExceptionIllegalStateException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (ServletRegistration.DynamicdoPrivileged("addServlet",
                    new Object[]{servletNameclassName});
        } else {
            return .addServlet(servletNameclassName);
        }
    }
    public ServletRegistration.Dynamic addServlet(String servletName,
            Class<? extends Servletclazzthrows IllegalArgumentException,
            IllegalStateException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (ServletRegistration.DynamicdoPrivileged("addServlet",
                    new Object[]{servletNameclazz});
        } else {
            return .addServlet(servletNameclazz);
        }
    }
    public ServletRegistration.Dynamic addServlet(String servletNameServlet servlet) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (ServletRegistration.DynamicdoPrivileged("addServlet",
                    new Object[]{servletNameservlet});
        } else {
            return .addServlet(servletNameservlet);
        }
    }
    public <T extends Filter> T createFilter(Class<T> c)
            throws ServletException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (T) doPrivileged("createFilter"new Object[]{c});
        } else {
            return .createFilter(c);
        }
    }
    public <T extends Servlet> T createServlet(Class<T> c)
            throws ServletException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (T) doPrivileged("createServlet"new Object[]{c});
        } else {
            return .createServlet(c);
        }
    }
    public boolean setInitParameter(String nameString value) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (BooleandoPrivileged("setInitParameter",
                    new Object[]{namevalue});
        } else {
            return .setInitParameter(namevalue);
        }
    }
    public FilterRegistration getFilterRegistration(String filterName) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (FilterRegistrationdoPrivileged("getFilterRegistration",
                    new Object[]{filterName});
        } else {
            return .getFilterRegistration(filterName);
        }
    }
    public ServletRegistration getServletRegistration(String servletName) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (ServletRegistrationdoPrivileged("getServletRegistration",
                    new Object[]{servletName});
        } else {
            return .getServletRegistration(servletName);
        }
    }
    
    
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (Map<StringServletRegistration>) doPrivileged("getServletRegistrations",
                    new Object[]{});
        } else {
            return .getServletRegistrations();
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (Map<StringFilterRegistration>) doPrivileged("getFilterRegistrations",
                    new Object[]{});
        } else {
            return .getFilterRegistrations();
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (EnumSet<SessionTrackingMode>)
                doPrivileged("getDefaultSessionTrackingModes"null);
        } else {
            return .getDefaultSessionTrackingModes();
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (EnumSet<SessionTrackingMode>)
                doPrivileged("getEffectiveSessionTrackingModes"null);
        } else {
            return .getEffectiveSessionTrackingModes();
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (SessionCookieConfig)
                doPrivileged("getSessionCookieConfig"null);
        } else {
            return .getSessionCookieConfig();
        }
    }
    public void setSessionTrackingModes(Set<SessionTrackingModesessionTrackingModes) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("setSessionTrackingModes",
                    new Object[]{sessionTrackingModes});
        } else {
            .setSessionTrackingModes(sessionTrackingModes);
        }
    }
    public void addListener(String className) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("addListener"new Object[]{className});
        } else {
            .addListener(className);
        }
    }
    public <T extends EventListenervoid addListener(T t) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("addListener"new Object[]{t});
        } else {
            .addListener(t);
        }
    }
    public void addListener(Class<? extends EventListenerlistenerClass) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("addListener"new Object[]{listenerClass});
        } else {
            .addListener(listenerClass);
        }
    }
    public <T extends EventListener> T createListener(Class<T> clazz)
            throws ServletException {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (T) doPrivileged("createListener"new Object[]{clazz});
        } else {
            return .createListener(clazz);
        }
    }
    public ClassLoader getClassLoader() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (ClassLoaderdoPrivileged("getClassLoader"null);
        } else {
            return .getClassLoader();
        }
    }
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (JspConfigDescriptordoPrivileged("getJspConfigDescriptor"null);
        } else {
            return .getJspConfigDescriptor();
        }
    }
    public int getEffectiveMajorVersion() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (IntegerdoPrivileged("getEffectiveMajorVersion"null);
        } else {
            return .getEffectiveMajorVersion();
        }
    }
    public int getEffectiveMinorVersion() {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            return (IntegerdoPrivileged("getEffectiveMinorVersion"null);
        } else {
            return .getEffectiveMinorVersion();
        }
    }
    public void declareRoles(String... roleNames) {
        if (SecurityUtil.isPackageProtectionEnabled()) {
            doPrivileged("declareRoles"new Object[]{roleNames});
        } else {
            .declareRoles(roleNames);
        }
    }
    
    
Use reflection to invoke the requested method. Cache the method object to speed up the process

Parameters:
appContext The AppliationContext object on which the method will be invoked
methodName The method to call.
params The arguments passed to the called method.
    private Object doPrivileged(ApplicationContext appContext,
                                final String methodName
                                final Object[] params) {
        try{
            return invokeMethod(appContextmethodNameparams );
        } catch (Throwable t){
            throw new RuntimeException(t.getMessage(), t);
        }
    }


    
Use reflection to invoke the requested method. Cache the method object to speed up the process will be invoked

Parameters:
methodName The method to call.
params The arguments passed to the called method.
    private Object doPrivileged(final String methodNamefinal Object[] params){
        try{
            return invokeMethod(methodNameparams);
        }catch(Throwable t){
            throw new RuntimeException(t.getMessage(), t);
        }
    }

    
    
Use reflection to invoke the requested method. Cache the method object to speed up the process

Parameters:
appContext The AppliationContext object on which the method will be invoked
methodName The method to call.
params The arguments passed to the called method.
    private Object invokeMethod(ApplicationContext appContext,
                                final String methodName
                                Object[] params
        throws Throwable{
        try{
            Method method = (Method).get(methodName);
            if (method == null){
                method = appContext.getClass()
                    .getMethod(methodName, (Class[]).get(methodName));
                .put(methodNamemethod);
            }
            
            return executeMethod(method,appContext,params);
        } catch (Exception ex){
            handleException(exmethodName);
            return null;
        } finally {
            params = null;
        }
    }
    
    
Use reflection to invoke the requested method. Cache the method object to speed up the process

Parameters:
methodName The method to invoke.
clazz The class where the method is.
params The arguments passed to the called method.
    
    private Object doPrivileged(final String methodName
                                final Class[] clazz,
                                Object[] params){
        try{
            Method method = .getClass()
                    .getMethod(methodName, (Class[])clazz);
            return executeMethod(method,,params);
        } catch (Exception ex){
            try{
                handleException(exmethodName);
            }catch (Throwable t){
                throw new RuntimeException(t.getMessage());
            }
            return null;
        } finally {
            params = null;
        }
    }
    
    
    
Executes the method of the specified ApplicationContext

Parameters:
method The method object to be invoked.
context The AppliationContext object on which the method will be invoked
params The arguments passed to the called method.
    private Object executeMethod(final Method method
                                 final ApplicationContext context,
                                 final Object[] params
            throws PrivilegedActionException
                   IllegalAccessException,
                   InvocationTargetException {
                                     
        if (SecurityUtil.isPackageProtectionEnabled()){
           return AccessController.doPrivileged(new PrivilegedExceptionAction(){
                public Object run() throws IllegalAccessExceptionInvocationTargetException{
                    return method.invoke(context,  params);
                }
            });
        } else {
            return method.invoke(contextparams);
        }        
    }

    
    
Throw the real exception.

Parameters:
ex The current exception
    private void handleException(Exception exString methodName)
	    throws Throwable {
        Throwable realException;
        
        if (ex instanceof PrivilegedActionException) {
            ex = ((PrivilegedActionExceptionex).getException();
        }
        
        if (ex instanceof InvocationTargetException) {
            realException =
                ((InvocationTargetExceptionex).getTargetException();
        } else {
            realException = ex;
        }   
        
        throw realException;
    }
New to GrepCode? Check out our FAQ X