Skip to content

Commit

Permalink
Proper nesting of the thread-local template rendering contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Mar 15, 2017
1 parent 9c078a7 commit 36ee14b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Expand Up @@ -48,34 +48,41 @@ public RapidoidTemplate(String filename, TemplateRenderer template, TemplateFact
}

void doRenderMulti(RapidoidThreadLocals locals, OutputStream output, List<Object> model) {
RenderCtxImpl renderCtx = initRenderCtx(locals);
// start using the render context
RenderCtxImpl renderCtx = getRenderCtx(locals);

renderCtx.out(output).factory(factory).filename(filename).multiModel(model);

template.render(renderCtx);

// stop using the render context
renderCtx.reset();
}

void doRender(RapidoidThreadLocals locals, OutputStream output, Object model) {
RenderCtxImpl renderCtx = initRenderCtx(locals);
// start using the render context
RenderCtxImpl renderCtx = getRenderCtx(locals);

renderCtx.out(output).factory(factory).filename(filename).model(model);

template.render(renderCtx);

// stop using the render context
renderCtx.reset();
}

private RenderCtxImpl initRenderCtx(RapidoidThreadLocals locals) {
private RenderCtxImpl getRenderCtx(RapidoidThreadLocals locals) {
RenderCtxImpl renderCtx = (RenderCtxImpl) locals.renderContext;

if (renderCtx == null) {
renderCtx = new RenderCtxImpl();
locals.renderContext = renderCtx;
}

return renderCtx;
if (!renderCtx.busy()) {
renderCtx.claim();
return renderCtx;
} else {
return new RenderCtxImpl();
}
}

public void renderMultiModel(OutputStream output, Object... model) {
Expand Down
Expand Up @@ -47,6 +47,8 @@ public class RenderCtxImpl extends RapidoidThing implements RenderCtx {
private volatile String ext;
private volatile TemplateFactory factory;

private volatile boolean busy;

@Override
public void printAscii(String s) throws IOException {
StreamUtils.writeAscii(out, s);
Expand Down Expand Up @@ -199,6 +201,14 @@ public RenderCtxImpl filename(String filename) {

public void reset() {
this.model.clear();
this.busy = false;
}

public boolean busy() {
return busy;
}

public void claim() {
this.busy = true;
}
}

0 comments on commit 36ee14b

Please sign in to comment.