Skip to content

Commit edf84a3

Browse files
kezhevatovartembilan
authored andcommitted
GH-3169: DSFL: addSessionFactory based on Object
Fixes #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<F> 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**
1 parent fa97ce0 commit edf84a3

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/DefaultSessionFactoryLocator.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,18 +19,24 @@
1919
import java.util.Map;
2020
import java.util.concurrent.ConcurrentHashMap;
2121

22+
import org.springframework.lang.Nullable;
23+
2224
/**
2325
* The default implementation of {@link SessionFactoryLocator} using a simple map lookup
2426
* and an optional default to fall back on.
2527
*
2628
* @author Gary Russell
29+
* @author Andrey Kezhevatov
30+
* @author Artem Bilan
31+
*
2732
* @since 4.2
2833
*
2934
*/
3035
public class DefaultSessionFactoryLocator<F> implements SessionFactoryLocator<F> {
3136

32-
private final Map<Object, SessionFactory<F>> factories = new ConcurrentHashMap<Object, SessionFactory<F>>();
37+
private final Map<Object, SessionFactory<F>> factories = new ConcurrentHashMap<>();
3338

39+
@Nullable
3440
private final SessionFactory<F> defaultFactory;
3541

3642
/**
@@ -44,7 +50,9 @@ public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories) {
4450
* @param factories A map of factories, keyed by lookup key.
4551
* @param defaultFactory A default to be used if the lookup fails.
4652
*/
47-
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) {
53+
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories,
54+
@Nullable SessionFactory<F> defaultFactory) {
55+
4856
this.factories.putAll(factories);
4957
this.defaultFactory = defaultFactory;
5058
}
@@ -53,8 +61,20 @@ public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories, Se
5361
* Add a session factory.
5462
* @param key the lookup key.
5563
* @param factory the factory.
64+
* @deprecated since 5.3 in favor of {@link #addSessionFactory(Object, SessionFactory)}
5665
*/
66+
@Deprecated
5767
public void addSessionFactory(String key, SessionFactory<F> factory) {
68+
addSessionFactory((Object) key, factory);
69+
}
70+
71+
/**
72+
* Add a session factory.
73+
* @param key the lookup key.
74+
* @param factory the factory.
75+
* @since 5.3
76+
*/
77+
public void addSessionFactory(Object key, SessionFactory<F> factory) {
5878
this.factories.put(key, factory);
5979
}
6080

@@ -68,12 +88,8 @@ public SessionFactory<F> removeSessionFactory(Object key) {
6888
}
6989

7090
@Override
71-
public SessionFactory<F> getSessionFactory(Object key) {
72-
if (key == null) {
73-
return this.defaultFactory;
74-
}
75-
SessionFactory<F> factory = this.factories.get(key);
76-
return factory != null ? factory : this.defaultFactory;
91+
public SessionFactory<F> getSessionFactory(@Nullable Object key) {
92+
return this.factories.getOrDefault(key, this.defaultFactory);
7793
}
7894

7995
}

spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/DelegatingSessionFactoryTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,8 +27,7 @@
2727
import java.util.List;
2828
import java.util.Map;
2929

30-
import org.junit.Test;
31-
import org.junit.runner.RunWith;
30+
import org.junit.jupiter.api.Test;
3231

3332
import org.springframework.beans.factory.annotation.Autowired;
3433
import org.springframework.context.annotation.Bean;
@@ -44,8 +43,7 @@
4443
import org.springframework.messaging.MessageHandler;
4544
import org.springframework.messaging.PollableChannel;
4645
import org.springframework.messaging.support.GenericMessage;
47-
import org.springframework.test.context.ContextConfiguration;
48-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4947

5048
/**
5149
* @author Gary Russell
@@ -54,8 +52,7 @@
5452
* @since 4.2
5553
*
5654
*/
57-
@ContextConfiguration
58-
@RunWith(SpringJUnit4ClassRunner.class)
55+
@SpringJUnitConfig
5956
public class DelegatingSessionFactoryTests {
6057

6158
@Autowired
@@ -86,7 +83,7 @@ public void testDelegates() {
8683
assertThat(this.dsf.getSession("foo")).isEqualTo(foo.mockSession);
8784
this.dsf.clearThreadKey();
8885
TestSessionFactory factory = new TestSessionFactory();
89-
this.sessionFactoryLocator.addSessionFactory("baz", factory);
86+
this.sessionFactoryLocator.addSessionFactory((Object) "baz", factory);
9087
this.dsf.setThreadKey("baz");
9188
assertThat(this.dsf.getSession("baz")).isEqualTo(factory.mockSession);
9289
this.dsf.clearThreadKey();

0 commit comments

Comments
 (0)