Search in sources :

Example 1 with TypeRegistry

use of org.apache.geode.pdx.internal.TypeRegistry in project geode by apache.

the class InternalDataSerializer method readPdxEnum.

/**
   * @throws IOException since 6.6.2
   */
private static Object readPdxEnum(DataInput in) throws IOException {
    int dsId = in.readByte();
    int tmp = readArrayLength(in);
    int enumId = dsId << 24 | tmp & 0xFFFFFF;
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "read PdxEnum id={}", enumId);
    }
    InternalCache internalCache = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.");
    TypeRegistry tr = internalCache.getPdxRegistry();
    Object result = tr.getEnumById(enumId);
    if (result instanceof PdxInstance) {
        getDMStats(internalCache).incPdxInstanceCreations();
    }
    return result;
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) InternalCache(org.apache.geode.internal.cache.InternalCache) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry)

Example 2 with TypeRegistry

use of org.apache.geode.pdx.internal.TypeRegistry in project geode by apache.

the class InternalDataSerializer method writePdxEnum.

/**
   * @since GemFire 6.6.2
   */
private static void writePdxEnum(Enum<?> e, DataOutput out) throws IOException {
    TypeRegistry tr = GemFireCacheImpl.getForPdx("PDX registry is unavailable because the Cache has been closed.").getPdxRegistry();
    int eId = tr.getEnumId(e);
    if (logger.isTraceEnabled(LogMarker.SERIALIZER)) {
        logger.trace(LogMarker.SERIALIZER, "write PdxEnum id={} enum={}", eId, e);
    }
    writePdxEnumId(eId, out);
}
Also used : TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry)

Example 3 with TypeRegistry

use of org.apache.geode.pdx.internal.TypeRegistry 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 4 with TypeRegistry

use of org.apache.geode.pdx.internal.TypeRegistry 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 5 with TypeRegistry

use of org.apache.geode.pdx.internal.TypeRegistry in project geode by apache.

the class InternalDataSerializer method readPdxInstance.

/**
   * Reads a PdxInstance from dataBytes and returns it. If the first object read is not pdx encoded
   * returns null.
   */
public static PdxInstance readPdxInstance(final byte[] dataBytes, InternalCache internalCache) {
    try {
        byte type = dataBytes[0];
        if (type == PDX) {
            PdxInputStream in = new PdxInputStream(dataBytes);
            // throw away the type byte
            in.readByte();
            int len = in.readInt();
            int typeId = in.readInt();
            PdxType pdxType = internalCache.getPdxRegistry().getType(typeId);
            if (pdxType == null) {
                throw new IllegalStateException("Unknown pdx type=" + typeId);
            }
            return new PdxInstanceImpl(pdxType, in, len);
        } else if (type == DSCODE.PDX_ENUM) {
            PdxInputStream in = new PdxInputStream(dataBytes);
            // throw away the type byte
            in.readByte();
            int dsId = in.readByte();
            int tmp = readArrayLength(in);
            int enumId = dsId << 24 | tmp & 0xFFFFFF;
            TypeRegistry tr = internalCache.getPdxRegistry();
            EnumInfo ei = tr.getEnumInfoById(enumId);
            if (ei == null) {
                throw new IllegalStateException("Unknown pdx enum id=" + enumId);
            }
            return ei.getPdxInstance(enumId);
        } else if (type == DSCODE.PDX_INLINE_ENUM) {
            PdxInputStream in = new PdxInputStream(dataBytes);
            // throw away the type byte
            in.readByte();
            String className = DataSerializer.readString(in);
            String enumName = DataSerializer.readString(in);
            int enumOrdinal = InternalDataSerializer.readArrayLength(in);
            return new PdxInstanceEnum(className, enumName, enumOrdinal);
        }
    } catch (IOException ignore) {
    }
    return null;
}
Also used : PdxInstanceEnum(org.apache.geode.pdx.internal.PdxInstanceEnum) PdxType(org.apache.geode.pdx.internal.PdxType) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) PdxInputStream(org.apache.geode.pdx.internal.PdxInputStream) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry)

Aggregations

TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)24 InternalCache (org.apache.geode.internal.cache.InternalCache)10 PdxType (org.apache.geode.pdx.internal.PdxType)10 Test (org.junit.Test)10 IOException (java.io.IOException)7 EnumInfo (org.apache.geode.pdx.internal.EnumInfo)6 UnitTest (org.apache.geode.test.junit.categories.UnitTest)6 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)5 Message (org.apache.geode.internal.cache.tier.sockets.Message)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)3 CancelException (org.apache.geode.CancelException)2 GemFireRethrowable (org.apache.geode.GemFireRethrowable)2 ToDataException (org.apache.geode.ToDataException)2 SystemTimer (org.apache.geode.internal.SystemTimer)2 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)2 PdxInstance (org.apache.geode.pdx.PdxInstance)2 PdxInstanceImpl (org.apache.geode.pdx.internal.PdxInstanceImpl)2 PdxOutputStream (org.apache.geode.pdx.internal.PdxOutputStream)2