1 | |
package org.apache.ojb.odmg; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
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 | |
|
80 | |
|
81 | |
|
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 | |
|
114 | |
|
115 | |
public Object getObj() |
116 | |
{ |
117 | |
return obj; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public Object getObjMaterialized() |
125 | |
{ |
126 | |
return handler != null ? (handler.alreadyMaterialized() ? handler.getRealSubject() : null) : obj; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
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 | |
|
143 | |
|
144 | |
public ClassDescriptor getCld() |
145 | |
{ |
146 | |
return cld; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
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 | |
|