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<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**
This commit is contained in:
Andrey Kezhevatov
2020-02-07 11:44:22 +04:00
committed by Artem Bilan
parent fa97ce0e66
commit edf84a393d
2 changed files with 30 additions and 17 deletions

View File

@@ -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<F> implements SessionFactoryLocator<F> {
private final Map<Object, SessionFactory<F>> factories = new ConcurrentHashMap<Object, SessionFactory<F>>();
private final Map<Object, SessionFactory<F>> factories = new ConcurrentHashMap<>();
@Nullable
private final SessionFactory<F> defaultFactory;
/**
@@ -44,7 +50,9 @@ public class DefaultSessionFactoryLocator<F> implements SessionFactoryLocator<F>
* @param factories A map of factories, keyed by lookup key.
* @param defaultFactory A default to be used if the lookup fails.
*/
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) {
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories,
@Nullable SessionFactory<F> defaultFactory) {
this.factories.putAll(factories);
this.defaultFactory = defaultFactory;
}
@@ -53,8 +61,20 @@ public class DefaultSessionFactoryLocator<F> implements SessionFactoryLocator<F>
* 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<F> 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<F> factory) {
this.factories.put(key, factory);
}
@@ -68,12 +88,8 @@ public class DefaultSessionFactoryLocator<F> implements SessionFactoryLocator<F>
}
@Override
public SessionFactory<F> getSessionFactory(Object key) {
if (key == null) {
return this.defaultFactory;
}
SessionFactory<F> factory = this.factories.get(key);
return factory != null ? factory : this.defaultFactory;
public SessionFactory<F> getSessionFactory(@Nullable Object key) {
return this.factories.getOrDefault(key, this.defaultFactory);
}
}

View File

@@ -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();