View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.internal.controller.impl;
18  
19  import java.util.Iterator;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  import org.seasar.cubby.action.ActionContext;
25  import org.seasar.cubby.action.ActionResult;
26  import org.seasar.cubby.internal.controller.ActionResultWrapper;
27  import org.seasar.cubby.plugin.ActionResultInvocation;
28  import org.seasar.cubby.plugin.Plugin;
29  import org.seasar.cubby.plugin.PluginRegistry;
30  
31  /**
32   * {@link org.seasar.cubby.action.ActionResult} のラッパの実装です。
33   * 
34   * @author baba
35   */
36  class ActionResultWrapperImpl implements ActionResultWrapper {
37  
38  	/** アクションの実行結果。 */
39  	private final ActionResult actionResult;
40  
41  	/** アクションコンテキスト。 */
42  	private final ActionContext actionContext;
43  
44  	/**
45  	 * 指定されたアクションの実行結果をラップしたインスタンスを生成します。
46  	 * 
47  	 * @param actionResult
48  	 *            アクションの実行結果
49  	 * @param actionContext
50  	 *            アクションコンテキスト
51  	 */
52  	public ActionResultWrapperImpl(final ActionResult actionResult,
53  			final ActionContext actionContext) {
54  		super();
55  		this.actionResult = actionResult;
56  		this.actionContext = actionContext;
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	public void execute(final HttpServletRequest request,
63  			final HttpServletResponse response) throws Exception {
64  		final ActionResultInvocation actionResultInvocation = new ActionResultInvocationImpl(
65  				request, response, actionContext, actionResult);
66  		actionResultInvocation.proceed();
67  	}
68  
69  	/**
70  	 * {@inheritDoc}
71  	 */
72  	public ActionResult getActionResult() {
73  		return actionResult;
74  	}
75  
76  	/**
77  	 * アクションの実行結果の実行情報の実装です。
78  	 * 
79  	 * @author baba
80  	 */
81  	static class ActionResultInvocationImpl implements ActionResultInvocation {
82  
83  		/** 要求。 */
84  		private final HttpServletRequest request;
85  
86  		/** 応答。 */
87  		private final HttpServletResponse response;
88  
89  		/** アクションのコンテキスト。 */
90  		private final ActionContext actionContext;
91  
92  		/** アクションの実行結果。 */
93  		private final ActionResult actionResult;
94  
95  		/** プラグインのイテレータ。 */
96  		private final Iterator<Plugin> pluginsIterator;
97  
98  		/**
99  		 * インスタンス化します。
100 		 * 
101 		 * @param request
102 		 *            要求
103 		 * @param response
104 		 *            応答
105 		 * @param actionContext
106 		 *            アクションのコンテキスト
107 		 * @param actionResult
108 		 *            アクションの実行結果
109 		 */
110 		public ActionResultInvocationImpl(final HttpServletRequest request,
111 				final HttpServletResponse response,
112 				final ActionContext actionContext,
113 				final ActionResult actionResult) {
114 			this.request = request;
115 			this.response = response;
116 			this.actionContext = actionContext;
117 			this.actionResult = actionResult;
118 
119 			final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
120 			this.pluginsIterator = pluginRegistry.getPlugins().iterator();
121 		}
122 
123 		/**
124 		 * {@inheritDoc}
125 		 */
126 		public Void proceed() throws Exception {
127 			if (pluginsIterator.hasNext()) {
128 				final Plugin plugin = pluginsIterator.next();
129 				plugin.invokeActionResult(this);
130 			} else {
131 				final ActionResult actionResult = getActionResult();
132 				actionResult.execute(actionContext, request, response);
133 			}
134 			return null;
135 		}
136 
137 		/**
138 		 * {@inheritDoc}
139 		 */
140 		public HttpServletRequest getRequest() {
141 			return request;
142 		}
143 
144 		/**
145 		 * {@inheritDoc}
146 		 */
147 		public HttpServletResponse getResponse() {
148 			return response;
149 		}
150 
151 		/**
152 		 * {@inheritDoc}
153 		 */
154 		public ActionContext getActionContext() {
155 			return actionContext;
156 		}
157 
158 		/**
159 		 * {@inheritDoc}
160 		 */
161 		public ActionResult getActionResult() {
162 			return actionResult;
163 		}
164 	}
165 
166 }