Adding support for retreival of a MetadataPersister implementation if possible.

This commit is contained in:
Josh Long
2010-08-24 08:20:19 +00:00
parent 06b7d00113
commit fa12ec61a6

View File

@@ -18,16 +18,11 @@ package org.springframework.integration.context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.*;
import org.springframework.core.convert.ConversionService;
import org.springframework.integration.Message;
import org.springframework.integration.context.metadata.MetadataPersister;
import org.springframework.integration.context.metadata.PropertiesBasedMetadataPersister;
import org.springframework.integration.history.MessageHistoryWriter;
import org.springframework.integration.history.NamedComponent;
import org.springframework.scheduling.TaskScheduler;
@@ -37,128 +32,153 @@ import org.springframework.util.StringUtils;
/**
* A base class that provides convenient access to the bean factory as
* well as {@link TaskScheduler} and {@link ConversionService} instances.
*
* <p/>
* <p>This is intended to be used as a base class for internal framework
* components whereas code built upon the integration framework should not
* require tight coupling with the context but rather rely on standard
* dependency injection.
*
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public abstract class IntegrationObjectSupport implements BeanNameAware, NamedComponent, BeanFactoryAware, InitializingBean {
/** Logger that is available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private volatile MessageHistoryWriter historyWriter;
/**
* Logger that is available to subclasses
*/
protected final Log logger = LogFactory.getLog(getClass());
private volatile String beanName;
private volatile String componentName;
private volatile MessageHistoryWriter historyWriter;
private volatile BeanFactory beanFactory;
private volatile MetadataPersister<?> metadataPersister;
private volatile TaskScheduler taskScheduler;
private volatile String beanName;
private volatile ConversionService conversionService;
private volatile String componentName;
private volatile BeanFactory beanFactory;
private volatile TaskScheduler taskScheduler;
private volatile ConversionService conversionService;
public final void setBeanName(String beanName) {
this.beanName = beanName;
}
public final void setBeanName(String beanName) {
this.beanName = beanName;
}
/**
* Will return the name of this component identified by {@link #componentName} field.
* If {@link #componentName} was not set this method will default to the 'beanName' of this component;
*/
public final String getComponentName() {
return StringUtils.hasText(this.componentName) ? this.componentName : this.beanName;
}
/**
* Sets the name of this component.
*
* @param componentName
*/
public void setComponentName(String componentName) {
this.componentName = componentName;
}
/**
* Will return the name of this component identified by {@link #componentName} field.
* If {@link #componentName} was not set this method will default to the 'beanName' of this component;
*/
public final String getComponentName() {
return StringUtils.hasText(this.componentName) ? this.componentName : this.beanName;
}
/**
* Subclasses may implement this method to provide component type information.
*/
public String getComponentType() {
return null;
}
/**
* Sets the name of this component.
*
* @param componentName
*/
public void setComponentName(String componentName) {
this.componentName = componentName;
}
public final void setBeanFactory(BeanFactory beanFactory) {
Assert.notNull(beanFactory, "beanFactory must not be null");
this.beanFactory = beanFactory;
}
/**
* Subclasses may implement this method to provide component type information.
*/
public String getComponentType() {
return null;
}
public final void afterPropertiesSet() {
try {
this.onInit();
}
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new BeanInitializationException("failed to initialize", e);
}
if (this.beanFactory != null) {
if (BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory)this.beanFactory, MessageHistoryWriter.class).size() == 1){
this.historyWriter = this.beanFactory.getBean(MessageHistoryWriter.class);
}
}
}
public final void setBeanFactory(BeanFactory beanFactory) {
Assert.notNull(beanFactory, "beanFactory must not be null");
this.beanFactory = beanFactory;
}
/**
* Subclasses may implement this for initialization logic.
*/
protected void onInit() throws Exception {
}
public final void afterPropertiesSet() {
try {
this.onInit();
}
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new BeanInitializationException("failed to initialize", e);
}
if (this.beanFactory != null) {
if (BeanFactoryUtils.beansOfTypeIncludingAncestors((ListableBeanFactory) this.beanFactory, MessageHistoryWriter.class).size() == 1) {
this.historyWriter = this.beanFactory.getBean(MessageHistoryWriter.class);
}
}
}
protected final BeanFactory getBeanFactory() {
return this.beanFactory;
}
/**
* Subclasses may implement this for initialization logic.
*/
protected void onInit() throws Exception {
}
protected TaskScheduler getTaskScheduler() {
if (this.taskScheduler == null && this.beanFactory != null) {
this.taskScheduler = IntegrationContextUtils.getTaskScheduler(this.beanFactory);
}
return this.taskScheduler;
}
protected final BeanFactory getBeanFactory() {
return this.beanFactory;
}
protected void setTaskScheduler(TaskScheduler taskScheduler) {
Assert.notNull(taskScheduler, "taskScheduler must not be null");
this.taskScheduler = taskScheduler;
}
protected MetadataPersister getRequiredMetadataPersister() {
if (this.metadataPersister == null && this.beanFactory != null) {
this.metadataPersister = IntegrationContextUtils.getMetadataPersister(this.beanFactory);
}
protected final ConversionService getConversionService() {
if (this.conversionService == null && this.beanFactory != null) {
this.conversionService = IntegrationContextUtils.getConversionService(this.beanFactory);
if (this.conversionService == null && logger.isDebugEnabled()) {
logger.debug("Unable to attempt conversion of Message payload types. Component '" +
this.getComponentName() + "' has no explicit ConversionService reference, " +
"and there is no 'integrationConversionService' bean within the context.");
}
}
return this.conversionService;
}
if (this.metadataPersister == null) {
PropertiesBasedMetadataPersister mp = new PropertiesBasedMetadataPersister();
try {
mp.afterPropertiesSet();
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new BeanInitializationException("failed to obtain reference to MetadataPersister strategy implementation.", e);
}
this.metadataPersister = mp;
}
return this.metadataPersister;
}
protected void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
protected TaskScheduler getTaskScheduler() {
if (this.taskScheduler == null && this.beanFactory != null) {
this.taskScheduler = IntegrationContextUtils.getTaskScheduler(this.beanFactory);
}
return this.taskScheduler;
}
@Override
public String toString() {
return (this.beanName != null) ? this.beanName : super.toString();
}
protected void setTaskScheduler(TaskScheduler taskScheduler) {
Assert.notNull(taskScheduler, "taskScheduler must not be null");
this.taskScheduler = taskScheduler;
}
protected void writeMessageHistory(Message<?> message) {
if (historyWriter != null && message != null) {
historyWriter.writeHistory(this, message.getHeaders().getHistory());
protected final ConversionService getConversionService() {
if (this.conversionService == null && this.beanFactory != null) {
this.conversionService = IntegrationContextUtils.getConversionService(this.beanFactory);
if (this.conversionService == null && logger.isDebugEnabled()) {
logger.debug("Unable to attempt conversion of Message payload types. Component '" +
this.getComponentName() + "' has no explicit ConversionService reference, " +
"and there is no 'integrationConversionService' bean within the context.");
}
}
return this.conversionService;
}
protected void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
@Override
public String toString() {
return (this.beanName != null) ? this.beanName : super.toString();
}
protected void writeMessageHistory(Message<?> message) {
if (historyWriter != null && message != null) {
historyWriter.writeHistory(this, message.getHeaders().getHistory());
}
}
}