Re: Problem sending a Map over captp [patch]

Thomas Leonard <tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]>
Newsgroups gmane.comp.lang.e.general
Organization IT Innovation
Message-ID <[email protected]>
On Thu, 2010-03-25 at 10:00 +0000, Thomas Leonard wrote:

[ error sending a SwitchableRef over captp ]

> new problem: <NullPointerException>
> #   # While printing an class org.erights.e.elib.tables.ConstMapImpl

Possible patch attached, but I don't know much about Java serialisation,
so maybe this isn't the best way.


Java serialization gives each object the chance to replace itself with
another object. ConstMapImpl uses this to change itself into a
ConstMap.fromColumns() call. However, when serializing a resolved
SwitchableRef Java only gives the SwitchableRef this option, not the
underlying object, so we should call it manually.

Also, if we do get a raw ConstMapImpl object, throw an expcetion
immediately rather than constructing an invalid object (with myTable ==
null).


-- 
Dr Thomas Leonard
IT Innovation Centre
2 Venture Road
Southampton
Hampshire SO16 7NP

Tel: +44 0 23 8076 0834
Fax: +44 0 23 8076 0833
mailto:tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]
http://www.it-innovation.soton.ac.uk

_______________________________________________
e-lang mailing list
[email protected]
http://www.eros-os.org/mailman/listinfo/e-lang
0001-Serialize-Switchable-refs-correctly.patch (text/x-patch, 3.8 KB)
>From 41f8c72e50b0202ef8c74a49c71598090f5dcefe Mon Sep 17 00:00:00 2001
From: Thomas Leonard <tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]>
Date: Thu, 25 Mar 2010 10:46:46 +0000
Subject: [PATCH] Serialize Switchable refs correctly

Java serialization gives each object the chance to replace itself with another
object. ConstMapImpl uses this to change itself into a ConstMap.fromColumns() call.
However, when serializing a resolved SwitchableRef Java only gives the SwitchableRef
this option, not the underlying object, so we should call it manually.

Also, if we do get a raw ConstMapImpl object, throw an expcetion immediately
rather than constructing an invalid object (with myTable == null).

Patch from the University of Southampton IT Innovation Centre.
---
 src/jsrc/net/captp/jcomm/CapTPReplacer.java        |   28 +++++++++++++++++++-
 .../org/erights/e/elib/tables/ConstMapImpl.java    |    7 +++++
 2 files changed, 34 insertions(+), 1 deletions(-)

diff --git a/src/jsrc/net/captp/jcomm/CapTPReplacer.java b/src/jsrc/net/captp/jcomm/CapTPReplacer.java
index 554084c..53487c0 100644
--- a/src/jsrc/net/captp/jcomm/CapTPReplacer.java
+++ b/src/jsrc/net/captp/jcomm/CapTPReplacer.java
@@ -14,6 +14,9 @@ import org.erights.e.elib.serial.Replacer;
 import org.erights.e.elib.slot.AuditChecker;
 import org.erights.e.elib.tables.ConstList;
 
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
 /**
  * Used to specialize the SerializationStream for encoding a reference over a
  * CapTP connection.
@@ -50,7 +53,30 @@ class CapTPReplacer extends Replacer {
      * sure we're actually permitted to pass it by construction.
      */
     public Object substitute(Object ref) {
-        ref = Ref.resolution(ref);
+        Object resolvedRef = Ref.resolution(ref);
+        if (resolvedRef != ref) {
+            /* Java serialization has given the original ref the chance to
+             * writeReplace itself, but not the target. Do that now.
+             */
+            Method writeReplaceMethod;
+            try {
+                writeReplaceMethod = resolvedRef.getClass().getDeclaredMethod("writeReplace", new Class[] {});
+            } catch (NoSuchMethodException ex) {
+                writeReplaceMethod = null;
+            }
+            if (writeReplaceMethod == null) {
+                ref = resolvedRef;
+            } else {
+                try {
+                    writeReplaceMethod.setAccessible(true);
+                    ref = writeReplaceMethod.invoke(resolvedRef, new Object[] {});
+                } catch (InvocationTargetException ex) {
+                    throw new RuntimeException(ex);
+                } catch (IllegalAccessException ex) {
+                    throw new RuntimeException(ex);
+                }
+            }
+        }
 
         if (Ref.isJOSSPBCRef(ref)) {
             return ref;
diff --git a/src/jsrc/org/erights/e/elib/tables/ConstMapImpl.java b/src/jsrc/org/erights/e/elib/tables/ConstMapImpl.java
index 54ee0b5..a45d6bf 100644
--- a/src/jsrc/org/erights/e/elib/tables/ConstMapImpl.java
+++ b/src/jsrc/org/erights/e/elib/tables/ConstMapImpl.java
@@ -22,6 +22,8 @@ Contributor(s): ______________________________________.
 import org.erights.e.elib.base.Thunk;
 import org.erights.e.elib.serial.RemoteCall;
 
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
 import java.io.ObjectStreamException;
 
 /**
@@ -107,4 +109,9 @@ class ConstMapImpl extends ConstMap {
     public Class valueType() {
         return myTable.valueType();
     }
+
+    /* Our super-class is Serializable, but we are not. */
+    private void readObject(ObjectInputStream stream) throws NotSerializableException {
+        throw new NotSerializableException("ConstMapImpl must be serialized specially.");
+    }
 }
-- 
1.6.3.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.