use of org.apache.geode.DataSerializable in project geode by apache.
the class InternalDataSerializer method invokeToData.
/**
* For backward compatibility this method should be used to invoke toData on a DSFID or
* DataSerializable. It will invoke the correct toData method based on the class's version
* information. This method does not write information about the class of the object. When
* deserializing use the method invokeFromData to read the contents of the object.
*
* @param ds the object to write
* @param out the output stream.
*/
public static void invokeToData(Object ds, DataOutput out) throws IOException {
boolean isDSFID = ds instanceof DataSerializableFixedID;
try {
boolean invoked = false;
Version v = InternalDataSerializer.getVersionForDataStreamOrNull(out);
if (v != null && v != Version.CURRENT) {
// get versions where DataOutput was upgraded
Version[] versions = null;
if (ds instanceof SerializationVersions) {
SerializationVersions sv = (SerializationVersions) ds;
versions = sv.getSerializationVersions();
}
// there has been a change in the message
if (versions != null && versions.length > 0) {
for (Version version : versions) {
// if peer version is less than the greatest upgraded version
if (v.compareTo(version) < 0) {
ds.getClass().getMethod("toDataPre_" + version.getMethodSuffix(), new Class[] { DataOutput.class }).invoke(ds, out);
invoked = true;
break;
}
}
}
}
if (!invoked) {
if (isDSFID) {
((DataSerializableFixedID) ds).toData(out);
} else {
((DataSerializable) ds).toData(out);
}
}
} catch (IOException io) {
// as a problem with the plugin code
if (isDSFID) {
throw io;
} else {
throw new ToDataException("toData failed on DataSerializable " + ds.getClass(), io);
}
} catch (CancelException | ToDataException | GemFireRethrowable ex) {
// Serializing a PDX can result in a cache closed exception. Just rethrow
throw ex;
} catch (VirtualMachineError err) {
SystemFailure.initiateFailure(err);
// now, so don't let this thread continue.
throw err;
} catch (Throwable t) {
// Whenever you catch Error or Throwable, you must also
// catch VirtualMachineError (see above). However, there is
// _still_ a possibility that you are dealing with a cascading
// error condition, so you also need to check to see if the JVM
// is still usable:
SystemFailure.checkFailure();
throw new ToDataException("toData failed on DataSerializable " + ds.getClass(), t);
}
}
use of org.apache.geode.DataSerializable 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.DataSerializable 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);
}
}
use of org.apache.geode.DataSerializable in project geode by apache.
the class DataSerializableJUnitTest method testInstantiator2.
@Test
public void testInstantiator2() throws Exception {
final boolean[] wasInvoked = new boolean[] { false };
Instantiator.register(new Instantiator(DataSerializableImpl.class, 20000) {
public DataSerializable newInstance() {
wasInvoked[0] = true;
return new DataSerializableImpl();
}
});
try {
byte id = (byte) 57;
Class_testInstantiator.supClass = DataSerializableImpl.class;
DataSerializer.register(Class_testInstantiator.class);
try {
Object o = new DataSerializableImpl(new Random());
DataSerializer.writeObject(o, getDataOutput());
Object o2 = DataSerializer.readObject(getDataInput());
assertTrue(wasInvoked[0]);
assertEquals(o, o2);
} finally {
InternalDataSerializer.unregister(id);
}
} finally {
InternalInstantiator.unregister(DataSerializableImpl.class, 20000);
}
}
use of org.apache.geode.DataSerializable in project geode by apache.
the class DataSerializableJUnitTest method testDataSerializable.
/**
* Tests serializing an object that implements {@link DataSerializable}
*/
@Test
public void testDataSerializable() throws Exception {
DataSerializable ds = new DataSerializableImpl(getRandom());
DataOutputStream out = getDataOutput();
ds.toData(out);
out.flush();
DataSerializable ds2 = new DataSerializableImpl();
ds2.fromData(getDataInput());
assertEquals(ds, ds2);
}
Aggregations