use of org.apache.geode.ToDataException 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);
}
}
Aggregations