Search in sources :

Example 1 with AutoClassInfo

use of org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo in project geode by apache.

the class InternalDataSerializer method autoSerialized.

public static boolean autoSerialized(Object o, DataOutput out) throws IOException {
    AutoSerializableManager asm = TypeRegistry.getAutoSerializableManager();
    if (asm != null) {
        AutoClassInfo aci = asm.getExistingClassInfo(o.getClass());
        if (aci != null) {
            InternalCache internalCache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
            TypeRegistry tr = internalCache.getPdxRegistry();
            PdxOutputStream os;
            if (out instanceof HeapDataOutputStream) {
                os = new PdxOutputStream((HeapDataOutputStream) out);
            } else {
                os = new PdxOutputStream();
            }
            PdxWriterImpl writer = new PdxWriterImpl(tr, o, aci, os);
            try {
                if (is662SerializationEnabled()) {
                    boolean alreadyInProgress = isPdxSerializationInProgress();
                    if (!alreadyInProgress) {
                        setPdxSerializationInProgress(true);
                        try {
                            asm.writeData(writer, o, aci);
                        } finally {
                            setPdxSerializationInProgress(false);
                        }
                    } else {
                        asm.writeData(writer, o, aci);
                    }
                } else {
                    asm.writeData(writer, o, aci);
                }
            } 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();
                throw new ToDataException("PdxSerializer failed when calling toData on " + o.getClass(), t);
            }
            int bytesWritten = writer.completeByteStreamGeneration();
            getDMStats(internalCache).incPdxSerialization(bytesWritten);
            if (!(out instanceof HeapDataOutputStream)) {
                writer.sendTo(out);
            }
            return true;
        }
    }
    return false;
}
Also used : PdxOutputStream(org.apache.geode.pdx.internal.PdxOutputStream) NonPortableClassException(org.apache.geode.pdx.NonPortableClassException) InternalCache(org.apache.geode.internal.cache.InternalCache) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry) AutoClassInfo(org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo) AutoSerializableManager(org.apache.geode.pdx.internal.AutoSerializableManager) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) CancelException(org.apache.geode.CancelException) PdxWriterImpl(org.apache.geode.pdx.internal.PdxWriterImpl)

Example 2 with AutoClassInfo

use of org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo 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

InternalCache (org.apache.geode.internal.cache.InternalCache)2 AutoClassInfo (org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo)2 IOException (java.io.IOException)1 CancelException (org.apache.geode.CancelException)1 GemFireRethrowable (org.apache.geode.GemFireRethrowable)1 InternalGemFireException (org.apache.geode.InternalGemFireException)1 ToDataException (org.apache.geode.ToDataException)1 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)1 PdxFieldTypeMismatchException (org.apache.geode.pdx.PdxFieldTypeMismatchException)1 PdxReader (org.apache.geode.pdx.PdxReader)1 PdxSerializable (org.apache.geode.pdx.PdxSerializable)1 PdxSerializationException (org.apache.geode.pdx.PdxSerializationException)1 PdxSerializer (org.apache.geode.pdx.PdxSerializer)1 AutoSerializableManager (org.apache.geode.pdx.internal.AutoSerializableManager)1 PdxOutputStream (org.apache.geode.pdx.internal.PdxOutputStream)1 PdxWriterImpl (org.apache.geode.pdx.internal.PdxWriterImpl)1 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)1