You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by dm...@apache.org on 2015/09/23 17:10:51 UTC

ignite git commit: ignite-1272: try to load class using default class loader when the class hasn't been registered in a cache

Repository: ignite
Updated Branches:
  refs/heads/ignite-1272 53980808b -> 9166dfb2b


ignite-1272: try to load class using default class loader when the class hasn't been registered in a cache


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/9166dfb2
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/9166dfb2
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/9166dfb2

Branch: refs/heads/ignite-1272
Commit: 9166dfb2b2780a0efc252a401f9ce0b93181f1a4
Parents: 5398080
Author: Denis Magda <dm...@gridgain.com>
Authored: Wed Sep 23 18:10:40 2015 +0300
Committer: Denis Magda <dm...@gridgain.com>
Committed: Wed Sep 23 18:10:40 2015 +0300

----------------------------------------------------------------------
 .../internal/portable/PortableContext.java      | 53 ++++++++++++--------
 1 file changed, 33 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/9166dfb2/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
index c7ad2d4..d5a3c5d 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
@@ -82,6 +82,9 @@ public class PortableContext implements Externalizable {
     private static final long serialVersionUID = 0L;
 
     /** */
+    private static final ClassLoader dfltLdr = U.gridClassLoader();
+
+    /** */
     static final PortableIdMapper DFLT_ID_MAPPER = new IdMapperWrapper(null);
 
     /** */
@@ -463,14 +466,14 @@ public class PortableContext implements Externalizable {
             return desc;
 
         if (ldr == null)
-            ldr = IgniteUtils.gridClassLoader();
+            ldr = dfltLdr;
 
         if (userType) {
             desc = userTypesMap(ldr).get(typeId);
 
             // If the type hasn't been loaded by default class loader then we mustn't return the descriptor from here
             // giving a chance to a custom class loader to reload type's class.
-            if (desc != null && ldr.equals(IgniteUtils.gridClassLoader()))
+            if (desc != null && ldr.equals(dfltLdr))
                 return desc;
         }
 
@@ -482,28 +485,15 @@ public class PortableContext implements Externalizable {
             desc = descByCls.get(cls);
         }
         catch (ClassNotFoundException e) {
-            if (userType && !ldr.equals(IgniteUtils.gridClassLoader())) {
-                // Type name can be not registered in a system cache. Try to get from local map.
-                desc = userTypesMap(ldr).get(typeId);
-
-                if (desc != null)
-                    return desc;
-
-                // Class might have been loaded by default class loader.
-                desc = userTypesMap(IgniteUtils.gridClassLoader()).get(typeId);
-
-                if (desc != null) {
-                    U.log(null, "Unable to load type's class with required class loader. Using type's class " +
-                        "loaded by system class loader [typeId=" + typeId + ", ldr=" + ldr + ", cls=" +
-                        desc.describedClass().getName());
-
-                    return desc;
-                }
-            }
+            if (userType && !ldr.equals(dfltLdr) && (desc = descriptorLoadingFailover(typeId, ldr)) != null)
+                return desc;
 
             throw new PortableInvalidClassException(e);
         }
         catch (IgniteCheckedException e) {
+            if (userType && !ldr.equals(dfltLdr) && (desc = descriptorLoadingFailover(typeId, ldr)) != null)
+                return desc;
+
             throw new PortableException("Failed resolve class for ID: " + typeId, e);
         }
 
@@ -517,6 +507,29 @@ public class PortableContext implements Externalizable {
     }
 
     /**
+     * The method must be used in case when it wasn't possible to load user type's class using custom class loader.
+     *
+     * There are several reasons why this may happen. First, type's name can be not registered in a system cache.
+     * Second, class might have been predefined explicitly and loaded by default class loader.
+     *
+     * @param typeId Type ID.
+     * @param ldr Class loader that failed to load type's class.
+     * @return Type descriptor on success, {@code null} on failure.
+     */
+    private PortableClassDescriptor descriptorLoadingFailover(int typeId, ClassLoader ldr) {
+        assert !ldr.equals(dfltLdr);
+
+        // Type name can be not registered in a system cache. Try to get from local map.
+        PortableClassDescriptor desc = userTypesMap(ldr).get(typeId);
+
+        if (desc == null)
+            // Class might have been loaded by default class loader.
+            desc = descriptorForTypeId(true, typeId, dfltLdr);
+
+        return desc;
+    }
+
+    /**
      * Creates and registers {@link PortableClassDescriptor} for the given {@code class}.
      *
      * @param cls Class.