/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.controller;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.strecks.context.ActionContext;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.injection.internal.InjectionWrapper;
import org.strecks.interceptor.AfterInterceptor;
import org.strecks.interceptor.BeforeInterceptor;
import org.strecks.lifecycle.LifecycleMethodAware;
import org.strecks.source.ActionBeanSource;
import org.strecks.view.ViewAdapter;
/**
* Provides base class support for <code>ControllerAction</code> implementations
* @author Phil Zoio
*/
public abstract class BaseControllerAction extends Action
implements
ControllerAction,
Injectable,
BeanSourceAware,
InterceptorAware,
LifecycleMethodAware
{
protected abstract ViewAdapter executeAction(Object actionBean, ActionContext context);
private Map<String, InjectionWrapper> injectionHandlers;
private ActionBeanSource beanSource;
private Collection<BeforeInterceptor> beforeInterceptors;
private Collection<AfterInterceptor> afterInterceptors;
private Method initMethod;
private Method closeMethod;
public void setInjectionHandlers(Map<String, InjectionWrapper> injectionHandlers)
{
this.injectionHandlers = injectionHandlers;
}
public void setBeanSource(ActionBeanSource beanSource)
{
this.beanSource = beanSource;
}
public void setBeforeInterceptors(List<BeforeInterceptor> beforeInterceptors)
{
this.beforeInterceptors = beforeInterceptors;
}
public void setAfterInterceptors(List<AfterInterceptor> afterInterceptors)
{
this.afterInterceptors = afterInterceptors;
}
public void setInitMethod(Method initMethod)
{
this.initMethod = initMethod;
}
public void setCloseMethod(Method closeMethod)
{
this.closeMethod = closeMethod;
}
@Override
public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
throw new UnsupportedOperationException();
}
@Override
public final ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request,
ServletResponse response) throws Exception
{
throw new UnsupportedOperationException();
}
public void preExecute(Object actionBean, ActionContext actionContext) throws Exception
{
doInjection(actionBean, actionContext);
doInitMethod(actionBean);
}
public void postExecute(Object actionBean, ActionContext actionContext, Exception e) throws Exception
{
doCloseMethod(actionBean);
}
public final ViewAdapter executeController(Object actionBean, ActionContext actionContext) throws Exception
{
return executeAction(actionBean, actionContext);
}
void doInjection(Object actionBean, ActionContext injectionContext)
{
if (injectionHandlers != null)
{
Set<String> keySet = injectionHandlers.keySet();
for (String propertyName : keySet)
{
InjectionWrapper wrapper = injectionHandlers.get(propertyName);
wrapper.inject(actionBean, injectionContext);
}
}
}
void doInitMethod(Object actionBean)
{
if (this.initMethod != null)
{
try
{
// TODO should be catch exception better
initMethod.invoke(actionBean, new Object[]
{});
}
catch (Exception e)
{
throw new ApplicationRuntimeException(e);
}
}
}
void doCloseMethod(Object actionBean)
{
if (this.closeMethod != null)
{
try
{
// TODO should be catch exception better
closeMethod.invoke(actionBean, new Object[]
{});
}
catch (Exception e)
{
throw new ApplicationRuntimeException(e);
}
}
}
protected boolean hasErrors(ActionContext context)
{
ActionErrors errors = (ActionErrors) context.getRequest().getAttribute(Globals.ERROR_KEY);
boolean hasErrors = (null != errors && !errors.isEmpty());
return hasErrors;
}
protected Method getInitMethod()
{
return initMethod;
}
protected Method getCloseMethod()
{
return closeMethod;
}
public ActionBeanSource getBeanSource()
{
return beanSource;
}
public Collection<AfterInterceptor> getAfterInterceptors()
{
return afterInterceptors;
}
public Collection<BeforeInterceptor> getBeforeInterceptors()
{
return beforeInterceptors;
}
}