001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.parsing;
038:
039: import java.lang.reflect.Field;
040: import java.security.AccessController;
041: import java.security.PrivilegedActionException;
042:
043: import oracle.toplink.essentials.descriptors.ClassDescriptor;
044: import oracle.toplink.essentials.mappings.AggregateMapping;
045: import oracle.toplink.essentials.mappings.DatabaseMapping;
046: import oracle.toplink.essentials.internal.sessions.AbstractSession;
047: import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
048: import oracle.toplink.essentials.internal.security.PrivilegedClassForName;
049: import oracle.toplink.essentials.internal.security.PrivilegedGetField;
050: import oracle.toplink.essentials.internal.helper.BasicTypeHelperImpl;
051:
052: /**
053: * INTERNAL
054: * <p><b>Purpose</b>: Implement type helper methods specified by TypeHelper.
055: * This implementation uses Class instances to represent a type.
056: */
057: public class TypeHelperImpl extends BasicTypeHelperImpl implements
058: TypeHelper {
059:
060: /** The session. */
061: private final AbstractSession session;
062:
063: /** The class loader used to resolve type names. */
064: private final ClassLoader classLoader;
065:
066: /** */
067: public TypeHelperImpl(AbstractSession session,
068: ClassLoader classLoader) {
069: this .session = session;
070: this .classLoader = classLoader;
071: }
072:
073: /** Returns a type representation for the specified type name or null if
074: * there is no such type.
075: */
076: public Object resolveTypeName(String typeName) {
077: try {
078: if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
079: try {
080: return (Class) AccessController
081: .doPrivileged(new PrivilegedClassForName(
082: typeName, true, classLoader));
083: } catch (PrivilegedActionException exception) {
084: return null;
085: }
086: } else {
087: return PrivilegedAccessHelper.getClassForName(typeName,
088: true, classLoader);
089: }
090: } catch (ClassNotFoundException ex) {
091: return null;
092: }
093: }
094:
095: /** Returns the type of the attribute with the specified name in the
096: * specified owner class.
097: */
098: public Object resolveAttribute(Object ownerClass, String attribute) {
099: DatabaseMapping mapping = resolveAttributeMapping(ownerClass,
100: attribute);
101: return getType(mapping);
102: }
103:
104: /** Returns the type of the class corresponding to the spcified abstract
105: * schema type.
106: */
107: public Object resolveSchema(String schemaName) {
108: ClassDescriptor descriptor = session
109: .getDescriptorForAlias(schemaName);
110: return (descriptor != null) ? descriptor.getJavaClass() : null;
111: }
112:
113: /** Returns the enum constant if the specified type denotes an enum type
114: * and the specified constant denotes a constant of the enum type.
115: */
116: public Object resolveEnumConstant(Object type, String constant) {
117: Class clazz = getJavaClass(type);
118: Object[] constants = clazz.getEnumConstants();
119: if (constants != null) {
120: for (int i = 0; i < constants.length; i++) {
121: if (constant.equals(constants[i].toString())) {
122: return constants[i];
123: }
124: }
125: }
126: return null;
127: }
128:
129: /** Returns true if the specified type denotes an entity class. */
130: public boolean isEntityClass(Object type) {
131: ClassDescriptor desc = getDescriptor(type);
132: return (desc != null) && !desc.isAggregateDescriptor();
133: }
134:
135: /** Returns true if the specified type denotes an embedded class. */
136: public boolean isEmbeddable(Object type) {
137: ClassDescriptor desc = getDescriptor(type);
138: return (desc != null) && desc.isAggregateDescriptor();
139: }
140:
141: /** Returns true if the specified type denotes an embedded attribute. */
142: public boolean isEmbeddedAttribute(Object ownerClass,
143: String attribute) {
144: DatabaseMapping mapping = resolveAttributeMapping(ownerClass,
145: attribute);
146: return (mapping != null) && mapping.isAggregateMapping();
147: }
148:
149: /** Returns true if the specified type denotes a simple state attribute. */
150: public boolean isSimpleStateAttribute(Object ownerClass,
151: String attribute) {
152: DatabaseMapping mapping = resolveAttributeMapping(ownerClass,
153: attribute);
154: return (mapping != null) && mapping.isDirectToFieldMapping();
155: }
156:
157: /** Returns true if the specified attribute denotes a single valued
158: * or collection valued relationship attribute.
159: */
160: public boolean isRelationship(Object ownerClass, String attribute) {
161: DatabaseMapping mapping = resolveAttributeMapping(ownerClass,
162: attribute);
163: return (mapping != null)
164: && (mapping.isObjectReferenceMapping()
165: || mapping.isOneToManyMapping() || mapping
166: .isManyToManyMapping());
167: }
168:
169: /** Returns true if the specified attribute denotes a single valued
170: * relationship attribute.
171: */
172: public boolean isSingleValuedRelationship(Object ownerClass,
173: String attribute) {
174: DatabaseMapping mapping = resolveAttributeMapping(ownerClass,
175: attribute);
176: return (mapping != null) && mapping.isObjectReferenceMapping();
177: }
178:
179: /** Returns true if the specified attribute denotes a collection valued
180: * relationship attribute.
181: */
182: public boolean isCollectionValuedRelationship(Object ownerClass,
183: String attribute) {
184: DatabaseMapping mapping = resolveAttributeMapping(ownerClass,
185: attribute);
186: return (mapping != null)
187: && (mapping.isOneToManyMapping() || mapping
188: .isManyToManyMapping());
189: }
190:
191: // ===== Internal helper methods =====
192:
193: /** Returns the class descriptor if the specified non-null type is a class
194: * object.
195: */
196: private ClassDescriptor getDescriptor(Object type) {
197: ClassDescriptor desc = null;
198: if (type instanceof Class) {
199: desc = session.getDescriptor((Class) type);
200: } else if (type instanceof ClassDescriptor) {
201: desc = (ClassDescriptor) type;
202: }
203: return desc;
204: }
205:
206: /** Returns the mapping for the specified attribute of the psecified
207: * class. The method returns null if the class is not known or if the
208: * class does not have an attribute of the specified name.
209: */
210: private DatabaseMapping resolveAttributeMapping(Object ownerClass,
211: String attr) {
212: ClassDescriptor desc = getDescriptor(ownerClass);
213: return (desc == null) ? null : desc
214: .getMappingForAttributeName(attr);
215: }
216:
217: /** */
218: private Object getType(DatabaseMapping mapping) {
219: if (mapping == null) {
220: return null;
221: }
222: Object type = null;
223: if (mapping.isForeignReferenceMapping()) {
224: ClassDescriptor descriptor = mapping
225: .getReferenceDescriptor();
226: type = descriptor == null ? null : descriptor
227: .getJavaClass();
228: } else if (mapping.isAggregateMapping()) {
229: // Return the ClassDescriptor as the type representation in case
230: // of an embeeded. This makese sure that any property or field
231: // access of the embedded uses the correct mapping information and
232: // not the shell of the descriptors as stored by the session.
233: type = ((AggregateMapping) mapping)
234: .getReferenceDescriptor();
235: } else {
236: type = mapping.getAttributeClassification();
237: }
238: return type;
239: }
240:
241: }
|