Search in sources :

Example 1 with GemFireRethrowable

use of org.apache.geode.GemFireRethrowable 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);
    }
}
Also used : PdxInstanceEnum(org.apache.geode.pdx.internal.PdxInstanceEnum) PdxSerializer(org.apache.geode.pdx.PdxSerializer) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) CancelException(org.apache.geode.CancelException) DataSerializer(org.apache.geode.DataSerializer)

Example 2 with GemFireRethrowable

use of org.apache.geode.GemFireRethrowable in project geode by apache.

the class InternalDataSerializer method writeDSFID.

public static void writeDSFID(DataSerializableFixedID o, DataOutput out) throws IOException {
    int dsfid = o.getDSFID();
    if (dsfidToClassMap != null && logger.isTraceEnabled(LogMarker.DEBUG_DSFID)) {
        logger.trace(LogMarker.DEBUG_DSFID, "writeDSFID {} class={}", dsfid, o.getClass());
        if (dsfid != DataSerializableFixedID.NO_FIXED_ID && dsfid != DataSerializableFixedID.ILLEGAL) {
            // consistency check to make sure that the same DSFID is not used
            // for two different classes
            String newClassName = o.getClass().getName();
            String existingClassName = (String) dsfidToClassMap.putIfAbsent(dsfid, newClassName);
            if (existingClassName != null && !existingClassName.equals(newClassName)) {
                logger.trace(LogMarker.DEBUG_DSFID, "dsfid={} is used for class {} and class {}", dsfid, existingClassName, newClassName);
            }
        }
    }
    if (dsfid == DataSerializableFixedID.NO_FIXED_ID) {
        out.writeByte(DS_NO_FIXED_ID);
        DataSerializer.writeClass(o.getClass(), out);
    } else {
        writeDSFIDHeader(dsfid, out);
    }
    try {
        invokeToData(o, out);
    } catch (IOException | CancelException | ToDataException | GemFireRethrowable io) {
        throw io;
    } 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 dsfid=" + dsfid + " msg:" + t.getMessage(), t);
    }
}
Also used : GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) CancelException(org.apache.geode.CancelException)

Example 3 with GemFireRethrowable

use of org.apache.geode.GemFireRethrowable 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 GemFireRethrowable

use of org.apache.geode.GemFireRethrowable 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 GemFireRethrowable

use of org.apache.geode.GemFireRethrowable in project geode by apache.

the class InternalDataSerializer method invokeToData.

/**
   * For backward compatibility this method should be used to invoke toData on a DSFID or
   * DataSerializable. It will invoke the correct toData method based on the class's version
   * information. This method does not write information about the class of the object. When
   * deserializing use the method invokeFromData to read the contents of the object.
   * 
   * @param ds the object to write
   * @param out the output stream.
   */
public static void invokeToData(Object ds, DataOutput out) throws IOException {
    boolean isDSFID = ds instanceof DataSerializableFixedID;
    try {
        boolean invoked = false;
        Version v = InternalDataSerializer.getVersionForDataStreamOrNull(out);
        if (v != null && v != Version.CURRENT) {
            // get versions where DataOutput was upgraded
            Version[] versions = null;
            if (ds instanceof SerializationVersions) {
                SerializationVersions sv = (SerializationVersions) ds;
                versions = sv.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("toDataPre_" + version.getMethodSuffix(), new Class[] { DataOutput.class }).invoke(ds, out);
                        invoked = true;
                        break;
                    }
                }
            }
        }
        if (!invoked) {
            if (isDSFID) {
                ((DataSerializableFixedID) ds).toData(out);
            } else {
                ((DataSerializable) ds).toData(out);
            }
        }
    } catch (IOException io) {
        // as a problem with the plugin code
        if (isDSFID) {
            throw io;
        } else {
            throw new ToDataException("toData failed on DataSerializable " + ds.getClass(), 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 DataSerializable " + ds.getClass(), t);
    }
}
Also used : DataOutput(java.io.DataOutput) IOException(java.io.IOException) GemFireIOException(org.apache.geode.GemFireIOException) DataSerializable(org.apache.geode.DataSerializable) GemFireRethrowable(org.apache.geode.GemFireRethrowable) ToDataException(org.apache.geode.ToDataException) ObjectStreamClass(java.io.ObjectStreamClass) CancelException(org.apache.geode.CancelException)

Aggregations

CancelException (org.apache.geode.CancelException)5 GemFireRethrowable (org.apache.geode.GemFireRethrowable)5 ToDataException (org.apache.geode.ToDataException)5 IOException (java.io.IOException)3 GemFireIOException (org.apache.geode.GemFireIOException)3 NonPortableClassException (org.apache.geode.pdx.NonPortableClassException)2 PdxOutputStream (org.apache.geode.pdx.internal.PdxOutputStream)2 PdxWriterImpl (org.apache.geode.pdx.internal.PdxWriterImpl)2 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)2 DataOutput (java.io.DataOutput)1 ObjectStreamClass (java.io.ObjectStreamClass)1 DataSerializable (org.apache.geode.DataSerializable)1 DataSerializer (org.apache.geode.DataSerializer)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 PdxSerializable (org.apache.geode.pdx.PdxSerializable)1 PdxSerializer (org.apache.geode.pdx.PdxSerializer)1 AutoSerializableManager (org.apache.geode.pdx.internal.AutoSerializableManager)1 AutoClassInfo (org.apache.geode.pdx.internal.AutoSerializableManager.AutoClassInfo)1 PdxInstanceEnum (org.apache.geode.pdx.internal.PdxInstanceEnum)1