use of org.apache.geode.CanonicalInstantiator in project geode by apache.
the class InternalDataSerializer method readUserDataSerializable.
private static Object readUserDataSerializable(final DataInput in, int classId) throws IOException {
Instantiator instantiator = InternalInstantiator.getInstantiator(classId);
if (instantiator == null) {
logger.error(LogMarker.SERIALIZER, LocalizedMessage.create(LocalizedStrings.DataSerializer_NO_INSTANTIATOR_HAS_BEEN_REGISTERED_FOR_CLASS_WITH_ID_0, classId));
throw new IOException(LocalizedStrings.DataSerializer_NO_INSTANTIATOR_HAS_BEEN_REGISTERED_FOR_CLASS_WITH_ID_0.toLocalizedString(classId));
} else {
try {
DataSerializable ds;
if (instantiator instanceof CanonicalInstantiator) {
CanonicalInstantiator ci = (CanonicalInstantiator) instantiator;
ds = ci.newInstance(in);
} else {
ds = instantiator.newInstance();
}
ds.fromData(in);
return ds;
} catch (Exception ex) {
throw new SerializationException(LocalizedStrings.DataSerializer_COULD_NOT_DESERIALIZE_AN_INSTANCE_OF_0.toLocalizedString(instantiator.getInstantiatedClass().getName()), ex);
}
}
}
use of org.apache.geode.CanonicalInstantiator in project geode by apache.
the class DataSerializableJUnitTest method testCanonicalInstantiator.
/**
* Tests that an <code>CanonicalInstantiator</code> is invoked at the appropriate times.
*/
@Test
public void testCanonicalInstantiator() throws Exception {
final boolean[] wasInvoked = new boolean[] { false };
Instantiator.register(new CanonicalInstantiator(CanonicalDataSerializableImpl.class, (byte) 45) {
public DataSerializable newInstance(DataInput di) throws IOException {
wasInvoked[0] = true;
return CanonicalDataSerializableImpl.create(di.readByte());
}
});
try {
byte id = (byte) 57;
Class_testInstantiator.supClass = CanonicalDataSerializableImpl.class;
DataSerializer.register(Class_testInstantiator.class);
try {
Object o = CanonicalDataSerializableImpl.create();
DataSerializer.writeObject(o, getDataOutput());
Object o2 = DataSerializer.readObject(getDataInput());
assertTrue(wasInvoked[0]);
assertTrue(o == o2);
} finally {
InternalDataSerializer.unregister(id);
}
} finally {
InternalInstantiator.unregister(CanonicalDataSerializableImpl.class, (byte) 45);
}
}
Aggregations