001: package org.apache.ojb.odmg;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.Identity;
019: import org.apache.ojb.broker.core.proxy.IndirectionHandler;
020: import org.apache.ojb.broker.core.proxy.ProxyHelper;
021: import org.apache.ojb.broker.metadata.ClassDescriptor;
022: import org.apache.commons.lang.builder.ToStringBuilder;
023:
024: /**
025: * Helper object encapsulates common used object properties/states, help to reduce
026: * needless metadata calls.
027: *
028: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
029: * @version $Id: RuntimeObject.java,v 1.1.2.7 2005/12/21 22:29:21 tomdz Exp $
030: */
031: public final class RuntimeObject {
032: private final Object obj;
033: private Identity identity;
034: private final TransactionImpl tx;
035: private Boolean isNew;
036: private ClassDescriptor cld;
037: private IndirectionHandler handler;
038:
039: public RuntimeObject(final Object obj, final TransactionImpl tx) {
040: this .tx = tx;
041: this .obj = obj;
042: initCld(tx);
043: doIsNewObjectCheck(tx);
044: }
045:
046: public RuntimeObject(final Object obj, final TransactionImpl tx,
047: final boolean isNew) {
048: this .tx = tx;
049: this .obj = obj;
050: this .isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
051: initCld(tx);
052: }
053:
054: public RuntimeObject(final Object obj, final Identity identity,
055: final TransactionImpl tx, final boolean isNew) {
056: this .tx = tx;
057: this .obj = obj;
058: this .identity = identity;
059: this .isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
060: initCld(tx);
061: }
062:
063: public RuntimeObject(final Object obj, final Identity oid,
064: final ClassDescriptor cld, final boolean isNew,
065: final boolean isProxy) {
066: this .tx = null;
067: this .obj = obj;
068: this .identity = oid;
069: this .isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
070: this .cld = cld;
071: if (isProxy) {
072: this .handler = ProxyHelper.getIndirectionHandler(obj);
073: }
074: }
075:
076: /*
077: try to avoid needless and unused method calls to provide
078: best performance, thus create Identity object only if needed
079: and do 'is new object' check only if needed.
080: */
081: private void initCld(final TransactionImpl tx) {
082: final IndirectionHandler handler = ProxyHelper
083: .getIndirectionHandler(obj);
084: if (handler != null) {
085: this .handler = handler;
086: isNew = Boolean.FALSE;
087: identity = handler.getIdentity();
088: if (handler.alreadyMaterialized()) {
089: cld = tx.getBroker().getClassDescriptor(
090: handler.getRealSubject().getClass());
091: } else {
092: cld = tx.getBroker().getClassDescriptor(
093: identity.getObjectsRealClass());
094: }
095: } else {
096: cld = tx.getBroker().getClassDescriptor(obj.getClass());
097: }
098: }
099:
100: void doIsNewObjectCheck(final TransactionImpl tx) {
101: boolean isNew = tx.isTransient(cld, obj, null);
102: this .isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
103: }
104:
105: /**
106: * Return the associated persistent object.
107: */
108: public Object getObj() {
109: return obj;
110: }
111:
112: /**
113: * Returns the materialized object (if proxy is materialized or a "normal"
114: * persistent object) or <em>null</em> if associated with unmaterialized proxy object.
115: */
116: public Object getObjMaterialized() {
117: return handler != null ? (handler.alreadyMaterialized() ? handler
118: .getRealSubject()
119: : null)
120: : obj;
121: }
122:
123: /**
124: * Returns the associated object {@link org.apache.ojb.broker.Identity}.
125: */
126: public Identity getIdentity() {
127: if (identity == null) {
128: identity = tx.getBroker().serviceIdentity().buildIdentity(
129: obj);
130: }
131: return identity;
132: }
133:
134: /**
135: * Returns the associated object {@link org.apache.ojb.broker.metadata.ClassDescriptor}.
136: */
137: public ClassDescriptor getCld() {
138: return cld;
139: }
140:
141: /**
142: * Returns <code>true</code> if the represented object is
143: * not yet persisted.
144: */
145: public boolean isNew() {
146: return isNew.booleanValue();
147: }
148:
149: public boolean isProxy() {
150: return handler != null;
151: }
152:
153: public IndirectionHandler getHandler() {
154: return handler;
155: }
156:
157: public String toString() {
158: return new ToStringBuilder(this ).append("identity", identity)
159: .append("isNew", isNew).append("isProxy",
160: handler != null).append("handler", handler)
161: .append("tx", tx).toString();
162: }
163: }
|