Search in sources :

Example 1 with CanonicalInstantiator

use of org.apache.geode.CanonicalInstantiator 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 2 with CanonicalInstantiator

use of org.apache.geode.CanonicalInstantiator 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)

Aggregations

IOException (java.io.IOException)2 CanonicalInstantiator (org.apache.geode.CanonicalInstantiator)2 DataSerializable (org.apache.geode.DataSerializable)2 DataInput (java.io.DataInput)1 EOFException (java.io.EOFException)1 NotSerializableException (java.io.NotSerializableException)1 UTFDataFormatException (java.io.UTFDataFormatException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CancelException (org.apache.geode.CancelException)1 GemFireIOException (org.apache.geode.GemFireIOException)1 Instantiator (org.apache.geode.Instantiator)1 SerializationException (org.apache.geode.SerializationException)1 ToDataException (org.apache.geode.ToDataException)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Test (org.junit.Test)1