Repository: ignite
Updated Branches:
  refs/heads/ignite-2257 [created] 0009b1345


IGNITE-2257: Fixed.


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

Branch: refs/heads/ignite-2257
Commit: e67eb6dcf400baa707eac4e54976802955ce3fa4
Parents: 61c072e
Author: vozerov-gridgain <voze...@gridgain.com>
Authored: Thu Dec 24 12:33:25 2015 +0300
Committer: vozerov-gridgain <voze...@gridgain.com>
Committed: Thu Dec 24 12:33:25 2015 +0300

----------------------------------------------------------------------
 .../ignite/internal/binary/BinaryContext.java   | 44 +-----------------
 .../internal/binary/BinaryEnumObjectImpl.java   |  4 +-
 .../internal/binary/BinaryObjectImpl.java       |  4 +-
 .../internal/binary/GridBinaryMarshaller.java   | 49 ++++++++++++++++++--
 4 files changed, 49 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e67eb6dc/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
index 5c63fbd..163532d 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java
@@ -31,10 +31,8 @@ import org.apache.ignite.cache.affinity.AffinityKey;
 import org.apache.ignite.cache.affinity.AffinityKeyMapped;
 import org.apache.ignite.configuration.BinaryConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
-import org.apache.ignite.internal.IgniteKernal;
 import org.apache.ignite.internal.IgnitionEx;
 import org.apache.ignite.internal.processors.cache.binary.BinaryMetadataKey;
-import 
org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl;
 import 
org.apache.ignite.internal.processors.datastructures.CollocatedQueueItemKey;
 import 
org.apache.ignite.internal.processors.datastructures.CollocatedSetItemKey;
 import org.apache.ignite.internal.util.IgniteUtils;
@@ -51,10 +49,6 @@ import org.jsr166.ConcurrentHashMap8;
 import java.io.Externalizable;
 import java.io.File;
 import java.io.IOException;
-import java.io.InvalidObjectException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.io.ObjectStreamException;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.net.URISyntaxException;
@@ -81,10 +75,7 @@ import java.util.jar.JarFile;
 /**
  * Binary context.
  */
-public class BinaryContext implements Externalizable {
-    /** */
-    private static final long serialVersionUID = 0L;
-
+public class BinaryContext {
     /** */
     private static final ClassLoader dfltLdr = U.gridClassLoader();
 
@@ -125,9 +116,6 @@ public class BinaryContext implements Externalizable {
     private MarshallerContext marshCtx;
 
     /** */
-    private String gridName;
-
-    /** */
     private IgniteConfiguration igniteCfg;
 
     /** Logger. */
@@ -162,8 +150,6 @@ public class BinaryContext implements Externalizable {
         this.igniteCfg = igniteCfg;
         this.log = log;
 
-        gridName = igniteCfg.getGridName();
-
         colTypes.put(ArrayList.class, GridBinaryMarshaller.ARR_LIST);
         colTypes.put(LinkedList.class, GridBinaryMarshaller.LINKED_LIST);
         colTypes.put(HashSet.class, GridBinaryMarshaller.HASH_SET);
@@ -727,34 +713,6 @@ public class BinaryContext implements Externalizable {
         return null;
     }
 
-    /** {@inheritDoc} */
-    @Override public void writeExternal(ObjectOutput out) throws IOException {
-        U.writeString(out, igniteCfg.getGridName());
-    }
-
-    /** {@inheritDoc} */
-    @Override public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
-        gridName = U.readString(in);
-    }
-
-    /**
-     * @return Binary context.
-     * @throws ObjectStreamException In case of error.
-     */
-    protected Object readResolve() throws ObjectStreamException {
-        try {
-            IgniteKernal g = IgnitionEx.gridx(gridName);
-
-            if (g == null)
-                throw new IllegalStateException("Failed to find grid for name: 
" + gridName);
-
-            return 
((CacheObjectBinaryProcessorImpl)g.context().cacheObjects()).binaryContext();
-        }
-        catch (IllegalStateException e) {
-            throw U.withCause(new InvalidObjectException(e.getMessage()), e);
-        }
-    }
-
     /**
      * @param cls Class.
      * @param id Type ID.

http://git-wip-us.apache.org/repos/asf/ignite/blob/e67eb6dc/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryEnumObjectImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryEnumObjectImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryEnumObjectImpl.java
index 001d2d7..536c582 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryEnumObjectImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryEnumObjectImpl.java
@@ -183,8 +183,6 @@ public class BinaryEnumObjectImpl implements 
BinaryObjectEx, Externalizable, Cac
 
     /** {@inheritDoc} */
     @Override public void writeExternal(ObjectOutput out) throws IOException {
-        out.writeObject(ctx);
-
         out.writeInt(typeId);
         out.writeObject(clsName);
         out.writeInt(ord);
@@ -192,7 +190,7 @@ public class BinaryEnumObjectImpl implements 
BinaryObjectEx, Externalizable, Cac
 
     /** {@inheritDoc} */
     @Override public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
-        ctx = (BinaryContext)in.readObject();
+        ctx = GridBinaryMarshaller.threadLocalContext();
 
         typeId = in.readInt();
         clsName = (String)in.readObject();

http://git-wip-us.apache.org/repos/asf/ignite/blob/e67eb6dc/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryObjectImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryObjectImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryObjectImpl.java
index fca8955..2843a0f 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryObjectImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryObjectImpl.java
@@ -427,8 +427,6 @@ public final class BinaryObjectImpl extends 
BinaryObjectExImpl implements Extern
 
     /** {@inheritDoc} */
     @Override public void writeExternal(ObjectOutput out) throws IOException {
-        out.writeObject(ctx);
-
         if (detachAllowed) {
             int len = length();
 
@@ -445,7 +443,7 @@ public final class BinaryObjectImpl extends 
BinaryObjectExImpl implements Extern
 
     /** {@inheritDoc} */
     @Override public void readExternal(ObjectInput in) throws IOException, 
ClassNotFoundException {
-        ctx = (BinaryContext)in.readObject();
+        ctx = GridBinaryMarshaller.threadLocalContext();
 
         arr = new byte[in.readInt()];
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/e67eb6dc/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
index 71ca018..95fbc0c 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java
@@ -34,6 +34,9 @@ public class GridBinaryMarshaller {
         }
     };
 
+    /** Binary context in TLS store. */
+    private static final ThreadLocal<BinaryContext> BINARY_CTX = new 
ThreadLocal<>();
+
     /** */
     static final byte OPTM_MARSH = -2;
 
@@ -237,7 +240,16 @@ public class GridBinaryMarshaller {
     @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader 
clsLdr) throws BinaryObjectException {
         assert bytes != null;
 
-        return (T)BinaryUtils.unmarshal(BinaryHeapInputStream.create(bytes, 
0), ctx, clsLdr);
+        assert BINARY_CTX.get() == null;
+
+        BINARY_CTX.set(ctx);
+
+        try {
+            return (T) 
BinaryUtils.unmarshal(BinaryHeapInputStream.create(bytes, 0), ctx, clsLdr);
+        }
+        finally {
+            BINARY_CTX.remove();
+        }
     }
 
     /**
@@ -247,7 +259,16 @@ public class GridBinaryMarshaller {
      */
     @SuppressWarnings("unchecked")
     @Nullable public <T> T unmarshal(BinaryInputStream in) throws 
BinaryObjectException {
-        return (T)BinaryUtils.unmarshal(in, ctx, null);
+        assert BINARY_CTX.get() == null;
+
+        BINARY_CTX.set(ctx);
+
+        try {
+            return (T)BinaryUtils.unmarshal(in, ctx, null);
+        }
+        finally {
+            BINARY_CTX.remove();
+        }
     }
 
     /**
@@ -264,7 +285,16 @@ public class GridBinaryMarshaller {
         if (arr[0] == NULL)
             return null;
 
-        return (T)new BinaryReaderExImpl(ctx, 
BinaryHeapInputStream.create(arr, 0), ldr).deserialize();
+        assert BINARY_CTX.get() == null;
+
+        BINARY_CTX.set(ctx);
+
+        try {
+            return (T)new BinaryReaderExImpl(ctx, 
BinaryHeapInputStream.create(arr, 0), ldr).deserialize();
+        }
+        finally {
+            BINARY_CTX.remove();
+        }
     }
 
     /**
@@ -293,4 +323,17 @@ public class GridBinaryMarshaller {
     public BinaryContext context() {
         return ctx;
     }
+
+    /**
+     * @return Thread-bound context.
+     */
+    public static BinaryContext threadLocalContext() {
+        BinaryContext ctx = GridBinaryMarshaller.BINARY_CTX.get();
+
+        if (ctx == null)
+            throw new IllegalStateException("Failed to get BinaryContext 
(binary objects can only be deserialized " +
+                "inside Ignite).");
+
+        return ctx;
+    }
 }

Reply via email to