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 public class ActionContextImpl implements ActionContext {
41
42
43 private static final Logger logger = Logger
44 .getLogger(ActionContextImpl.class);
45
46
47 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 this.actionDef = actionDef;
63 this.action = null;
64 }
65
66
67
68
69 public boolean isInitialized() {
70 return this.actionDef != null;
71 }
72
73
74
75
76 public FormDxo getFormDxo() {
77 return formDxo;
78 }
79
80
81
82
83
84
85
86 public void setFormDxo(final FormDxo formDxo) {
87 this.formDxo = formDxo;
88 }
89
90
91
92
93 public ComponentDef getComponentDef() {
94 return actionDef.getComponentDef();
95 }
96
97
98
99
100 public Method getMethod() {
101 return actionDef.getMethod();
102 }
103
104
105
106
107 public Action getAction() {
108 if (action == null) {
109 action = (Action) actionDef.getComponentDef().getComponent();
110 }
111 return action;
112 }
113
114
115
116
117 public Validation getValidation() {
118 return actionDef.getMethod().getAnnotation(Validation.class);
119 }
120
121
122
123
124 public ActionResult invoke() throws Exception {
125 try {
126 final ActionResult result = (ActionResult) actionDef.getMethod()
127 .invoke(getAction(), EMPTY_ARGS);
128 return result;
129 } catch (final InvocationTargetException ex) {
130 logger.log(ex);
131 Throwable target = ex.getTargetException();
132 if (target instanceof Error) {
133 throw (Error) target;
134 } else if (target instanceof RuntimeException) {
135 throw (RuntimeException) target;
136 } else {
137 throw (Exception) target;
138 }
139 }
140 }
141
142
143
144
145 public Object getFormBean() {
146 final Object formBean;
147 final Action action = getAction();
148 final Form form = actionDef.getMethod().getAnnotation(Form.class);
149 if (form != null && form.binding() == false) {
150 formBean = null;
151 } else if (form == null || Form.THIS.equals(form.value())) {
152 formBean = action;
153 } else {
154 final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(action
155 .getClass());
156 final PropertyDesc propertyDesc = beanDesc.getPropertyDesc(form
157 .value());
158 formBean = propertyDesc.getValue(action);
159 }
160 return formBean;
161 }
162
163 }