KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > mappings > converters > SerializedObjectConverter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
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 /**
33  * <p><b>Purpose</b>: The serialized object converter can be used to store an arbitrary object or set of objects into a database blob field.
34  * It uses the Java serializer so the target must be serializable.
35  *
36  * @author James Sutherland
37  * @since OracleAS TopLink 10<i>g</i> (10.0.3)
38  */

39 public class SerializedObjectConverter implements Converter {
40     protected DatabaseMapping mapping;
41
42     /**
43      * PUBLIC:
44      * Default constructor.
45      */

46     public SerializedObjectConverter() {
47     }
48
49     /**
50      * PUBLIC:
51      * Default constructor.
52      */

53     public SerializedObjectConverter(DatabaseMapping mapping) {
54         this.mapping = mapping;
55     }
56
57     /**
58      * INTERNAL:
59      * The fieldValue will be a byte array. Create a ByteArrayInputStream
60      * on the fieldValue. Create an ObjectInputStream on the ByteArrayInputStream
61      * to read in the objects.
62      */

63     public Object JavaDoc convertDataValueToObjectValue(Object JavaDoc 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 JavaDoc object = null;
76         try {
77             // BUG# 2813583
78
CustomObjectInputStream objectIn = new CustomObjectInputStream(byteIn, session);
79             object = objectIn.readObject();
80         } catch (Exception JavaDoc exception) {
81             throw DescriptorException.notDeserializable(getMapping(), exception);
82         }
83
84         return object;
85     }
86
87     /**
88      * INTERNAL:
89      * Convert the object to a byte array through serialize.
90      */

91     public Object JavaDoc convertObjectValueToDataValue(Object JavaDoc 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     /**
104      * INTERNAL:
105      * Set the mapping.
106      */

107     public void initialize(DatabaseMapping mapping, Session session) {
108         this.mapping = mapping;
109         // CR#... Mapping must also have the field classification.
110
if (getMapping().isDirectToFieldMapping()) {
111             AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping();
112
113             // Allow user to specify field type to override computed value. (i.e. blob, nchar)
114
if (directMapping.getFieldClassification() == null) {
115                 directMapping.setFieldClassification(ClassConstants.APBYTE);
116             }
117         }
118     }
119
120     /**
121      * INTERNAL:
122      * Return the mapping.
123      */

124     protected DatabaseMapping getMapping() {
125         return mapping;
126     }
127
128     /**
129      * INTERNAL:
130      * If the converter converts the value to a non-atomic value, i.e.
131      * a value that can have its' parts changed without being replaced,
132      * then it must return false, serialization can be non-atomic.
133      */

134     public boolean isMutable() {
135         return true;
136     }
137 }
138
Popular Tags