/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed 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 org.strecks.source;
import javax.servlet.ServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.strecks.context.ActionContext;
import org.strecks.util.Assert;
/**
* <code>ActionBeanSource</code> implementation which locates a Spring-registered action bean.
* Note that this action bean should not be registered as a singleton
* @author Phil Zoio
*/
public class SpringActionBeanSource implements ActionBeanSource
{
private Class actionBeanClass;
private String beanName;
private boolean checkNotSingleton;
public SpringActionBeanSource(Class actionBeanClass, String beanName)
{
super();
Assert.notNull(actionBeanClass);
Assert.notNull(beanName);
this.actionBeanClass = actionBeanClass;
this.beanName = beanName;
}
public Object createBean(ActionContext context)
{
ServletContext servletContext = context.getContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
synchronized (this)
{
if (!checkNotSingleton)
{
Assert.isTrue(!wac.isSingleton(beanName), "Spring-registered action bean " + beanName
+ " cannot be a singleton");
checkNotSingleton = true;
}
}
return wac.getBean(beanName, actionBeanClass);
}
public Class getBeanClass()
{
return actionBeanClass;
}
}