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 | 23 | public class ActionContextImpl implements ActionContext { |
40 | |
|
41 | 1 | private static final Logger logger = Logger |
42 | |
.getLogger(ActionContextImpl.class); |
43 | |
|
44 | 1 | private static final Object[] EMPTY_ARGS = new Object[0]; |
45 | |
|
46 | |
private ActionDef actionDef; |
47 | |
|
48 | |
private Action action; |
49 | |
|
50 | |
private FormDxo formDxo; |
51 | |
|
52 | |
public void initialize(final ActionDef actionDef) { |
53 | 8 | this.actionDef = actionDef; |
54 | 8 | this.action = null; |
55 | 8 | } |
56 | |
|
57 | |
public boolean isInitialized() { |
58 | 9 | return this.actionDef != null; |
59 | |
} |
60 | |
|
61 | |
public FormDxo getFormDxo() { |
62 | 4 | return formDxo; |
63 | |
} |
64 | |
|
65 | |
public void setFormDxo(final FormDxo formDxo) { |
66 | 13 | this.formDxo = formDxo; |
67 | 13 | } |
68 | |
|
69 | |
public ComponentDef getComponentDef() { |
70 | 7 | return actionDef.getComponentDef(); |
71 | |
} |
72 | |
|
73 | |
public Method getMethod() { |
74 | 1 | return actionDef.getMethod(); |
75 | |
} |
76 | |
|
77 | |
public Action getAction() { |
78 | 8 | if (action == null) { |
79 | 6 | action = (Action) actionDef.getComponentDef().getComponent(); |
80 | |
} |
81 | 6 | return action; |
82 | |
} |
83 | |
|
84 | |
public Validation getValidation() { |
85 | 1 | return actionDef.getMethod().getAnnotation(Validation.class); |
86 | |
} |
87 | |
|
88 | |
public ActionResult invoke() throws Exception { |
89 | |
try { |
90 | 1 | final ActionResult result = (ActionResult) actionDef.getMethod() |
91 | |
.invoke(getAction(), EMPTY_ARGS); |
92 | 0 | return result; |
93 | 0 | } catch (final InvocationTargetException ex) { |
94 | 0 | logger.log(ex); |
95 | 0 | Throwable target = ex.getTargetException(); |
96 | 0 | if (target instanceof Error) { |
97 | 0 | throw (Error) target; |
98 | 0 | } else if (target instanceof RuntimeException) { |
99 | 0 | throw (RuntimeException) target; |
100 | |
} else { |
101 | 0 | throw (Exception) target; |
102 | |
} |
103 | |
} |
104 | |
} |
105 | |
|
106 | |
public Object getFormBean() { |
107 | |
final Object formBean; |
108 | 1 | final Action action = getAction(); |
109 | 0 | final Form form = actionDef.getMethod().getAnnotation(Form.class); |
110 | 0 | if (form != null && form.binding() == false) { |
111 | 0 | formBean = null; |
112 | 0 | } else if (form == null || Form.THIS.equals(form.value())) { |
113 | 0 | formBean = action; |
114 | |
} else { |
115 | 0 | final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action |
116 | |
.getClass()); |
117 | 0 | final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(form |
118 | |
.value()); |
119 | 0 | formBean = propertyDesc.getValue(action); |
120 | |
} |
121 | 0 | return formBean; |
122 | |
} |
123 | |
|
124 | |
} |