Skip to content

Instantly share code, notes, and snippets.

@rhusar
Forked from pferraro/gist:8693403
Created January 29, 2014 18:03
Show Gist options
  • Save rhusar/8693447 to your computer and use it in GitHub Desktop.
Save rhusar/8693447 to your computer and use it in GitHub Desktop.
public interface ThreadSetupActionFactory<C> {
ThreadSetupAction createThreadSetupAction(C context);
}
public class LazyThreadSetupAction<C> implements ThreadSetupAction {
private static final Handle EMPTY_HANDLE = new Handle() {
@Override
public void tearDown() {
}
};
private final C context;
private final Value<ThreadSetupActionFactory<C>> factory;
private volatile ThreadSetupAction action;
public LazyThreadSetupAction(Value<ThreadSetupActionFactory<C>> factory, C context) {
this.factory = factory;
this.context = context;
}
@Override
public Handle setup(HttpServerExchange exchange) {
// This is first called with a null exchange during DeploymentManager.deploy(...)
if (exchange == null) {
if (this.action == null) {
this.action = this.factory.getValue().createThreadSetupAction(this.context);
}
return EMPTY_HANDLE;
}
if (this.action == null) {
throw new IllegalStateException();
}
return this.action.setup(exchange);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment