diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileInboundChannelAdapterParser.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileInboundChannelAdapterParser.java index 4b3c559696..686ff21aa6 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileInboundChannelAdapterParser.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileInboundChannelAdapterParser.java @@ -18,17 +18,13 @@ package org.springframework.integration.file.config; import org.w3c.dom.Element; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import org.springframework.beans.BeanMetadataElement; -import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.ExpressionFactoryBean; import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; -import org.springframework.integration.file.remote.session.CachingSessionFactory; +import org.springframework.integration.file.remote.session.SessionFactoryFactoryBean; import org.springframework.util.StringUtils; /** @@ -40,37 +36,17 @@ import org.springframework.util.StringUtils; */ public abstract class AbstractRemoteFileInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser { - private final Log logger = LogFactory.getLog(this.getClass()); - @Override protected final BeanMetadataElement parseSource(Element element, ParserContext parserContext) { BeanDefinitionBuilder synchronizerBuilder = BeanDefinitionBuilder.genericBeanDefinition( this.getInboundFileSynchronizerClassname()); - // This whole block must be refactored once the cache-session attribute is removed - String sessionFactoryName = element.getAttribute("session-factory"); - BeanDefinition sessionFactoryDefinition = parserContext.getReaderContext().getRegistry().getBeanDefinition(sessionFactoryName); - String sessionFactoryClassName = sessionFactoryDefinition.getBeanClassName(); - if (StringUtils.hasText(sessionFactoryClassName) && sessionFactoryClassName.endsWith(CachingSessionFactory.class.getName())) { - synchronizerBuilder.addConstructorArgValue(sessionFactoryDefinition); - } - else { - String cacheSessions = element.getAttribute("cache-sessions"); - if (StringUtils.hasText(cacheSessions) && logger.isWarnEnabled()) { - logger.warn("The 'cache-sessions' attribute is deprecated as of version 2.1. " + - "Please configure a CachingSessionFactory explicitly instead."); - } - if ("false".equalsIgnoreCase(cacheSessions)) { - synchronizerBuilder.addConstructorArgReference(element.getAttribute("session-factory")); - } - else { - BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(CachingSessionFactory.class); - sessionFactoryBuilder.addConstructorArgReference(sessionFactoryName); - synchronizerBuilder.addConstructorArgValue(sessionFactoryBuilder.getBeanDefinition()); - } - } - // end of what needs to be refactored once cache-session is removed - + BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SessionFactoryFactoryBean.class); + sessionFactoryBuilder.addConstructorArgReference(element.getAttribute("session-factory")); + sessionFactoryBuilder.addConstructorArgValue(element.getAttribute("cache-sessions")); + + synchronizerBuilder.addConstructorArgValue(sessionFactoryBuilder.getBeanDefinition()); + // configure the InboundFileSynchronizer properties IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "remote-directory"); IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "delete-remote-files"); diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java index f14c43b32f..340ead7d37 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileOutboundGatewayParser.java @@ -15,16 +15,14 @@ */ package org.springframework.integration.file.config; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.config.BeanDefinition; +import org.w3c.dom.Element; + import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractConsumerEndpointParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; -import org.springframework.integration.file.remote.session.CachingSessionFactory; +import org.springframework.integration.file.remote.session.SessionFactoryFactoryBean; import org.springframework.util.StringUtils; -import org.w3c.dom.Element; /** * @author Gary Russell @@ -33,8 +31,6 @@ import org.w3c.dom.Element; */ public abstract class AbstractRemoteFileOutboundGatewayParser extends AbstractConsumerEndpointParser { - private final Log logger = LogFactory.getLog(this.getClass()); - @Override protected String getInputChannelAttributeName() { return "request-channel"; @@ -45,30 +41,11 @@ public abstract class AbstractRemoteFileOutboundGatewayParser extends AbstractCo BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(getGatewayClassName()); // build the SessionFactory and provide it as a constructor argument - - // This whole block must be refactored once cache-session attribute is removed - String sessionFactoryName = element.getAttribute("session-factory"); - BeanDefinition sessionFactoryDefinition = parserContext.getReaderContext().getRegistry().getBeanDefinition(sessionFactoryName); - String sessionFactoryClassName = sessionFactoryDefinition.getBeanClassName(); - if (StringUtils.hasText(sessionFactoryClassName) && sessionFactoryClassName.endsWith(CachingSessionFactory.class.getName())) { - builder.addConstructorArgValue(sessionFactoryDefinition); - } - else { - String cacheSessions = element.getAttribute("cache-sessions"); - if (StringUtils.hasText(cacheSessions) && logger.isWarnEnabled()) { - logger.warn("The 'cache-sessions' attribute is deprecated as of version 2.1." + - "Please configure a CachingSessionFactory explicitly instead."); - } - if ("false".equalsIgnoreCase(cacheSessions)) { - builder.addConstructorArgReference(element.getAttribute("session-factory")); - } - else { - BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(CachingSessionFactory.class); - sessionFactoryBuilder.addConstructorArgReference(sessionFactoryName); - builder.addConstructorArgValue(sessionFactoryBuilder.getBeanDefinition()); - } - } - // end of what needs to be refactored once cache-session is removed + BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SessionFactoryFactoryBean.class); + sessionFactoryBuilder.addConstructorArgReference(element.getAttribute("session-factory")); + sessionFactoryBuilder.addConstructorArgValue(element.getAttribute("cache-sessions")); + + builder.addConstructorArgValue(sessionFactoryBuilder.getBeanDefinition()); builder.addConstructorArgValue(element.getAttribute("command")); builder.addConstructorArgValue(element.getAttribute(EXPRESSION_ATTRIBUTE)); diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/RemoteFileOutboundChannelAdapterParser.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/RemoteFileOutboundChannelAdapterParser.java index dd1e3ea1bc..aebd118fbd 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/RemoteFileOutboundChannelAdapterParser.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/RemoteFileOutboundChannelAdapterParser.java @@ -16,8 +16,8 @@ package org.springframework.integration.file.config; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Element; + import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; @@ -30,9 +30,8 @@ import org.springframework.integration.config.xml.AbstractOutboundChannelAdapter import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.integration.file.DefaultFileNameGenerator; import org.springframework.integration.file.remote.handler.FileTransferringMessageHandler; -import org.springframework.integration.file.remote.session.CachingSessionFactory; +import org.springframework.integration.file.remote.session.SessionFactoryFactoryBean; import org.springframework.util.StringUtils; -import org.w3c.dom.Element; /** * @author Oleg Zhurakousky @@ -40,33 +39,16 @@ import org.w3c.dom.Element; * @since 2.0 */ public class RemoteFileOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { - private final Log logger = LogFactory.getLog(this.getClass()); + @Override protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { BeanDefinitionBuilder handlerBuilder = BeanDefinitionBuilder.genericBeanDefinition(FileTransferringMessageHandler.class); - // This whole block must be refactored once cache-session attribute is removed - String sessionFactoryName = element.getAttribute("session-factory"); - BeanDefinition sessionFactoryDefinition = parserContext.getReaderContext().getRegistry().getBeanDefinition(sessionFactoryName); - String sessionFactoryClassName = sessionFactoryDefinition.getBeanClassName(); - if (StringUtils.hasText(sessionFactoryClassName) && sessionFactoryClassName.endsWith(CachingSessionFactory.class.getName())){ - handlerBuilder.addConstructorArgValue(sessionFactoryDefinition); - } - else { - String cacheSessions = element.getAttribute("cache-sessions"); - if (StringUtils.hasText(cacheSessions)){ - logger.warn("The 'cache-sessions' attribute is deprecated since v2.1. Consider configuring CachingSessionFactory explicitly"); - } - if ("false".equalsIgnoreCase(cacheSessions)) { - handlerBuilder.addConstructorArgReference(element.getAttribute("session-factory")); - } - else { - BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(CachingSessionFactory.class); - sessionFactoryBuilder.addConstructorArgReference(sessionFactoryName); - handlerBuilder.addConstructorArgValue(sessionFactoryBuilder.getBeanDefinition()); - } - } - // end of what needs to be refactored once cache-session is removed + BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.genericBeanDefinition(SessionFactoryFactoryBean.class); + sessionFactoryBuilder.addConstructorArgReference(element.getAttribute("session-factory")); + sessionFactoryBuilder.addConstructorArgValue(element.getAttribute("cache-sessions")); + + handlerBuilder.addConstructorArgValue(sessionFactoryBuilder.getBeanDefinition()); // configure MessageHandler properties IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "temporary-file-suffix"); diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/SessionFactoryFactoryBean.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/SessionFactoryFactoryBean.java new file mode 100644 index 0000000000..feb3769e1d --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/SessionFactoryFactoryBean.java @@ -0,0 +1,54 @@ +/* + * 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. + * 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.springframework.integration.file.remote.session; + +import org.springframework.beans.factory.FactoryBean; + +/** + * Temporary factory bean to manage SessionFactory until deprecated 'cache-sessions' attribute + * is removed. + * + * @author Oleg Zhurakousky + * @since 2.1 + * + */ +public class SessionFactoryFactoryBean implements FactoryBean> { + + private final SessionFactory sessionFactory; + + public SessionFactoryFactoryBean(SessionFactory sessionFactory, boolean cacheSessions){ + if (cacheSessions && !(sessionFactory instanceof CachingSessionFactory)){ + this.sessionFactory = new CachingSessionFactory(sessionFactory); + } + else { + this.sessionFactory = sessionFactory; + } + } + + public SessionFactory getObject() throws Exception { + return this.sessionFactory; + } + + + public Class getObjectType() { + return this.sessionFactory.getClass(); + } + + public boolean isSingleton() { + return true; + } + +}