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