KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.mappings.converters;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
26
27 import oracle.toplink.essentials.mappings.*;
28 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping;
29 import oracle.toplink.essentials.sessions.*;
30 import oracle.toplink.essentials.exceptions.ConversionException;
31 import oracle.toplink.essentials.exceptions.ValidationException;
32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
33 import oracle.toplink.essentials.internal.security.PrivilegedClassForName;
34 import oracle.toplink.essentials.internal.sessions.AbstractSession;
35
36 /**
37  * <b>Purpose</b>: Type conversion converters are used to explicitly map a database type to a
38  * Java type.
39  *
40  * @author James Sutherland
41  * @since OracleAS TopLink 10<i>g</i> (10.0.3)
42  */

43 public class TypeConversionConverter implements Converter {
44     protected DatabaseMapping mapping;
45
46     /** Field type */
47     protected Class JavaDoc dataClass;
48     protected String JavaDoc dataClassName;
49
50     /** Object type */
51     protected Class JavaDoc objectClass;
52     protected String JavaDoc objectClassName;
53
54     /**
55      * PUBLIC:
56      * Default constructor.
57      */

58     public TypeConversionConverter() {
59     }
60
61     /**
62      * PUBLIC:
63      * Default constructor.
64      */

65     public TypeConversionConverter(DatabaseMapping mapping) {
66         this.mapping = mapping;
67     }
68
69     /**
70      * INTERNAL:
71      * Convert all the class-name-based settings in this converter to actual class-based
72      * settings. This method is used when converting a project that has been built
73      * with class names to a project with classes.
74      * This method is implemented by subclasses as necessary.
75      * @param classLoader
76      */

77     public void convertClassNamesToClasses(ClassLoader JavaDoc classLoader){
78         Class JavaDoc dataClass = null;
79         Class JavaDoc objectClass = null;
80         try{
81             if (dataClassName != null){
82                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
83                     try {
84                         dataClass = (Class JavaDoc)AccessController.doPrivileged(new PrivilegedClassForName(dataClassName, true, classLoader));
85                     } catch (PrivilegedActionException JavaDoc exception) {
86                         throw ValidationException.classNotFoundWhileConvertingClassNames(dataClassName, exception.getException());
87                     }
88                 } else {
89                     dataClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(dataClassName, true, classLoader);
90                 }
91                 setDataClass(dataClass);
92             }
93         } catch (ClassNotFoundException JavaDoc exc){
94             throw ValidationException.classNotFoundWhileConvertingClassNames(dataClassName, exc);
95         }
96         try {
97             if (objectClassName != null){
98                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
99                     try {
100                         objectClass = (Class JavaDoc)AccessController.doPrivileged(new PrivilegedClassForName(objectClassName, true, classLoader));
101                     } catch (PrivilegedActionException JavaDoc exception) {
102                         throw ValidationException.classNotFoundWhileConvertingClassNames(objectClassName, exception.getException());
103                     }
104                 } else {
105                     objectClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(objectClassName, true, classLoader);
106                 }
107                 setObjectClass(objectClass);
108             }
109         } catch (ClassNotFoundException JavaDoc exc){
110             throw ValidationException.classNotFoundWhileConvertingClassNames(objectClassName, exc);
111         }
112     };
113
114     /**
115      * INTERNAL:
116      * The field value must first be converted to the field type, then the attribute type.
117      */

118     public Object JavaDoc convertDataValueToObjectValue(Object JavaDoc fieldValue, Session session) {
119         Object JavaDoc attributeValue = fieldValue;
120         if (attributeValue != null) {
121             try {
122                 attributeValue = ((AbstractSession)session).getDatasourcePlatform().convertObject(attributeValue, getDataClass());
123             } catch (ConversionException e) {
124                 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e);
125             }
126
127             try {
128                 attributeValue = ((AbstractSession)session).getDatasourcePlatform().convertObject(attributeValue, getObjectClass());
129             } catch (ConversionException e) {
130                 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e);
131             }
132         }
133
134         return attributeValue;
135     }
136
137     /**
138      * PUBLIC:
139      * Returns the class type of the object value.
140      */

141     public Class JavaDoc getObjectClass() {
142         return objectClass;
143     }
144
145     /**
146      * INTERNAL:
147      * Return the name of the object type for the MW usage.
148      */

149     public String JavaDoc getObjectClassName() {
150         if ((objectClassName == null) && (objectClass != null)) {
151             objectClassName = objectClass.getName();
152         }
153         return objectClassName;
154     }
155
156     /**
157      * PUBLIC:
158      * Returns the class type of the data value.
159      */

160     public Class JavaDoc getDataClass() {
161         return dataClass;
162     }
163
164     /**
165      * INTERNAL:
166      * Return the name of the data type for the MW usage.
167      */

168     public String JavaDoc getDataClassName() {
169         if ((dataClassName == null) && (dataClass != null)) {
170             dataClassName = dataClass.getName();
171         }
172         return dataClassName;
173     }
174
175     /**
176      * PUBLIC:
177      * Set the class type of the data value.
178      */

179     public void setDataClass(Class JavaDoc dataClass) {
180         this.dataClass = dataClass;
181     }
182
183     /**
184      * INTERNAL:
185      * Set the name of the data type for the MW usage.
186      */

187     public void setDataClassName(String JavaDoc dataClassName) {
188         this.dataClassName = dataClassName;
189     }
190
191     /**
192      * PUBLIC:
193      * Set the class type of the object value.
194      */

195     public void setObjectClass(Class JavaDoc objectClass) {
196         this.objectClass = objectClass;
197     }
198
199     /**
200      * INTERNAL:
201      * Set the name of the object type for the MW usage.
202      */

203     public void setObjectClassName(String JavaDoc objectClassName) {
204         this.objectClassName = objectClassName;
205     }
206
207     /**
208      * INTERNAL:
209      * Convert to the field class.
210      */

211     public Object JavaDoc convertObjectValueToDataValue(Object JavaDoc attributeValue, Session session) {
212         try {
213             return ((AbstractSession)session).getDatasourcePlatform().convertObject(attributeValue, getDataClass());
214         } catch (ConversionException e) {
215             throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e);
216         }
217     }
218
219     /**
220      * INTERNAL:
221      * Set the mapping.
222      */

223     public void initialize(DatabaseMapping mapping, Session session) {
224         this.mapping = mapping;
225         // CR#... Mapping must also have the field classification.
226
if (getMapping().isDirectToFieldMapping()) {
227             AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping();
228
229             // Allow user to specify field type to override computed value. (i.e. blob, nchar)
230
if (directMapping.getFieldClassification() == null) {
231                 directMapping.setFieldClassification(getDataClass());
232             }
233             
234             // Set the object class from the attribute, if null.
235
if (getObjectClass() == null) {
236                 setObjectClass(directMapping.getAttributeClassification());
237             }
238         }
239     }
240
241     /**
242      * INTERNAL:
243      * Return the mapping.
244      */

245     protected DatabaseMapping getMapping() {
246         return mapping;
247     }
248
249     /**
250      * INTERNAL:
251      * If the converter converts the value to a non-atomic value, i.e.
252      * a value that can have its' parts changed without being replaced,
253      * then it must return false, serialization can be non-atomic.
254      */

255     public boolean isMutable() {
256         return false;
257     }
258 }
259
Popular Tags