1 | |
package org.apache.ojb.broker.metadata.fieldaccess; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import org.apache.commons.beanutils.DynaBean; |
19 | |
import org.apache.ojb.broker.PersistenceBrokerException; |
20 | |
import org.apache.ojb.broker.metadata.MetadataException; |
21 | |
import org.apache.ojb.broker.util.logging.Logger; |
22 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class PersistentFieldDynaBeanImpl extends PersistentFieldBase |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
private static final long serialVersionUID = 4728858060905429509L; |
43 | |
|
44 | |
public PersistentFieldDynaBeanImpl() |
45 | |
{ |
46 | |
super(); |
47 | |
} |
48 | |
|
49 | |
public PersistentFieldDynaBeanImpl(Class aPropertyType, String aPropertyName) |
50 | |
{ |
51 | |
super(aPropertyType, aPropertyName); |
52 | |
checkNested(aPropertyName); |
53 | |
} |
54 | |
|
55 | |
public void set(Object anObject, Object aValue) throws MetadataException |
56 | |
{ |
57 | |
if(anObject == null) return; |
58 | |
if (anObject instanceof DynaBean) |
59 | |
{ |
60 | |
DynaBean dynaBean = (DynaBean) anObject; |
61 | |
try |
62 | |
{ |
63 | |
dynaBean.set(getName(), aValue); |
64 | |
} |
65 | |
catch (Throwable t) |
66 | |
{ |
67 | |
String msg = dynaBean.getClass().getName(); |
68 | |
logSetProblem(anObject, aValue, msg); |
69 | |
throw new PersistenceBrokerException(t); |
70 | |
} |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | |
String msg = "the object is not a DynaBean"; |
75 | |
logSetProblem(anObject, aValue, msg); |
76 | |
throw new PersistenceBrokerException(msg); |
77 | |
} |
78 | |
} |
79 | |
|
80 | |
public Object get(Object anObject) throws MetadataException |
81 | |
{ |
82 | |
if(anObject == null) return null; |
83 | |
if (anObject instanceof DynaBean) |
84 | |
{ |
85 | |
DynaBean dynaBean = (DynaBean) anObject; |
86 | |
try |
87 | |
{ |
88 | |
return dynaBean.get(getName()); |
89 | |
} |
90 | |
catch (Throwable t) |
91 | |
{ |
92 | |
String msg = dynaBean.getClass().getName(); |
93 | |
logGetProblem(anObject, msg); |
94 | |
throw new PersistenceBrokerException(t); |
95 | |
} |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | |
String msg = "the object is not a DynaBean"; |
100 | |
logGetProblem(anObject, msg); |
101 | |
throw new PersistenceBrokerException(msg); |
102 | |
} |
103 | |
} |
104 | |
|
105 | |
private void checkNested(String fieldName) |
106 | |
{ |
107 | |
if(fieldName.indexOf(PATH_TOKEN) > -1) |
108 | |
{ |
109 | |
throw new MetadataException("This implementation does not support nested fields"); |
110 | |
} |
111 | |
} |
112 | |
|
113 | |
public Class getType() |
114 | |
{ |
115 | |
return getDeclaringClass(); |
116 | |
} |
117 | |
|
118 | |
protected boolean makeAccessible() |
119 | |
{ |
120 | |
return false; |
121 | |
} |
122 | |
|
123 | |
public boolean usesAccessorsAndMutators() |
124 | |
{ |
125 | |
return false; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
protected void logSetProblem(Object anObject, Object aValue, String msg) |
132 | |
{ |
133 | |
Logger logger = LoggerFactory.getDefaultLogger(); |
134 | |
logger.error("Error in operation [set] of object [" + this.getClass().getName() + "], " + msg); |
135 | |
logger.error("Property Name [" + getName() + "]"); |
136 | |
if (anObject instanceof DynaBean) |
137 | |
{ |
138 | |
DynaBean dynaBean = (DynaBean) anObject; |
139 | |
logger.error("anObject was DynaClass [" + dynaBean.getDynaClass().getName() + "]"); |
140 | |
} |
141 | |
else if (anObject != null) |
142 | |
{ |
143 | |
logger.error("anObject was class [" + anObject.getClass().getName() + "]"); |
144 | |
} |
145 | |
else |
146 | |
{ |
147 | |
logger.error("anObject was null"); |
148 | |
} |
149 | |
if (aValue != null) |
150 | |
logger.error("aValue was class [" + aValue.getClass().getName() + "]"); |
151 | |
else |
152 | |
logger.error("aValue was null"); |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
protected void logGetProblem(Object anObject, String msg) |
159 | |
{ |
160 | |
Logger logger = LoggerFactory.getDefaultLogger(); |
161 | |
logger.error("Error in operation [get of object [" + this.getClass().getName() + "], " + msg); |
162 | |
logger.error("Property Name [" + getName() + "]"); |
163 | |
if (anObject instanceof DynaBean) |
164 | |
{ |
165 | |
DynaBean dynaBean = (DynaBean) anObject; |
166 | |
logger.error("anObject was DynaClass [" + dynaBean.getDynaClass().getName() + "]"); |
167 | |
} |
168 | |
else if (anObject != null) |
169 | |
{ |
170 | |
logger.error("anObject was class [" + anObject.getClass().getName() + "]"); |
171 | |
} |
172 | |
else |
173 | |
{ |
174 | |
logger.error("anObject was null"); |
175 | |
} |
176 | |
} |
177 | |
} |