001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.interceptor;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import com.opensymphony.xwork.ActionInvocation;
011: import com.opensymphony.xwork.Preparable;
012:
013: /**
014: * <!-- START SNIPPET: description -->
015: *
016: * This interceptor calls prepare() on actions which implement {@link Preparable}. This interceptor is very useful for
017: * any situation where you need to ensure some logic runs before the actual execute method runs.
018: *
019: * <p/> A typical use of this is to run some logic to load an object from the database so that when parameters are set
020: * they can be set on this object. For example, suppose you have a User object with two properties: <i>id</i> and
021: * <i>name</i>. Provided that the params interceptor is called twice (once before and once after this interceptor), you
022: * can load the User object using the id property, and then when the second params interceptor is called the parameter
023: * <i>user.name</i> will be set, as desired, on the actual object loaded from the database. See the example for more
024: * info.
025: *
026: * <!-- END SNIPPET: description -->
027: *
028: * <p/> <u>Interceptor parameters:</u>
029: *
030: * <!-- START SNIPPET: parameters -->
031: *
032: * <ul>
033: *
034: * <li>None</li>
035: *
036: * </ul>
037: *
038: * <!-- END SNIPPET: parameters -->
039: *
040: * <p/> <u>Extending the interceptor:</u>
041: *
042: * <p/>
043: *
044: * <!-- START SNIPPET: extending -->
045: *
046: * There are no known extension points to this interceptor.
047: *
048: * <!-- END SNIPPET: extending -->
049: *
050: * <p/> <u>Example code:</u>
051: *
052: * <pre>
053: * <!-- START SNIPPET: example -->
054: * <!-- Calls the params interceptor twice, allowing you to
055: * pre-load data for the second time parameters are set -->
056: * <action name="someAction" class="com.examples.SomeAction">
057: * <interceptor-ref name="params"/>
058: * <interceptor-ref name="prepare"/>
059: * <interceptor-ref name="basicStack"/>
060: * <result name="success">good_result.ftl</result>
061: * </action>
062: * <!-- END SNIPPET: example -->
063: * </pre>
064: *
065: * Date: Nov 5, 2003 2:33:11 AM
066: *
067: * @author Jason Carreira
068: * @author Philip Luppens
069: * @author tm_jee
070: * @see com.opensymphony.xwork.Preparable
071: */
072: public class PrepareInterceptor extends AroundInterceptor {
073:
074: private static final long serialVersionUID = -7182584510651751135L;
075:
076: private static final Log _log = LogFactory
077: .getLog(PrepareInterceptor.class);
078:
079: private final static String PREPARE_PREFIX = "prepare";
080: private final static String ALT_PREPARE_PREFIX = "prepareDo";
081:
082: private boolean alwaysInvokePrepare = true;
083:
084: public void setAlwaysInvokePrepare(String alwaysInvokePrepare) {
085: this .alwaysInvokePrepare = Boolean.valueOf(alwaysInvokePrepare)
086: .booleanValue();
087: }
088:
089: protected void after(ActionInvocation dispatcher, String result)
090: throws Exception {
091:
092: }
093:
094: protected void before(ActionInvocation invocation) throws Exception {
095: Object action = invocation.getAction();
096:
097: if (action instanceof Preparable) {
098: try {
099: PrefixMethodInvocationUtil.invokePrefixMethod(
100: invocation, new String[] { PREPARE_PREFIX,
101: ALT_PREPARE_PREFIX });
102: } catch (Exception e) {
103: // just in case there's an exception while doing reflection,
104: // we still want prepare() to be able to get called.
105: _log
106: .warn(
107: "an exception occured while trying to execute prefixed method",
108: e);
109: }
110: if (alwaysInvokePrepare) {
111: ((Preparable) action).prepare();
112: }
113: }
114: }
115: }
|