Search in sources :

Example 1 with PdxSerializable

use of org.apache.geode.pdx.PdxSerializable in project geode by apache.

the class InternalDataSerializer method writePdx.

public static boolean writePdx(DataOutput out, InternalCache internalCache, Object pdx, PdxSerializer pdxSerializer) throws IOException {
    TypeRegistry tr = null;
    if (internalCache != null) {
        tr = internalCache.getPdxRegistry();
    }
    PdxOutputStream os;
    if (out instanceof HeapDataOutputStream) {
        os = new PdxOutputStream((HeapDataOutputStream) out);
    } else {
        os = new PdxOutputStream();
    }
    PdxWriterImpl writer = new PdxWriterImpl(tr, pdx, os);
    try {
        if (pdxSerializer != null) {
            // serializer
            if (isGemfireObject(pdx)) {
                return false;
            }
            if (is662SerializationEnabled()) {
                boolean alreadyInProgress = isPdxSerializationInProgress();
                if (!alreadyInProgress) {
                    setPdxSerializationInProgress(true);
                    try {
                        if (!pdxSerializer.toData(pdx, writer)) {
                            return false;
                        }
                    } finally {
                        setPdxSerializationInProgress(false);
                    }
                } else {
                    if (!pdxSerializer.toData(pdx, writer)) {
                        return false;
                    }
                }
            } else {
                if (!pdxSerializer.toData(pdx, writer)) {
                    return false;
                }
            }
        } else {
            if (is662SerializationEnabled()) {
                boolean alreadyInProgress = isPdxSerializationInProgress();
                if (!alreadyInProgress) {
                    setPdxSerializationInProgress(true);
                    try {
                        ((PdxSerializable) pdx).toData(writer);
                    } finally {
                        setPdxSerializationInProgress(false);
                    }
                } else {
                    ((PdxSerializable) pdx).toData(writer);
                }
            } else {
                ((PdxSerializable) pdx).toData(writer);
            }
        }
    } catch (ToDataException | CancelException | NonPortableClassException | GemFireRethrowable ex) {
        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();
        if (pdxSerializer != null) {
            throw new ToDataException("PdxSerializer failed when calling toData on " + pdx.getClass(), t);
        } else {
            throw new ToDataException("toData failed on PdxSerializable " + pdx.getClass(), t);
        }
    }
    int bytesWritten = writer.completeByteStreamGeneration();
    getDMStats(internalCache).incPdxSerialization(bytesWritten);
    if (!(out instanceof HeapDataOutputStream)) {
        writer.sendTo(out);
    }
    return true;
}
Also used : PdxOutputStream(org.apache.geode.pdx.internal.PdxOutputStream) NonPortableClassException(org.apache.geode.pdx.NonPortableClassException) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) CancelException(org.apache.geode.CancelException) PdxWriterImpl(org.apache.geode.pdx.internal.PdxWriterImpl) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

Example 2 with PdxSerializable

use of org.apache.geode.pdx.PdxSerializable 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);
}
Also used : PdxSerializer(org.apache.geode.pdx.PdxSerializer) PdxInstance(org.apache.geode.pdx.PdxInstance) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) IOException(java.io.IOException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

Example 3 with PdxSerializable

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

Example 4 with PdxSerializable

use of org.apache.geode.pdx.PdxSerializable 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;
}
Also used : PdxSerializer(org.apache.geode.pdx.PdxSerializer) PdxReader(org.apache.geode.pdx.PdxReader) InternalCache(org.apache.geode.internal.cache.InternalCache) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) IOException(java.io.IOException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) PdxFieldTypeMismatchException(org.apache.geode.pdx.PdxFieldTypeMismatchException) InternalGemFireException(org.apache.geode.InternalGemFireException) AutoClassInfo(org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

Aggregations

PdxSerializable (org.apache.geode.pdx.PdxSerializable)4 IOException (java.io.IOException)2 PdxInstance (org.apache.geode.pdx.PdxInstance)2 PdxSerializationException (org.apache.geode.pdx.PdxSerializationException)2 PdxSerializer (org.apache.geode.pdx.PdxSerializer)2 NotSerializableException (java.io.NotSerializableException)1 ObjectStreamClass (java.io.ObjectStreamClass)1 Serializable (java.io.Serializable)1 CancelException (org.apache.geode.CancelException)1 DataSerializable (org.apache.geode.DataSerializable)1 GemFireRethrowable (org.apache.geode.GemFireRethrowable)1 InternalGemFireException (org.apache.geode.InternalGemFireException)1 ToDataException (org.apache.geode.ToDataException)1 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 StoredObject (org.apache.geode.internal.offheap.StoredObject)1 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)1 PdxFieldTypeMismatchException (org.apache.geode.pdx.PdxFieldTypeMismatchException)1 PdxReader (org.apache.geode.pdx.PdxReader)1 AutoClassInfo (org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo)1