diff options
author | David Mollitor <dmollitor@apache.org> | 2021-02-03 16:58:17 -0500 |
---|---|---|
committer | David Mollitor <dmollitor@apache.org> | 2021-02-03 16:58:17 -0500 |
commit | 30da56bad198fe78c42c20cb1c8cf6a0b7ffef8d (patch) | |
tree | 0785608e7b369a46e795edd915e669947c2699bf | |
parent | 0f21e39c9ba1b20a50d035f01c14836885678d08 (diff) | |
download | thrift-THRIFT-5345.tar.gz |
THRIFT-5345: Allow the ServerContext to be Unwrapped ProgrammaticallyTHRIFT-5345
-rw-r--r-- | lib/java/src/org/apache/thrift/server/ServerContext.java | 29 | ||||
-rw-r--r-- | lib/java/test/org/apache/thrift/test/TestServer.java | 22 |
2 files changed, 46 insertions, 5 deletions
diff --git a/lib/java/src/org/apache/thrift/server/ServerContext.java b/lib/java/src/org/apache/thrift/server/ServerContext.java index 9b0b99eea..b7c587f37 100644 --- a/lib/java/src/org/apache/thrift/server/ServerContext.java +++ b/lib/java/src/org/apache/thrift/server/ServerContext.java @@ -18,9 +18,32 @@ */ /** - * Interface for storing server's connection context + * Interface for storing server's connection context. */ - package org.apache.thrift.server; -public interface ServerContext {} +public interface ServerContext { + + /** + * Returns an object that implements the given interface to allow access to + * application specific contexts. + * + * @param iface A Class defining an interface that the result must implement + * @return an object that implements the interface + * @throws RuntimeException If the context cannot be unwrapped to the provided + * class + */ + <T> T unwrap(Class<T> iface); + + /** + * Returns true if this server context is a wrapper for the provided + * application specific context interface argument or returns false otherwise. + * + * @param iface a Class defining the underlying context + * @return true if this implements the interface can be unwrapped to the + * provided class + * @throws RuntimeException if an error occurs while determining whether the + * provided class can be unwrapped from this context. + */ + boolean isWrapperFor(Class<?> iface); +} diff --git a/lib/java/test/org/apache/thrift/test/TestServer.java b/lib/java/test/org/apache/thrift/test/TestServer.java index 02e8ad7f6..386f2b60b 100644 --- a/lib/java/test/org/apache/thrift/test/TestServer.java +++ b/lib/java/test/org/apache/thrift/test/TestServer.java @@ -81,6 +81,24 @@ public class TestServer { this.connectionId = connectionId; } + @Override + public <T> T unwrap(Class<T> iface) { + try { + if (isWrapperFor(iface)) { + return iface.cast(this); + } else { + throw new RuntimeException("The context is not a wrapper for " + iface.getName()); + } + } catch (Exception e) { + throw new RuntimeException("The context is not a wrapper and does not implement the interface"); + } + } + + @Override + public boolean isWrapperFor(Class<?> iface) { + return iface.isInstance(this); + } + } static class TestServerEventHandler implements TServerEventHandler { @@ -99,12 +117,12 @@ public class TestServer { } public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) { - TestServerContext ctx = (TestServerContext)serverContext; + TestServerContext ctx = serverContext.unwrap(TestServerContext.class); System.out.println("TServerEventHandler.deleteContext - connection #"+ctx.getConnectionId()+" terminated"); } public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport) { - TestServerContext ctx = (TestServerContext)serverContext; + TestServerContext ctx = serverContext.unwrap(TestServerContext.class); System.out.println("TServerEventHandler.processContext - connection #"+ctx.getConnectionId()+" is ready to process next request"); } |