KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.helper.*;
29 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping;
30 import oracle.toplink.essentials.sessions.*;
31 import oracle.toplink.essentials.exceptions.*;
32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
33 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass;
34 import oracle.toplink.essentials.internal.sessions.AbstractSession;
35
36 /**
37  * <b>Purpose</b>: Allows a class name to be converter to and from a new instance of the class.
38  *
39  * @author James Sutherland
40  * @since OracleAS TopLink 10<i>g</i> (10.0.3)
41  */

42 public class ClassInstanceConverter implements Converter {
43     protected DatabaseMapping mapping;
44
45     /**
46      * PUBLIC:
47      * Default constructor.
48      */

49     public ClassInstanceConverter() {
50     }
51
52     /**
53      * INTERNAL:
54      * Convert the class name to a class, then create an instance of the class.
55      */

56     public Object JavaDoc convertDataValueToObjectValue(Object JavaDoc fieldValue, Session session) {
57         Object JavaDoc attributeValue = null;
58         if (fieldValue != null) {
59             Class JavaDoc attributeClass = (Class JavaDoc)((AbstractSession)session).getDatasourcePlatform().convertObject(fieldValue, ClassConstants.CLASS);
60             try {
61                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
62                     try {
63                         attributeValue = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(attributeClass));
64                     } catch (PrivilegedActionException JavaDoc exception) {
65                         throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception.getException());
66                     }
67                 } else {
68                     attributeValue = PrivilegedAccessHelper.newInstanceFromClass(attributeClass);
69                 }
70             } catch (Exception JavaDoc exception) {
71                 throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception);
72             }
73         }
74
75         return attributeValue;
76     }
77
78     /**
79      * INTERNAL:
80      * Convert to the field class.
81      */

82     public Object JavaDoc convertObjectValueToDataValue(Object JavaDoc attributeValue, Session session) {
83         if (attributeValue == null) {
84             return null;
85         }
86         return attributeValue.getClass().getName();
87     }
88
89     /**
90      * INTERNAL:
91      * Set the mapping.
92      */

93     public void initialize(DatabaseMapping mapping, Session session) {
94         this.mapping = mapping;
95         // CR#... Mapping must also have the field classification.
96
if (getMapping().isDirectToFieldMapping()) {
97             AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping();
98
99             // Allow user to specify field type to override computed value. (i.e. blob, nchar)
100
if (directMapping.getFieldClassification() == null) {
101                 directMapping.setFieldClassification(ClassConstants.STRING);
102             }
103         }
104     }
105
106     /**
107      * INTERNAL:
108      * Return the mapping.
109      */

110     protected DatabaseMapping getMapping() {
111         return mapping;
112     }
113
114     /**
115      * INTERNAL:
116      * If the converter converts the value to a non-atomic value, i.e.
117      * a value that can have its' parts changed without being replaced,
118      * then it must return false, serialization can be non-atomic.
119      */

120     public boolean isMutable() {
121         return false;
122     }
123 }
124
Popular Tags