From edf84a393d4dd8f5db8de0d0fdba581be86efa50 Mon Sep 17 00:00:00 2001 From: Andrey Kezhevatov Date: Fri, 7 Feb 2020 11:44:22 +0400 Subject: [PATCH] GH-3169: DSFL: addSessionFactory based on Object Fixes https://github.com/spring-projects/spring-integration/issues/3169 All other `DefaultSessionFactoryLocator` contracts are based on the `Object`, so this `addSessionFactor`y has to be on `Object` as well. * Add `DefaultSessionFactoryLocator.addSessionFactory(Object key, SessionFactory factory)` * Deprecate existing one based on `String` * Fix tests do no use a deprecated API * Some other code style clean up in the affected classes **Cherry-pick to 5.2.x** --- .../session/DefaultSessionFactoryLocator.java | 34 ++++++++++++++----- .../DelegatingSessionFactoryTests.java | 13 +++---- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/DefaultSessionFactoryLocator.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/DefaultSessionFactoryLocator.java index 0628a7f95d..15bdd175f1 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/DefaultSessionFactoryLocator.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/DefaultSessionFactoryLocator.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 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. @@ -19,18 +19,24 @@ package org.springframework.integration.file.remote.session; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.springframework.lang.Nullable; + /** * The default implementation of {@link SessionFactoryLocator} using a simple map lookup * and an optional default to fall back on. * * @author Gary Russell + * @author Andrey Kezhevatov + * @author Artem Bilan + * * @since 4.2 * */ public class DefaultSessionFactoryLocator implements SessionFactoryLocator { - private final Map> factories = new ConcurrentHashMap>(); + private final Map> factories = new ConcurrentHashMap<>(); + @Nullable private final SessionFactory defaultFactory; /** @@ -44,7 +50,9 @@ public class DefaultSessionFactoryLocator implements SessionFactoryLocator * @param factories A map of factories, keyed by lookup key. * @param defaultFactory A default to be used if the lookup fails. */ - public DefaultSessionFactoryLocator(Map> factories, SessionFactory defaultFactory) { + public DefaultSessionFactoryLocator(Map> factories, + @Nullable SessionFactory defaultFactory) { + this.factories.putAll(factories); this.defaultFactory = defaultFactory; } @@ -53,8 +61,20 @@ public class DefaultSessionFactoryLocator implements SessionFactoryLocator * Add a session factory. * @param key the lookup key. * @param factory the factory. + * @deprecated since 5.3 in favor of {@link #addSessionFactory(Object, SessionFactory)} */ + @Deprecated public void addSessionFactory(String key, SessionFactory factory) { + addSessionFactory((Object) key, factory); + } + + /** + * Add a session factory. + * @param key the lookup key. + * @param factory the factory. + * @since 5.3 + */ + public void addSessionFactory(Object key, SessionFactory factory) { this.factories.put(key, factory); } @@ -68,12 +88,8 @@ public class DefaultSessionFactoryLocator implements SessionFactoryLocator } @Override - public SessionFactory getSessionFactory(Object key) { - if (key == null) { - return this.defaultFactory; - } - SessionFactory factory = this.factories.get(key); - return factory != null ? factory : this.defaultFactory; + public SessionFactory getSessionFactory(@Nullable Object key) { + return this.factories.getOrDefault(key, this.defaultFactory); } } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/DelegatingSessionFactoryTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/DelegatingSessionFactoryTests.java index cbee53e539..7f5ac544f3 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/DelegatingSessionFactoryTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/DelegatingSessionFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 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. @@ -27,8 +27,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -44,8 +43,7 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author Gary Russell @@ -54,8 +52,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @since 4.2 * */ -@ContextConfiguration -@RunWith(SpringJUnit4ClassRunner.class) +@SpringJUnitConfig public class DelegatingSessionFactoryTests { @Autowired @@ -86,7 +83,7 @@ public class DelegatingSessionFactoryTests { assertThat(this.dsf.getSession("foo")).isEqualTo(foo.mockSession); this.dsf.clearThreadKey(); TestSessionFactory factory = new TestSessionFactory(); - this.sessionFactoryLocator.addSessionFactory("baz", factory); + this.sessionFactoryLocator.addSessionFactory((Object) "baz", factory); this.dsf.setThreadKey("baz"); assertThat(this.dsf.getSession("baz")).isEqualTo(factory.mockSession); this.dsf.clearThreadKey();