Skip to content

Commit b8ccfed

Browse files
committed
Polishing; PR Comments
Also add/remove factories.
1 parent 1d958d4 commit b8ccfed

File tree

6 files changed

+87
-34
lines changed

6 files changed

+87
-34
lines changed
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,52 @@
1919
import java.util.concurrent.ConcurrentHashMap;
2020

2121
/**
22-
* The default implementation of {@link SessionFactoryFactory} using a simple map lookup.
22+
* The default implementation of {@link SessionFactoryLocator} using a simple map lookup
23+
* and an optional default to fall back on.
2324
*
2425
* @author Gary Russell
2526
* @since 4.2
2627
*
2728
*/
28-
public class DefaultSessionFactoryFactory<F> implements SessionFactoryFactory<F> {
29+
public class DefaultSessionFactoryLocator<F> implements SessionFactoryLocator<F> {
2930

3031
private final Map<Object, SessionFactory<F>> factories = new ConcurrentHashMap<Object, SessionFactory<F>>();
3132

3233
private final SessionFactory<F> defaultFactory;
3334

34-
public DefaultSessionFactoryFactory(Map<Object, SessionFactory<F>> factories) {
35+
/**
36+
* @param factories A map of factories, keyed by lookup key.
37+
*/
38+
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories) {
3539
this(factories, null);
3640
}
3741

38-
public DefaultSessionFactoryFactory(Map<Object, SessionFactory<F>> factories, Object defaultKey) {
42+
/**
43+
* @param factories A map of factories, keyed by lookup key.
44+
* @param defaultFactory A default to be used if the lookup fails.
45+
*/
46+
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) {
3947
this.factories.putAll(factories);
40-
if (defaultKey != null) {
41-
this.defaultFactory = factories.get(defaultKey);
42-
}
43-
else {
44-
this.defaultFactory = null;
45-
}
48+
this.defaultFactory = defaultFactory;
4649
}
4750

51+
/**
52+
* Add a session factory.
53+
* @param key the lookup key.
54+
* @param factory the factory.
55+
*/
56+
public void addSessionFactory(String key, SessionFactory<F> factory) {
57+
this.factories.put(key, factory);
58+
}
59+
60+
/**
61+
* Remove a session factory.
62+
* @param key the lookup key.
63+
* @return the factory, if it was present.
64+
*/
65+
public SessionFactory<F> removeSessionFactory(Object key) {
66+
return this.factories.remove(key);
67+
}
4868

4969
@Override
5070
public SessionFactory<F> getSessionFactory(Object key) {

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,43 @@
2222

2323
/**
2424
* {@link SessionFactory} that delegates to a {@link SessionFactory} retrieved from a
25-
* {@link SessionFactoryFactory}.
25+
* {@link SessionFactoryLocator}.
2626
*
2727
* @author Gary Russell
2828
* @since 4.2
2929
*
3030
*/
3131
public class DelegatingSessionFactory<F> implements SessionFactory<F> {
3232

33-
private final SessionFactoryFactory<F> factoryFactory;
33+
private final SessionFactoryLocator<F> factoryLocator;
3434

3535
private final ThreadLocal<Object> threadKey = new ThreadLocal<Object>();
3636

3737
/**
38-
* Construct an instance with a {@link DefaultSessionFactoryFactory} using the
38+
* Construct an instance with a {@link DefaultSessionFactoryLocator} using the
3939
* supplied factories and default key.
4040
* @param factories the factories.
41-
* @param defaultKey the default key.
41+
* @param defaultFactory the default to use if the lookup fails.
4242
*/
43-
public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, Object defaultKey) {
44-
this(new DefaultSessionFactoryFactory<F>(factories, defaultKey));
43+
public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) {
44+
this(new DefaultSessionFactoryLocator<F>(factories, defaultFactory));
4545
}
4646

4747
/**
4848
* Construct an instance using the supplied factory.
49-
* @param factoryFactory the factory.
49+
* @param factoryLocator the factory.
5050
*/
51-
public DelegatingSessionFactory(SessionFactoryFactory<F> factoryFactory) {
52-
Assert.notNull(factoryFactory, "'factoryFactory' cannot be null");
53-
this.factoryFactory = factoryFactory;
51+
public DelegatingSessionFactory(SessionFactoryLocator<F> factoryLocator) {
52+
Assert.notNull(factoryLocator, "'factoryFactory' cannot be null");
53+
this.factoryLocator = factoryLocator;
54+
}
55+
56+
/**
57+
* Return this factory's locator.
58+
* @return the locator.
59+
*/
60+
public SessionFactoryLocator<F> getFactoryLocator() {
61+
return factoryLocator;
5462
}
5563

5664
/**
@@ -97,7 +105,7 @@ public Session<F> getSession() {
97105
}
98106

99107
public Session<F> getSession(Object key) {
100-
SessionFactory<F> sessionFactory = this.factoryFactory.getSessionFactory(key);
108+
SessionFactory<F> sessionFactory = this.factoryLocator.getSessionFactory(key);
101109
Assert.notNull(sessionFactory, "No default SessionFactory configured");
102110
return sessionFactory.getSession();
103111
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @since 4.2
2424
*
2525
*/
26-
public interface SessionFactoryFactory<F> {
26+
public interface SessionFactoryLocator<F> {
2727

2828
/**
2929
* Return a {@link SessionFactory} for the key.

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.junit.Assert.assertEquals;
1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertSame;
2122
import static org.mockito.Mockito.mock;
2223
import static org.mockito.Mockito.verify;
2324

@@ -70,15 +71,24 @@ public class DelegatingSessionFactoryTests {
7071
@Autowired
7172
PollableChannel out;
7273

74+
@Autowired
75+
DefaultSessionFactoryLocator<String> sessionFactoryLocator;
76+
7377
@Test
7478
public void testDelegates() {
75-
assertEquals(foo.mockSession, dsf.getSession("foo"));
76-
assertEquals(bar.mockSession, dsf.getSession("bar"));
77-
assertEquals(bar.mockSession, dsf.getSession("junk"));
78-
assertEquals(bar.mockSession, dsf.getSession());
79-
dsf.setThreadKey("foo");
80-
assertEquals(foo.mockSession, dsf.getSession("foo"));
81-
dsf.clearThreadKey();
79+
assertEquals(foo.mockSession, this.dsf.getSession("foo"));
80+
assertEquals(bar.mockSession, this.dsf.getSession("bar"));
81+
assertEquals(bar.mockSession, this.dsf.getSession("junk"));
82+
assertEquals(bar.mockSession, this.dsf.getSession());
83+
this.dsf.setThreadKey("foo");
84+
assertEquals(foo.mockSession, this.dsf.getSession("foo"));
85+
this.dsf.clearThreadKey();
86+
TestSessionFactory factory = new TestSessionFactory();
87+
this.sessionFactoryLocator.addSessionFactory("baz", factory);
88+
this.dsf.setThreadKey("baz");
89+
assertEquals(factory.mockSession, this.dsf.getSession("baz"));
90+
this.dsf.clearThreadKey();
91+
assertSame(factory, sessionFactoryLocator.removeSessionFactory("baz"));
8292
}
8393

8494
@Test
@@ -107,11 +117,18 @@ TestSessionFactory bar() {
107117

108118
@Bean
109119
DelegatingSessionFactory<String> dsf() {
120+
SessionFactoryLocator<String> sff = sessionFactoryLocator();
121+
return new DelegatingSessionFactory<String>(sff);
122+
}
123+
124+
@Bean
125+
public SessionFactoryLocator<String> sessionFactoryLocator() {
110126
Map<Object, SessionFactory<String>> factories = new HashMap<Object, SessionFactory<String>>();
111127
factories.put("foo", foo());
112-
factories.put("bar", bar());
113-
SessionFactoryFactory<String> sff = new DefaultSessionFactoryFactory<String>(factories, "bar");
114-
return new DelegatingSessionFactory<String>(sff);
128+
TestSessionFactory bar = bar();
129+
factories.put("bar", bar);
130+
SessionFactoryLocator<String> sff = new DefaultSessionFactoryLocator<String>(factories, bar);
131+
return sff;
115132
}
116133

117134
@ServiceActivator(inputChannel="c1")

src/reference/asciidoc/ftp.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ Convenience methods have been added so this can easily be done from a message fl
135135
[source, xml]
136136
----
137137
<bean id="dsf" class="org.springframework.integration.file.remote.session.DelegatingSessionFactory">
138-
<!-- delegate factories here -->
138+
<constructor-arg>
139+
<bean class="o.s.i.file.remote.session.DefaultSessionFactoryLocator">
140+
<!-- delegate factories here -->
141+
</bean>
142+
</constructor-arg>
139143
</bean>
140144
141145
<int:service-activator input-channel="in" output-channel="c1"

src/reference/asciidoc/sftp.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ Convenience methods have been added so this can easily be done from a message fl
173173
[source, xml]
174174
----
175175
<bean id="dsf" class="org.springframework.integration.file.remote.session.DelegatingSessionFactory">
176-
<!-- delegate factories here -->
176+
<constructor-arg>
177+
<bean class="o.s.i.file.remote.session.DefaultSessionFactoryLocator">
178+
<!-- delegate factories here -->
179+
</bean>
180+
</constructor-arg>
177181
</bean>
178182
179183
<int:service-activator input-channel="in" output-channel="c1"

0 commit comments

Comments
 (0)