Search in sources :

Example 11 with ToDataException

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);
    }
}
Also used : DataOutput(java.io.DataOutput) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) DataSerializable(org.apache.geode.DataSerializable) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) ObjectStreamClass(java.io.ObjectStreamClass) CancelException(org.apache.geode.CancelException)

Aggregations

ToDataException (org.apache.geode.ToDataException)11 CancelException (org.apache.geode.CancelException)7 IOException (java.io.IOException)6 GemFireRethrowable (org.apache.geode.GemFireRethrowable)5 NotSerializableException (java.io.NotSerializableException)3 GemFireIOException (org.apache.geode.GemFireIOException)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 InternalGemFireException (org.apache.geode.InternalGemFireException)2 Cache (org.apache.geode.cache.Cache)2 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)2 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)2 PdxOutputStream (org.apache.geode.pdx.internal.PdxOutputStream)2 PdxWriterImpl (org.apache.geode.pdx.internal.PdxWriterImpl)2 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)2 DataOutput (java.io.DataOutput)1 ObjectStreamClass (java.io.ObjectStreamClass)1 UnknownHostException (java.net.UnknownHostException)1