Search in sources :

Example 6 with DataSerializable

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);
    }
}
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)

Example 7 with DataSerializable

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);
        }
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) SerializationException(org.apache.geode.SerializationException) CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) DataSerializable(org.apache.geode.DataSerializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) NonPortableClassException(org.apache.geode.pdx.NonPortableClassException) IOException(java.io.IOException) CancelException(org.apache.geode.CancelException) EOFException(java.io.EOFException) UTFDataFormatException(java.io.UTFDataFormatException) GemFireIOException(org.apache.geode.GemFireIOException) SerializationException(org.apache.geode.SerializationException) CacheClosedException(org.apache.geode.cache.CacheClosedException) NotSerializableException(java.io.NotSerializableException) ToDataException(org.apache.geode.ToDataException)

Example 8 with DataSerializable

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);
    }
}
Also used : DataInput(java.io.DataInput) CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) IOException(java.io.IOException) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 9 with DataSerializable

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);
    }
}
Also used : CanonicalInstantiator(org.apache.geode.CanonicalInstantiator) Instantiator(org.apache.geode.Instantiator) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 10 with DataSerializable

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);
}
Also used : DataOutputStream(java.io.DataOutputStream) DataSerializable(org.apache.geode.DataSerializable) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

DataSerializable (org.apache.geode.DataSerializable)14 Test (org.junit.Test)9 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)6 UnitTest (org.apache.geode.test.junit.categories.UnitTest)6 IOException (java.io.IOException)5 Instantiator (org.apache.geode.Instantiator)5 DataInput (java.io.DataInput)4 NotSerializableException (java.io.NotSerializableException)4 ObjectStreamClass (java.io.ObjectStreamClass)4 CancelException (org.apache.geode.CancelException)4 GemFireIOException (org.apache.geode.GemFireIOException)4 ToDataException (org.apache.geode.ToDataException)4 EOFException (java.io.EOFException)3 UTFDataFormatException (java.io.UTFDataFormatException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SerializationException (org.apache.geode.SerializationException)3 CacheClosedException (org.apache.geode.cache.CacheClosedException)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2