jfarcand    2004/09/30 08:26:21

  Modified:    catalina/src/share/org/apache/catalina/core
                        ApplicationContextFacade.java
                        ApplicationFilterChain.java StandardWrapper.java
  Log:
  Avoid creating objects on every call since it clearly produces a memory leak when 
the SecurityManager is turned on. All Tcks passed except there is two jsp regressions:
  
  com/sun/ts/tests/jsp/spec/i18n/URLClient.java#i18nEncodingMismatchTest: Failed. Test 
case throws exception: [BaseUrlClient] null failed!  Check output for cause of failure.
  Test finished: 
com/sun/ts/tests/jsp/spec/configuration/encoding/URLClient.java#jspConfigurationDifferentEncodingTest:
 Failed. Test case throws exception: [BaseUrlClient] null failed!  Check output for 
cause of failure.
  
  The same exceptions occurs when the SecurityManager is turned off.
  
  Revision  Changes    Path
  1.11      +55 -28    
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationContextFacade.java
  
  Index: ApplicationContextFacade.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationContextFacade.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ApplicationContextFacade.java     26 May 2004 15:36:14 -0000      1.10
  +++ ApplicationContextFacade.java     30 Sep 2004 15:26:20 -0000      1.11
  @@ -52,8 +52,13 @@
       /**
        * Cache Class object used for reflection.
        */
  -    private HashMap classCache;
  +    private static HashMap classCache = new HashMap();
       
  +    static {
  +        if (System.getSecurityManager() != null) {
  +            initClassCache();
  +        }
  +    }
       
       /**
        * Cache method object.
  @@ -64,7 +69,13 @@
       private static org.apache.commons.logging.Log sysLog=
           org.apache.commons.logging.LogFactory.getLog( 
ApplicationContextFacade.class );
   
  -        
  +    
  +    /**
  +     * Object repository used when the SecurityManager is turned on and
  +     * Filter.doFilter is invoked.
  +     */    
  +    private Object[] objectArg = new Object[1];
  +    private Object[] objectArgs = new Object[2];        
       // ----------------------------------------------------------- Constructors
   
   
  @@ -78,13 +89,12 @@
           super();
           this.context = context;
           
  -        classCache = new HashMap();
           objectCache = new HashMap();
  -        initClassCache();
  +
       }
       
       
  -    private void initClassCache(){
  +    private static void initClassCache(){
           Class[] clazz = new Class[]{String.class};
           classCache.put("getContext", clazz);
           classCache.put("getMimeType", clazz);
  @@ -119,8 +129,8 @@
       public ServletContext getContext(String uripath) {
           ServletContext theContext = null;
           if (System.getSecurityManager() != null) {
  -            theContext = (ServletContext)
  -                doPrivileged("getContext", new Object[]{uripath});
  +            objectArg[0] = uripath; 
  +            theContext = (ServletContext)doPrivileged("getContext", objectArg);
           } else {
               theContext = context.getContext(uripath);
           }
  @@ -144,7 +154,8 @@
   
       public String getMimeType(String file) {
           if (System.getSecurityManager() != null) {
  -            return (String)doPrivileged("getMimeType", new Object[]{file});
  +            objectArg[0] = file;
  +            return (String)doPrivileged("getMimeType", objectArg);
           } else {
               return context.getMimeType(file);
           }
  @@ -153,7 +164,8 @@
   
       public Set getResourcePaths(String path) {
           if (System.getSecurityManager() != null){
  -            return (Set)doPrivileged("getResourcePaths", new Object[]{path});
  +            objectArg[0] = path;
  +            return (Set)doPrivileged("getResourcePaths", objectArg);
           } else {
               return context.getResourcePaths(path);
           }
  @@ -164,8 +176,9 @@
           throws MalformedURLException {
           if (System.getSecurityManager() != null) {
               try {
  +                objectArg[0] = path;
                   return (URL) invokeMethod(context, "getResource", 
  -                                          new Object[]{path});
  +                                          objectArg);
               } catch(Throwable t) {
                   if (t instanceof MalformedURLException){
                       throw (MalformedURLException)t;
  @@ -180,8 +193,9 @@
   
       public InputStream getResourceAsStream(String path) {
           if (System.getSecurityManager() != null) {
  +            objectArg[0] = path;
               return (InputStream) doPrivileged("getResourceAsStream", 
  -                                              new Object[]{path});
  +                                              objectArg);
           } else {
               return context.getResourceAsStream(path);
           }
  @@ -190,8 +204,9 @@
   
       public RequestDispatcher getRequestDispatcher(final String path) {
           if (System.getSecurityManager() != null) {
  +            objectArg[0] = path;
               return (RequestDispatcher) doPrivileged("getRequestDispatcher", 
  -                                                    new Object[]{path});
  +                                                    objectArg);
           } else {
               return context.getRequestDispatcher(path);
           }
  @@ -200,8 +215,9 @@
   
       public RequestDispatcher getNamedDispatcher(String name) {
           if (System.getSecurityManager() != null) {
  +            objectArg[0] = name;
               return (RequestDispatcher) doPrivileged("getNamedDispatcher", 
  -                                                    new Object[]{name});
  +                                                    objectArg);
           } else {
               return context.getNamedDispatcher(name);
           }
  @@ -212,8 +228,9 @@
           throws ServletException {
           if (System.getSecurityManager() != null) {
               try {
  +                objectArg[0] = name;
                   return (Servlet) invokeMethod(context, "getServlet", 
  -                                              new Object[]{name});
  +                                              objectArg);
               } catch (Throwable t) {
                   if (t instanceof ServletException) {
                       throw (ServletException) t;
  @@ -246,7 +263,8 @@
   
       public void log(String msg) {
           if (System.getSecurityManager() != null) {
  -            doPrivileged("log", new Object[]{msg} );
  +            objectArg[0] = msg;
  +            doPrivileged("log", objectArg );
           } else {
               context.log(msg);
           }
  @@ -265,8 +283,10 @@
   
       public void log(String message, Throwable throwable) {
           if (System.getSecurityManager() != null) {
  +            objectArgs[0] = message;
  +            objectArgs[1] = throwable;
               doPrivileged("log", new Class[]{String.class, Throwable.class}, 
  -                         new Object[]{message, throwable});
  +                         objectArgs);
           } else {
               context.log(message, throwable);
           }
  @@ -275,7 +295,8 @@
   
       public String getRealPath(String path) {
           if (System.getSecurityManager() != null) {
  -            return (String) doPrivileged("getRealPath", new Object[]{path});
  +            objectArg[0] = path;
  +            return (String) doPrivileged("getRealPath", objectArg);
           } else {
               return context.getRealPath(path);
           }
  @@ -293,8 +314,9 @@
   
       public String getInitParameter(String name) {
           if (System.getSecurityManager() != null) {
  +            objectArg[0] = name;
               return (String) doPrivileged("getInitParameter", 
  -                                         new Object[]{name});
  +                                         objectArg);
           } else {
               return context.getInitParameter(name);
           }
  @@ -312,7 +334,8 @@
   
       public Object getAttribute(String name) {
           if (System.getSecurityManager() != null) {
  -            return doPrivileged("getAttribute", new Object[]{name});
  +            objectArg[0] = name;
  +            return doPrivileged("getAttribute", objectArg);
           } else {
               return context.getAttribute(name);
           }
  @@ -320,7 +343,7 @@
   
   
       public Enumeration getAttributeNames() {
  -        if (System.getSecurityManager() != null) {
  +        if (System.getSecurityManager() != null) {           
               return (Enumeration) doPrivileged("getAttributeNames", null);
           } else {
               return context.getAttributeNames();
  @@ -330,7 +353,9 @@
   
       public void setAttribute(String name, Object object) {
           if (System.getSecurityManager() != null) {
  -            doPrivileged("setAttribute", new Object[]{name,object});
  +            objectArgs[0] = name;
  +            objectArgs[1] = object;
  +            doPrivileged("setAttribute", objectArgs);
           } else {
               context.setAttribute(name, object);
           }
  @@ -339,7 +364,8 @@
   
       public void removeAttribute(String name) {
           if (System.getSecurityManager() != null) {
  -            doPrivileged("removeAttribute", new Object[]{name});
  +            objectArg[0] = name;
  +            doPrivileged("removeAttribute", objectArg);
           } else {
               context.removeAttribute(name);
           }
  @@ -369,7 +395,7 @@
           try{
               return invokeMethod(appContext, methodName, params );
           } catch (Throwable t){
  -            throw new RuntimeException(t.getMessage());
  +            throw new RuntimeException(t);
           }
   
       }
  @@ -386,7 +412,7 @@
           try{
               return invokeMethod(context, methodName, params);
           }catch(Throwable t){
  -            throw new RuntimeException(t.getMessage());
  +            throw new RuntimeException(t);
           }
       }
   
  @@ -422,8 +448,9 @@
       /**
        * Use reflection to invoke the requested method. Cache the method object 
        * to speed up the process
  -     * @param methodName The method to invoke.
  -     * @param clazz The class where the method is.
  +     * @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.
        */    
       private Object doPrivileged(final String methodName, 
  @@ -438,7 +465,7 @@
               try{
                   handleException(ex, methodName);
               }catch (Throwable t){
  -                throw new RuntimeException(t.getMessage());
  +                throw new RuntimeException(t);
               }
               return null;
           }
  
  
  
  1.11      +42 -12    
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationFilterChain.java
  
  Index: ApplicationFilterChain.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/ApplicationFilterChain.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ApplicationFilterChain.java       26 May 2004 15:36:20 -0000      1.10
  +++ ApplicationFilterChain.java       30 Sep 2004 15:26:20 -0000      1.11
  @@ -111,6 +111,35 @@
        */
       private InstanceSupport support = null;
   
  +    
  +    /**
  +     * Static class array used when the SecurityManager is turned on and 
  +     * <code>doFilter</code is invoked.
  +     */
  +    private static Class[] classType = new Class[]{ServletRequest.class, 
  +                                                   ServletResponse.class,
  +                                                   FilterChain.class};
  +                                                   
  +    /**
  +     * Static class array used when the SecurityManager is turned on and 
  +     * <code>service</code is invoked.
  +     */                                                 
  +    private static Class[] classTypeUsedInService = new Class[]{
  +                                                         ServletRequest.class,
  +                                                         ServletResponse.class};
  +                                                   
  +    /**
  +     * Object repository used when the SecurityManager is turned on and
  +     * Filter.doFilter is invoked.
  +     */
  +    private Object[] filterType = new Object[3];
  +    
  +
  +    /**
  +     * Object repository used when the SecurityManager is turned on and
  +     * Filter.doFilter is invoked.
  +     */
  +    private Object[] serviceType = new Object[2];
   
       // ---------------------------------------------------- FilterChain Methods
   
  @@ -176,12 +205,12 @@
                       final ServletResponse res = response;
                       Principal principal = 
                           ((HttpServletRequest) req).getUserPrincipal();
  -                    Class[] classType = new Class[]{ServletRequest.class, 
  -                                                    ServletResponse.class,
  -                                                    FilterChain.class};
  -                    Object[] args = new Object[]{req, res, this};
  +                    
  +                    filterType[0] = req;
  +                    filterType[1] = res;
  +                    filterType[2] = this;
                       SecurityUtil.doAsPrivilege
  -                        ("doFilter", filter, classType, args);
  +                        ("doFilter", filter, classType, filterType);
                   } else {  
                       filter.doFilter(request, response, this);
                   }
  @@ -225,14 +254,15 @@
                       final ServletResponse res = response;
                       Principal principal = 
                           ((HttpServletRequest) req).getUserPrincipal();
  -                    Class[] classType = new Class[]{ServletRequest.class, 
  -                                                     ServletResponse.class};
  -                    Object[] args = new Object[]{req, res};
  +                    
  +                    serviceType[0] = req;
  +                    serviceType[1] = res;
  +                    
                       SecurityUtil.doAsPrivilege("service",
                                                  servlet,
  -                                               classType, 
  -                                               args,
  -                                               principal);                          
                         
  +                                               classTypeUsedInService, 
  +                                               serviceType,
  +                                               principal);                          
                       
                   } else {  
                       servlet.service((HttpServletRequest) request,
                                       (HttpServletResponse) response);
  
  
  
  1.46      +37 -9     
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/StandardWrapper.java
  
  Index: StandardWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/core/StandardWrapper.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- StandardWrapper.java      29 Jul 2004 15:33:38 -0000      1.45
  +++ StandardWrapper.java      30 Sep 2004 15:26:20 -0000      1.46
  @@ -238,7 +238,36 @@
       private StandardWrapperValve swValve;
       private long loadTime=0;
       private int classLoadTime=0;
  +    
  +    
  +   /**
  +     * Static class array used when the SecurityManager is turned on and 
  +     * <code>Servlet.init</code> is invoked.
  +     */
  +    private static Class[] classType = new Class[]{ServletConfig.class};
  +    
  +    
  +    /**
  +     * Object repository used when the SecurityManager is turned on and
  +     * <code>Servlet.init</code> is invoked.
  +     */
  +    private Object[] initType = new Object[1];
  +    
  +    
  +    /**
  +     * Static class array used when the SecurityManager is turned on and 
  +     * <code>Servlet.service</code>  is invoked.
  +     */                                                 
  +    private static Class[] classTypeUsedInService = new Class[]{
  +                                                         ServletRequest.class,
  +                                                         ServletResponse.class};
  +    
   
  +    /**
  +     * Object repository used when the SecurityManager is turned on and
  +     * <code>Servlet.service</code> is invoked.
  +     */
  +    private Object[] serviceType = new Object[2];
       // ------------------------------------------------------------- Properties
   
   
  @@ -993,12 +1022,12 @@
                                                     servlet);
   
                   if( System.getSecurityManager() != null) {
  -                    Class[] classType = new Class[]{ServletConfig.class};
  -                    Object[] args = new Object[]{((ServletConfig)facade)};
  +                    initType[0] = facade;
                       SecurityUtil.doAsPrivilege("init",
                                                  servlet,
                                                  classType,
  -                                               args);
  +                                               initType);
  +
                   } else {
                       servlet.init(facade);
                   }
  @@ -1012,13 +1041,12 @@
                       DummyResponse res = new DummyResponse();
   
                       if( System.getSecurityManager() != null) {
  -                        Class[] classType = new Class[]{ServletRequest.class,
  -                                                        ServletResponse.class};
  -                        Object[] args = new Object[]{req, res};
  +                        serviceType[0] = req;
  +                        serviceType[1] = res;                
                           SecurityUtil.doAsPrivilege("service",
                                                      servlet,
  -                                                   classType,
  -                                                   args);
  +                                                   classTypeUsedInService,
  +                                                   serviceType);
                       } else {
                           servlet.service(req, res);
                       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to