public class HotHttpHandler extends Object implements HttpHandler
A hot handler is a "shell" that forwards handle(request)
calls to
the app handler that does the actual work. When source files are changed,
a new app handler instance is created in a new class loader.
Reloading is done by a HotReloader
.
Example Usage:
// HttpHandler handler = new MyHandler(); HotHttpHandler handler = new HotHttpHandler(MyHandler.class).onJavaFiles(SRC_DIR); HttpServer server = new HttpServer(handler);
Constructor and Description |
---|
HotHttpHandler(Class appHandlerClass)
Create a hot handler.
|
HotHttpHandler(HotReloader reloader,
Class appHandlerClass)
Create a hot handler.
|
HotHttpHandler(HotReloader reloader,
String appHandlerClassName)
Create a hot handler.
|
Instance Methods | |
---|---|
Async<HttpResponse> |
handle(HttpRequest request)
Handle an http request.
|
protected HttpResponse |
handleReloadError(Exception error)
Create a response in case the reloader encounters an error.
|
HotReloader |
reloader()
Get the HotReloader.
|
HotHttpHandler |
onJavaFiles(String... srcDirs)
Reload on java file changes.
|
HotHttpHandler |
onClassFiles()
Reload on class file changes.
|
HotHttpHandler |
onFiles(String fileDesc,
String filePattern,
String... srcDirs)
Reload on file changes.
|
public HotHttpHandler(Class appHandlerClass)
This constructor is equivalent to
HotHttpHandler( new HotReloader(), appHandlerClass.getName() )
.
public HotHttpHandler(HotReloader reloader, Class appHandlerClass)
This constructor is equivalent to
HotHttpHandler( reloader, appHandlerClass.getName() )
.
public HotHttpHandler(HotReloader reloader, String appHandlerClassName)
The app handler class must implement HttpHandler and have a public 0-arg constructor.
public Async<HttpResponse> handle(HttpRequest request)
This method obtains the app handler by
reloader().getAppInstance(appHandlerClassName)
,
then forwards the call to the app handler.
handle
in interface HttpHandler
protected HttpResponse handleReloadError(Exception error)
For example, the reloader may fail due to compilation errors;
then the handle(HttpRequest)
method invokes this method for the response.
This implementation creates a response with the error stacktrace; a subclass may override this method to provide a different response.
public HotReloader reloader()
public HotHttpHandler onJavaFiles(String... srcDirs) throws Exception
This method is equivalent to reloader().
onJavaFiles(srcDirs)
.
Exception
public HotHttpHandler onClassFiles()
This method is equivalent to reloader().
onClassFiles(srcDirs)
.
public HotHttpHandler onFiles(String fileDesc, String filePattern, String... srcDirs)
This method is equivalent to reloader().
onFiles(srcDirs)
.