Search in sources :

Example 1 with WithLazyParams

use of com.opensymphony.xwork2.interceptor.WithLazyParams in project struts by apache.

the class DefaultInterceptorFactory method buildInterceptor.

public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map<String, String> interceptorRefParams) throws ConfigurationException {
    String interceptorClassName = interceptorConfig.getClassName();
    Map<String, String> thisInterceptorClassParams = interceptorConfig.getParams();
    Map<String, String> params = (thisInterceptorClassParams == null) ? new HashMap<String, String>() : new HashMap<>(thisInterceptorClassParams);
    params.putAll(interceptorRefParams);
    String message;
    Throwable cause;
    try {
        // interceptor instances are long-lived and used across user sessions, so don't try to pass in any extra context
        Object o = objectFactory.buildBean(interceptorClassName, null);
        if (o instanceof WithLazyParams) {
            LOG.debug("Interceptor {} is marked with interface {} and params will be set during action invocation", interceptorClassName, WithLazyParams.class.getName());
        } else {
            reflectionProvider.setProperties(params, o);
        }
        if (o instanceof Interceptor) {
            Interceptor interceptor = (Interceptor) o;
            interceptor.init();
            return interceptor;
        }
        throw new ConfigurationException("Class [" + interceptorClassName + "] does not implement Interceptor", interceptorConfig);
    } catch (InstantiationException e) {
        cause = e;
        message = "Unable to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
    } catch (IllegalAccessException e) {
        cause = e;
        message = "IllegalAccessException while attempting to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
    } catch (ClassCastException e) {
        cause = e;
        message = "Class [" + interceptorClassName + "] does not implement com.opensymphony.xwork2.interceptor.Interceptor";
    } catch (Exception e) {
        cause = e;
        message = "Caught Exception while registering Interceptor class " + interceptorClassName;
    } catch (NoClassDefFoundError e) {
        cause = e;
        message = "Could not load class " + interceptorClassName + ". Perhaps it exists but certain dependencies are not available?";
    }
    throw new ConfigurationException(message, cause, interceptorConfig);
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) WithLazyParams(com.opensymphony.xwork2.interceptor.WithLazyParams) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor)

Example 2 with WithLazyParams

use of com.opensymphony.xwork2.interceptor.WithLazyParams in project struts by apache.

the class DefaultActionInvocation method invoke.

/**
 * @throws ConfigurationException If no result can be found with the returned code
 */
public String invoke() throws Exception {
    if (executed) {
        throw new IllegalStateException("Action has already executed");
    }
    if (asyncManager == null || !asyncManager.hasAsyncActionResult()) {
        if (interceptors.hasNext()) {
            final InterceptorMapping interceptorMapping = interceptors.next();
            Interceptor interceptor = interceptorMapping.getInterceptor();
            if (interceptor instanceof WithLazyParams) {
                interceptor = lazyParamInjector.injectParams(interceptor, interceptorMapping.getParams(), invocationContext);
            }
            resultCode = interceptor.intercept(DefaultActionInvocation.this);
        } else {
            resultCode = invokeActionOnly();
        }
    } else {
        Object asyncActionResult = asyncManager.getAsyncActionResult();
        if (asyncActionResult instanceof Throwable) {
            throw new Exception((Throwable) asyncActionResult);
        }
        asyncAction = null;
        resultCode = saveResult(proxy.getConfig(), asyncActionResult);
    }
    if (asyncManager == null || asyncAction == null) {
        // return above and flow through again
        if (!executed) {
            if (preResultListeners != null) {
                LOG.trace("Executing PreResultListeners for result [{}]", result);
                for (Object preResultListener : preResultListeners) {
                    PreResultListener listener = (PreResultListener) preResultListener;
                    listener.beforeResult(this, resultCode);
                }
            }
            // now execute the result, if we're supposed to
            if (proxy.getExecuteResult()) {
                executeResult();
            }
            executed = true;
        }
    } else {
        asyncManager.invokeAsyncAction(asyncAction);
    }
    return resultCode;
}
Also used : WithLazyParams(com.opensymphony.xwork2.interceptor.WithLazyParams) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) NoSuchPropertyException(ognl.NoSuchPropertyException) StrutsException(org.apache.struts2.StrutsException) MethodFailedException(ognl.MethodFailedException)

Aggregations

ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)2 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)2 WithLazyParams (com.opensymphony.xwork2.interceptor.WithLazyParams)2 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)1 PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)1 MethodFailedException (ognl.MethodFailedException)1 NoSuchPropertyException (ognl.NoSuchPropertyException)1 StrutsException (org.apache.struts2.StrutsException)1