use of org.apache.geode.pdx.PdxSerializer in project geode by apache.
the class TypeRegistry method setPdxSerializer.
public static void setPdxSerializer(PdxSerializer v) {
if (v == null) {
PdxSerializer oldValue = pdxSerializer.getAndSet(null);
if (oldValue instanceof ReflectionBasedAutoSerializer) {
asm.compareAndSet((AutoSerializableManager) ((ReflectionBasedAutoSerializer) oldValue).getManager(), null);
}
} else {
pdxSerializerWasSet = true;
pdxSerializer.set(v);
if (v instanceof ReflectionBasedAutoSerializer) {
asm.set((AutoSerializableManager) ((ReflectionBasedAutoSerializer) v).getManager());
}
}
}
use of org.apache.geode.pdx.PdxSerializer in project geode by apache.
the class InternalDataSerializer method writeUserObject.
/**
* Data serializes an instance of a "user class" (that is, a class that can be handled by a
* registered {@code DataSerializer}) to the given {@code DataOutput}.
*
* @return {@code true} if {@code o} was written to {@code out}.
*/
private static boolean writeUserObject(Object o, DataOutput out, boolean ensurePdxCompatibility) throws IOException {
final Class<?> c = o.getClass();
final DataSerializer serializer = InternalDataSerializer.getSerializer(c);
if (serializer != null) {
int id = serializer.getId();
if (id != 0) {
checkPdxCompatible(o, ensurePdxCompatibility);
// id will be 0 if it is a WellKnowDS
if (id <= Byte.MAX_VALUE && id >= Byte.MIN_VALUE) {
out.writeByte(USER_CLASS);
out.writeByte((byte) id);
} else if (id <= Short.MAX_VALUE && id >= Short.MIN_VALUE) {
out.writeByte(USER_CLASS_2);
out.writeShort(id);
} else {
out.writeByte(USER_CLASS_4);
out.writeInt(id);
}
} else {
if (ensurePdxCompatibility) {
if (!(serializer instanceof WellKnownPdxDS)) {
checkPdxCompatible(o, ensurePdxCompatibility);
}
}
}
boolean toDataResult;
try {
toDataResult = serializer.toData(o, out);
} catch (IOException io) {
if (serializer instanceof WellKnownDS) {
// see bug 44659
throw io;
} else {
// with the plugin code.
throw new ToDataException("toData failed on DataSerializer with id=" + id + " for class " + c, 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 DataSerializer with id=" + id + " for class " + c, t);
}
if (toDataResult) {
return true;
} else {
throw new ToDataException(LocalizedStrings.DataSerializer_SERIALIZER_0_A_1_SAID_THAT_IT_COULD_SERIALIZE_AN_INSTANCE_OF_2_BUT_ITS_TODATA_METHOD_RETURNED_FALSE.toLocalizedString(serializer.getId(), serializer.getClass().getName(), o.getClass().getName()));
}
// Do byte[][] and Object[] here to fix bug 44060
} else if (o instanceof byte[][]) {
byte[][] byteArrays = (byte[][]) o;
out.writeByte(ARRAY_OF_BYTE_ARRAYS);
writeArrayOfByteArrays(byteArrays, out);
return true;
} else if (o instanceof Object[]) {
Object[] array = (Object[]) o;
out.writeByte(OBJECT_ARRAY);
writeObjectArray(array, out, ensurePdxCompatibility);
return true;
} else if (is662SerializationEnabled() && (o.getClass().isEnum() || /* for bug 52271 */
(o.getClass().getSuperclass() != null && o.getClass().getSuperclass().isEnum()))) {
if (isPdxSerializationInProgress()) {
writePdxEnum((Enum<?>) o, out);
} else {
checkPdxCompatible(o, ensurePdxCompatibility);
writeGemFireEnum((Enum<?>) o, out);
}
return true;
} else {
PdxSerializer pdxSerializer = TypeRegistry.getPdxSerializer();
return pdxSerializer != null && writePdx(out, null, o, pdxSerializer);
}
}
use of org.apache.geode.pdx.PdxSerializer in project geode by apache.
the class CacheXmlParser method endPdxSerializer.
/**
*
*/
private void endPdxSerializer() {
Declarable d = createDeclarable();
if (!(d instanceof PdxSerializer)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_PDX_SERIALIZER.toLocalizedString(d.getClass().getName()));
}
PdxSerializer serializer = (PdxSerializer) d;
this.cache.setPdxSerializer(serializer);
}
use of org.apache.geode.pdx.PdxSerializer in project geode by apache.
the class AbstractRegionEntry method checkPdxEquals.
/**
* This method fixes bug 43643
*/
private static boolean checkPdxEquals(PdxInstance pdx, Object obj) {
if (!(obj instanceof PdxInstance)) {
// if we are not readSerialized.
if (obj instanceof CachedDeserializable) {
CachedDeserializable cdObj = (CachedDeserializable) obj;
if (!cdObj.isSerialized()) {
// obj is actually a byte[] which will never be equal to a PdxInstance
return false;
}
Object cdVal = cdObj.getValue();
if (cdVal instanceof byte[]) {
byte[] cdValBytes = (byte[]) cdVal;
PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality"));
if (pi != null) {
return pi.equals(pdx);
} else {
// since obj is serialized as something other than pdx it must not equal our pdx
return false;
}
} else {
// remove the cd wrapper so that obj is the actual value we want to compare.
obj = cdVal;
}
}
if (obj != null && obj.getClass().getName().equals(pdx.getClassName())) {
InternalCache internalCache = GemFireCacheImpl.getForPdx("Could not access Pdx registry");
if (internalCache != null) {
PdxSerializer pdxSerializer;
if (obj instanceof PdxSerializable) {
pdxSerializer = null;
} else {
pdxSerializer = internalCache.getPdxSerializer();
}
if (pdxSerializer != null || obj instanceof PdxSerializable) {
// try to convert obj to a PdxInstance
HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
try {
if (InternalDataSerializer.autoSerialized(obj, hdos) || InternalDataSerializer.writePdx(hdos, internalCache, obj, pdxSerializer)) {
PdxInstance pi = InternalDataSerializer.readPdxInstance(hdos.toByteArray(), internalCache);
if (pi != null) {
obj = pi;
}
}
} catch (IOException | PdxSerializationException ignore) {
// we are not able to convert it so just fall through
}
}
}
}
}
return basicEquals(obj, pdx);
}
use of org.apache.geode.pdx.PdxSerializer in project geode by apache.
the class PdxReaderImpl method basicGetObject.
protected Object basicGetObject() {
String pdxClassName = getPdxType().getClassName();
Class<?> pdxClass = getPdxType().getPdxClass();
{
AutoClassInfo ci = getPdxType().getAutoInfo(pdxClass);
if (ci != null) {
Object obj = ci.newInstance(pdxClass);
this.orderedDeserialize(obj, ci);
return obj;
}
}
PdxReader pdxReader = this;
// only create a tracking one if we might need it
UnreadPdxType unreadLocalPdxType = null;
boolean needToTrackReads = TESTHOOK_TRACKREADS;
InternalCache cache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
TypeRegistry tr = cache.getPdxRegistry();
if (!cache.getPdxIgnoreUnreadFields()) {
PdxType localPdxType = tr.getExistingTypeForClass(pdxClass);
if (localPdxType != null) {
if (getPdxType().getTypeId() != localPdxType.getTypeId() && getPdxType().hasExtraFields(localPdxType)) {
// we could calculate the extra fields here
needToTrackReads = true;
}
} else {
// we don't know what our local type would be
needToTrackReads = true;
}
}
if (needToTrackReads) {
unreadLocalPdxType = tr.getExistingTypeForClass(pdxClass, getPdxType().getTypeId());
if (unreadLocalPdxType != null) {
needToTrackReads = false;
} else {
pdxReader = new TrackingPdxReaderImpl(this, tr, pdxClass);
}
}
Object result;
if (PdxSerializable.class.isAssignableFrom(pdxClass)) {
try {
result = pdxClass.newInstance();
} catch (Exception e) {
PdxSerializationException ex = new PdxSerializationException(LocalizedStrings.DataSerializer_COULD_NOT_CREATE_AN_INSTANCE_OF_A_CLASS_0.toLocalizedString(pdxClassName), e);
throw ex;
}
((PdxSerializable) result).fromData(pdxReader);
} else {
PdxSerializer pdxSerializer = cache.getPdxSerializer();
if (pdxSerializer != null) {
result = pdxSerializer.fromData(pdxClass, pdxReader);
if (result == null) {
throw new PdxSerializationException("Could not deserialize pdx because the pdx serializer's fromData returned false for a pdx of class " + pdxClassName);
}
} else {
throw new PdxSerializationException("Could not deserialize pdx because a PdxSerializer does not exist.");
}
}
{
PdxUnreadData ud = getReadUnreadFieldsCalled();
if (ud != null) {
// User called PdxReader.readUnreadFields()
if (unreadLocalPdxType != null) {
if (unreadLocalPdxType.getUnreadFieldIndexes() != null) {
ud.initialize(unreadLocalPdxType, this);
}
} else if (needToTrackReads) {
((TrackingPdxReaderImpl) pdxReader).internalReadUnreadFields(ud);
}
} else {
if (needToTrackReads) {
ud = ((TrackingPdxReaderImpl) pdxReader).internalReadUnreadFields(new PdxUnreadData());
if (ud != null && !ud.isEmpty()) {
tr.putUnreadData(result, ud);
}
} else if (unreadLocalPdxType != null) {
if (unreadLocalPdxType.getUnreadFieldIndexes() != null) {
tr.putUnreadData(result, new PdxUnreadData(unreadLocalPdxType, this));
}
}
}
}
return result;
}
Aggregations