You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2003/07/14 00:34:57 UTC

cvs commit: xml-axis/java/src/org/apache/axis/description TypeDesc.java

dims        2003/07/13 15:34:57

  Modified:    java/src/org/apache/axis/encoding/ser BaseFactory.java
               java/src/org/apache/axis/description TypeDesc.java
  Added:       java/src/org/apache/axis/utils/cache MethodCache.java
  Log:
  Fix for Bug 19899 - Performance bottleneck in TypeDesc.getTypeDescForClass()
  
  Notes:
  - Modified version of patch from Sebastian.Dietrich@gmx.at.
  
  Revision  Changes    Path
  1.2       +2 -35     xml-axis/java/src/org/apache/axis/encoding/ser/BaseFactory.java
  
  Index: BaseFactory.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/ser/BaseFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BaseFactory.java	1 May 2003 17:05:51 -0000	1.1
  +++ BaseFactory.java	13 Jul 2003 22:34:57 -0000	1.2
  @@ -55,6 +55,7 @@
   package org.apache.axis.encoding.ser;
   
   import org.apache.axis.utils.ClassUtils;
  +import org.apache.axis.utils.cache.MethodCache;
   
   import javax.xml.namespace.QName;
   import java.util.Map;
  @@ -69,48 +70,14 @@
    */
   public abstract class BaseFactory {
       /**
  -     * Cache for Methods
  -     */
  -    transient protected ThreadLocal methodCache = new ThreadLocal();
  -
  -    /**
  -     * Returns the per thread hashmap (for method caching)  
  -     */
  -    private Map getMethodCache() {
  -        Map map = (Map) methodCache.get();
  -        if (map == null) {
  -            map = new HashMap();
  -            methodCache.set(map);
  -        }
  -        return map;
  -    }
  -
  -    /**
        * Returns the the specified method - if any.
        */
       protected Method getMethod(Class clazz, String methodName) {
  -        String className = clazz.getName();
  -        Map cache = getMethodCache();
           Method method = null;
  -        
  -        // Check the cache first.
  -        if (cache.containsKey(className)) {
  -            method = (Method) cache.get(clazz);
  -            return method;
  -        }
           try {
  -            method = clazz.getMethod(methodName, new Class[]{String.class, Class.class, QName.class});
  +            method = MethodCache.getInstance().getMethod(clazz, methodName, new Class[]{String.class, Class.class, QName.class});
           } catch (NoSuchMethodException e) {
           }
  -        if (method == null) {
  -            try {
  -                Class helper = ClassUtils.forName(clazz.getName() + "_Helper");
  -                method = helper.getMethod(methodName, new Class[]{String.class, Class.class, QName.class});
  -            } catch (NoSuchMethodException e) {
  -            } catch (ClassNotFoundException e) {
  -            }
  -        }
  -        cache.put(className, method);
           return method;
       }
   }
  
  
  
  1.1                  xml-axis/java/src/org/apache/axis/utils/cache/MethodCache.java
  
  Index: MethodCache.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Axis" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.axis.utils.cache;
  
  import org.apache.axis.utils.ClassUtils;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.HashMap;
  import java.lang.reflect.Method;
  
  /**
   * A cache for methods. 
   * Used to get methods by their signature and stores them in a local
   * cache for performance reasons.
   * This class is a singleton - so use getInstance to get an instance of it.
   * 
   * @author Davanum Srinivas <di...@yahoo.com>
   * @author Sebastian Dietrich <se...@anecon.com>
   */
  public class MethodCache {
  	/**
  	 * The only instance of this class
  	 */
  	transient private static MethodCache instance;
  	
      /**
       * Cache for Methods
       * In fact this is a map (with classes as keys) of a map (with method-names as keys)
       */
      transient private static ThreadLocal cache;
  
  	/**
  	 * The <i>private</i> constructor for this class.
  	 * Use getInstance to get an instance (the only one).
  	 */
  	private MethodCache() {
  		cache = new ThreadLocal();
  	}
  	
  	/**
  	 * Gets the only instance of this class
  	 * @return the only instance of this class
  	 */
  	public static MethodCache getInstance() {
  		if (instance == null) {
  			instance = new MethodCache();
  		}
  		return instance;				
  	}
  	
      /**
       * Returns the per thread hashmap (for method caching)  
       */
      private Map getMethodCache() {
          Map map = (Map) cache.get();
          if (map == null) {
              map = new HashMap();
  			cache.set(map);
          }
          return map;
      }
  
  	/**
  	 * Returns the specified method - if any.
  	 * 
  	 * @param clazz the class to get the method from
  	 * @param methodName the name of the method
  	 * @param parameterTypes the parameters of the method
  	 * @return the found method
  	 * 
  	 * @throws NoSuchMethodException if the method can't be found
  	 */
      public Method getMethod(Class clazz, String methodName, Class[] parameterTypes) throws NoSuchMethodException {
          String className = clazz.getName();
          Map cache = getMethodCache();
          Method method = null;
  		Collection methods = null;
          
          // Check the cache first.
          if (cache.containsKey(className)) {
              methods = (Collection) cache.get(clazz);
              if (methods != null) {
                  Iterator it = methods.iterator();
                  while (it.hasNext()) {
                      method = (Method) it.next();
                      if (method.getName().equals(methodName) && method.getParameterTypes().equals(parameterTypes)) {
                          return method;
                      }
                  }
              }
          } 
          
          try {
              method = clazz.getMethod(methodName, parameterTypes);
          } catch (NoSuchMethodException e1) {
              try {
                  Class helper = ClassUtils.forName(clazz.getName() + "_Helper");
                  method = helper.getMethod(methodName, parameterTypes);
              } catch (ClassNotFoundException e2) {
              	// should never happen --> assert false;
              }
          }
  
  		if (methods == null) {
  			methods = new ArrayList();
  			cache.put(className, methods);
  		}
  		
          methods.add(method);
          return method;
      }
  
  }
  
  
  
  1.33      +3 -15     xml-axis/java/src/org/apache/axis/description/TypeDesc.java
  
  Index: TypeDesc.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/description/TypeDesc.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- TypeDesc.java	22 Apr 2003 19:34:22 -0000	1.32
  +++ TypeDesc.java	13 Jul 2003 22:34:57 -0000	1.33
  @@ -59,6 +59,7 @@
   import org.apache.axis.utils.BeanUtils;
   import org.apache.axis.utils.ClassUtils;
   import org.apache.axis.utils.Messages;
  +import org.apache.axis.utils.cache.MethodCache;
   
   import javax.xml.namespace.QName;
   import java.lang.reflect.Method;
  @@ -119,22 +120,9 @@
           }
           
           try {
  -            Method getTypeDesc = null;
  -            try {
  -                getTypeDesc =
  -                    cls.getMethod("getTypeDesc", noClasses);
  -            } catch (NoSuchMethodException e) {}
  -            if (getTypeDesc == null) {
  -                // Look for a Helper Class
  -                Class helper = ClassUtils.forName(cls.getName() + "_Helper");
  -                try {
  -                    getTypeDesc =
  -                        helper.getMethod("getTypeDesc", noClasses);
  -                } catch (NoSuchMethodException e) {}
  -            }
  +            Method getTypeDesc = MethodCache.getInstance().getMethod(cls, "getTypeDesc", noClasses);
               if (getTypeDesc != null) {
  -                return (TypeDesc)getTypeDesc.invoke(null,
  -                                                    noObjects);
  +                return (TypeDesc)getTypeDesc.invoke(null, noObjects);
               }
           } catch (Exception e) {
           }