Skip to content

Commit

Permalink
THRIFT-5345: Allow the ServerContext to be Unwrapped Programmatically
Browse files Browse the repository at this point in the history
Client: Java
Patch: David Mollitor
  • Loading branch information
belugabehr committed Feb 4, 2021
1 parent 55016bf commit ebc2ab5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
29 changes: 26 additions & 3 deletions lib/java/src/org/apache/thrift/server/ServerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
22 changes: 20 additions & 2 deletions lib/java/test/org/apache/thrift/test/TestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ public void setConnectionId(int connectionId) {
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 {
Expand All @@ -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");
}

Expand Down

0 comments on commit ebc2ab5

Please sign in to comment.