1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.controller.impl; |
17 | |
|
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.lang.reflect.Method; |
20 | |
|
21 | |
import org.seasar.cubby.action.Action; |
22 | |
import org.seasar.cubby.action.ActionResult; |
23 | |
import org.seasar.cubby.action.Form; |
24 | |
import org.seasar.cubby.action.Validation; |
25 | |
import org.seasar.cubby.controller.ActionContext; |
26 | |
import org.seasar.cubby.controller.ActionDef; |
27 | |
import org.seasar.cubby.dxo.FormDxo; |
28 | |
import org.seasar.framework.beans.BeanDesc; |
29 | |
import org.seasar.framework.beans.PropertyDesc; |
30 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
31 | |
import org.seasar.framework.container.ComponentDef; |
32 | |
import org.seasar.framework.log.Logger; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 23 | public class ActionContextImpl implements ActionContext { |
41 | |
|
42 | |
|
43 | 1 | private static final Logger logger = Logger |
44 | |
.getLogger(ActionContextImpl.class); |
45 | |
|
46 | |
|
47 | 1 | private static final Object[] EMPTY_ARGS = new Object[0]; |
48 | |
|
49 | |
|
50 | |
private ActionDef actionDef; |
51 | |
|
52 | |
|
53 | |
private Action action; |
54 | |
|
55 | |
|
56 | |
private FormDxo formDxo; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public void initialize(final ActionDef actionDef) { |
62 | 8 | this.actionDef = actionDef; |
63 | 8 | this.action = null; |
64 | 8 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public boolean isInitialized() { |
70 | 9 | return this.actionDef != null; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public FormDxo getFormDxo() { |
77 | 4 | return formDxo; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public void setFormDxo(final FormDxo formDxo) { |
87 | 13 | this.formDxo = formDxo; |
88 | 13 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public ComponentDef getComponentDef() { |
94 | 7 | return actionDef.getComponentDef(); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public Method getMethod() { |
101 | 1 | return actionDef.getMethod(); |
102 | |
} |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public Action getAction() { |
108 | 8 | if (action == null) { |
109 | 6 | action = (Action) actionDef.getComponentDef().getComponent(); |
110 | |
} |
111 | 6 | return action; |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public Validation getValidation() { |
118 | 1 | return actionDef.getMethod().getAnnotation(Validation.class); |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public ActionResult invoke() throws Exception { |
125 | |
try { |
126 | 1 | final ActionResult result = (ActionResult) actionDef.getMethod() |
127 | |
.invoke(getAction(), EMPTY_ARGS); |
128 | 0 | return result; |
129 | 0 | } catch (final InvocationTargetException ex) { |
130 | 0 | logger.log(ex); |
131 | 0 | Throwable target = ex.getTargetException(); |
132 | 0 | if (target instanceof Error) { |
133 | 0 | throw (Error) target; |
134 | 0 | } else if (target instanceof RuntimeException) { |
135 | 0 | throw (RuntimeException) target; |
136 | |
} else { |
137 | 0 | throw (Exception) target; |
138 | |
} |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public Object getFormBean() { |
146 | |
final Object formBean; |
147 | 1 | final Action action = getAction(); |
148 | 0 | final Form form = actionDef.getMethod().getAnnotation(Form.class); |
149 | 0 | if (form != null && form.binding() == false) { |
150 | 0 | formBean = null; |
151 | 0 | } else if (form == null || Form.THIS.equals(form.value())) { |
152 | 0 | formBean = action; |
153 | |
} else { |
154 | 0 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
155 | |
.getClass()); |
156 | 0 | final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(form |
157 | |
.value()); |
158 | 0 | formBean = propertyDesc.getValue(action); |
159 | |
} |
160 | 0 | return formBean; |
161 | |
} |
162 | |
|
163 | |
} |