KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
27 import oracle.toplink.essentials.sessions.Session;
28 import oracle.toplink.essentials.mappings.DatabaseMapping;
29 import oracle.toplink.essentials.exceptions.ValidationException;
30 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
31 import oracle.toplink.essentials.internal.security.PrivilegedClassForName;
32
33 /**
34  * <b>Purpose</b>: Object type converter is used to match a fixed number of
35  * database data values to a Java enum object value. It can be used when the
36  * values on the database and in the Java differ. To create an object type
37  * converter, simply specify the set of conversion value pairs. A default value
38  * and one-way conversion are also supported for legacy data situations.
39  *
40  * @author Guy Pelletier
41  * @since Toplink 10.1.4RI
42  */

43 public class EnumTypeConverter extends ObjectTypeConverter {
44     private Class JavaDoc m_enumClass;
45     private String JavaDoc m_enumClassName;
46     private boolean m_usesOrdinalValues;
47
48     /**
49      * PUBLIC:
50      */

51     public EnumTypeConverter(DatabaseMapping mapping, String JavaDoc enumClassName, boolean usesOrdinalValues) {
52         super(mapping);
53
54         m_enumClassName = enumClassName;
55         m_usesOrdinalValues = usesOrdinalValues;
56     }
57     
58     /**
59      * INTERNAL:
60      * Convert all the class-name-based settings in this converter to actual
61      * class-based settings. This method is used when converting a project
62      * that has been built with class names to a project with classes.
63      * @param classLoader
64      */

65     public void convertClassNamesToClasses(ClassLoader JavaDoc classLoader){
66         try {
67             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
68                 try {
69                     m_enumClass = (Class JavaDoc)AccessController.doPrivileged(new PrivilegedClassForName(m_enumClassName, true, classLoader));
70                 } catch (PrivilegedActionException JavaDoc exception) {
71                     throw ValidationException.classNotFoundWhileConvertingClassNames(m_enumClassName, exception.getException());
72                 }
73             } else {
74                 m_enumClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(m_enumClassName, true, classLoader);
75             }
76         } catch (ClassNotFoundException JavaDoc exception){
77             throw ValidationException.classNotFoundWhileConvertingClassNames(m_enumClassName, exception);
78         }
79     }
80     
81     /**
82      * INTERNAL:
83      */

84     public void initialize(DatabaseMapping mapping, Session session) {
85         Iterator<Enum JavaDoc> i = EnumSet.allOf(m_enumClass).iterator();
86         
87         while (i.hasNext()) {
88             Enum JavaDoc theEnum = i.next();
89             
90             if (m_usesOrdinalValues) {
91                 addConversionValue(theEnum.ordinal(), theEnum);
92             } else {
93                 addConversionValue(theEnum.name(), theEnum);
94             }
95         }
96         
97         super.initialize(mapping, session);
98     }
99     
100     /**
101      * PUBLIC:
102      * Returns true if this converter uses ordinal values for the enum
103      * conversion.
104      */

105     public boolean usesOrdinalValues() {
106         return m_usesOrdinalValues;
107     }
108 }
109
Popular Tags