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"); }