fix #10075 fix deserialization by overriding PreferredLoaderObjectInputStream from...
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / net / sf / ehcache / util / PreferredLoaderObjectInputStream.java
diff --git a/eu.etaxonomy.taxeditor.cdmlib/src/main/java/net/sf/ehcache/util/PreferredLoaderObjectInputStream.java b/eu.etaxonomy.taxeditor.cdmlib/src/main/java/net/sf/ehcache/util/PreferredLoaderObjectInputStream.java
new file mode 100644 (file)
index 0000000..4263627
--- /dev/null
@@ -0,0 +1,87 @@
+/**
+ *  Copyright Terracotta, Inc.
+ *
+ *  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 net.sf.ehcache.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectStreamClass;
+import java.util.HashMap;
+
+/**
+ * Note by AM (2022-06-15): this is an exact copy of the same class in ehcache (v2.x)
+ *     but with correct handling for primitive types.
+ *     This class should be removed when upgrading to ehcache 3.x as it seems not to exist in
+ *     this version anymore.
+ *
+ *     For further information see #10075 and #10077
+ *
+ * =========================================
+ * Original javadoc:
+ *
+ * ObjectInputStream that uses a supplied classloader when resolving classes
+ *
+ * @author teck
+ */
+public class PreferredLoaderObjectInputStream extends ObjectInputStream {
+
+    private final ClassLoader loader;
+
+    /**
+     * Constructor
+     *
+     * @param in
+     * @throws IOException
+     */
+    public PreferredLoaderObjectInputStream(InputStream in, ClassLoader loader) throws IOException {
+        super(in);
+        this.loader = loader;
+    }
+
+// ************** REPLACED METHOD ********************************/
+
+    @Override
+    protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
+
+        try {
+            return Class.forName(desc.getName(), false, loader);
+        } catch (ClassNotFoundException ex) {
+            Class<?> cl = primClasses.get(desc.getName());
+            if (cl != null) {
+                return cl;
+            } else {
+                throw ex;
+            }
+        }
+    }
+    //copied from
+    /** table mapping primitive type names to corresponding class objects */
+    private static final HashMap<String, Class<?>> primClasses
+        = new HashMap<>(8, 1.0F);
+    static {
+        primClasses.put("boolean", boolean.class);
+        primClasses.put("byte", byte.class);
+        primClasses.put("char", char.class);
+        primClasses.put("short", short.class);
+        primClasses.put("int", int.class);
+        primClasses.put("long", long.class);
+        primClasses.put("float", float.class);
+        primClasses.put("double", double.class);
+        primClasses.put("void", void.class);
+    }
+
+}