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.lang.reflect.Method;
018: import java.util.Collection;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.Set;
022:
023: import javax.servlet.ServletRequest;
024: import javax.servlet.ServletResponse;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.struts.Globals;
029: import org.apache.struts.action.Action;
030: import org.apache.struts.action.ActionErrors;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.strecks.context.ActionContext;
035: import org.strecks.exceptions.ApplicationRuntimeException;
036: import org.strecks.injection.internal.InjectionWrapper;
037: import org.strecks.interceptor.AfterInterceptor;
038: import org.strecks.interceptor.BeforeInterceptor;
039: import org.strecks.lifecycle.LifecycleMethodAware;
040: import org.strecks.source.ActionBeanSource;
041: import org.strecks.view.ViewAdapter;
042:
043: /**
044: * Provides base class support for <code>ControllerAction</code> implementations
045: * @author Phil Zoio
046: */
047: public abstract class BaseControllerAction extends Action implements
048: ControllerAction, Injectable, BeanSourceAware,
049: InterceptorAware, LifecycleMethodAware
050:
051: {
052:
053: protected abstract ViewAdapter executeAction(Object actionBean,
054: ActionContext context);
055:
056: private Map<String, InjectionWrapper> injectionHandlers;
057:
058: private ActionBeanSource beanSource;
059:
060: private Collection<BeforeInterceptor> beforeInterceptors;
061:
062: private Collection<AfterInterceptor> afterInterceptors;
063:
064: private Method initMethod;
065:
066: private Method closeMethod;
067:
068: public void setInjectionHandlers(
069: Map<String, InjectionWrapper> injectionHandlers) {
070: this .injectionHandlers = injectionHandlers;
071: }
072:
073: public void setBeanSource(ActionBeanSource beanSource) {
074: this .beanSource = beanSource;
075: }
076:
077: public void setBeforeInterceptors(
078: List<BeforeInterceptor> beforeInterceptors) {
079: this .beforeInterceptors = beforeInterceptors;
080: }
081:
082: public void setAfterInterceptors(
083: List<AfterInterceptor> afterInterceptors) {
084: this .afterInterceptors = afterInterceptors;
085: }
086:
087: public void setInitMethod(Method initMethod) {
088: this .initMethod = initMethod;
089: }
090:
091: public void setCloseMethod(Method closeMethod) {
092: this .closeMethod = closeMethod;
093: }
094:
095: @Override
096: public final ActionForward execute(ActionMapping mapping,
097: ActionForm form, HttpServletRequest request,
098: HttpServletResponse response) throws Exception {
099: throw new UnsupportedOperationException();
100: }
101:
102: @Override
103: public final ActionForward execute(ActionMapping mapping,
104: ActionForm form, ServletRequest request,
105: ServletResponse response) throws Exception {
106: throw new UnsupportedOperationException();
107: }
108:
109: public void preExecute(Object actionBean,
110: ActionContext actionContext) throws Exception {
111: doInjection(actionBean, actionContext);
112: doInitMethod(actionBean);
113: }
114:
115: public void postExecute(Object actionBean,
116: ActionContext actionContext, Exception e) throws Exception {
117: doCloseMethod(actionBean);
118: }
119:
120: public final ViewAdapter executeController(Object actionBean,
121: ActionContext actionContext) throws Exception {
122: return executeAction(actionBean, actionContext);
123: }
124:
125: void doInjection(Object actionBean, ActionContext injectionContext) {
126: if (injectionHandlers != null) {
127: Set<String> keySet = injectionHandlers.keySet();
128: for (String propertyName : keySet) {
129: InjectionWrapper wrapper = injectionHandlers
130: .get(propertyName);
131: wrapper.inject(actionBean, injectionContext);
132: }
133: }
134: }
135:
136: void doInitMethod(Object actionBean) {
137: if (this .initMethod != null) {
138: try {
139: // TODO should be catch exception better
140: initMethod.invoke(actionBean, new Object[] {});
141: } catch (Exception e) {
142: throw new ApplicationRuntimeException(e);
143: }
144: }
145: }
146:
147: void doCloseMethod(Object actionBean) {
148: if (this .closeMethod != null) {
149: try {
150: // TODO should be catch exception better
151: closeMethod.invoke(actionBean, new Object[] {});
152: } catch (Exception e) {
153: throw new ApplicationRuntimeException(e);
154: }
155: }
156: }
157:
158: protected boolean hasErrors(ActionContext context) {
159: ActionErrors errors = (ActionErrors) context.getRequest()
160: .getAttribute(Globals.ERROR_KEY);
161: boolean hasErrors = (null != errors && !errors.isEmpty());
162: return hasErrors;
163: }
164:
165: protected Method getInitMethod() {
166: return initMethod;
167: }
168:
169: protected Method getCloseMethod() {
170: return closeMethod;
171: }
172:
173: public ActionBeanSource getBeanSource() {
174: return beanSource;
175: }
176:
177: public Collection<AfterInterceptor> getAfterInterceptors() {
178: return afterInterceptors;
179: }
180:
181: public Collection<BeforeInterceptor> getBeforeInterceptors() {
182: return beforeInterceptors;
183: }
184:
185: }
|