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 2005/11/12 00:55:05 UTC

svn commit: r332680 - in /webservices/axis/trunk/java/src/org/apache/axis: description/TypeDesc.java i18n/resource.properties

Author: dims
Date: Fri Nov 11 15:54:59 2005
New Revision: 332680

URL: http://svn.apache.org/viewcvs?rev=332680&view=rev
Log:
Fix for AXIS-2232 - Mappings in TypeDesc can't be GC'ed
from Kevan Miller


Modified:
    webservices/axis/trunk/java/src/org/apache/axis/description/TypeDesc.java
    webservices/axis/trunk/java/src/org/apache/axis/i18n/resource.properties

Modified: webservices/axis/trunk/java/src/org/apache/axis/description/TypeDesc.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/src/org/apache/axis/description/TypeDesc.java?rev=332680&r1=332679&r2=332680&view=diff
==============================================================================
--- webservices/axis/trunk/java/src/org/apache/axis/description/TypeDesc.java (original)
+++ webservices/axis/trunk/java/src/org/apache/axis/description/TypeDesc.java Fri Nov 11 15:54:59 2005
@@ -16,14 +16,19 @@
 
 package org.apache.axis.description;
 
+import org.apache.axis.components.logger.LogFactory;
 import org.apache.axis.utils.BeanPropertyDescriptor;
 import org.apache.axis.utils.BeanUtils;
 import org.apache.axis.utils.Messages;
 import org.apache.axis.utils.cache.MethodCache;
 
+import org.apache.commons.logging.Log;
+
 import javax.xml.namespace.QName;
 import java.io.Serializable;
+import java.lang.ref.WeakReference;
 import java.lang.reflect.Method;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
@@ -40,8 +45,8 @@
     public static final Class [] noClasses = new Class [] {};
     public static final Object[] noObjects = new Object[] {};
     
-    /** A map of {classloader-> map of {class -> TypeDesc}} */
-    private static Map classMaps = new WeakHashMap();
+    /** A map of {class -> TypeDesc}} */
+    private static Map classMap = Collections.synchronizedMap(new WeakHashMap());
 
     /** Have we already introspected for the special "any" property desc? */
     private boolean lookedForAny = false;
@@ -53,6 +58,9 @@
     /** My superclass TypeDesc */
     private TypeDesc parentDesc = null;
 
+    protected static Log log =
+        LogFactory.getLog(TypeDesc.class.getName());
+
     /**
      * Creates a new <code>TypeDesc</code> instance.  The type desc can search
      * the metadata of its type'sparent classes.
@@ -71,7 +79,7 @@
      * its type's parent classes.
      */
     public TypeDesc(Class javaClass, boolean canSearchParents) {
-        this.javaClass = javaClass;
+        this.javaClassRef = new WeakReference(javaClass);
         this.canSearchParents = canSearchParents;
         Class cls = javaClass.getSuperclass();
         if (cls != null && !cls.getName().startsWith("java.")) {
@@ -88,23 +96,9 @@
      */ 
     public static void registerTypeDescForClass(Class cls, TypeDesc td)
     {
-        Map classMap = getTypeDescMap(cls.getClassLoader());
         classMap.put(cls, td);
     }
 
-    private static Map getTypeDescMap(ClassLoader classLoader)
-    {
-        Map classMap;
-        synchronized (classMaps) {
-            classMap = (Map) classMaps.get(classLoader);
-            if (classMap == null){
-                classMap = new Hashtable();
-                classMaps.put(classLoader, classMap);
-            }
-        }
-        return classMap;
-    }
-
     /**
      * Static function for centralizing access to type metadata for a
      * given class.  
@@ -117,8 +111,6 @@
      */
     public static TypeDesc getTypeDescForClass(Class cls)
     {
-        Map classMap = getTypeDescMap(cls.getClassLoader());
-
         // First see if we have one explicitly registered
         // or cached from previous lookup
         TypeDesc result = (TypeDesc)classMap.get(cls);
@@ -142,8 +134,8 @@
         return result;
     }
 
-    /** The Java class for this type */
-    private Class javaClass = null;
+    /** WeakReference to the Java class for this type */
+    private WeakReference javaClassRef = null;
 
     /** The XML type QName for this type */
     private QName xmlType = null;
@@ -453,6 +445,18 @@
         if (propertyDescriptors != null)
             return;
 
+        // javaClassRef is a WeakReference. So, our javaClass may have been GC'ed.
+        // Protect against this case...
+        Class javaClass = (Class)javaClassRef.get();
+        if (javaClass == null) {
+            // could throw a RuntimeException, but instead log error and dummy up descriptors
+            log.error(Messages.getMessage("classGCed"));
+            propertyDescriptors = new BeanPropertyDescriptor[0];
+            anyDesc = null;
+            lookedForAny = true;
+            return;
+        }
+
         propertyDescriptors = BeanUtils.getPd(javaClass, this);
         if (!lookedForAny) {
             anyDesc = BeanUtils.getAnyContentPD(javaClass);
@@ -462,7 +466,17 @@
 
     public BeanPropertyDescriptor getAnyContentDescriptor() {
         if (!lookedForAny) {
-            anyDesc = BeanUtils.getAnyContentPD(javaClass);
+            // javaClassRef is a WeakReference. So, our javaClass may have been GC'ed.
+            // Protect against this case...
+            Class javaClass = (Class)javaClassRef.get();
+            if (javaClass != null) {
+                anyDesc = BeanUtils.getAnyContentPD(javaClass);
+            }
+            else {
+                log.error(Messages.getMessage("classGCed"));
+                anyDesc = null;
+            }
+
             lookedForAny = true;
         }
         return anyDesc;

Modified: webservices/axis/trunk/java/src/org/apache/axis/i18n/resource.properties
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/src/org/apache/axis/i18n/resource.properties?rev=332680&r1=332679&r2=332680&view=diff
==============================================================================
--- webservices/axis/trunk/java/src/org/apache/axis/i18n/resource.properties (original)
+++ webservices/axis/trunk/java/src/org/apache/axis/i18n/resource.properties Fri Nov 11 15:54:59 2005
@@ -125,6 +125,10 @@
 
 changePwd00=Changing admin password
 childPresent=MessageElement.setObjectValue called when a child element is present
+
+# Note: in classGCed, do not translate "TypeDesc" and "Class"
+classGCed=TypeDesc referenced a Class object which has been garbage collected
+
 compiling00=Compiling:  {0}
 ctor00=Constructor
 convert00=Trying to convert {0} to {1}