com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.opensymphony.xwork2.spring.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.spring.SpringObjectFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
/**
*
* TODO: Give a description of the Interceptor.
*
*
*
* TODO: Describe the parameters for this Interceptor.
*
*
*
* TODO: Discuss some possible extension of the Interceptor.
*
*
*
*
* <!-- TODO: Describe how the Interceptor reference will effect execution -->
* <action name="someAction" class="com.examples.SomeAction">
* TODO: fill in the interceptor reference.
* <interceptor-ref name=""/>
* <result name="success">good_result.ftl</result>
* </action>
*
*
*
*
* Autowires action classes to Spring beans. The strategy for autowiring the beans can be configured
* by setting the parameter on the interceptor. Actions that need access to the ActionContext
* can implements the ApplicationContextAware
interface. The context will also be placed on
* the action context under the APPLICATION_CONTEXT attribute.
*
*
* @author Simon Stewart
* @author Eric Hauser
*/
public class ActionAutowiringInterceptor extends AbstractInterceptor implements ApplicationContextAware {
private static final Logger LOG = LogManager.getLogger(ActionAutowiringInterceptor.class);
public static final String APPLICATION_CONTEXT = "com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor.applicationContext";
private boolean initialized = false;
private ApplicationContext context;
private SpringObjectFactory factory;
private Integer autowireStrategy;
/**
* @param autowireStrategy the autowire strategy
*/
public void setAutowireStrategy(Integer autowireStrategy) {
this.autowireStrategy = autowireStrategy;
}
/**
*
* Looks for the ApplicationContext
under the attribute that the Spring listener sets in
* the servlet context. The configuration is done the first time here instead of in init() since the
* ActionContext
is not available during Interceptor
initialization.
*
*
*
* Autowires the action to Spring beans and places the ApplicationContext
* on the ActionContext
*
*
*
* TODO: Should this check to see if the SpringObjectFactory
has already been configured instead of instantiating a new one? Or is there a good reason for the interceptor to have it's own factory?
*
*
* @param invocation the action invocation
* @throws Exception in case of any errors
*/
@Override public String intercept(ActionInvocation invocation) throws Exception {
if (!initialized) {
ApplicationContext applicationContext = (ApplicationContext) ActionContext.getContext().getApplication().get(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (applicationContext == null) {
LOG.warn("ApplicationContext could not be found. Action classes will not be autowired.");
} else {
setApplicationContext(applicationContext);
factory = new SpringObjectFactory();
factory.setContainer(ActionContext.getContext().getContainer());
factory.setApplicationContext(getApplicationContext());
if (autowireStrategy != null) {
factory.setAutowireStrategy(autowireStrategy);
}
}
initialized = true;
}
if (factory != null) {
Object bean = invocation.getAction();
factory.autoWireBean(bean);
ActionContext.getContext().put(APPLICATION_CONTEXT, context);
}
return invocation.invoke();
}
/**
* @param applicationContext the application context
* @throws BeansException in case of errors
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
/**
* @return the application context
*/
protected ApplicationContext getApplicationContext() {
return context;
}
}