} else {
template = velocityEngine.getTemplate(templatePath);
}
VelocityWriter velocityWriter = null;
try {
velocityWriter = (VelocityWriter) writerPool.get();
if (velocityWriter == null) {
velocityWriter =
new VelocityWriter(writer, WRITER_BUFFER_SIZE, true);
} else {
velocityWriter.recycle(writer);
}
template.merge(velocityContext, velocityWriter);
} catch (Exception error) {
// Exception occured merging template and model. It is possible
// that some output has already been written, so we will append the
// error report to the previous output.
ErrorReport errorReport =
new ErrorReport(error,
null,
configService.isProductionMode(),
Context.getThreadLocalContext().getRequest(),
configService.getServletContext());
if (velocityWriter == null) {
velocityWriter =
new VelocityWriter(writer, WRITER_BUFFER_SIZE, true);
}
velocityWriter.write(errorReport.toString());
throw error;
} finally {
if (velocityWriter != null) {
// flush and put back into the pool don't close to allow
// us to play nicely with others.
velocityWriter.flush();
// Clear the VelocityWriter's reference to its
// internal Writer to allow the latter
// to be GC'd while vw is pooled.
velocityWriter.recycle(null);
writerPool.put(velocityWriter);
}
writer.flush();