1 | |
package org.seasar.cubby.controller.impl; |
2 | |
|
3 | |
import java.lang.reflect.InvocationTargetException; |
4 | |
import java.lang.reflect.Method; |
5 | |
|
6 | |
import org.seasar.cubby.action.Action; |
7 | |
import org.seasar.cubby.action.ActionResult; |
8 | |
import org.seasar.cubby.action.Form; |
9 | |
import org.seasar.cubby.action.Validation; |
10 | |
import org.seasar.cubby.controller.ActionContext; |
11 | |
import org.seasar.cubby.controller.ActionDef; |
12 | |
import org.seasar.cubby.controller.Populator; |
13 | |
import org.seasar.framework.beans.BeanDesc; |
14 | |
import org.seasar.framework.beans.PropertyDesc; |
15 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
16 | |
import org.seasar.framework.container.ComponentDef; |
17 | |
import org.seasar.framework.log.Logger; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 7 | public class ActionContextImpl implements ActionContext { |
25 | |
|
26 | 1 | private static final Logger logger = Logger |
27 | |
.getLogger(ActionContextImpl.class); |
28 | |
|
29 | 1 | private static final Object[] EMPTY_ARGS = new Object[0]; |
30 | |
|
31 | |
private ActionDef actionDef; |
32 | |
|
33 | |
private Action action; |
34 | |
|
35 | |
private Populator populator; |
36 | |
|
37 | |
public void initialize(final ActionDef actionDef) { |
38 | 7 | this.actionDef = actionDef; |
39 | 7 | } |
40 | |
|
41 | |
public boolean isInitialized() { |
42 | |
return this.actionDef != null; |
43 | |
} |
44 | |
|
45 | |
public Populator getPopulator() { |
46 | |
return populator; |
47 | |
} |
48 | |
|
49 | |
public void setPopulator(final Populator populator) { |
50 | 7 | this.populator = populator; |
51 | 7 | } |
52 | |
|
53 | |
public ComponentDef getComponentDef() { |
54 | 6 | return actionDef.getComponentDef(); |
55 | |
} |
56 | |
|
57 | |
public Method getMethod() { |
58 | |
return actionDef.getMethod(); |
59 | |
} |
60 | |
|
61 | |
public Action getAction() { |
62 | 6 | if (action == null) { |
63 | 4 | action = (Action) actionDef.getComponentDef().getComponent(); |
64 | |
} |
65 | 6 | return action; |
66 | |
} |
67 | |
|
68 | |
public Validation getValidation() { |
69 | |
return actionDef.getMethod().getAnnotation(Validation.class); |
70 | |
} |
71 | |
|
72 | |
public ActionResult invoke() throws Throwable { |
73 | |
try { |
74 | |
final ActionResult result = (ActionResult) actionDef.getMethod().invoke(getAction(), |
75 | |
EMPTY_ARGS); |
76 | |
return result; |
77 | |
} catch (final InvocationTargetException ex) { |
78 | |
logger.log(ex); |
79 | |
throw ex.getCause(); |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
public Object getFormBean() { |
84 | |
final Form form = getForm(); |
85 | |
if (form == null) { |
86 | |
return null; |
87 | |
} |
88 | |
final Action action = getAction(); |
89 | |
final String formName = form.value(); |
90 | |
if (Form.THIS.equals(formName)) { |
91 | |
return action; |
92 | |
} else { |
93 | |
final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action.getClass()); |
94 | |
final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(formName); |
95 | |
final Object value = propertyDesc.getValue(action); |
96 | |
return value; |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
private Form getForm() { |
101 | |
return actionDef.getMethod().getAnnotation(Form.class); |
102 | |
} |
103 | |
|
104 | |
} |