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:
038: package oracle.toplink.essentials.internal.indirection;
039:
040: import oracle.toplink.essentials.exceptions.DescriptorException;
041: import oracle.toplink.essentials.indirection.ValueHolder;
042: import oracle.toplink.essentials.indirection.ValueHolderInterface;
043: import oracle.toplink.essentials.indirection.WeavedAttributeValueHolderInterface;
044: import oracle.toplink.essentials.internal.helper.ClassConstants;
045: import oracle.toplink.essentials.internal.helper.ConversionManager;
046: import oracle.toplink.essentials.internal.helper.Helper;
047: import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper;
048: import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker;
049: import oracle.toplink.essentials.internal.sessions.AbstractRecord;
050: import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
051: import oracle.toplink.essentials.mappings.DatabaseMapping;
052: import oracle.toplink.essentials.mappings.ForeignReferenceMapping;
053:
054: import java.lang.reflect.InvocationTargetException;
055: import java.lang.reflect.Method;
056: import java.security.AccessController;
057: import java.security.PrivilegedActionException;
058:
059: /**
060: * INTERNAL:
061: * A WeavedObjectBasicIndirectionPolicy is used by OneToOne mappings that are LAZY through weaving
062: * and which use Property(method) access.
063: *
064: * It extends BasicIndirection by providing the capability of calling the set method that was initially
065: * mapped in addition to the set method for the weaved valueholder in order to coordinate the value of the
066: * underlying property with the value stored in the valueholder
067: *
068: * @author Tom Ware
069: *
070: */
071: public class WeavedObjectBasicIndirectionPolicy extends
072: BasicIndirectionPolicy {
073:
074: protected String setMethodName = null; // name of the initial set method.
075: protected Method setMethod = null; // lazily initialized set method based on the set method name
076:
077: public WeavedObjectBasicIndirectionPolicy(String setMethodName) {
078: super ();
079: this .setMethodName = setMethodName;
080: }
081:
082: /**
083: * INTERNAL:
084: * Return the "real" attribute value, as opposed to any wrapper.
085: * This will trigger the wrapper to instantiate the value. In a weaved policy, this will
086: * also call the initial setter method to coordinate the values of the valueholder with
087: * the underlying data
088: *
089: */
090: public Object getRealAttributeValueFromObject(Object object,
091: Object attribute) {
092: Object value = super .getRealAttributeValueFromObject(object,
093: attribute);
094: // Provide the indirection policy with a callback that allows it to do any updates it needs as the result of getting the value
095: updateValueInObject(object, value, attribute);
096: return value;
097: }
098:
099: /**
100: * This method will lazily initialize the set method
101: * Lazy initialization occurs to that we are not required to have a handle on
102: * the actual class that we are using until runtime. This helps to satisfy the
103: * weaving requirement that demands that we avoid loading domain classes into
104: * the main class loader until after weaving occurs.
105: * @return
106: */
107: protected Method getSetMethod() {
108: if (setMethod == null) {
109: ForeignReferenceMapping sourceMapping = (ForeignReferenceMapping) mapping;
110: // The parameter type for the set method must always be the return type of the get method.
111: Class[] parameterTypes = new Class[1];
112: parameterTypes[0] = sourceMapping.getReferenceClass();
113: try {
114: setMethod = Helper.getDeclaredMethod(sourceMapping
115: .getDescriptor().getJavaClass(), setMethodName,
116: parameterTypes);
117: } catch (NoSuchMethodException e) {
118: throw DescriptorException
119: .errorAccessingSetMethodOfEntity(sourceMapping
120: .getDescriptor().getJavaClass(),
121: setMethodName, sourceMapping
122: .getDescriptor(), e);
123: }
124: }
125: return setMethod;
126: }
127:
128: /**
129: * Coordinate the valueholder for this mapping with the underlying property by calling the
130: * initial setter method
131: */
132: public void updateValueInObject(Object object, Object value,
133: Object attributeValue) {
134: setRealAttributeValueInObject(object, value);
135: ((WeavedAttributeValueHolderInterface) attributeValue)
136: .setIsCoordinatedWithProperty(true);
137: }
138:
139: /**
140: * INTERNAL:
141: * Set the value of the appropriate attribute of target to attributeValue.
142: * In this case, place the value inside the target's ValueHolder.
143: */
144: public void setRealAttributeValueInObject(Object target,
145: Object attributeValue) {
146: Object[] parameters = new Object[1];
147: parameters[0] = attributeValue;
148: try {
149: if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()) {
150: try {
151: AccessController
152: .doPrivileged(new PrivilegedMethodInvoker(
153: getSetMethod(), target, parameters));
154: } catch (PrivilegedActionException exception) {
155: Exception throwableException = exception
156: .getException();
157: if (throwableException instanceof IllegalAccessException) {
158: throw DescriptorException
159: .illegalAccessWhileSettingValueThruMethodAccessor(
160: setMethod.getName(),
161: attributeValue,
162: throwableException);
163: } else {
164: throw DescriptorException
165: .targetInvocationWhileSettingValueThruMethodAccessor(
166: setMethod.getName(),
167: attributeValue,
168: throwableException);
169: }
170: }
171: } else {
172: PrivilegedAccessHelper.invokeMethod(getSetMethod(),
173: target, parameters);
174: }
175: } catch (IllegalAccessException exception) {
176: throw DescriptorException
177: .illegalAccessWhileSettingValueThruMethodAccessor(
178: setMethod.getName(), attributeValue,
179: exception);
180: } catch (IllegalArgumentException exception) {
181: throw DescriptorException
182: .illegalArgumentWhileSettingValueThruMethodAccessor(
183: setMethod.getName(), attributeValue,
184: exception);
185: } catch (InvocationTargetException exception) {
186: throw DescriptorException
187: .targetInvocationWhileSettingValueThruMethodAccessor(
188: setMethod.getName(), attributeValue,
189: exception);
190: }
191: }
192: }
|