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