ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akuznet...@apache.org
Subject [03/50] [abbrv] incubator-ignite git commit: IGNITE-141 - Marshallers refactoring
Date Wed, 11 Mar 2015 06:21:41 GMT
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2866f02f/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedClassDescriptor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedClassDescriptor.java
b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedClassDescriptor.java
index e7d8f1e..74a491f 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedClassDescriptor.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedClassDescriptor.java
@@ -19,6 +19,7 @@ package org.apache.ignite.marshaller.optimized;
 
 import org.apache.ignite.internal.util.*;
 import org.apache.ignite.internal.util.typedef.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
 import org.apache.ignite.lang.*;
 import org.apache.ignite.marshaller.*;
 import sun.misc.*;
@@ -166,9 +167,6 @@ class OptimizedClassDescriptor {
     /** {@code True} if descriptor is for {@link Class}. */
     private boolean isCls;
 
-    /** Array component type. */
-    private Class<?> arrCompType;
-
     /** Enumeration values. */
     private Object[] enumVals;
 
@@ -293,11 +291,8 @@ class OptimizedClassDescriptor {
                 type = TYPE_CHAR_ARR;
             else if (cls == boolean[].class)
                 type = TYPE_BOOLEAN_ARR;
-            else if (cls.isArray()) {
+            else if (cls.isArray())
                 type = TYPE_OBJ_ARR;
-
-                arrCompType = cls.getComponentType();
-            }
             else if (cls == String.class)
                 type = TYPE_STR;
             else if (cls.isEnum()) {
@@ -734,6 +729,7 @@ class OptimizedClassDescriptor {
                 break;
 
             case TYPE_OBJ_ARR:
+                out.writeUTF(obj.getClass().getComponentType().getName());
                 out.writeArray((Object[])obj);
 
                 break;
@@ -881,7 +877,7 @@ class OptimizedClassDescriptor {
                 return in.readBooleanArray();
 
             case TYPE_OBJ_ARR:
-                return in.readArray(arrCompType);
+                return in.readArray(U.forName(in.readUTF(), in.classLoader()));
 
             case TYPE_STR:
                 return in.readString();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2866f02f/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
index f726891..1c8d2f8 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
@@ -20,6 +20,7 @@ package org.apache.ignite.marshaller.optimized;
 import org.apache.ignite.internal.util.*;
 import org.apache.ignite.internal.util.typedef.*;
 import org.apache.ignite.marshaller.*;
+import org.apache.ignite.marshaller.jdk.*;
 import org.jdk8.backport.*;
 import sun.misc.*;
 
@@ -46,9 +47,15 @@ class OptimizedMarshallerUtils {
     /** Object reference. */
     static final byte OBJECT = (byte)0x72;
 
+    /** Object marshalled with JDK marshaller. */
+    static final byte JDK = (byte)0x73;
+
     /** UTF-8 character name. */
     static final Charset UTF_8 = Charset.forName("UTF-8");
 
+    /** JDK marshaller. */
+    static final JdkMarshaller JDK_MARSH = new JdkMarshaller();
+
     /** Class descriptors by class. */
     private static final ConcurrentMap<Class, OptimizedClassDescriptor> DESC_BY_CLS
= new ConcurrentHashMap8<>();
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2866f02f/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
index 50e5551..724abbf 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.marshaller.optimized;
 
+import org.apache.ignite.*;
 import org.apache.ignite.internal.util.*;
 import org.apache.ignite.internal.util.io.*;
 import org.apache.ignite.internal.util.typedef.*;
@@ -155,6 +156,19 @@ class OptimizedObjectInputStream extends ObjectInputStream {
 
                 return desc.read(this);
 
+            case JDK:
+                try {
+                    return JDK_MARSH.unmarshal(this, clsLdr);
+                }
+                catch (IgniteCheckedException e) {
+                    IOException ioEx = e.getCause(IOException.class);
+
+                    if (ioEx != null)
+                        throw ioEx;
+                    else
+                        throw new IOException("Failed to deserialize object with JDK marshaller.",
e);
+                }
+
             default:
                 SB msg = new SB("Unexpected error occurred during unmarshalling");
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2866f02f/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
index 4a79a1d..0bcefe4 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.marshaller.optimized;
 
+import org.apache.ignite.*;
 import org.apache.ignite.internal.util.*;
 import org.apache.ignite.internal.util.io.*;
 import org.apache.ignite.internal.util.typedef.*;
@@ -150,42 +151,59 @@ class OptimizedObjectOutputStream extends ObjectOutputStream {
         if (obj == null)
             writeByte(NULL);
         else {
-            OptimizedClassDescriptor desc = classDescriptor(obj.getClass(), ctx, mapper);
+            if (obj instanceof Throwable && !(obj instanceof Externalizable)) {
+                writeByte(JDK);
 
-            if (desc.excluded()) {
-                writeByte(NULL);
+                try {
+                    JDK_MARSH.marshal(obj, this);
+                }
+                catch (IgniteCheckedException e) {
+                    IOException ioEx = e.getCause(IOException.class);
 
-                return;
+                    if (ioEx != null)
+                        throw ioEx;
+                    else
+                        throw new IOException("Failed to serialize object with JDK marshaller:
" + obj, e);
+                }
             }
+            else {
+                OptimizedClassDescriptor desc = classDescriptor(
+                    obj instanceof Object[] ? Object[].class : obj.getClass(), ctx, mapper);
 
-            Object obj0 = desc.replace(obj);
+                if (desc.excluded()) {
+                    writeByte(NULL);
 
-            if (obj0 == null) {
-                writeByte(NULL);
+                    return;
+                }
 
-                return;
-            }
+                Object obj0 = desc.replace(obj);
 
-            int handle = -1;
+                if (obj0 == null) {
+                    writeByte(NULL);
 
-            if (!desc.isPrimitive() && !desc.isEnum() && !desc.isClass())
-                handle = handles.lookup(obj);
+                    return;
+                }
 
-            if (obj0 != obj) {
-                obj = obj0;
+                int handle = -1;
 
-                desc = classDescriptor(obj.getClass(), ctx, mapper);
-            }
+                if (!desc.isPrimitive() && !desc.isEnum() && !desc.isClass())
handle = handles.lookup(obj);
 
-            if (handle >= 0) {
-                writeByte(HANDLE);
-                writeInt(handle);
-            }
-            else {
-                writeByte(OBJECT);
-                writeInt(desc.typeId());
+                if (obj0 != obj) {
+                    obj = obj0;
+
+                    desc = classDescriptor(obj instanceof Object[] ? Object[].class : obj.getClass(),
ctx, mapper);
+                }
 
-                desc.write(this, obj);
+                if (handle >= 0) {
+                    writeByte(HANDLE);
+                    writeInt(handle);
+                }
+                else {
+                    writeByte(OBJECT);
+                    writeInt(desc.typeId());
+
+                    desc.write(this, obj);
+                }
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2866f02f/modules/core/src/test/java/org/apache/ignite/ClassesGenerator.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/ClassesGenerator.java b/modules/core/src/test/java/org/apache/ignite/ClassesGenerator.java
deleted file mode 100644
index 182e6d3..0000000
--- a/modules/core/src/test/java/org/apache/ignite/ClassesGenerator.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.ignite;
-
-import org.apache.ignite.internal.util.typedef.internal.*;
-
-import java.io.*;
-import java.lang.reflect.*;
-import java.net.*;
-import java.util.*;
-import java.util.jar.*;
-
-/**
- * Serialized classes generator.
- */
-public class ClassesGenerator {
-    /** */
-    private static final String PATH = "modules/core/src/main/java/org/apache/ignite/internal/classnames.properties";
-
-    /** */
-    private static final String HEADER =
-        "#\n" +
-        "# Licensed to the Apache Software Foundation (ASF) under one or more\n" +
-        "# contributor license agreements.  See the NOTICE file distributed with\n" +
-        "# this work for additional information regarding copyright ownership.\n" +
-        "# The ASF licenses this file to You under the Apache License, Version 2.0\n" +
-        "# (the \"License\"); you may not use this file except in compliance with\n" +
-        "# the License.  You may obtain a copy of the License at\n" +
-        "#\n" +
-        "#      http://www.apache.org/licenses/LICENSE-2.0\n" +
-        "#\n" +
-        "# Unless required by applicable law or agreed to in writing, software\n" +
-        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
-        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
-        "# See the License for the specific language governing permissions and\n" +
-        "# limitations under the License.\n" +
-        "#";
-
-    /** */
-    private static final String[] INCLUDED_PACKAGES = {
-        "org.apache.ignite",
-        "org.jdk8.backport",
-        "org.pcollections",
-        "com.romix.scala",
-        "java.lang",
-        "java.util",
-        "java.net"
-    };
-
-    /**
-     * @param args Arguments.
-     * @throws Exception In case of error.
-     */
-    public static void main(String[] args) throws Exception {
-        PrintStream out = new PrintStream(new File(U.getIgniteHome(), PATH));
-
-        out.println(HEADER);
-        out.println();
-
-        for (Class cls : classes())
-            out.println(cls.getName());
-    }
-
-    /**
-     * @return Classes.
-     * @throws Exception In case of error.
-     */
-    private static Collection<Class> classes() throws Exception {
-        Collection<Class> col = new TreeSet<>(new Comparator<Class>() {
-            @Override public int compare(Class c1, Class c2) {
-                return c1.getName().compareTo(c2.getName());
-            }
-        });
-
-        URLClassLoader ldr = (URLClassLoader)ClassesGenerator.class.getClassLoader();
-
-        for (URL url : ldr.getURLs()) {
-            File file = new File(url.toURI());
-
-            int prefixLen = file.getPath().length() + 1;
-
-            processFile(file, ldr, prefixLen, col);
-        }
-
-        return col;
-    }
-
-    /**
-     * @param file File.
-     * @param ldr Class loader.
-     * @param prefixLen Prefix length.
-     * @param col Classes.
-     * @throws Exception In case of error.
-     */
-    private static void processFile(File file, ClassLoader ldr, int prefixLen, Collection<Class>
col) throws Exception {
-        if (!file.exists())
-            throw new FileNotFoundException("File doesn't exist: " + file);
-
-        if (file.isDirectory()) {
-            for (File f : file.listFiles())
-                processFile(f, ldr, prefixLen, col);
-        }
-        else {
-            assert file.isFile();
-
-            String path = file.getPath();
-
-            if (path.toLowerCase().endsWith(".jar")) {
-                try (JarInputStream jin = new JarInputStream(new BufferedInputStream(new
FileInputStream(path)))) {
-                    JarEntry entry;
-
-                    while ((entry = jin.getNextJarEntry()) != null) {
-                        if (!entry.isDirectory() && entry.getName().toLowerCase().endsWith(".class"))
-                            processClassFile(entry.getName(), ldr, 0, col);
-                    }
-                }
-            }
-            else if (path.toLowerCase().endsWith(".class"))
-                processClassFile(path, ldr, prefixLen, col);
-        }
-    }
-
-    /**
-     * @param path File path.
-     * @param ldr Class loader.
-     * @param prefixLen Prefix length.
-     * @param col Classes.
-     * @throws Exception In case of error.
-     */
-    private static void processClassFile(String path, ClassLoader ldr, int prefixLen, Collection<Class>
col)
-        throws Exception {
-        String clsName = path.substring(prefixLen, path.length() - 6).replace(File.separatorChar,
'.');
-
-        boolean included = false;
-
-        for (String pkg : INCLUDED_PACKAGES) {
-            if (clsName.startsWith(pkg)) {
-                included = true;
-
-                break;
-            }
-        }
-
-        if (included) {
-            Class<?> cls = Class.forName(clsName, false, ldr);
-
-            if (!cls.isInterface() && !Modifier.isAbstract(cls.getModifiers()) &&
-                Serializable.class.isAssignableFrom(cls))
-                col.add((Class)cls);
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2866f02f/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamSelfTest.java
index 1f9108b..c376195 100644
--- a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedObjectStreamSelfTest.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.marshaller.optimized;
 
 import org.apache.ignite.*;
+import org.apache.ignite.internal.processors.cache.*;
 import org.apache.ignite.internal.util.io.*;
 import org.apache.ignite.internal.util.typedef.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
@@ -994,6 +995,20 @@ public class OptimizedObjectStreamSelfTest extends GridCommonAbstractTest
{
     }
 
     /**
+     * @throws Exception If failed.
+     */
+    @SuppressWarnings("ThrowableInstanceNeverThrown")
+    public void testThrowable() throws Exception {
+        Throwable t = new Throwable("Throwable");
+
+        assertEquals(t.getMessage(), ((Throwable)marshalUnmarshal(t)).getMessage());
+
+        CacheFlagException flagEx = new CacheFlagException(CacheFlag.CLONE, CacheFlag.READ);
+
+        assertEquals(flagEx.flags(), ((CacheFlagException)marshalUnmarshal(flagEx)).flags());
+    }
+
+    /**
      * Marshals and unmarshals object.
      *
      * @param obj Original object.


Mime
View raw message