FileDocCategorySizeDatePackage
SerializedObjectConverter.javaAPI DocGlassfish v2 API5874Tue May 22 16:54:46 BST 2007oracle.toplink.essentials.mappings.converters

SerializedObjectConverter

public class SerializedObjectConverter extends Object implements Converter

Purpose: The serialized object converter can be used to store an arbitrary object or set of objects into a database blob field. It uses the Java serializer so the target must be serializable.

author
James Sutherland
since
OracleAS TopLink 10g (10.0.3)

Fields Summary
protected DatabaseMapping
mapping
Constructors Summary
public SerializedObjectConverter()
PUBLIC: Default constructor.

    
public SerializedObjectConverter(DatabaseMapping mapping)
PUBLIC: Default constructor.

        this.mapping = mapping;
    
Methods Summary
public java.lang.ObjectconvertDataValueToObjectValue(java.lang.Object fieldValue, oracle.toplink.essentials.sessions.Session session)
INTERNAL: The fieldValue will be a byte array. Create a ByteArrayInputStream on the fieldValue. Create an ObjectInputStream on the ByteArrayInputStream to read in the objects.

        byte[] bytes;
        try {
            bytes = (byte[])((AbstractSession)session).getDatasourcePlatform().convertObject(fieldValue, ClassConstants.APBYTE);
        } catch (ConversionException e) {
            throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e);
        }

        if ((bytes == null) || (bytes.length == 0)) {
            return null;
        }
        ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
        Object object = null;
        try {
            // BUG# 2813583
            CustomObjectInputStream objectIn = new CustomObjectInputStream(byteIn, session);
            object = objectIn.readObject();
        } catch (Exception exception) {
            throw DescriptorException.notDeserializable(getMapping(), exception);
        }

        return object;
    
public java.lang.ObjectconvertObjectValueToDataValue(java.lang.Object attributeValue, oracle.toplink.essentials.sessions.Session session)
INTERNAL: Convert the object to a byte array through serialize.

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
            objectOut.writeObject(attributeValue);
            objectOut.flush();
        } catch (IOException exception) {
            throw DescriptorException.notSerializable(getMapping(), exception);
        }
        return byteOut.toByteArray();
    
protected oracle.toplink.essentials.mappings.DatabaseMappinggetMapping()
INTERNAL: Return the mapping.

        return mapping;
    
public voidinitialize(oracle.toplink.essentials.mappings.DatabaseMapping mapping, oracle.toplink.essentials.sessions.Session session)
INTERNAL: Set the mapping.

        this.mapping = mapping;
        // CR#... Mapping must also have the field classification.
        if (getMapping().isDirectToFieldMapping()) {
            AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping();

            // Allow user to specify field type to override computed value. (i.e. blob, nchar)
            if (directMapping.getFieldClassification() == null) {
                directMapping.setFieldClassification(ClassConstants.APBYTE);
            }
        }
    
public booleanisMutable()
INTERNAL: If the converter converts the value to a non-atomic value, i.e. a value that can have its' parts changed without being replaced, then it must return false, serialization can be non-atomic.

        return true;