001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.controller;
016:
017: import java.util.Collection;
018: import java.util.Collections;
019:
020: import javax.servlet.http.HttpServletRequest;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.struts.action.ActionForward;
025: import org.strecks.constants.InfrastructureKeys;
026: import org.strecks.context.ActionContext;
027: import org.strecks.exceptions.ApplicationRuntimeException;
028: import org.strecks.interceptor.AfterInterceptor;
029: import org.strecks.interceptor.BeforeInterceptor;
030: import org.strecks.util.Assert;
031: import org.strecks.util.ReflectHelper;
032: import org.strecks.view.ActionForwardViewAdapter;
033: import org.strecks.view.RenderingViewAdapter;
034: import org.strecks.view.ViewAdapter;
035:
036: /**
037: * Delegate which carries out Strecks-specific request processing on behalf of the
038: * <code>ControllerRequestProcessor</code> class.
039: * @author Phil Zoio
040: */
041: public class ControllerProcessorDelegateImpl implements
042: ControllerProcessorDelegate {
043:
044: private static Log log = LogFactory
045: .getLog(ControllerProcessorDelegateImpl.class);
046:
047: private Collection<BeforeInterceptor> beforeInterceptors = Collections
048: .emptyList();
049:
050: private Collection<AfterInterceptor> afterInterceptors = Collections
051: .emptyList();
052:
053: public ControllerProcessorDelegateImpl() {
054: }
055:
056: public ActionForward handleActionPerform(ControllerAction action,
057: ActionContext context) throws Exception {
058:
059: HttpServletRequest request = context.getRequest();
060: ViewAdapter viewAdapter = null;
061: Object actionBean = null;
062:
063: Exception ee = null;
064:
065: try {
066:
067: actionBean = action.getBeanSource().createBean(context);
068:
069: // Dependency injection and init method done here
070: action.preExecute(actionBean, context);
071:
072: // BeforeInterceptor exceptions are handled by the Struts-configured exception handler
073: for (BeforeInterceptor beforeInterceptor : beforeInterceptors) {
074: runBeforeInterceptor(actionBean, beforeInterceptor,
075: context);
076: }
077:
078: Collection<BeforeInterceptor> actionBeforeInterceptors = action
079: .getBeforeInterceptors();
080:
081: if (actionBeforeInterceptors != null) {
082: for (BeforeInterceptor beforeInterceptor : actionBeforeInterceptors) {
083: runBeforeInterceptor(actionBean, beforeInterceptor,
084: context);
085: }
086: }
087:
088: viewAdapter = action.executeController(actionBean, context);
089:
090: } catch (Exception e) {
091: ee = e;
092: log.error(e);
093: throw e;
094: } finally {
095: Collection<AfterInterceptor> actionAfterInterceptors = action
096: .getAfterInterceptors();
097:
098: if (actionAfterInterceptors != null) {
099: for (AfterInterceptor afterInterceptor : actionAfterInterceptors) {
100: runAfterInterceptor(actionBean, afterInterceptor,
101: context, ee);
102: }
103: }
104:
105: // AfterInterceptor exceptions are caught, logged and ignored afterwards
106: for (AfterInterceptor afterInterceptor : afterInterceptors) {
107: runAfterInterceptor(actionBean, afterInterceptor,
108: context, ee);
109: }
110:
111: action.postExecute(actionBean, context, ee);
112:
113: }
114:
115: request
116: .setAttribute(InfrastructureKeys.ACTION_BEAN,
117: actionBean);
118:
119: if (viewAdapter == null) {
120: return null;
121: } else {
122: if (viewAdapter instanceof ActionForwardViewAdapter) {
123: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
124: ActionForward actionForward = va.getActionForward();
125: request.setAttribute(InfrastructureKeys.ACTION_FORWARD,
126: actionForward);
127: return actionForward;
128: } else if (viewAdapter instanceof RenderingViewAdapter) {
129: RenderingViewAdapter va = (RenderingViewAdapter) viewAdapter;
130: va.render(context);
131: }
132: }
133: return null;
134:
135: }
136:
137: @SuppressWarnings("unchecked")
138: void runAfterInterceptor(Object actionBean,
139: AfterInterceptor afterInterceptor, ActionContext context,
140: Exception ee) {
141: try {
142: afterInterceptor.afterExecute(actionBean, context, ee);
143: } catch (ClassCastException e) {
144: throw new ApplicationRuntimeException(
145: "Action bean class "
146: + actionBean.getClass().getName()
147: + " is not compatible with AfterInterceptor implementation "
148: + afterInterceptor.getClass().getName()
149: + ", which is parameterized with the type "
150: + ReflectHelper.getGenericType(
151: afterInterceptor.getClass(),
152: AfterInterceptor.class), e);
153: } catch (Exception e) {
154: log.error("Error during execution of afterInterceptor", e);
155: }
156: }
157:
158: @SuppressWarnings("unchecked")
159: void runBeforeInterceptor(Object actionBean,
160: BeforeInterceptor beforeInterceptor, ActionContext context) {
161: try {
162: beforeInterceptor.beforeExecute(actionBean, context);
163: } catch (ClassCastException e) {
164: throw new ApplicationRuntimeException(
165: "Action bean class "
166: + actionBean.getClass().getName()
167: + " is not compatible with BeforeInterceptor implementation "
168: + beforeInterceptor.getClass().getName()
169: + ", which is parameterized with the type "
170: + ReflectHelper.getGenericType(
171: beforeInterceptor.getClass(),
172: BeforeInterceptor.class));
173: }
174: }
175:
176: public Collection<BeforeInterceptor> getBeforeInterceptors() {
177: return this .beforeInterceptors;
178: }
179:
180: public Collection<AfterInterceptor> getAfterInterceptors() {
181: return this .afterInterceptors;
182: }
183:
184: public void setAfterInterceptors(
185: Collection<AfterInterceptor> afterInterceptors) {
186: Assert.notNull(afterInterceptors);
187: this .afterInterceptors = afterInterceptors;
188: }
189:
190: public void setBeforeInterceptors(
191: Collection<BeforeInterceptor> beforeInterceptors) {
192: Assert.notNull(beforeInterceptors);
193: this.beforeInterceptors = beforeInterceptors;
194: }
195:
196: }
|