use of org.apache.geode.GemFireRethrowable in project geode by apache.
the class InternalDataSerializer method writeUserObject.
/**
* Data serializes an instance of a "user class" (that is, a class that can be handled by a
* registered {@code DataSerializer}) to the given {@code DataOutput}.
*
* @return {@code true} if {@code o} was written to {@code out}.
*/
private static boolean writeUserObject(Object o, DataOutput out, boolean ensurePdxCompatibility) throws IOException {
final Class<?> c = o.getClass();
final DataSerializer serializer = InternalDataSerializer.getSerializer(c);
if (serializer != null) {
int id = serializer.getId();
if (id != 0) {
checkPdxCompatible(o, ensurePdxCompatibility);
// id will be 0 if it is a WellKnowDS
if (id <= Byte.MAX_VALUE && id >= Byte.MIN_VALUE) {
out.writeByte(USER_CLASS);
out.writeByte((byte) id);
} else if (id <= Short.MAX_VALUE && id >= Short.MIN_VALUE) {
out.writeByte(USER_CLASS_2);
out.writeShort(id);
} else {
out.writeByte(USER_CLASS_4);
out.writeInt(id);
}
} else {
if (ensurePdxCompatibility) {
if (!(serializer instanceof WellKnownPdxDS)) {
checkPdxCompatible(o, ensurePdxCompatibility);
}
}
}
boolean toDataResult;
try {
toDataResult = serializer.toData(o, out);
} catch (IOException io) {
if (serializer instanceof WellKnownDS) {
// see bug 44659
throw io;
} else {
// with the plugin code.
throw new ToDataException("toData failed on DataSerializer with id=" + id + " for class " + c, 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 DataSerializer with id=" + id + " for class " + c, t);
}
if (toDataResult) {
return true;
} else {
throw new ToDataException(LocalizedStrings.DataSerializer_SERIALIZER_0_A_1_SAID_THAT_IT_COULD_SERIALIZE_AN_INSTANCE_OF_2_BUT_ITS_TODATA_METHOD_RETURNED_FALSE.toLocalizedString(serializer.getId(), serializer.getClass().getName(), o.getClass().getName()));
}
// Do byte[][] and Object[] here to fix bug 44060
} else if (o instanceof byte[][]) {
byte[][] byteArrays = (byte[][]) o;
out.writeByte(ARRAY_OF_BYTE_ARRAYS);
writeArrayOfByteArrays(byteArrays, out);
return true;
} else if (o instanceof Object[]) {
Object[] array = (Object[]) o;
out.writeByte(OBJECT_ARRAY);
writeObjectArray(array, out, ensurePdxCompatibility);
return true;
} else if (is662SerializationEnabled() && (o.getClass().isEnum() || /* for bug 52271 */
(o.getClass().getSuperclass() != null && o.getClass().getSuperclass().isEnum()))) {
if (isPdxSerializationInProgress()) {
writePdxEnum((Enum<?>) o, out);
} else {
checkPdxCompatible(o, ensurePdxCompatibility);
writeGemFireEnum((Enum<?>) o, out);
}
return true;
} else {
PdxSerializer pdxSerializer = TypeRegistry.getPdxSerializer();
return pdxSerializer != null && writePdx(out, null, o, pdxSerializer);
}
}
use of org.apache.geode.GemFireRethrowable in project geode by apache.
the class InternalDataSerializer method writeDSFID.
public static void writeDSFID(DataSerializableFixedID o, DataOutput out) throws IOException {
int dsfid = o.getDSFID();
if (dsfidToClassMap != null && logger.isTraceEnabled(LogMarker.DEBUG_DSFID)) {
logger.trace(LogMarker.DEBUG_DSFID, "writeDSFID {} class={}", dsfid, o.getClass());
if (dsfid != DataSerializableFixedID.NO_FIXED_ID && dsfid != DataSerializableFixedID.ILLEGAL) {
// consistency check to make sure that the same DSFID is not used
// for two different classes
String newClassName = o.getClass().getName();
String existingClassName = (String) dsfidToClassMap.putIfAbsent(dsfid, newClassName);
if (existingClassName != null && !existingClassName.equals(newClassName)) {
logger.trace(LogMarker.DEBUG_DSFID, "dsfid={} is used for class {} and class {}", dsfid, existingClassName, newClassName);
}
}
}
if (dsfid == DataSerializableFixedID.NO_FIXED_ID) {
out.writeByte(DS_NO_FIXED_ID);
DataSerializer.writeClass(o.getClass(), out);
} else {
writeDSFIDHeader(dsfid, out);
}
try {
invokeToData(o, out);
} catch (IOException | CancelException | ToDataException | GemFireRethrowable io) {
throw io;
} 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 dsfid=" + dsfid + " msg:" + t.getMessage(), t);
}
}
use of org.apache.geode.GemFireRethrowable in project geode by apache.
the class InternalDataSerializer method writePdx.
public static boolean writePdx(DataOutput out, InternalCache internalCache, Object pdx, PdxSerializer pdxSerializer) throws IOException {
TypeRegistry tr = null;
if (internalCache != null) {
tr = internalCache.getPdxRegistry();
}
PdxOutputStream os;
if (out instanceof HeapDataOutputStream) {
os = new PdxOutputStream((HeapDataOutputStream) out);
} else {
os = new PdxOutputStream();
}
PdxWriterImpl writer = new PdxWriterImpl(tr, pdx, os);
try {
if (pdxSerializer != null) {
// serializer
if (isGemfireObject(pdx)) {
return false;
}
if (is662SerializationEnabled()) {
boolean alreadyInProgress = isPdxSerializationInProgress();
if (!alreadyInProgress) {
setPdxSerializationInProgress(true);
try {
if (!pdxSerializer.toData(pdx, writer)) {
return false;
}
} finally {
setPdxSerializationInProgress(false);
}
} else {
if (!pdxSerializer.toData(pdx, writer)) {
return false;
}
}
} else {
if (!pdxSerializer.toData(pdx, writer)) {
return false;
}
}
} else {
if (is662SerializationEnabled()) {
boolean alreadyInProgress = isPdxSerializationInProgress();
if (!alreadyInProgress) {
setPdxSerializationInProgress(true);
try {
((PdxSerializable) pdx).toData(writer);
} finally {
setPdxSerializationInProgress(false);
}
} else {
((PdxSerializable) pdx).toData(writer);
}
} else {
((PdxSerializable) pdx).toData(writer);
}
}
} catch (ToDataException | CancelException | NonPortableClassException | GemFireRethrowable ex) {
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();
if (pdxSerializer != null) {
throw new ToDataException("PdxSerializer failed when calling toData on " + pdx.getClass(), t);
} else {
throw new ToDataException("toData failed on PdxSerializable " + pdx.getClass(), t);
}
}
int bytesWritten = writer.completeByteStreamGeneration();
getDMStats(internalCache).incPdxSerialization(bytesWritten);
if (!(out instanceof HeapDataOutputStream)) {
writer.sendTo(out);
}
return true;
}
use of org.apache.geode.GemFireRethrowable in project geode by apache.
the class InternalDataSerializer method autoSerialized.
public static boolean autoSerialized(Object o, DataOutput out) throws IOException {
AutoSerializableManager asm = TypeRegistry.getAutoSerializableManager();
if (asm != null) {
AutoClassInfo aci = asm.getExistingClassInfo(o.getClass());
if (aci != null) {
InternalCache internalCache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
TypeRegistry tr = internalCache.getPdxRegistry();
PdxOutputStream os;
if (out instanceof HeapDataOutputStream) {
os = new PdxOutputStream((HeapDataOutputStream) out);
} else {
os = new PdxOutputStream();
}
PdxWriterImpl writer = new PdxWriterImpl(tr, o, aci, os);
try {
if (is662SerializationEnabled()) {
boolean alreadyInProgress = isPdxSerializationInProgress();
if (!alreadyInProgress) {
setPdxSerializationInProgress(true);
try {
asm.writeData(writer, o, aci);
} finally {
setPdxSerializationInProgress(false);
}
} else {
asm.writeData(writer, o, aci);
}
} else {
asm.writeData(writer, o, aci);
}
} catch (ToDataException | CancelException | NonPortableClassException | GemFireRethrowable ex) {
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("PdxSerializer failed when calling toData on " + o.getClass(), t);
}
int bytesWritten = writer.completeByteStreamGeneration();
getDMStats(internalCache).incPdxSerialization(bytesWritten);
if (!(out instanceof HeapDataOutputStream)) {
writer.sendTo(out);
}
return true;
}
}
return false;
}
use of org.apache.geode.GemFireRethrowable 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