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.indirection;
038:
039: import java.io.*;
040: import oracle.toplink.essentials.internal.helper.*;
041: import oracle.toplink.essentials.indirection.*;
042: import oracle.toplink.essentials.exceptions.*;
043: import oracle.toplink.essentials.internal.localization.*;
044: import oracle.toplink.essentials.internal.sessions.AbstractRecord;
045: import oracle.toplink.essentials.internal.sessions.AbstractSession;
046:
047: /**
048: * DatabaseValueHolder wraps a database-stored object and implements
049: * behavior to access it. The object is read only once from database
050: * after which is cached for faster access.
051: *
052: * @see ValueHolderInterface
053: * @author Dorin Sandu
054: */
055: public abstract class DatabaseValueHolder implements
056: WeavedAttributeValueHolderInterface, Cloneable, Serializable {
057:
058: /** Stores the object after it is read from the database. */
059: protected Object value;
060:
061: /** Indicates whether the object has been read from the database or not. */
062: protected boolean isInstantiated;
063:
064: /** Stores the session for the database that contains the object. */
065: protected transient AbstractSession session;
066:
067: /** Stores the row representation of the object. */
068: protected AbstractRecord row;
069:
070: /**
071: * The variable below is used as part of the implementation of WeavedAttributeValueHolderInterface
072: * It is used to track whether a valueholder that has been weaved into a class is coordinated
073: * with the underlying property
074: * Set internally in TopLink when the state of coordination between a weaved valueholder and the underlying property is known
075: */
076: protected boolean isCoordinatedWithProperty = false;
077:
078: public Object clone() {
079: try {
080: return super .clone();
081: } catch (CloneNotSupportedException exception) {
082: throw new InternalError();
083: }
084: }
085:
086: /**
087: * Return the row.
088: */
089: public AbstractRecord getRow() {
090: return row;
091: }
092:
093: /**
094: * Return the session.
095: */
096: public AbstractSession getSession() {
097: return session;
098: }
099:
100: /**
101: * Return the object.
102: */
103: public synchronized Object getValue() {
104: if (!isInstantiated()) {
105: // The value must be set directly because the setValue can also cause instatiation under UOW.
106: privilegedSetValue(instantiate());
107: setInstantiated();
108: resetFields();
109: }
110: return value;
111: }
112:
113: /**
114: * Instantiate the object.
115: */
116: protected abstract Object instantiate() throws DatabaseException;
117:
118: /**
119: * Triggers UnitOfWork valueholders directly without triggering the wrapped
120: * valueholder (this).
121: * <p>
122: * When in transaction and/or for pessimistic locking the UnitOfWorkValueHolder
123: * needs to be triggered directly without triggering the wrapped valueholder.
124: * However only the wrapped valueholder knows how to trigger the indirection,
125: * i.e. it may be a batchValueHolder, and it stores all the info like the row
126: * and the query.
127: * Note: Implementations of this method are not necessarily thread-safe. They must
128: * be used in a synchronizaed manner
129: */
130: public abstract Object instantiateForUnitOfWorkValueHolder(
131: UnitOfWorkValueHolder unitOfWorkValueHolder);
132:
133: /**
134: * This method is used as part of the implementation of WeavedAttributeValueHolderInterface
135: * It is used to check whether a valueholder that has been weaved into a class is coordinated
136: * with the underlying property
137: */
138: public boolean isCoordinatedWithProperty() {
139: return isCoordinatedWithProperty;
140: }
141:
142: /**
143: * This method is used as part of the implementation of WeavedAttributeValueHolderInterface.
144: *
145: * A DatabaseValueHolder is set up by TopLink and will never be a newly weaved valueholder.
146: * As a result, this method is stubbed out.
147: */
148: public boolean isNewlyWeavedValueHolder() {
149: return false;
150: }
151:
152: /**
153: * INTERNAL:
154: * Answers if this valueholder is easy to instantiate.
155: * @return true if getValue() won't trigger a database read.
156: */
157: public boolean isEasilyInstantiated() {
158: return isInstantiated();
159: }
160:
161: /**
162: * Return a boolean indicating whether the object
163: * has been read from the database or not.
164: */
165: public boolean isInstantiated() {
166: return isInstantiated;
167: }
168:
169: /**
170: * Answers if this valueholder is a pessimistic locking one. Such valueholders
171: * are special in that they can be triggered multiple times by different
172: * UnitsOfWork. Each time a lock query will be issued. Hence even if
173: * instantiated it may have to be instantiated again, and once instantatiated
174: * all fields can not be reset.
175: * Note: Implementations of this method are not necessarily thread-safe. They must
176: * be used in a synchronizaed manner
177: */
178: public abstract boolean isPessimisticLockingValueHolder();
179:
180: /**
181: * Answers if this valueholder is referenced only by a UnitOfWork valueholder.
182: * I.e. it was built in valueFromRow which was called by buildCloneFromRow.
183: * <p>
184: * Sometimes in transaction a UnitOfWork clone, and all valueholders, are built
185: * directly from the row; however a UnitOfWorkValueHolder does not know how to
186: * instantiate itself so wraps this which does.
187: * <p>
188: * On a successful merge must be released to the session cache with
189: * releaseWrappedValueHolder.
190: */
191: protected boolean isTransactionalValueHolder() {
192: return ((session != null) && session.isUnitOfWork());
193: }
194:
195: /**
196: * Used to determine if this is a remote uow value holder that was serialized to the server.
197: * It has no reference to its wrapper value holder, so must find its original object to be able to instantiate.
198: */
199: public boolean isSerializedRemoteUnitOfWorkValueHolder() {
200: return false;
201: }
202:
203: /**
204: * Set the object. This is used only by the privileged methods. One must be very careful in using this method.
205: */
206: public void privilegedSetValue(Object value) {
207: this .value = value;
208: isCoordinatedWithProperty = false;
209: }
210:
211: /**
212: * Releases a wrapped valueholder privately owned by a particular unit of work.
213: * <p>
214: * When unit of work clones are built directly from rows no object in the shared
215: * cache points to this valueholder, so it can store the unit of work as its
216: * session. However once that UnitOfWork commits and the valueholder is merged
217: * into the shared cache, the session needs to be reset to the root session, ie.
218: * the server session.
219: */
220: public void releaseWrappedValueHolder() {
221: AbstractSession session = getSession();
222: if ((session != null) && session.isUnitOfWork()) {
223: setSession(session.getRootSession(null));
224: }
225: }
226:
227: /**
228: * Reset all the fields that are not needed after instantiation.
229: */
230: protected void resetFields() {
231: setRow(null);
232: setSession(null);
233: }
234:
235: /**
236: * This method is used as part of the implementation of WeavedAttributeValueHolderInterface
237: * It is used internally by TopLink to set whether a valueholder that has been weaved into a class is coordinated
238: * with the underlying property
239: */
240: public void setIsCoordinatedWithProperty(boolean coordinated) {
241: this .isCoordinatedWithProperty = coordinated;
242: }
243:
244: /**
245: * This method is used as part of the implementation of WeavedAttributeValueHolderInterface
246: *
247: * A DatabaseValueHolder is set up by TopLink and will never be a newly weaved valueholder
248: * As a result, this method is stubbed out.
249: */
250: public void setIsNewlyWeavedValueHolder(boolean isNew) {
251: }
252:
253: /**
254: * Set the instantiated flag to true.
255: */
256: public void setInstantiated() {
257: isInstantiated = true;
258: }
259:
260: /**
261: * Set the row.
262: */
263: public void setRow(AbstractRecord row) {
264: this .row = row;
265: }
266:
267: /**
268: * Set the session.
269: */
270: public void setSession(AbstractSession session) {
271: this .session = session;
272: }
273:
274: /**
275: * Set the instantiated flag to false.
276: */
277: public void setUninstantiated() {
278: isInstantiated = false;
279: }
280:
281: /**
282: * Set the object.
283: */
284: public void setValue(Object value) {
285: this .value = value;
286: setInstantiated();
287: }
288:
289: public String toString() {
290: if (isInstantiated()) {
291: return "{" + getValue() + "}";
292: } else {
293: return "{"
294: + Helper.getShortClassName(getClass())
295: + ": "
296: + ToStringLocalization.buildMessage(
297: "not_instantiated", (Object[]) null) + "}";
298: }
299: }
300: }
|