From ebc2ab558dce946b2a3134028b08ed59d49cd1e3 Mon Sep 17 00:00:00 2001 From: belugabehr <12578579+belugabehr@users.noreply.github.com> Date: Thu, 4 Feb 2021 09:14:11 -0500 Subject: [PATCH] THRIFT-5345: Allow the ServerContext to be Unwrapped Programmatically Client: Java Patch: David Mollitor --- .../apache/thrift/server/ServerContext.java | 29 +++++++++++++++++-- .../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 9b0b99eeaa..b7c587f378 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 unwrap(Class 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 02e8ad7f6d..386f2b60bf 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 void setConnectionId(int connectionId) { this.connectionId = connectionId; } + @Override + public T unwrap(Class 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 ServerContext createContext(TProtocol input, TProtocol output) { } 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"); }