1 21 package oracle.toplink.essentials.mappings.converters; 23 24 import java.io.*; 25 import oracle.toplink.essentials.mappings.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping; 29 import oracle.toplink.essentials.sessions.*; 30 import oracle.toplink.essentials.internal.sessions.AbstractSession; 31 32 39 public class SerializedObjectConverter implements Converter { 40 protected DatabaseMapping mapping; 41 42 46 public SerializedObjectConverter() { 47 } 48 49 53 public SerializedObjectConverter(DatabaseMapping mapping) { 54 this.mapping = mapping; 55 } 56 57 63 public Object convertDataValueToObjectValue(Object fieldValue, Session session) throws DescriptorException { 64 byte[] bytes; 65 try { 66 bytes = (byte[])((AbstractSession)session).getDatasourcePlatform().convertObject(fieldValue, ClassConstants.APBYTE); 67 } catch (ConversionException e) { 68 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e); 69 } 70 71 if ((bytes == null) || (bytes.length == 0)) { 72 return null; 73 } 74 ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes); 75 Object object = null; 76 try { 77 CustomObjectInputStream objectIn = new CustomObjectInputStream(byteIn, session); 79 object = objectIn.readObject(); 80 } catch (Exception exception) { 81 throw DescriptorException.notDeserializable(getMapping(), exception); 82 } 83 84 return object; 85 } 86 87 91 public Object convertObjectValueToDataValue(Object attributeValue, Session session) { 92 ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 93 try { 94 ObjectOutputStream objectOut = new ObjectOutputStream(byteOut); 95 objectOut.writeObject(attributeValue); 96 objectOut.flush(); 97 } catch (IOException exception) { 98 throw DescriptorException.notSerializable(getMapping(), exception); 99 } 100 return byteOut.toByteArray(); 101 } 102 103 107 public void initialize(DatabaseMapping mapping, Session session) { 108 this.mapping = mapping; 109 if (getMapping().isDirectToFieldMapping()) { 111 AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping(); 112 113 if (directMapping.getFieldClassification() == null) { 115 directMapping.setFieldClassification(ClassConstants.APBYTE); 116 } 117 } 118 } 119 120 124 protected DatabaseMapping getMapping() { 125 return mapping; 126 } 127 128 134 public boolean isMutable() { 135 return true; 136 } 137 } 138 | Popular Tags |