added documentation and assertion to IntegrationContextUtils to clarify that the BeanFactory must not be null

This commit is contained in:
Mark Fisher
2011-05-03 19:04:20 -04:00
parent 5ccc5f62ff
commit 3fe88e626b

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 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.
@@ -17,68 +17,91 @@
package org.springframework.integration.context;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.store.MetadataStore;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
/**
* Utility methods for accessing common integration components from the BeanFactory.
*
*
* @author Mark Fisher
* @author Josh Long
* @author Josh Long
*/
public abstract class IntegrationContextUtils {
public static final String TASK_SCHEDULER_BEAN_NAME = "taskScheduler";
public static final String TASK_SCHEDULER_BEAN_NAME = "taskScheduler";
public static final String ERROR_CHANNEL_BEAN_NAME = "errorChannel";
public static final String ERROR_CHANNEL_BEAN_NAME = "errorChannel";
public static final String NULL_CHANNEL_BEAN_NAME = "nullChannel";
public static final String NULL_CHANNEL_BEAN_NAME = "nullChannel";
public static final String METADATA_STORE_BEAN_NAME = "metadataStore";
public static final String METADATA_STORE_BEAN_NAME = "metadataStore";
public static final String INTEGRATION_CONVERSION_SERVICE_BEAN_NAME = "integrationConversionService";
public static final String INTEGRATION_CONVERSION_SERVICE_BEAN_NAME = "integrationConversionService";
public static final String DEFAULT_POLLER_METADATA_BEAN_NAME = "org.springframework.integration.context.defaultPollerMetadata";
public static final String DEFAULT_POLLER_METADATA_BEAN_NAME = "org.springframework.integration.context.defaultPollerMetadata";
public static MetadataStore getMetadataStore(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, METADATA_STORE_BEAN_NAME, MetadataStore.class);
}
/**
* Return the {@link MetadataStore} bean whose name is "metadataStore".
* @param beanFactory BeanFactory for lookup, must not be null.
*/
public static MetadataStore getMetadataStore(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, METADATA_STORE_BEAN_NAME, MetadataStore.class);
}
public static MessageChannel getErrorChannel(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, ERROR_CHANNEL_BEAN_NAME, MessageChannel.class);
}
/**
* Return the {@link MessageChannel} bean whose name is "errorChannel".
* @param beanFactory BeanFactory for lookup, must not be null.
*/
public static MessageChannel getErrorChannel(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, ERROR_CHANNEL_BEAN_NAME, MessageChannel.class);
}
public static TaskScheduler getTaskScheduler(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class);
}
/**
* Return the {@link TaskScheduler} bean whose name is "taskScheduler" if available.
* @param beanFactory BeanFactory for lookup, must not be null.
*/
public static TaskScheduler getTaskScheduler(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class);
}
public static TaskScheduler getRequiredTaskScheduler(BeanFactory beanFactory) {
TaskScheduler taskScheduler = getTaskScheduler(beanFactory);
Assert.state(taskScheduler != null, "No such bean '" + TASK_SCHEDULER_BEAN_NAME + "'");
return taskScheduler;
}
/**
* Return the {@link TaskScheduler} bean whose name is "taskScheduler".
* @param beanFactory BeanFactory for lookup, must not be null.
* @throws IllegalStateException if no such bean is available
*/
public static TaskScheduler getRequiredTaskScheduler(BeanFactory beanFactory) {
TaskScheduler taskScheduler = getTaskScheduler(beanFactory);
Assert.state(taskScheduler != null, "No such bean '" + TASK_SCHEDULER_BEAN_NAME + "'");
return taskScheduler;
}
public static PollerMetadata getDefaultPollerMetadata(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, DEFAULT_POLLER_METADATA_BEAN_NAME, PollerMetadata.class);
}
/**
* Return the default {@link PollerMetadata} bean if available.
* @param beanFactory BeanFactory for lookup, must not be null.
*/
public static PollerMetadata getDefaultPollerMetadata(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, DEFAULT_POLLER_METADATA_BEAN_NAME, PollerMetadata.class);
}
public static ConversionService getConversionService(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, ConversionService.class);
}
/**
* Return the {@link ConversionService} bean whose name is "integrationConversionService" if available.
* @param beanFactory BeanFactory for lookup, must not be null.
*/
public static ConversionService getConversionService(BeanFactory beanFactory) {
return getBeanOfType(beanFactory, INTEGRATION_CONVERSION_SERVICE_BEAN_NAME, ConversionService.class);
}
private static <T> T getBeanOfType(BeanFactory beanFactory, String beanName, Class<T> type) {
Assert.notNull(beanFactory, "BeanFactory must not be null");
if (!beanFactory.containsBean(beanName)) {
return null;
}
return beanFactory.getBean(beanName, type);
}
private static <T> T getBeanOfType(BeanFactory beanFactory, String beanName, Class<T> type) {
if (!beanFactory.containsBean(beanName)) {
return null;
}
return beanFactory.getBean(beanName, type);
}
}