Skip to content
Permalink
Browse files
Merge branch 'class_marshalling' into 'ibm-trunk'
Class marshalling

Sorts out conversion of Class to ClassDesc during marshalling and ClassDesc to Class during unmarshalling.

Also correct ArrayValueDescriptor so that ValueDescriptor.writeReplace is called for each element in the array.

See merge request !47
  • Loading branch information
ngmr committed May 27, 2015
2 parents f6e0625 + fc6c682 commit ca997b7ea84dcd6743650203d62ca24aaafc335b
Showing 8 changed files with 173 additions and 98 deletions.
@@ -348,6 +348,20 @@ public void testHashMap() throws RemoteException {
assertSame(map2.get(3), map2.get(4));
assertSame(map2.get(0), map2.get(1));
}

public void testClass() throws RemoteException {
final Class<?> type = Object.class;
sample.setSerializable(type);
Serializable s = sample.getSerializable();
assertSame(s, type);
}

public void testClassArray() throws RemoteException {
final Class<?>[] types = { Object.class, Map.class, String.class, Map.class };
sample.setSerializable(types);
Object[] oa = (Object[])sample.getSerializable();
assertArrayEquals(types, oa);
}
}

public static void main(String[] args) throws Exception {
@@ -392,6 +406,8 @@ public static void main(String[] args) throws Exception {
test.testTimeUnit();
test.testTimeUnitArray();
test.testCmsfv2Data();
test.testClass();
test.testClassArray();
//myORB.destroy();
System.out.println("Testing complete");
}
@@ -76,7 +76,6 @@
org.apache.yoko.rmi.util,
org.apache.yoko.rmi.util.corba,
org.apache.yoko.rmi.util.stub,
org.apache.yoko.rmi.cmsf
</Export-Package>
<Import-Package>
!sun.*,
@@ -18,7 +18,9 @@

package org.apache.yoko.rmi.impl;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Logger;

@@ -28,6 +30,7 @@
import org.omg.CORBA.ORB;
import org.omg.CORBA.TypeCode;
import org.omg.CORBA.ValueMember;
import org.omg.CORBA.portable.InputStream;

public abstract class ArrayDescriptor extends ValueDescriptor {
protected int order;
@@ -424,27 +427,21 @@ public void writeValue(org.omg.CORBA.portable.OutputStream out,
}
}

public java.io.Serializable readValue(
org.omg.CORBA.portable.InputStream in, java.util.Map offsetMap,
Integer key) {
try {
ObjectReader reader = makeCorbaObjectReader(in, offsetMap, null);

int length = reader.readInt();
Object[] arr = (Object[]) Array.newInstance(elementType, length);
offsetMap.put(key, arr);
// System.out.println ("ValueArrayDescriptor::readValue
// len="+length+"; type="+elementType);
public Serializable readValue(InputStream in, Map offsetMap, Integer key) {
final int length = in.read_long();
Object[] arr = (Object[]) Array.newInstance(elementType, length);
offsetMap.put(key, arr);

for (int i = 0; i < length; i++) {
arr[i] = reader.readValueObject(elementType);
final org.omg.CORBA_2_3.portable.InputStream _in = (org.omg.CORBA_2_3.portable.InputStream) in;
for (int i = 0; i < length; i++) {
try {
arr[i] = _in.read_value(elementType);
} catch (org.omg.CORBA.portable.IndirectionException ex) {
arr[i] = offsetMap.get(new Integer(ex.offset));
}

return (java.io.Serializable) arr;
} catch (java.io.IOException ex) {
throw (MARSHAL)new MARSHAL(ex.getMessage()).initCause(ex);
}

return arr;
}

Object copyObject(Object value, CopyState state) {
@@ -0,0 +1,32 @@
package org.apache.yoko.rmi.impl;

import java.lang.reflect.Field;

import javax.rmi.CORBA.ClassDesc;

import org.omg.CORBA.MARSHAL;

public abstract class ClassBaseDescriptor extends ValueDescriptor {

ClassBaseDescriptor(Class type, TypeRepository repository) {
super(type, repository);
}

@Override
public void init() {
super.init();

Class<?> clz = ClassDesc.class;
try {
final Field repid_field = clz.getDeclaredField("repid");
repid_field.setAccessible(true);
final Field codebase_field = clz.getDeclaredField("codebase");
codebase_field.setAccessible(true);
init(repid_field, codebase_field);
} catch (NoSuchFieldException ex) {
throw new MARSHAL("no such field: " + ex);
}
}

abstract void init(Field repid_field, Field codebase_field);
}
@@ -0,0 +1,71 @@
package org.apache.yoko.rmi.impl;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.rmi.CORBA.ClassDesc;
import javax.rmi.CORBA.Util;

import org.omg.CORBA.MARSHAL;

public class ClassDescDescriptor extends ClassBaseDescriptor {
private static final Logger logger = Logger.getLogger(ClassDescDescriptor.class.getName());

private Field repid_field;
private Field codebase_field;

ClassDescDescriptor(TypeRepository repository) {
super(ClassDesc.class, repository);
}

@Override
void init(Field repid_field, Field codebase_field) {
this.repid_field = repid_field;
this.codebase_field = codebase_field;
}

/** Read an instance of this value from a CDR stream */
@Override
public Serializable readResolve(final Serializable value) {
final ClassDesc desc = (ClassDesc) value;

Class<?> result = AccessController
.doPrivileged(new PrivilegedAction<Class<?>>() {
public Class<?> run() {
String className = "<unknown>";
try {
String repid = (String) repid_field.get(desc);
String codebase = (String) codebase_field.get(desc);

TypeDescriptor typeDesc = repository.getDescriptor(repid);
if (null != typeDesc) {
Class<?> type = typeDesc.getJavaClass();
if (null != type) return type;
}

int beg = repid.indexOf(':');
int end = repid.indexOf(':', beg + 1);

className = repid.substring(beg + 1, end);
ClassLoader loader = Thread.currentThread().getContextClassLoader();

return Util.loadClass(className, codebase, loader);
} catch (ClassNotFoundException ex) {
throw (MARSHAL)new MARSHAL("cannot load class " + className).initCause(ex);
} catch (IllegalAccessException ex) {
throw (MARSHAL)new MARSHAL("no such field: " + ex).initCause(ex);
}
}
});

if (logger.isLoggable(Level.FINE))
logger.fine(String.format("readResolve %s => %s", value, result));

return result;
}

}
@@ -18,115 +18,71 @@

package org.apache.yoko.rmi.impl;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ClassDescriptor extends ValueDescriptor {
static Logger logger = Logger.getLogger(ClassDescriptor.class.getName());
import javax.rmi.CORBA.ClassDesc;
import javax.rmi.CORBA.Util;
import javax.rmi.CORBA.ValueHandler;

ClassDescriptor(TypeRepository repository) {
super(javax.rmi.CORBA.ClassDesc.class, repository);
}

java.lang.reflect.Field repid_field;

java.lang.reflect.Field codebase_field;

String _repid_arr;
import org.omg.CORBA.MARSHAL;

public void init() {
super.init();
public class ClassDescriptor extends ClassBaseDescriptor {
private static final Logger logger = Logger.getLogger(ClassDescriptor.class.getName());

Class clz = javax.rmi.CORBA.ClassDesc.class;
try {
repid_field = clz.getDeclaredField("repid");
repid_field.setAccessible(true);
codebase_field = clz.getDeclaredField("codebase");
codebase_field.setAccessible(true);
} catch (java.lang.NoSuchFieldException ex) {
throw new org.omg.CORBA.MARSHAL("no such field: " + ex);
}
private Field repid_field;
private Field codebase_field;

ValueDescriptor class_desc = new ValueDescriptor(Class.class,
getTypeRepository());
class_desc.init();
_repid_arr = class_desc.getRepositoryID();
ClassDescriptor(TypeRepository repository) {
super(Class.class, repository);
}

public String getRepositoryIDForArray() {
return _repid_arr;
@Override
void init(Field repid_field, Field codebase_field) {
this.repid_field = repid_field;
this.codebase_field = codebase_field;
}

@Override
Object copyObject(Object orig, CopyState state) {
state.put(orig, orig);
return orig;
}

/** Read an instance of this value from a CDR stream */
public java.io.Serializable readResolve(final java.io.Serializable value) {
final javax.rmi.CORBA.ClassDesc desc = (javax.rmi.CORBA.ClassDesc) value;

java.io.Serializable result = (java.io.Serializable) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
String className = "<unknown>";
try {
String repid = (String) repid_field.get(desc);
String codebase = (String) codebase_field.get(desc);

int beg = repid.indexOf(':');
int end = repid.indexOf(':', beg + 1);

className = repid.substring(beg + 1, end);
ClassLoader loader = Thread.currentThread()
.getContextClassLoader();

return javax.rmi.CORBA.Util.loadClass(className,
codebase, loader);
} catch (java.lang.ClassNotFoundException ex) {
throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL(
"cannot load class " + className).initCause(ex);
} catch (java.lang.IllegalAccessException ex) {
throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL(
"no such field: " + ex).initCause(ex);
}
}
});

logger.fine("readResolve " + value + " => " + result);

return result;
}

/** Write an instance of this value to a CDR stream */
public java.io.Serializable writeReplace(final java.io.Serializable value) {
final Class type = (Class) value;

final javax.rmi.CORBA.ClassDesc desc = new javax.rmi.CORBA.ClassDesc();
@Override
public Serializable writeReplace(final Serializable value) {
final Class<?> type = (Class<?>) value;

return (java.io.Serializable) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
final ClassDesc result = AccessController
.doPrivileged(new PrivilegedAction<ClassDesc>() {
public ClassDesc run() {
try {
final ClassDesc desc = new ClassDesc();

javax.rmi.CORBA.ValueHandler handler = javax.rmi.CORBA.Util
.createValueHandler();
ValueHandler handler = Util.createValueHandler();
String repId = handler.getRMIRepositoryID(type);
repid_field.set(desc, repId);

String codebase = javax.rmi.CORBA.Util
.getCodebase(type);
String codebase = Util.getCodebase(type);
codebase_field.set(desc, codebase);

return desc;

} catch (java.lang.IllegalAccessException ex) {
throw (org.omg.CORBA.MARSHAL)new org.omg.CORBA.MARSHAL(
"no such field: " + ex).initCause(ex);
} catch (IllegalAccessException ex) {
throw (MARSHAL)new MARSHAL("no such field: " + ex).initCause(ex);
}
}
});

if (logger.isLoggable(Level.FINE))
logger.fine(String.format("writeReplace %s => %s", value, result));

return result;
}

}
@@ -100,7 +100,7 @@ protected TypeDescriptor computeValue(Class<?> type) {
} else if (type == Class.class) {
return new ClassDescriptor(repo);
} else if (type == ClassDesc.class) {
return this.get(Class.class);
return new ClassDescDescriptor(repo);
} else if (type == java.util.Date.class) {
return new DateValueDescriptor(repo);
} else if (staticAnyTypes.contains(type)) {
@@ -599,7 +599,11 @@ public Object run() {

readValue(reader, value);

return readResolve(value);
final Serializable resolved = readResolve(value);
if (value != resolved) {
offsetMap.put(offset, resolved);
}
return resolved;

} catch (IOException ex) {
throw (MARSHAL) new MARSHAL(ex.getMessage()).initCause(ex);

0 comments on commit ca997b7

Please sign in to comment.