You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dm...@apache.org on 2021/02/04 14:14:26 UTC

[thrift] branch master updated: THRIFT-5345: Allow the ServerContext to be Unwrapped Programmatically

This is an automated email from the ASF dual-hosted git repository.

dmollitor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new ebc2ab5  THRIFT-5345: Allow the ServerContext to be Unwrapped Programmatically
ebc2ab5 is described below

commit ebc2ab558dce946b2a3134028b08ed59d49cd1e3
Author: belugabehr <12...@users.noreply.github.com>
AuthorDate: Thu Feb 4 09:14:11 2021 -0500

    THRIFT-5345: Allow the ServerContext to be Unwrapped Programmatically
    
    Client: Java
    Patch: David Mollitor
---
 .../org/apache/thrift/server/ServerContext.java    | 29 +++++++++++++++++++---
 .../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 9b0b99e..b7c587f 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 02e8ad7..386f2b6 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");
         }