001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.interceptor;
006:
007: import com.opensymphony.xwork.Action;
008: import com.opensymphony.xwork.ActionInvocation;
009:
010: /**
011: * AbstractLifecycleInterceptor is a convenience interceptor that is like an
012: * @link com.opensymphony.xwork.interceptor.AroundInterceptor that allows before
013: * and after action invocation callback but also berfore result is being executed
014: * callback as it implements @link com.opensymphony.xwork.interceptor.PreResultListener
015: *
016: * @author Jason Carreira
017: * @version $Date: 2006-03-19 17:17:47 +0100 (So, 19 Mrz 2006) $ $Id: AbstractLifecycleInterceptor.java 913 2006-03-19 16:17:47Z tmjee $
018: */
019: public abstract class AbstractLifecycleInterceptor implements
020: Interceptor, PreResultListener {
021:
022: /**
023: * This callback method will be called after the Action execution and before the Result execution.
024: *
025: * @param invocation
026: * @param resultCode
027: */
028: public void beforeResult(ActionInvocation invocation,
029: String resultCode) {
030: }
031:
032: /**
033: * Called to let an interceptor clean up any resources it has allocated.
034: */
035: public void destroy() {
036: }
037:
038: /**
039: * Called after an Interceptor is created, but before any requests are processed using the intercept() methodName. This
040: * gives the Interceptor a chance to initialize any needed resources.
041: */
042: public void init() {
043: }
044:
045: /**
046: * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the
047: * request by the DefaultActionInvocation or to short-circuit the processing and just return a String return code.
048: *
049: * @param invocation
050: * @return result
051: * @throws Exception
052: */
053: public String intercept(ActionInvocation invocation)
054: throws Exception {
055: invocation.addPreResultListener(this );
056:
057: String result = null;
058:
059: try {
060: before(invocation);
061: result = invocation.invoke();
062: after(invocation, result);
063: } catch (Exception e) {
064: result = Action.ERROR;
065: handleException(e);
066: }
067:
068: return result;
069: }
070:
071: /**
072: * Called after the Action and Result have been executed.
073: *
074: * @param invocation
075: * @param result
076: * @throws Exception
077: */
078: protected void after(ActionInvocation invocation, String result)
079: throws Exception {
080: }
081:
082: /**
083: * Called before the rest of the ActionInvocation is forwarded to.
084: *
085: * @param invocation
086: * @throws Exception
087: */
088: protected void before(ActionInvocation invocation) throws Exception {
089: }
090:
091: /**
092: * Called if an Exception is caught while executing the before(), the rest of the ActionInvocation, including the
093: * Action and Result execution, or the after() call. The default implementation just rethrows the Exception. Subclasses
094: * can choose to either throw an Exception or do some processing.
095: *
096: * @param e the Exception caught
097: * @throws Exception
098: */
099: protected void handleException(Exception e) throws Exception {
100: throw e;
101: }
102: }
|