-
Notifications
You must be signed in to change notification settings - Fork 1.1k
INT-3491: (S)FTP Delegating Session Factory #1523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ava/org/springframework/integration/file/remote/session/DefaultSessionFactoryLocator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2015 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.integration.file.remote.session; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* The default implementation of {@link SessionFactoryLocator} using a simple map lookup | ||
* and an optional default to fall back on. | ||
* | ||
* @author Gary Russell | ||
* @since 4.2 | ||
* | ||
*/ | ||
public class DefaultSessionFactoryLocator<F> implements SessionFactoryLocator<F> { | ||
|
||
private final Map<Object, SessionFactory<F>> factories = new ConcurrentHashMap<Object, SessionFactory<F>>(); | ||
|
||
private final SessionFactory<F> defaultFactory; | ||
|
||
/** | ||
* @param factories A map of factories, keyed by lookup key. | ||
*/ | ||
public DefaultSessionFactoryLocator(Map<Object, SessionFactory<F>> factories) { | ||
this(factories, null); | ||
} | ||
|
||
/** | ||
* @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) { | ||
this.factories.putAll(factories); | ||
this.defaultFactory = defaultFactory; | ||
} | ||
|
||
/** | ||
* Add a session factory. | ||
* @param key the lookup key. | ||
* @param factory the factory. | ||
*/ | ||
public void addSessionFactory(String key, SessionFactory<F> factory) { | ||
this.factories.put(key, factory); | ||
} | ||
|
||
/** | ||
* Remove a session factory. | ||
* @param key the lookup key. | ||
* @return the factory, if it was present. | ||
*/ | ||
public SessionFactory<F> removeSessionFactory(Object key) { | ||
return this.factories.remove(key); | ||
} | ||
|
||
@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; | ||
} | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
...in/java/org/springframework/integration/file/remote/session/DelegatingSessionFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2015 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.integration.file.remote.session; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.messaging.Message; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* {@link SessionFactory} that delegates to a {@link SessionFactory} retrieved from a | ||
* {@link SessionFactoryLocator}. | ||
* | ||
* @author Gary Russell | ||
* @since 4.2 | ||
* | ||
*/ | ||
public class DelegatingSessionFactory<F> implements SessionFactory<F> { | ||
|
||
private final SessionFactoryLocator<F> factoryLocator; | ||
|
||
private final ThreadLocal<Object> threadKey = new ThreadLocal<Object>(); | ||
|
||
/** | ||
* Construct an instance with a {@link DefaultSessionFactoryLocator} using the | ||
* supplied factories and default key. | ||
* @param factories the factories. | ||
* @param defaultFactory the default to use if the lookup fails. | ||
*/ | ||
public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, SessionFactory<F> defaultFactory) { | ||
this(new DefaultSessionFactoryLocator<F>(factories, defaultFactory)); | ||
} | ||
|
||
/** | ||
* Construct an instance using the supplied factory. | ||
* @param factoryLocator the factory. | ||
*/ | ||
public DelegatingSessionFactory(SessionFactoryLocator<F> factoryLocator) { | ||
Assert.notNull(factoryLocator, "'factoryFactory' cannot be null"); | ||
this.factoryLocator = factoryLocator; | ||
} | ||
|
||
/** | ||
* Return this factory's locator. | ||
* @return the locator. | ||
*/ | ||
public SessionFactoryLocator<F> getFactoryLocator() { | ||
return factoryLocator; | ||
} | ||
|
||
/** | ||
* Set a key to be used for {@link #getSession()} on this thread. | ||
* @param key the key. | ||
*/ | ||
public void setThreadKey(Object key) { | ||
this.threadKey.set(key); | ||
} | ||
|
||
/** | ||
* Clear the key for this thread. | ||
*/ | ||
public void clearThreadKey() { | ||
this.threadKey.remove(); | ||
} | ||
|
||
/** | ||
* Messaging-friendly version of {@link #setThreadKey(Object)} that can be invoked from | ||
* a service activator. | ||
* @param message the message. | ||
* @param key the key. | ||
* @return the message (unchanged). | ||
*/ | ||
public Message<?> setThreadKey(Message<?> message, Object key) { | ||
this.threadKey.set(key); | ||
return message; | ||
} | ||
|
||
/** | ||
* Messaging-friendly version of {@link #clearThreadKey()} that can be invoked from | ||
* a service activator. | ||
* @param message the message. | ||
* @return the message (unchanged). | ||
*/ | ||
public Message<?> clearThreadKey(Message<?> message) { | ||
this.threadKey.remove(); | ||
return message; | ||
} | ||
|
||
@Override | ||
public Session<F> getSession() { | ||
return getSession(this.threadKey.get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... If I want to rely on the default factory I get performance impact with this |
||
} | ||
|
||
public Session<F> getSession(Object key) { | ||
SessionFactory<F> sessionFactory = this.factoryLocator.getSessionFactory(key); | ||
Assert.notNull(sessionFactory, "No default SessionFactory configured"); | ||
return sessionFactory.getSession(); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
.../main/java/org/springframework/integration/file/remote/session/SessionFactoryLocator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2015 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.integration.file.remote.session; | ||
|
||
/** | ||
* | ||
* A factory returning a {@link SessionFactory} based on some key. | ||
* | ||
* @author Gary Russell | ||
* @since 4.2 | ||
* | ||
*/ | ||
public interface SessionFactoryLocator<F> { | ||
|
||
/** | ||
* Return a {@link SessionFactory} for the key. | ||
* @param key the key. | ||
* @return the session factory. | ||
*/ | ||
SessionFactory<F> getSessionFactory(Object key); | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... We talked in the past with Rossen and Rob that always rely on the
ThreadLocal
is a bad idea. And people really may have some broader view on the matter. That's whySecurityContextHolder
is based on theStrategy
for that purpose...Although we can live with this premise from the beginning...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "strategy" here is
SessionFactory<F>
- that's why I abstractedSessionFactoryLocator
.This is very lightweight - if someone wants another implementation then replace this only.
Maybe this should be called
ThreadLocalDelegatingSessionFactory
or similar.