GH-2605: (S)FTP test cached sessions
Resolves https://github.com/spring-projects/spring-integration/issues/2605 * Suppress unused field warning. * Fix typos. * Use lstat() - don't follow symbolic lincs
This commit is contained in:
committed by
Artem Bilan
parent
eed5fe7a88
commit
bd7a3bc4ed
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -48,6 +48,8 @@ public class CachingSessionFactory<F> implements SessionFactory<F>, DisposableBe
|
||||
|
||||
private final boolean isSharedSessionCapable;
|
||||
|
||||
private boolean testSession;
|
||||
|
||||
private volatile long sharedSessionEpoch;
|
||||
|
||||
/**
|
||||
@@ -83,7 +85,7 @@ public class CachingSessionFactory<F> implements SessionFactory<F>, DisposableBe
|
||||
|
||||
@Override
|
||||
public boolean isStale(Session<F> session) {
|
||||
return !session.isOpen();
|
||||
return CachingSessionFactory.this.testSession ? !session.test() : !session.isOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,6 +117,15 @@ public class CachingSessionFactory<F> implements SessionFactory<F>, DisposableBe
|
||||
this.pool.setPoolSize(poolSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to true to test the session when checking one out from the cache.
|
||||
* @param testSession true to test.
|
||||
* @since 5.1
|
||||
*/
|
||||
public void setTestSession(boolean testSession) {
|
||||
this.testSession = testSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a session from the pool (or block if none available).
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -107,4 +107,14 @@ public interface Session<F> extends Closeable {
|
||||
*/
|
||||
Object getClientInstance();
|
||||
|
||||
/**
|
||||
* Test the session is still alive, e.g. when checking out from a pool.
|
||||
* The default implementation simply delegates to {@link #isOpen()}.
|
||||
* @return true if the test is successful.
|
||||
* @since 5.1
|
||||
*/
|
||||
default boolean test() {
|
||||
return this.isOpen();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@@ -52,6 +52,7 @@ public class CachingSessionFactoryTests {
|
||||
public void testCacheAndReset() {
|
||||
TestSessionFactory factory = new TestSessionFactory();
|
||||
CachingSessionFactory<String> cache = new CachingSessionFactory<String>(factory);
|
||||
cache.setTestSession(true);
|
||||
Session<String> sess1 = cache.getSession();
|
||||
assertEquals("session:1", TestUtils.getPropertyValue(sess1, "targetSession.id"));
|
||||
Session<String> sess2 = cache.getSession();
|
||||
@@ -61,6 +62,7 @@ public class CachingSessionFactoryTests {
|
||||
assertTrue(sess1.isOpen());
|
||||
sess1 = cache.getSession();
|
||||
assertEquals("session:1", TestUtils.getPropertyValue(sess1, "targetSession.id"));
|
||||
assertTrue((TestUtils.getPropertyValue(sess1, "targetSession.testCalled", Boolean.class)));
|
||||
sess1.close();
|
||||
assertTrue(sess1.isOpen());
|
||||
// reset the cache; should close idle (sess1); sess2 should closed later
|
||||
@@ -122,6 +124,9 @@ public class CachingSessionFactoryTests {
|
||||
|
||||
private volatile boolean open = true;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private boolean testCalled;
|
||||
|
||||
private TestSession(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
@@ -197,6 +202,12 @@ public class CachingSessionFactoryTests {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test() {
|
||||
this.testCalled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user