BATCH-1213: tidied up BeanDefinition handling
This commit is contained in:
@@ -100,7 +100,7 @@ public abstract class AbstractStepParser {
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(jobFactoryRef)) {
|
||||
bd.getPropertyValues().addPropertyValue("jobParserJobFactoryBeanRef", jobFactoryRef);
|
||||
bd.setAttribute("jobParserJobFactoryBeanRef", jobFactoryRef);
|
||||
}
|
||||
|
||||
return bd;
|
||||
|
||||
@@ -20,10 +20,7 @@ import org.springframework.batch.core.step.AbstractStep;
|
||||
import org.springframework.beans.PropertyValue;
|
||||
import org.springframework.beans.PropertyValues;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
@@ -32,87 +29,29 @@ import org.springframework.util.StringUtils;
|
||||
public class CoreNamespaceBeanDefinitionUtils {
|
||||
|
||||
/**
|
||||
* @param bd a {@link BeanDefinition}
|
||||
* @param name the name of a bean definition in the bean factory
|
||||
* @param beanFactory a {@link BeanFactory}
|
||||
* @return TRUE if the bean represents an {@link AbstractStep} (or
|
||||
* {@link StepParserStepFactoryBean}).
|
||||
* {@link StepParserStepFactoryBean}).
|
||||
*/
|
||||
public static boolean isAbstractStep(BeanDefinition bd, ConfigurableListableBeanFactory beanFactory) {
|
||||
return isBeanClassAssignable(bd, new Class<?>[] { StepParserStepFactoryBean.class, AbstractStep.class },
|
||||
beanFactory);
|
||||
public static boolean isAbstractStep(String name, ConfigurableListableBeanFactory beanFactory) {
|
||||
if (beanFactory.isFactoryBean(name)) {
|
||||
return beanFactory.isTypeMatch(BeanFactory.FACTORY_BEAN_PREFIX + name, StepParserStepFactoryBean.class);
|
||||
}
|
||||
return beanFactory.isTypeMatch(name, AbstractStep.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bd a {@link BeanDefinition}
|
||||
* @param types an array of {@link Class} objects.
|
||||
* @param beanFactory a {@link BeanFactory}
|
||||
* @return TRUE if the given {@link BeanDefinition}'s bean class is one of
|
||||
* the given types (or a subtype thereof).
|
||||
*/
|
||||
public static boolean isBeanClassAssignable(BeanDefinition bd, Class<?>[] types,
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
Class<?> stepClass = getClass(bd, beanFactory);
|
||||
for (Class<?> type : types) {
|
||||
if (ClassUtils.isAssignable(type, stepClass)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bd a {@link BeanDefinition}
|
||||
* @param beanFactory a {@link BeanFactory}
|
||||
* @return The class of the bean. Search parent hierarchy if necessary.
|
||||
* Return null if none is found.
|
||||
*/
|
||||
public static Class<?> getClass(BeanDefinition bd, ConfigurableListableBeanFactory beanFactory) {
|
||||
// Get the declared class of the bean
|
||||
String className = bd.getBeanClassName();
|
||||
if (StringUtils.hasText(className)) {
|
||||
try {
|
||||
return ClassUtils.forName(className);
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Search the parent until you find it
|
||||
String parentName = bd.getParentName();
|
||||
if (StringUtils.hasText(parentName)) {
|
||||
return getClass(beanFactory.getBeanDefinition(parentName), beanFactory);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bd a {@link BeanDefinition}
|
||||
* @param name a bean definition name
|
||||
* @param propertyName the name of the property
|
||||
* @param beanFactory a {@link BeanFactory}
|
||||
* @return The {@link PropertyValue} for the {@link JobRepository} of the
|
||||
* bean. Search parent hierarchy if necessary. Return null if none
|
||||
* is found.
|
||||
* bean. Search parent hierarchy if necessary. Return null if none is found.
|
||||
*/
|
||||
public static PropertyValue getPropertyValue(BeanDefinition bd, String propertyName,
|
||||
public static PropertyValue getPropertyValue(String name, String propertyName,
|
||||
ConfigurableListableBeanFactory beanFactory) {
|
||||
PropertyValues jobDefPvs = bd.getPropertyValues();
|
||||
if (jobDefPvs.contains(propertyName)) {
|
||||
// return the property
|
||||
return jobDefPvs.getPropertyValue(propertyName);
|
||||
}
|
||||
else {
|
||||
// Search the parent until you find it
|
||||
String parentName = bd.getParentName();
|
||||
if (StringUtils.hasText(parentName)) {
|
||||
return getPropertyValue(beanFactory.getBeanDefinition(parentName), propertyName, beanFactory);
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
PropertyValues jobDefPvs = beanFactory.getMergedBeanDefinition(name).getPropertyValues();
|
||||
return jobDefPvs.getPropertyValue(propertyName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,10 +56,10 @@ public class CoreNamespacePostProcessor implements BeanPostProcessor, BeanFactor
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
for (String beanName : beanFactory.getBeanDefinitionNames()) {
|
||||
BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
|
||||
MutablePropertyValues pvs = (MutablePropertyValues) bd.getPropertyValues();
|
||||
if (pvs.contains(JOB_FACTORY_PROPERTY_NAME)) {
|
||||
if (CoreNamespaceBeanDefinitionUtils.isAbstractStep(bd, beanFactory)) {
|
||||
String jobName = (String) pvs.getPropertyValue(JOB_FACTORY_PROPERTY_NAME).getValue();
|
||||
if (bd.hasAttribute(JOB_FACTORY_PROPERTY_NAME)) {
|
||||
MutablePropertyValues pvs = (MutablePropertyValues) bd.getPropertyValues();
|
||||
if (CoreNamespaceBeanDefinitionUtils.isAbstractStep(beanName, beanFactory)) {
|
||||
String jobName = (String) bd.getAttribute(JOB_FACTORY_PROPERTY_NAME);
|
||||
PropertyValue jobRepository = getJobRepository(jobName, beanFactory);
|
||||
if (jobRepository != null) {
|
||||
// Set the job's JobRepository onto the step
|
||||
@@ -72,7 +72,6 @@ public class CoreNamespacePostProcessor implements BeanPostProcessor, BeanFactor
|
||||
pvs.addPropertyValue(JOB_REPOSITORY_PROPERTY_NAME, jobRepositoryBeanRef);
|
||||
}
|
||||
}
|
||||
pvs.removePropertyValue(JOB_FACTORY_PROPERTY_NAME);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,8 +84,7 @@ public class CoreNamespacePostProcessor implements BeanPostProcessor, BeanFactor
|
||||
* is found.
|
||||
*/
|
||||
private PropertyValue getJobRepository(String jobName, ConfigurableListableBeanFactory beanFactory) {
|
||||
BeanDefinition jobDef = beanFactory.getBeanDefinition(jobName);
|
||||
return CoreNamespaceBeanDefinitionUtils.getPropertyValue(jobDef, JOB_REPOSITORY_PROPERTY_NAME, beanFactory);
|
||||
return CoreNamespaceBeanDefinitionUtils.getPropertyValue(jobName, JOB_REPOSITORY_PROPERTY_NAME, beanFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user