All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opensymphony.xwork2.interceptor.PrepareInterceptor Maven / Gradle / Ivy

Go to download

XWork is an command-pattern framework that is used to power WebWork as well as other applications. XWork provides an Inversion of Control container, a powerful expression language, data type conversion, validation, and pluggable configuration.

The newest version!
/*
 * Copyright (c) 2002-2007 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import java.lang.reflect.InvocationTargetException;


/**
 * 
 *
 * This interceptor calls prepare() on actions which implement
 * {@link Preparable}. This interceptor is very useful for any situation where
 * you need to ensure some logic runs before the actual execute method runs.
 *
 * 

A typical use of this is to run some logic to load an object from the * database so that when parameters are set they can be set on this object. For * example, suppose you have a User object with two properties: id and * name. Provided that the params interceptor is called twice (once * before and once after this interceptor), you can load the User object using * the id property, and then when the second params interceptor is called the * parameter user.name will be set, as desired, on the actual object * loaded from the database. See the example for more info. * *

* Note: Since XWork 2.0.2, this interceptor extends {@link MethodFilterInterceptor}, therefore being * able to deal with excludeMethods / includeMethods parameters. See [Workflow Interceptor] * (class {@link DefaultWorkflowInterceptor}) for documentation and examples on how to use this feature. * *

Update: Added logic to execute a prepare{MethodName} and conditionally * the a general prepare() Method, depending on the 'alwaysInvokePrepare' parameter/property * which is by default true. This allows us to run some logic based on the method * name we specify in the {@link com.opensymphony.xwork2.ActionProxy}. For example, you can specify a * prepareInput() method that will be run before the invocation of the input method. * * * *

Interceptor parameters: * * * *

    * *
  • alwaysInvokePrepare - Default to true. If true, prepare will always be invoked, * otherwise it will not.
  • * *
* * * *

Extending the interceptor: * *

* * * * There are no known extension points to this interceptor. * * * *

Example code: * *

 * 
 * <!-- Calls the params interceptor twice, allowing you to
 *       pre-load data for the second time parameters are set -->
 *  <action name="someAction" class="com.examples.SomeAction">
 *      <interceptor-ref name="params"/>
 *      <interceptor-ref name="prepare"/>
 *      <interceptor-ref name="basicStack"/>
 *      <result name="success">good_result.ftl</result>
 *  </action>
 * 
 * 
* * @author Jason Carreira * @author Philip Luppens * @author tm_jee * @see com.opensymphony.xwork2.Preparable */ public class PrepareInterceptor extends MethodFilterInterceptor { private static final long serialVersionUID = -5216969014510719786L; private static final Logger LOG = LoggerFactory.getLogger(PrepareInterceptor.class); private final static String PREPARE_PREFIX = "prepare"; private final static String ALT_PREPARE_PREFIX = "prepareDo"; private boolean alwaysInvokePrepare = true; /** * Sets if the preapare method should always be executed. *

* Default is true. * * @param alwaysInvokePrepare if prepare should always be executed or not. */ public void setAlwaysInvokePrepare(String alwaysInvokePrepare) { this.alwaysInvokePrepare = Boolean.parseBoolean(alwaysInvokePrepare); } @Override public String doIntercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (action instanceof Preparable) { try { PrefixMethodInvocationUtil.invokePrefixMethod(invocation, new String[]{PREPARE_PREFIX, ALT_PREPARE_PREFIX}); } catch (InvocationTargetException e) { // just in case there's an exception while doing reflection, // we still want prepare() to be able to get called. LOG.warn("an exception occured while trying to execute prefixed method", e); } catch (IllegalAccessException e) { // just in case there's an exception while doing reflection, // we still want prepare() to be able to get called. LOG.warn("an exception occured while trying to execute prefixed method", e); } catch (Exception e) { // just in case there's an exception while doing reflection, // we still want prepare() to be able to get called. LOG.warn("an exception occured while trying to execute prefixed method", e); } if (alwaysInvokePrepare) { ((Preparable) action).prepare(); } } return invocation.invoke(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy