View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * @author baba
38   * @since 1.0.0
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  	/** リクエストパラメータとフォームオブジェクトを変換する DXO。 */
56  	private FormDxo formDxo;
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	public void initialize(final ActionDef actionDef) {
62  		this.actionDef = actionDef;
63  		this.action = null;
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 */
69  	public boolean isInitialized() {
70  		return this.actionDef != null;
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	public FormDxo getFormDxo() {
77  		return formDxo;
78  	}
79  
80  	/**
81  	 * リクエストパラメータとフォームオブジェクトを変換する DXO を設定します。
82  	 * 
83  	 * @param formDxo
84  	 *            リクエストパラメータとフォームオブジェクトを変換する DXO
85  	 */
86  	public void setFormDxo(final FormDxo formDxo) {
87  		this.formDxo = formDxo;
88  	}
89  
90  	/**
91  	 * {@inheritDoc}
92  	 */
93  	public ComponentDef getComponentDef() {
94  		return actionDef.getComponentDef();
95  	}
96  
97  	/**
98  	 * {@inheritDoc}
99  	 */
100 	public Method getMethod() {
101 		return actionDef.getMethod();
102 	}
103 
104 	/**
105 	 * {@inheritDoc}
106 	 */
107 	public Action getAction() {
108 		if (action == null) {
109 			action = (Action) actionDef.getComponentDef().getComponent();
110 		}
111 		return action;
112 	}
113 
114 	/**
115 	 * {@inheritDoc}
116 	 */
117 	public Validation getValidation() {
118 		return actionDef.getMethod().getAnnotation(Validation.class);
119 	}
120 
121 	/**
122 	 * {@inheritDoc}
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 	 * {@inheritDoc}
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 }