Search in sources :

Example 1 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class InternalDataSerializer method invokeFromData.

/**
   * For backward compatibility this method should be used to invoke fromData on a DSFID or
   * DataSerializable. It will invoke the correct fromData method based on the class's version
   * information. This method does not read information about the class of the object. When
   * serializing use the method invokeToData to write the contents of the object.
   * 
   * @param ds the object to write
   * @param in the input stream.
   */
public static void invokeFromData(Object ds, DataInput in) throws IOException, ClassNotFoundException {
    try {
        boolean invoked = false;
        Version v = InternalDataSerializer.getVersionForDataStreamOrNull(in);
        if (v != null && v != Version.CURRENT) {
            // get versions where DataOutput was upgraded
            Version[] versions = null;
            if (ds instanceof SerializationVersions) {
                SerializationVersions vds = (SerializationVersions) ds;
                versions = vds.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("fromDataPre" + '_' + version.getMethodSuffix(), new Class[] { DataInput.class }).invoke(ds, in);
                        invoked = true;
                        break;
                    }
                }
            }
        }
        if (!invoked) {
            if (ds instanceof DataSerializableFixedID) {
                ((DataSerializableFixedID) ds).fromData(in);
            } else {
                ((DataSerializable) ds).fromData(in);
            }
        }
    } catch (EOFException | ClassNotFoundException | CacheClosedException ex) {
        // client went away - ignore
        throw ex;
    } catch (Exception ex) {
        throw new SerializationException(LocalizedStrings.DataSerializer_COULD_NOT_CREATE_AN_INSTANCE_OF_0.toLocalizedString(ds.getClass().getName()), ex);
    }
}
Also used : SerializationException(org.apache.geode.SerializationException) CacheClosedException(org.apache.geode.cache.CacheClosedException) 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) DataInput(java.io.DataInput) EOFException(java.io.EOFException) ObjectStreamClass(java.io.ObjectStreamClass)

Example 2 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class InternalDataSerializer method readDataSerializable.

private static Object readDataSerializable(final DataInput in) throws IOException, ClassNotFoundException {
    Class c = readClass(in);
    try {
        Constructor init = c.getConstructor(new Class[0]);
        init.setAccessible(true);
        Object o = init.newInstance(new Object[0]);
        Assert.assertTrue(o instanceof DataSerializable);
        invokeFromData(o, in);
        if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
            logger.trace(LogMarker.SERIALIZER, "Read DataSerializable {}", o);
        }
        return o;
    } catch (EOFException ex) {
        // client went away - ignore
        throw ex;
    } catch (Exception ex) {
        throw new SerializationException(LocalizedStrings.DataSerializer_COULD_NOT_CREATE_AN_INSTANCE_OF_0.toLocalizedString(c.getName()), ex);
    }
}
Also used : SerializationException(org.apache.geode.SerializationException) Constructor(java.lang.reflect.Constructor) EOFException(java.io.EOFException) ObjectStreamClass(java.io.ObjectStreamClass) 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 3 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class DataSerializableJUnitTest method testInstantiatorExceptions.

/**
   * Tests that the appropriate exceptions are thrown by {@link Instantiator#register} when given
   * bad input.
   */
@Test
public void testInstantiatorExceptions() {
    try {
        new Instantiator(null, (byte) 42) {

            public DataSerializable newInstance() {
                return null;
            }
        };
        fail("Should have thrown a NullPointerException");
    } catch (NullPointerException ex) {
    // pass...
    }
    try {
        Instantiator.register(null);
        fail("Should have thrown a NullPointerException");
    } catch (NullPointerException ex) {
    // pass...
    }
    Instantiator.register(new Instantiator(DataSerializableImpl.class, (byte) 42) {

        public DataSerializable newInstance() {
            return null;
        }
    });
    try {
        try {
            Instantiator.register(new Instantiator(DataSerializableImpl.class, (byte) 41) {

                public DataSerializable newInstance() {
                    return null;
                }
            });
            fail("Should have thrown an IllegalStateException");
        } catch (IllegalStateException ex) {
        // pass...
        }
        try {
            Instantiator.register(new Instantiator(DSIntWrapper.class, (byte) 42) {

                public DataSerializable newInstance() {
                    return null;
                }
            });
            fail("Should have thrown an IllegalStateException");
        } catch (IllegalStateException ex) {
        // pass...
        }
    } finally {
        InternalInstantiator.unregister(DataSerializableImpl.class, (byte) 42);
    }
}
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 4 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class ResultsDataSerializabilityJUnitTest method testImplementsDataSerializable.

/*
   * In the absence of some kind of hook into the DataSerializer, just test to see if the known
   * implementation classes that are part of query results implement the DataSerializable interface
   */
@Test
public void testImplementsDataSerializable() throws Exception {
    Class[] classes = new Class[] { SortedResultSet.class, ResultsCollectionWrapper.class, ResultsSet.class, SortedStructSet.class, StructImpl.class, StructSet.class, Undefined.class, // QRegion.class, // QRegions remain unserializable
    CollectionTypeImpl.class, MapTypeImpl.class, ObjectTypeImpl.class, StructTypeImpl.class };
    List list = new ArrayList();
    for (int i = 0; i < classes.length; i++) {
        Class nextClass = classes[i];
        if (!DataSerializable.class.isAssignableFrom(nextClass)) {
            if (!DataSerializableFixedID.class.isAssignableFrom(nextClass)) {
                list.add(nextClass.getName());
            }
        }
    }
    assertTrue(list + " are not DataSerializable", list.isEmpty());
}
Also used : Undefined(org.apache.geode.cache.query.internal.Undefined) SortedStructSet(org.apache.geode.cache.query.internal.SortedStructSet) StructSet(org.apache.geode.cache.query.internal.StructSet) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) MapTypeImpl(org.apache.geode.cache.query.internal.types.MapTypeImpl) SortedResultSet(org.apache.geode.cache.query.internal.SortedResultSet) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) ArrayList(java.util.ArrayList) DataSerializable(org.apache.geode.DataSerializable) SortedStructSet(org.apache.geode.cache.query.internal.SortedStructSet) StructImpl(org.apache.geode.cache.query.internal.StructImpl) ResultsCollectionWrapper(org.apache.geode.cache.query.internal.ResultsCollectionWrapper) CollectionTypeImpl(org.apache.geode.cache.query.internal.types.CollectionTypeImpl) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) DataSerializableFixedID(org.apache.geode.internal.DataSerializableFixedID) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with DataSerializable

use of org.apache.geode.DataSerializable in project geode by apache.

the class InternalDataSerializer method basicWriteObject.

public static void basicWriteObject(Object o, DataOutput out, boolean ensurePdxCompatibility) throws IOException {
    checkOut(out);
    final boolean isDebugEnabled_SERIALIZER = logger.isTraceEnabled(LogMarker.SERIALIZER);
    if (isDebugEnabled_SERIALIZER) {
        logger.trace(LogMarker.SERIALIZER, "basicWriteObject: {}", o);
    }
    // Handle special objects first
    if (o == null) {
        out.writeByte(NULL);
    } else if (o instanceof DataSerializableFixedID) {
        checkPdxCompatible(o, ensurePdxCompatibility);
        DataSerializableFixedID dsfid = (DataSerializableFixedID) o;
        writeDSFID(dsfid, out);
    } else if (autoSerialized(o, out)) {
    // all done
    } else if (o instanceof DataSerializable.Replaceable) {
        // do this first to fix bug 31609
        // do this before DataSerializable
        Object replacement = ((DataSerializable.Replaceable) o).replace();
        basicWriteObject(replacement, out, ensurePdxCompatibility);
    } else if (o instanceof PdxSerializable) {
        writePdx(out, GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed."), o, null);
    } else if (o instanceof DataSerializable) {
        if (isDebugEnabled_SERIALIZER) {
            logger.trace(LogMarker.SERIALIZER, "Writing DataSerializable: {}", o);
        }
        checkPdxCompatible(o, ensurePdxCompatibility);
        Class c = o.getClass();
        // Is "c" a user class registered with an Instantiator?
        int classId = InternalInstantiator.getClassId(c);
        if (classId != 0) {
            writeUserDataSerializableHeader(classId, out);
        } else {
            out.writeByte(DATA_SERIALIZABLE);
            DataSerializer.writeClass(c, out);
        }
        DataSerializable ds = (DataSerializable) o;
        invokeToData(ds, out);
    } else if (o instanceof Sendable) {
        if (!(o instanceof PdxInstance) || o instanceof PdxInstanceEnum) {
            checkPdxCompatible(o, ensurePdxCompatibility);
        }
        ((Sendable) o).sendTo(out);
    } else if (writeWellKnownObject(o, out, ensurePdxCompatibility)) {
    // Nothing more to do...
    } else {
        checkPdxCompatible(o, ensurePdxCompatibility);
        if (logger.isTraceEnabled(LogMarker.DUMP_SERIALIZED)) {
            logger.trace(LogMarker.DUMP_SERIALIZED, "DataSerializer Serializing an instance of {}", o.getClass().getName());
        }
        /*
       * If the (internally known) ThreadLocal named "DataSerializer.DISALLOW_JAVA_SERIALIZATION" is
       * set, then an exception will be thrown if we try to do standard Java Serialization. This is
       * used to catch Java serialization early for the case where the data is being sent to a
       * non-Java client
       */
        if (disallowJavaSerialization() && o instanceof Serializable) {
            throw new NotSerializableException(LocalizedStrings.DataSerializer_0_IS_NOT_DATASERIALIZABLE_AND_JAVA_SERIALIZATION_IS_DISALLOWED.toLocalizedString(o.getClass().getName()));
        }
        writeSerializableObject(o, out);
    }
}
Also used : PdxInstanceEnum(org.apache.geode.pdx.internal.PdxInstanceEnum) Serializable(java.io.Serializable) DataSerializable(org.apache.geode.DataSerializable) PdxSerializable(org.apache.geode.pdx.PdxSerializable) NotSerializableException(java.io.NotSerializableException) PdxInstance(org.apache.geode.pdx.PdxInstance) ObjectStreamClass(java.io.ObjectStreamClass) DataSerializable(org.apache.geode.DataSerializable) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

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