View Javadoc

1   package org.apache.ojb.odmg;
2   
3   /* Copyright 2002-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import org.apache.ojb.broker.Identity;
19  import org.apache.ojb.broker.core.proxy.IndirectionHandler;
20  import org.apache.ojb.broker.core.proxy.ProxyHelper;
21  import org.apache.ojb.broker.metadata.ClassDescriptor;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  
24  /**
25   * Helper object encapsulates common used object properties/states, help to reduce
26   * needless metadata calls.
27   *
28   * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
29   * @version $Id: RuntimeObject.java,v 1.1 2007-08-24 22:17:37 ewestfal Exp $
30   */
31  public final class RuntimeObject
32  {
33      private final Object obj;
34      private Identity identity;
35      private final TransactionImpl tx;
36      private Boolean isNew;
37      private ClassDescriptor cld;
38      private IndirectionHandler handler;
39  
40      public RuntimeObject(final Object obj, final TransactionImpl tx)
41      {
42          this.tx = tx;
43          this.obj = obj;
44          initCld(tx);
45          doIsNewObjectCheck(tx);
46      }
47  
48      public RuntimeObject(final Object obj, final TransactionImpl tx, final boolean isNew)
49      {
50          this.tx = tx;
51          this.obj = obj;
52          this.isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
53          initCld(tx);
54      }
55  
56      public RuntimeObject(final Object obj, final Identity identity, final TransactionImpl tx, final boolean isNew)
57      {
58          this.tx = tx;
59          this.obj = obj;
60          this.identity = identity;
61          this.isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
62          initCld(tx);
63      }
64  
65      public RuntimeObject(final Object obj, final Identity oid, final ClassDescriptor cld, final boolean isNew, final boolean isProxy)
66      {
67          this.tx = null;
68          this.obj = obj;
69          this.identity = oid;
70          this.isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
71          this.cld = cld;
72          if(isProxy)
73          {
74              this.handler = ProxyHelper.getIndirectionHandler(obj);
75          }
76      }
77  
78      /*
79      try to avoid needless and unused method calls to provide
80      best performance, thus create Identity object only if needed
81      and do 'is new object' check only if needed.
82      */
83      private void initCld(final TransactionImpl tx)
84      {
85          final IndirectionHandler handler = ProxyHelper.getIndirectionHandler(obj);
86          if(handler != null)
87          {
88              this.handler = handler;
89              isNew = Boolean.FALSE;
90              identity = handler.getIdentity();
91              if(handler.alreadyMaterialized())
92              {
93                  cld = tx.getBroker().getClassDescriptor(handler.getRealSubject().getClass());
94              }
95              else
96              {
97                  cld = tx.getBroker().getClassDescriptor(identity.getObjectsRealClass());
98              }
99          }
100         else
101         {
102             cld = tx.getBroker().getClassDescriptor(obj.getClass());
103         }
104     }
105 
106     void doIsNewObjectCheck(final TransactionImpl tx)
107     {
108         boolean isNew = tx.isTransient(cld, obj, null);
109         this.isNew = isNew ? Boolean.TRUE : Boolean.FALSE;
110     }
111 
112     /**
113      * Return the associated persistent object.
114      */
115     public Object getObj()
116     {
117         return obj;
118     }
119 
120     /**
121      * Returns the materialized object (if proxy is materialized or a "normal"
122      * persistent object) or <em>null</em> if associated with unmaterialized proxy object.
123      */
124     public Object getObjMaterialized()
125     {
126         return handler != null ? (handler.alreadyMaterialized() ? handler.getRealSubject() : null) : obj;
127     }
128 
129     /**
130      * Returns the associated object {@link org.apache.ojb.broker.Identity}.
131      */
132     public Identity getIdentity()
133     {
134         if(identity == null)
135         {
136             identity = tx.getBroker().serviceIdentity().buildIdentity(obj);
137         }
138         return identity;
139     }
140 
141     /**
142      * Returns the associated object {@link org.apache.ojb.broker.metadata.ClassDescriptor}.
143      */
144     public ClassDescriptor getCld()
145     {
146         return cld;
147     }
148 
149     /**
150      * Returns <code>true</code> if the represented object is
151      * not yet persisted.
152      */
153     public boolean isNew()
154     {
155         return isNew.booleanValue();
156     }
157 
158     public boolean isProxy()
159     {
160         return handler != null;
161     }
162 
163     public IndirectionHandler getHandler()
164     {
165         return handler;
166     }
167 
168     public String toString()
169     {
170         return new ToStringBuilder(this)
171                 .append("identity", identity)
172                 .append("isNew", isNew)
173                 .append("isProxy", handler != null)
174                 .append("handler", handler)
175                 .append("tx", tx)
176                 .toString();
177     }
178 }
179