Created
January 29, 2014 18:01
-
-
Save pferraro/8693403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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