-
Notifications
You must be signed in to change notification settings - Fork 13
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
Allow customization of C* cluster and session creation. #14
Open
llinder
wants to merge
2
commits into
SmartThingsOSS:master
Choose a base branch
from
llinder:sessionoverride
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
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 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
This file contains 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
217 changes: 217 additions & 0 deletions
217
ratpack-cassandra/src/main/java/smartthings/ratpack/cassandra/AbstractSession.java
This file contains 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,217 @@ | ||
package smartthings.ratpack.cassandra; | ||
|
||
import com.datastax.driver.core.CloseFuture; | ||
import com.datastax.driver.core.Cluster; | ||
import com.datastax.driver.core.PreparedStatement; | ||
import com.datastax.driver.core.RegularStatement; | ||
import com.datastax.driver.core.ResultSet; | ||
import com.datastax.driver.core.ResultSetFuture; | ||
import com.datastax.driver.core.Session; | ||
import com.datastax.driver.core.Statement; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import ratpack.exec.Promise; | ||
import ratpack.service.Service; | ||
import ratpack.service.StartEvent; | ||
import ratpack.service.StopEvent; | ||
|
||
/** | ||
* Abstract class that transparently provides the role of both an | ||
* injectable {@link Session} and fills the contract of a Ratpack {@link Service}. | ||
* | ||
* This is an abstract class to allow users to extend it for different reasons including | ||
* the ability to provide different types for DI resolution. | ||
*/ | ||
public abstract class AbstractSession implements RatpackSession, Session, Service { | ||
|
||
private final Cluster cluster; | ||
private final Optional<String> keyspace; | ||
private Session delegate; | ||
|
||
AbstractSession(Cluster cluster) { | ||
this.cluster = cluster; | ||
this.keyspace = Optional.empty(); | ||
} | ||
|
||
AbstractSession(Cluster cluster, String keyspace) { | ||
this.cluster = cluster; | ||
this.keyspace = Optional.of(keyspace); | ||
} | ||
|
||
@Override | ||
public String getLoggedKeyspace() { | ||
return getDelegate().getLoggedKeyspace(); | ||
} | ||
|
||
@Override | ||
public Session init() { | ||
return getDelegate().init(); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Session> initAsync() { | ||
return getDelegate().initAsync(); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(String s) { | ||
return getDelegate().execute(s); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(String s, Object... objects) { | ||
return getDelegate().execute(s, objects); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(String s, Map<String, Object> map) { | ||
return getDelegate().execute(s, map); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(Statement statement) { | ||
return getDelegate().execute(statement); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(String s) { | ||
return getDelegate().executeAsync(s); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(String s, Object... objects) { | ||
return getDelegate().executeAsync(s, objects); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(String s, Map<String, Object> map) { | ||
return getDelegate().executeAsync(s, map); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(Statement statement) { | ||
return getDelegate().executeAsync(statement); | ||
} | ||
|
||
@Override | ||
public PreparedStatement prepare(String s) { | ||
return getDelegate().prepare(s); | ||
} | ||
|
||
@Override | ||
public PreparedStatement prepare(RegularStatement regularStatement) { | ||
return getDelegate().prepare(regularStatement); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<PreparedStatement> prepareAsync(String s) { | ||
return getDelegate().prepareAsync(s); | ||
} | ||
|
||
@Override | ||
public | ||
ListenableFuture<PreparedStatement> prepareAsync(RegularStatement regularStatement) { | ||
return getDelegate().prepareAsync(regularStatement); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(String query) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(query); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(String query, Object... values) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(query, values); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(String query, Map<String, Object> values) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(query, values); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(Statement statement) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(statement); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<PreparedStatement> preparePromise(String query) { | ||
return Promise.async(upstream -> { | ||
ListenableFuture<PreparedStatement> f = getDelegate().prepareAsync(query); | ||
upstream.accept(f); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<PreparedStatement> preparePromise(RegularStatement statement) { | ||
return Promise.async(upstream -> { | ||
ListenableFuture<PreparedStatement> f = getDelegate().prepareAsync(statement); | ||
upstream.accept(f); | ||
}); | ||
} | ||
|
||
@Override | ||
public CloseFuture closeAsync() { | ||
return getDelegate().closeAsync(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
getDelegate().close(); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return getDelegate().isClosed(); | ||
} | ||
|
||
@Override | ||
public Cluster getCluster() { | ||
return cluster; | ||
} | ||
|
||
@Override | ||
public State getState() { | ||
return getDelegate().getState(); | ||
} | ||
|
||
@Override | ||
public void onStart(StartEvent event) throws Exception { | ||
delegate = getDelegate(); | ||
} | ||
|
||
@Override | ||
public void onStop(StopEvent event) throws Exception { | ||
if (getDelegate() != null && !getDelegate().isClosed()) { | ||
getDelegate().closeAsync(); | ||
} | ||
} | ||
|
||
protected final Session getDelegate() { | ||
if (delegate == null) { | ||
synchronized (this) { | ||
if (delegate == null) { | ||
delegate = createDelegate(); | ||
} | ||
} | ||
} | ||
return delegate; | ||
} | ||
|
||
protected Session createDelegate() { | ||
return (keyspace.isPresent()) ? cluster.connect(keyspace.get()) : cluster.connect(); | ||
} | ||
} |
This file contains 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
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.
Does this work?
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.
I believe this works as long as the provided implementation extends
@Service
. It would be good to test that part more though.