KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > TypeHelperImpl


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.internal.parsing;
23
24 import java.lang.reflect.Field JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27
28 import oracle.toplink.essentials.descriptors.ClassDescriptor;
29 import oracle.toplink.essentials.mappings.AggregateMapping;
30 import oracle.toplink.essentials.mappings.DatabaseMapping;
31 import oracle.toplink.essentials.internal.sessions.AbstractSession;
32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
33 import oracle.toplink.essentials.internal.security.PrivilegedClassForName;
34 import oracle.toplink.essentials.internal.security.PrivilegedGetField;
35 import oracle.toplink.essentials.internal.helper.BasicTypeHelperImpl;
36
37 /**
38  * INTERNAL
39  * <p><b>Purpose</b>: Implement type helper methods specified by TypeHelper.
40  * This implementation uses Class instances to represent a type.
41  */

42 public class TypeHelperImpl
43     extends BasicTypeHelperImpl implements TypeHelper {
44
45     /** The session. */
46     private final AbstractSession session;
47
48     /** The class loader used to resolve type names. */
49     private final ClassLoader JavaDoc classLoader;
50
51     /** */
52     public TypeHelperImpl(AbstractSession session, ClassLoader JavaDoc classLoader) {
53         this.session = session;
54         this.classLoader = classLoader;
55     }
56     
57     /** */
58     public Object JavaDoc resolveTypeName(String JavaDoc typeName) {
59         try {
60             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
61                 try {
62                     return (Class JavaDoc)AccessController.doPrivileged(
63                         new PrivilegedClassForName(typeName, true, classLoader));
64                 } catch (PrivilegedActionException JavaDoc exception) {
65                     return null;
66                 }
67             } else {
68                 return PrivilegedAccessHelper.getClassForName(typeName, true, classLoader);
69             }
70         } catch (ClassNotFoundException JavaDoc ex) {
71             return null;
72         }
73     }
74
75     /** */
76     public Object JavaDoc resolveAttribute(Object JavaDoc ownerClass, String JavaDoc attribute) {
77         Class JavaDoc clazz = (Class JavaDoc)ownerClass;
78         DatabaseMapping mapping = resolveAttributeMapping((Class JavaDoc)ownerClass, attribute);
79         Object JavaDoc type = getType(mapping);
80         if ((type == null) && (mapping != null)) {
81             // mapping might not be initialized, so the type returned from the
82
// mapping might be null => check directly
83
Field JavaDoc field = null;
84             try {
85                 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
86                     try {
87                         field = (Field JavaDoc)AccessController.doPrivileged(
88                             new PrivilegedGetField(clazz, attribute, true));
89                     } catch (PrivilegedActionException JavaDoc exception) {
90                         // ignore
91
}
92                 } else {
93                     field = PrivilegedAccessHelper.getField(clazz, attribute, true);
94                 }
95                 if (field != null) {
96                     type = field.getType();
97                 }
98             } catch (NoSuchFieldException JavaDoc ex) {
99                 // ignore, return null
100
}
101         }
102         return type;
103     }
104     
105     /** */
106     public Object JavaDoc resolveSchema(String JavaDoc schemaName) {
107         ClassDescriptor descriptor = session.getDescriptorForAlias(schemaName);
108         return (descriptor != null) ? descriptor.getJavaClass() : null;
109     }
110
111     /** */
112     public String JavaDoc getTypeName(Object JavaDoc type) {
113         if (type == null) {
114             return null;
115         }
116         return ((Class JavaDoc)type).getName();
117     }
118
119     /** */
120     public Object JavaDoc resolveEnumConstant(Object JavaDoc type, String JavaDoc constant) {
121         Class JavaDoc clazz = (Class JavaDoc)type;
122         Object JavaDoc[] constants = clazz.getEnumConstants();
123         if (constants != null) {
124             for (int i = 0; i < constants.length; i++) {
125                 if (constant.equals(constants[i].toString())) {
126                     return constants[i];
127                 }
128             }
129         }
130         return null;
131     }
132
133     /** */
134     public boolean isEntityClass(Object JavaDoc type) {
135         return session.getDescriptor((Class JavaDoc)type) != null;
136     }
137
138     public boolean isEmbeddable(Object JavaDoc type) {
139         return session.getDescriptor((Class JavaDoc)type).isAggregateDescriptor();
140     }
141
142     public boolean isEmbeddedAttribute(Object JavaDoc ownerClass, String JavaDoc attribute) {
143         ClassDescriptor desc = session.getDescriptor((Class JavaDoc)ownerClass);
144         return desc.getMappingForAttributeName(attribute).isAggregateMapping();
145     }
146
147     // ===== Internal helper methods =====
148

149     /** */
150     private DatabaseMapping resolveAttributeMapping(Class JavaDoc ownerClass, String JavaDoc attribute) {
151         ClassDescriptor descriptor = session.getDescriptor(ownerClass);
152         return (descriptor==null) ? null : descriptor.getMappingForAttributeName(attribute);
153     }
154
155     /** */
156     private Object JavaDoc getType(DatabaseMapping mapping) {
157         if (mapping == null) {
158             return null;
159         }
160         Object JavaDoc type = null;
161         if (mapping.isForeignReferenceMapping()) {
162             ClassDescriptor descriptor = mapping.getReferenceDescriptor();
163             type = descriptor == null ? null : descriptor.getJavaClass();
164         } else if (mapping.isAggregateMapping()) {
165             type = ((AggregateMapping)mapping).getReferenceClass();
166         } else {
167             type = mapping.getAttributeClassification();
168         }
169         return type;
170     }
171
172 }
173
Popular Tags